Skip to content

Commit

Permalink
Fav API for simulator
Browse files Browse the repository at this point in the history
  • Loading branch information
ctag-fh-kiel committed Nov 13, 2021
1 parent 0f058e5 commit 8a748be
Show file tree
Hide file tree
Showing 11 changed files with 146 additions and 30 deletions.
4 changes: 3 additions & 1 deletion main/FavoritesModel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@ respective component folders / files if different from this license.

#pragma once


#include <string>
#include "rapidjson/document.h"
#include "rapidjson/stringbuffer.h"
#include "ctagResources.hpp"
#include "SPManagerDataModel.hpp"

using namespace std;
Expand All @@ -44,7 +46,7 @@ namespace CTAG {

private:
Document m;
const string MODELJSONFN = "/spiffs/data/favs.jsn";
string MODELJSONFN = CTAG::RESOURCES::spiffsRoot + "/data/favs.jsn";
};
}
}
6 changes: 3 additions & 3 deletions main/RestServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,7 @@ esp_err_t RestServer::StartRestServer() {
httpd_uri_t favorite_get_uri = {
.uri = "/api/v1/favorite*",
.method = HTTP_POST,
.handler = &RestServer::favorite_get_handler,
.handler = &RestServer::favorite_post_handler,
.user_ctx = rest_context
};
httpd_register_uri_handler(server, &favorite_get_uri);
Expand Down Expand Up @@ -849,8 +849,8 @@ esp_err_t RestServer::get_iocaps_handler(httpd_req_t *req) {
return ESP_OK;
}

esp_err_t RestServer::favorite_get_handler(httpd_req_t *req) {
ESP_LOGD("favorite_get_handler", "1: Mem freesize internal %d, largest block %d, free SPIRAM %d, largest block SPIRAM %d!",
esp_err_t RestServer::favorite_post_handler(httpd_req_t *req) {
ESP_LOGD("favorite_post_handler", "1: Mem freesize internal %d, largest block %d, free SPIRAM %d, largest block SPIRAM %d!",
heap_caps_get_free_size(MALLOC_CAP_8BIT | MALLOC_CAP_INTERNAL),
heap_caps_get_largest_free_block(MALLOC_CAP_8BIT | MALLOC_CAP_INTERNAL),
heap_caps_get_free_size(MALLOC_CAP_SPIRAM),
Expand Down
2 changes: 1 addition & 1 deletion main/RestServer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ namespace CTAG {

static esp_err_t get_preset_json_handler(httpd_req_t *req);

static esp_err_t favorite_get_handler(httpd_req_t *req);
static esp_err_t favorite_post_handler(httpd_req_t *req);

static esp_err_t set_preset_json_handler(httpd_req_t *req);

Expand Down
26 changes: 1 addition & 25 deletions main/SPManagerDataModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ respective component folders / files if different from this license.

#include <cstdio>
#include <string>
#include <fstream>
#include "SPManagerDataModel.hpp"
#include "rapidjson/filereadstream.h"
#include "rapidjson/writer.h"
Expand All @@ -32,13 +33,6 @@ respective component folders / files if different from this license.

using namespace CTAG::AUDIO;

/*
#ifndef TBD_SIM
#define SPIFFS_PATH "/spiffs"
#else
#define SPIFFS_PATH "../../spiffs_image"
#endif
*/

SPManagerDataModel::SPManagerDataModel() {
ESP_LOGI("SPModel", "Trying to read config file");
Expand Down Expand Up @@ -247,15 +241,6 @@ void SPManagerDataModel::ResetNetworkConfiguration() {
}

const char *SPManagerDataModel::GetCStrJSONSoundProcessorPresets(const string &id) {
// check if file exists
DIR *dir;
dir = opendir(string(CTAG::RESOURCES::spiffsRoot + "/data/sp/mp-" + id + ".jsn").c_str());
if (dir == NULL) {
ESP_LOGE("SPM", "Preset file for processors %s could not be opened!\n", id.c_str());
return nullptr;
}
closedir(dir);
// prepare JSON output string
json.Clear();
Document d;
loadJSON(d, CTAG::RESOURCES::spiffsRoot + "/data/sp/mp-" + id + ".jsn");
Expand All @@ -265,15 +250,6 @@ const char *SPManagerDataModel::GetCStrJSONSoundProcessorPresets(const string &i
}

void SPManagerDataModel::SetJSONSoundProcessorPreset(const string &id, const string &data) {
// check if file exists
DIR *dir;
dir = opendir(string(CTAG::RESOURCES::spiffsRoot + "/data/sp/mp-" + id + ".jsn").c_str());
if (dir == NULL) {
ESP_LOGE("SPM", "Preset file for processors %s could not be opened!\n", id.c_str());
return;
}
closedir(dir);

ESP_LOGD("Model", "String %s", data.c_str());
Document presets;
presets.Parse(data);
Expand Down
1 change: 1 addition & 0 deletions simulator/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ endif()
file(GLOB SRC_FILES *.cpp *.hpp)
set(SRC_FILES2
"../main/SPManagerDataModel.cpp"
"../main/FavoritesModel.cpp"
"fake-idf/esp_heap_caps.c"
"fake-idf/esp_spi_flash.c"
)
Expand Down
28 changes: 28 additions & 0 deletions simulator/SimSPManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ void SimSPManager::StartSoundProcessor(int iSoundCardID, string wavFile, string
spi_flash_emu_init(sromFile.c_str());
// Initialize simulator parameters
simModel = std::make_unique<SimDataModel>();
favModel = std::make_unique<FAV::FavoritesModel>();
int mode[6], value[6];
for (int i = 0; i < 6; i++) {
mode[i] = simModel->GetArrayElement("mode", i);
Expand Down Expand Up @@ -200,6 +201,11 @@ void SimSPManager::StopSoundProcessor() {

void SimSPManager::SetSoundProcessorChannel(const int chan, const string &id) {
printf("Switching plugin %d to %s", chan, id.c_str());
// does the SP exist?
if(!model->HasPluginID(id)) return;

// when trying to set chan 1 and chan 0 is a stereo plugin, return
if(chan == 1 && model->IsStereo(model->GetActiveProcessorID(0))) return;
audioMutex.lock();
sp[chan] = nullptr; // destruct smart ptr
if (model->IsStereo(id) && chan == 0) {
Expand Down Expand Up @@ -271,10 +277,32 @@ void SimSPManager::SetProcessParams(const string &params) {
stimulus.UpdateStimulus(mode, value);
}

string SimSPManager::GetAllFavorites() {
return favModel->GetAllFavorites();
}

void SimSPManager::StoreFavorite(const int &id, const string &fav) {
favModel->SetFavorite(id, fav);
}

void SimSPManager::ActivateFavorite(const int &id) {
if(id < 0 || id > 9) return;
// NOTE: all checks if plugins exists and if presets exists are done in SPManager
string p0id = favModel->GetFavoritePluginID(id, 0);
int p0pre = favModel->GetFavoritePreset(id, 0);
SetSoundProcessorChannel(0, p0id);
ChannelLoadPreset(0, p0pre);
string p1id = favModel->GetFavoritePluginID(id, 1);
int p1pre = favModel->GetFavoritePreset(id, 1);
SetSoundProcessorChannel(1, p1id);
ChannelLoadPreset(1, p1pre);
}


RtAudio SimSPManager::audio;
std::unique_ptr<ctagSoundProcessor> SimSPManager::sp[2];
std::unique_ptr<SPManagerDataModel> SimSPManager::model;
std::unique_ptr<CTAG::FAV::FavoritesModel> SimSPManager::favModel;
std::unique_ptr<SimDataModel> SimSPManager::simModel;
SimStimulus SimSPManager::stimulus;

6 changes: 6 additions & 0 deletions simulator/SimSPManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ respective component folders / files if different from this license.
#include "ctagSoundProcessorFactory.hpp"
#include "ctagSoundProcessor.hpp"
#include "SPManagerDataModel.hpp"
#include "FavoritesModel.hpp"
#include <RtAudio.h>
#include <atomic>
#include "SimDataModel.hpp"
Expand Down Expand Up @@ -92,13 +93,18 @@ namespace CTAG {
return simModel->GetModelJSONCString();
}

// favorites api
static string GetAllFavorites();
static void StoreFavorite(int const &id, const string &fav);
static void ActivateFavorite(const int &id);

private:

static void updateConfiguration();

static std::unique_ptr<ctagSoundProcessor> sp[2];
static std::unique_ptr<SPManagerDataModel> model;
static std::unique_ptr<FAV::FavoritesModel> favModel;
static RtAudio audio;

static int inout(void *outputBuffer, void *inputBuffer, unsigned int nBufferFrames,
Expand Down
40 changes: 40 additions & 0 deletions simulator/WebServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,46 @@ void WebServer::Start() {
// 1 thread is usually faster than several threads
server.config.port = 8080;

server.resource["^/api/v1/favorites/getAll"]["POST"] = [&](shared_ptr<HttpServer::Response> response, shared_ptr<HttpServer::Request> request) {
// Retrieve string:
SimpleWeb::CaseInsensitiveMultimap header;
header.emplace("Content-Type", "application/json");
response->write(SimSPManager::GetAllFavorites(), header);
};

server.resource["^/api/v1/favorites/store/([0-9])$"]["POST"] = [&](shared_ptr<HttpServer::Response> response,
shared_ptr<HttpServer::Request> request) {
// Retrieve string:
int fav = std::stoi(request->path_match[1].str());
string content = request->content.string();
SimSPManager::StoreFavorite(fav, content);
response->write(SimpleWeb::StatusCode::success_ok);
};

server.resource["^/api/v1/favorites/recall/([0-9])$"]["POST"] = [&](shared_ptr<HttpServer::Response> response,
shared_ptr<HttpServer::Request> request) {
// Retrieve string:
int fav = std::stoi(request->path_match[1].str());
SimSPManager::ActivateFavorite(fav);
response->write(SimpleWeb::StatusCode::success_ok);
};

server.resource["^/api/v1/setPresetData/(.+)"]["POST"] = [&](shared_ptr<HttpServer::Response> response, shared_ptr<HttpServer::Request> request) {
// Retrieve string:
string id = request->path_match[1].str();
string content = request->content.string();
SimSPManager::SetJSONSoundProcessorPreset(id, content);
response->write(SimpleWeb::StatusCode::success_ok);
};

server.resource["^/api/v1/getPresetData/(.+)"]["GET"] = [&](shared_ptr<HttpServer::Response> response,
shared_ptr<HttpServer::Request> request) {
string id = request->path_match[1].str();
SimpleWeb::CaseInsensitiveMultimap header;
header.emplace("Content-Type", "application/json");
response->write(string(SimSPManager::GetCStrJSONSoundProcessorPresets(id)), header);
};

server.resource["^/api/v1/srom/getSize$"]["POST"] = [](shared_ptr<HttpServer::Response> response,
shared_ptr<HttpServer::Request> request) {
response->write(to_string(1024*1024*5));
Expand Down
21 changes: 21 additions & 0 deletions tbd4vcv/src/plugin.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,24 @@
/***************
CTAG TBD >>to be determined<< is an open source eurorack synthesizer module.
A project conceived within the Creative Technologies Arbeitsgruppe of
Kiel University of Applied Sciences: https://www.creative-technologies.de
(c) 2020 by Robert Manzke. All rights reserved.
The CTAG TBD software is licensed under the GNU General Public License
(GPL 3.0), available here: https://www.gnu.org/licenses/gpl-3.0.txt
The CTAG TBD hardware design is released under the Creative Commons
Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0).
Details here: https://creativecommons.org/licenses/by-nc-sa/4.0/
CTAG TBD is provided "as is" without any express or implied warranties.
License and copyright details for specific submodules are included in their
respective component folders / files if different from this license.
***************/

#include "plugin.hpp"


Expand Down
21 changes: 21 additions & 0 deletions tbd4vcv/src/plugin.hpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,24 @@
/***************
CTAG TBD >>to be determined<< is an open source eurorack synthesizer module.
A project conceived within the Creative Technologies Arbeitsgruppe of
Kiel University of Applied Sciences: https://www.creative-technologies.de
(c) 2020 by Robert Manzke. All rights reserved.
The CTAG TBD software is licensed under the GNU General Public License
(GPL 3.0), available here: https://www.gnu.org/licenses/gpl-3.0.txt
The CTAG TBD hardware design is released under the Creative Commons
Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0).
Details here: https://creativecommons.org/licenses/by-nc-sa/4.0/
CTAG TBD is provided "as is" without any express or implied warranties.
License and copyright details for specific submodules are included in their
respective component folders / files if different from this license.
***************/

#pragma once
#include <rack.hpp>

Expand Down
21 changes: 21 additions & 0 deletions tbd4vcv/src/tbd4vcv.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,24 @@
/***************
CTAG TBD >>to be determined<< is an open source eurorack synthesizer module.
A project conceived within the Creative Technologies Arbeitsgruppe of
Kiel University of Applied Sciences: https://www.creative-technologies.de
(c) 2020 by Robert Manzke. All rights reserved.
The CTAG TBD software is licensed under the GNU General Public License
(GPL 3.0), available here: https://www.gnu.org/licenses/gpl-3.0.txt
The CTAG TBD hardware design is released under the Creative Commons
Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0).
Details here: https://creativecommons.org/licenses/by-nc-sa/4.0/
CTAG TBD is provided "as is" without any express or implied warranties.
License and copyright details for specific submodules are included in their
respective component folders / files if different from this license.
***************/

#include "plugin.hpp"
#include "WebServer.hpp"
#include "SPManager.hpp"
Expand Down

0 comments on commit 8a748be

Please sign in to comment.