Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MOve all the non-SixSines-specific patch logic to plugininfra #131

Merged
merged 1 commit into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion libs/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
161 changes: 0 additions & 161 deletions src/synth/patch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,170 +14,9 @@
*/

#include "patch.h"
#include <sstream>
#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)
Expand Down
43 changes: 9 additions & 34 deletions src/synth/patch.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,27 +25,25 @@
#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)
{
value = val;
return *this;
}

operator const float &() const { return value; }

uint64_t adhocFeatures{0};
enum AdHocFeatureValues : uint64_t
{
Expand All @@ -54,12 +52,10 @@ struct Param
};
};

struct Patch
struct Patch : pats::PatchBase<Patch, Param>
{
bool dirty{false};
static constexpr uint32_t patchVersion{8};
std::vector<const Param *> params;
std::unordered_map<uint32_t, Param *> 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};
Expand All @@ -73,29 +69,15 @@ struct Patch
static md_t intMd() { return md_t().asInt().withFlags(boolFlags); }

Patch()
: sourceNodes(scpu::make_array_bind_first_index<SourceNode, numOps>()),
: pats::PatchBase<Patch, Param>(),
sourceNodes(scpu::make_array_bind_first_index<SourceNode, numOps>()),
selfNodes(scpu::make_array_bind_first_index<SelfNode, numOps>()),
matrixNodes(scpu::make_array_bind_first_index<MatrixNode, matrixSize>()),
mixerNodes(scpu::make_array_bind_first_index<MixerNode, numOps>()),
macroNodes(scpu::make_array_bind_first_index<MacroNode, numMacros>()),
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);
Expand Down Expand Up @@ -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);
};
Expand Down
Loading