From d5c7144e326bbae48810373068e7072f18e59038 Mon Sep 17 00:00:00 2001 From: Andrew Belt Date: Mon, 10 Feb 2020 22:16:17 -0500 Subject: [PATCH] Add Module::paramsToJson and paramsFromJson. --- include/engine/Module.hpp | 3 +- src/engine/Module.cpp | 99 +++++++++++++++++++++++---------------- 2 files changed, 61 insertions(+), 41 deletions(-) diff --git a/include/engine/Module.hpp b/include/engine/Module.hpp index 9f48d41f..1e693846 100644 --- a/include/engine/Module.hpp +++ b/include/engine/Module.hpp @@ -200,7 +200,8 @@ struct Module { Remember to call `Module::fromJson(rootJ)` within your overridden method. */ virtual void fromJson(json_t* rootJ); - + virtual json_t* paramsToJson(); + virtual void paramsFromJson(json_t* rootJ); /** Override to store extra internal data in the "data" property of the module's JSON object. */ virtual json_t* dataToJson() { return NULL; diff --git a/src/engine/Module.cpp b/src/engine/Module.cpp index dbd4a523..94ce0cde 100644 --- a/src/engine/Module.cpp +++ b/src/engine/Module.cpp @@ -14,10 +14,12 @@ struct Module::Internal { bool bypassed = false; }; + Module::Module() { internal = new Internal; } + Module::~Module() { for (ParamQuantity* paramQuantity : paramQuantities) { if (paramQuantity) @@ -34,6 +36,7 @@ Module::~Module() { delete internal; } + void Module::config(int numParams, int numInputs, int numOutputs, int numLights) { // This method should only be called once. assert(params.empty() && inputs.empty() && outputs.empty() && lights.empty() && paramQuantities.empty()); @@ -57,6 +60,7 @@ void Module::config(int numParams, int numInputs, int numOutputs, int numLights) } } + void Module::processBypass(const ProcessArgs& args) { for (BypassRoute& bypassRoute : bypassRoutes) { // Route input voltages to output @@ -71,6 +75,7 @@ void Module::processBypass(const ProcessArgs& args) { } } + json_t* Module::toJson() { json_t* rootJ = json_object(); @@ -87,19 +92,9 @@ json_t* Module::toJson() { json_object_set_new(rootJ, "model", json_string(model->slug.c_str())); // params - json_t* paramsJ = json_array(); - for (size_t paramId = 0; paramId < params.size(); paramId++) { - // Don't serialize unbounded Params - if (!paramQuantities[paramId]->isBounded()) - continue; - - json_t* paramJ = params[paramId].toJson(); - - json_object_set_new(paramJ, "id", json_integer(paramId)); - - json_array_append(paramsJ, paramJ); - } - json_object_set_new(rootJ, "params", paramsJ); + json_t* paramsJ = paramsToJson(); + if (paramsJ) + json_object_set_new(rootJ, "params", paramsJ); // bypassed if (internal->bypassed) @@ -122,6 +117,7 @@ json_t* Module::toJson() { return rootJ; } + void Module::fromJson(json_t* rootJ) { // Check if plugin and model are incorrect json_t* pluginJ = json_object_get(rootJ, "plugin"); @@ -165,33 +161,8 @@ void Module::fromJson(json_t* rootJ) { // params json_t* paramsJ = json_object_get(rootJ, "params"); - size_t i; - json_t* paramJ; - json_array_foreach(paramsJ, i, paramJ) { - // Get paramId - json_t* paramIdJ = json_object_get(paramJ, "id"); - // Legacy v0.6 to = params.size()) - continue; - - // Check that the Param is bounded - if (!paramQuantities[paramId]->isBounded()) - continue; - - json_t* valueJ = json_object_get(paramJ, "value"); - if (valueJ) - params[paramId].setValue(json_number_value(valueJ)); - } + if (paramsJ) + paramsFromJson(paramsJ); // bypassed json_t* bypassedJ = json_object_get(rootJ, "bypassed"); @@ -222,6 +193,54 @@ void Module::fromJson(json_t* rootJ) { } +json_t* Module::paramsToJson() { + json_t* rootJ = json_array(); + for (size_t paramId = 0; paramId < params.size(); paramId++) { + // Don't serialize unbounded Params + if (!paramQuantities[paramId]->isBounded()) + continue; + + json_t* paramJ = params[paramId].toJson(); + + json_object_set_new(paramJ, "id", json_integer(paramId)); + + json_array_append(rootJ, paramJ); + } + return rootJ; +} + + +void Module::paramsFromJson(json_t* rootJ) { + size_t i; + json_t* paramJ; + json_array_foreach(rootJ, i, paramJ) { + // Get paramId + json_t* paramIdJ = json_object_get(paramJ, "id"); + // Legacy v0.6 to = params.size()) + continue; + + // Check that the Param is bounded + if (!paramQuantities[paramId]->isBounded()) + continue; + + json_t* valueJ = json_object_get(paramJ, "value"); + if (valueJ) + params[paramId].setValue(json_number_value(valueJ)); + } +} + + void Module::onReset(const ResetEvent& e) { // Reset all parameters for (ParamQuantity* pq : paramQuantities) {