Browse Source

Add Module::paramsToJson and paramsFromJson.

tags/v2.0.0
Andrew Belt 4 years ago
parent
commit
d5c7144e32
2 changed files with 61 additions and 41 deletions
  1. +2
    -1
      include/engine/Module.hpp
  2. +59
    -40
      src/engine/Module.cpp

+ 2
- 1
include/engine/Module.hpp View File

@@ -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;


+ 59
- 40
src/engine/Module.cpp View File

@@ -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 <v1
if (!paramIdJ)
paramIdJ = json_object_get(paramJ, "paramId");
size_t paramId;
if (paramIdJ)
paramId = json_integer_value(paramIdJ);
// Use index if all else fails
else
paramId = i;

// Check ID bounds
if (paramId >= 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 <v1
if (!paramIdJ)
paramIdJ = json_object_get(paramJ, "paramId");
size_t paramId;
if (paramIdJ)
paramId = json_integer_value(paramIdJ);
// Use index if all else fails
else
paramId = i;

// Check ID bounds
if (paramId >= 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) {


Loading…
Cancel
Save