From 9cd43f8217db8a6dc12b49bfb032128c194f49f7 Mon Sep 17 00:00:00 2001 From: Paul Walker Date: Thu, 16 Jan 2025 14:31:55 -0500 Subject: [PATCH] MOve all the non-SixSines-specific patch logic to plugininfra Observation part one: things like 'have a patch' and 'write it to the state' should be shared. This gets us there. --- CMakeLists.txt | 6 +- libs/CMakeLists.txt | 3 +- libs/sst/sst-plugininfra | 2 +- src/synth/patch.cpp | 161 --------------------------------------- src/synth/patch.h | 43 +++-------- 5 files changed, 17 insertions(+), 198 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 9288acb..1924e75 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -91,7 +91,11 @@ target_link_libraries(${PROJECT_NAME}-impl PRIVATE mts-esp-client fmt-header-only sst-basic-blocks sst-voicemanager sst-jucegui sst-cpputils - sst-plugininfra sst-plugininfra::filesystem sst-plugininfra::tinyxml sst-plugininfra::strnatcmp + sst-plugininfra + sst-plugininfra::filesystem + sst-plugininfra::tinyxml + sst-plugininfra::strnatcmp + sst-plugininfra::patchbase sst::clap_juce_shim sst::clap_juce_shim_headers ${PROJECT_NAME}-patches samplerate diff --git a/libs/CMakeLists.txt b/libs/CMakeLists.txt index 8f56cc2..17fce9b 100644 --- a/libs/CMakeLists.txt +++ b/libs/CMakeLists.txt @@ -13,7 +13,8 @@ add_subdirectory(sst/sst-jucegui) add_subdirectory(sst/sst-voicemanager) add_subdirectory(sst/sst-cpputils) -set(SST_PLUGININFRA_PROVIDE_TINYXML ON CACHE BOOL "no xml") +set(SST_PLUGININFRA_PROVIDE_TINYXML ON CACHE BOOL "yesxml") +set(SST_PLUGININFRA_PROVIDE_PATCHBASE ON CACHE BOOL "patchbase pls") add_subdirectory(sst/sst-plugininfra) add_library(mts-esp-client STATIC MTS-ESP/Client/libMTSClient.cpp) diff --git a/libs/sst/sst-plugininfra b/libs/sst/sst-plugininfra index 163a595..82a351a 160000 --- a/libs/sst/sst-plugininfra +++ b/libs/sst/sst-plugininfra @@ -1 +1 @@ -Subproject commit 163a595a5104a690eea83b7a502afd4383e3505d +Subproject commit 82a351ad46507e8d78e191864a6f814fcd41f6be diff --git a/src/synth/patch.cpp b/src/synth/patch.cpp index 00b949f..134dd0f 100644 --- a/src/synth/patch.cpp +++ b/src/synth/patch.cpp @@ -14,170 +14,9 @@ */ #include "patch.h" -#include -#include "tinyxml/tinyxml.h" - namespace baconpaul::six_sines { -void Patch::resetToInit() -{ - dirty = false; - // Sweep any new param since this stream to default - for (auto [id, p] : paramMap) - { - p->value = p->meta.defaultVal; - } - strncpy(name, "Init", 255); -} -std::string Patch::toState() const -{ - TiXmlDocument doc; - TiXmlElement rootNode("patch"); - rootNode.SetAttribute("id", "org.baconpaul.six-sines"); - rootNode.SetAttribute("version", Patch::patchVersion); - rootNode.SetAttribute("name", name); - - TiXmlElement paramsNode("params"); - - for (auto p : params) - { - TiXmlElement param("p"); - param.SetAttribute("id", p->meta.id); - param.SetDoubleAttribute("v", p->value); - paramsNode.InsertEndChild(param); - } - - rootNode.InsertEndChild(paramsNode); - doc.InsertEndChild(rootNode); - - std::ostringstream oss; - oss << doc; - return oss.str(); -} -bool Patch::fromState(const std::string &idata) -{ - resetToInit(); - if (idata.substr(0, 12) == "SIXSINES;V=1") - { - return fromStateV1(idata); - } - - TiXmlDocument doc; - doc.Parse(idata.c_str()); - - auto rn = doc.FirstChildElement("patch"); - if (!rn) - { - SXSNLOG("No Patch"); - return false; - } - if (strcmp(rn->Attribute("id"), "org.baconpaul.six-sines") != 0) - { - SXSNLOG("Wrong ID"); - return false; - } - int ver; - if (rn->QueryIntAttribute("version", &ver) != TIXML_SUCCESS) - { - SXSNLOG("No Version"); - return false; - } - if (ver < 2 || ver > Patch::patchVersion) - { - SXSNLOG("Unknown version " << ver); - return false; - } - if (rn->Attribute("name")) - { - memset(name, 0, sizeof(name)); - strncpy(name, rn->Attribute("name"), 255); - } - - auto pars = rn->FirstChildElement("params"); - if (!pars) - { - SXSNLOG("No Params"); - return false; - } - - auto *par = pars->FirstChildElement("p"); - while (par) - { - int id; - double value; - if (par->QueryIntAttribute("id", &id) == TIXML_SUCCESS && - par->QueryDoubleAttribute("v", &value) == TIXML_SUCCESS) - { - auto it = paramMap.find(id); - if (it == paramMap.end()) - { - // SXSNLOG(" - vestigal param " << id); - } - else - { - auto *param = it->second; - value = migrateParamValueFromVersion(param, value, ver); - it->second->value = value; - } - } - else - { - SXSNLOG("Par missing id or value"); - } - par = par->NextSiblingElement("p"); - } - - if (ver != patchVersion) - migratePatchFromVersion(ver); - return true; -} - -bool Patch::fromStateV1(const std::string &idata) -{ - std::string data = idata; - auto p = data.find('\n'); - bool first{true}; - while (p != std::string::npos && !data.empty()) - { - auto ss = data.substr(0, p); - if (first) - { - if (ss != "SIXSINES;V=1") - { - SXSNLOG("Bad version string [" << ss << "]"); - return false; - } - first = false; - } - else - { - auto sl = ss.find('|'); - auto id = ss.substr(0, sl); - auto v = ss.substr(sl + 1); - - auto idv = std::atoi(id.c_str()); - auto vv = std::atof(v.c_str()); - - auto it = paramMap.find(idv); - if (it == paramMap.end()) - { - // SXSNLOG("Ignoring vestigal param " << idv); - } - else - { - auto *param = it->second; - vv = migrateParamValueFromVersion(param, vv, 1); - it->second->value = vv; - } - } - data = data.substr(p + 1); - p = data.find('\n'); - } - - return true; -} - float Patch::migrateParamValueFromVersion(Param *p, float value, uint32_t version) { if ((p->adhocFeatures & Param::AdHocFeatureValues::ENVTIME) && version <= 2) diff --git a/src/synth/patch.h b/src/synth/patch.h index 742f7ee..2e794bd 100644 --- a/src/synth/patch.h +++ b/src/synth/patch.h @@ -25,18 +25,18 @@ #include "sst/cpputils/constructors.h" #include "sst/basic-blocks/params/ParamMetadata.h" #include "sst/basic-blocks/modulators/DAHDSREnvelope.h" +#include "sst/plugininfra/patch-support/patch_base.h" #include "synth/matrix_index.h" #include "dsp/sintable.h" namespace baconpaul::six_sines { namespace scpu = sst::cpputils; +namespace pats = sst::plugininfra::patch_support; using md_t = sst::basic_blocks::params::ParamMetaData; -struct Param +struct Param : pats::ParamBase { - Param(const md_t &m) : value(m.defaultVal), meta(m) {} - float value{0}; - const md_t meta{}; + Param(const md_t &m) : pats::ParamBase(m) {} Param &operator=(const float &val) { @@ -44,8 +44,6 @@ struct Param return *this; } - operator const float &() const { return value; } - uint64_t adhocFeatures{0}; enum AdHocFeatureValues : uint64_t { @@ -54,12 +52,10 @@ struct Param }; }; -struct Patch +struct Patch : pats::PatchBase { - bool dirty{false}; static constexpr uint32_t patchVersion{8}; - std::vector params; - std::unordered_map paramMap; + static constexpr const char *id{"org.baconpaul.six-sines"}; static constexpr uint32_t floatFlags{CLAP_PARAM_IS_AUTOMATABLE}; static constexpr uint32_t boolFlags{CLAP_PARAM_IS_AUTOMATABLE | CLAP_PARAM_IS_STEPPED}; @@ -73,29 +69,15 @@ struct Patch static md_t intMd() { return md_t().asInt().withFlags(boolFlags); } Patch() - : sourceNodes(scpu::make_array_bind_first_index()), + : pats::PatchBase(), + sourceNodes(scpu::make_array_bind_first_index()), selfNodes(scpu::make_array_bind_first_index()), matrixNodes(scpu::make_array_bind_first_index()), mixerNodes(scpu::make_array_bind_first_index()), macroNodes(scpu::make_array_bind_first_index()), fineTuneMod("Fine Tune Mod", "Fine Tune", 0), mainPanMod("Main Pan Mod", "Main Pan", 1) { - auto pushParams = [this](auto &from) - { - auto m = from.params(); - params.insert(params.end(), m.begin(), m.end()); - for (auto &p : m) - { - if (paramMap.find(p->meta.id) != paramMap.end()) - { - SXSNLOG("Duplicate param id " << p->meta.id); - SXSNLOG(" - New Param : '" << p->meta.name << "'"); - SXSNLOG(" - Other Param : '" << paramMap[p->meta.id]->meta.name << "'"); - std::terminate(); - } - paramMap.emplace(p->meta.id, p); - } - }; + auto pushParams = [this](auto &from) { this->pushMultipleParams(from.params()); }; pushParams(output); std::for_each(sourceNodes.begin(), sourceNodes.end(), pushParams); @@ -1242,15 +1224,8 @@ struct Patch ModulationOnlyNode fineTuneMod, mainPanMod; - void resetToInit(); - std::string toState() const; - bool fromState(const std::string &); - char name[256]{"Init"}; - private: - bool fromStateV1(const std::string &); - float migrateParamValueFromVersion(Param *p, float value, uint32_t version); void migratePatchFromVersion(uint32_t version); };