MixGroupsController.cpp (3406B)
1 #include "MixGroupsController.h" 2 3 using namespace MixGroupsConstants; 4 5 MixGroupsController::MixGroupsController (AudioProcessorValueTreeState& vts, 6 AudioProcessor* proc) : vts (vts) 7 { 8 // load parameters 9 auto params = proc->getParameters(); 10 loadParameterList (params); 11 12 // set up receiver 13 sharedData->loadParameterList (paramList); 14 sharedData->addListener (this); 15 16 mixGroupParam = vts.getRawParameterValue (mixGroupParamID); 17 } 18 19 MixGroupsController::~MixGroupsController() 20 { 21 sharedData->removeListener (this); 22 } 23 24 void MixGroupsController::createParameterLayout (chowdsp::Parameters& params) 25 { 26 using namespace chowdsp::ParamUtils; 27 emplace_param<chowdsp::ChoiceParameter> (params, mixGroupParamID, "Mix Group", StringArray ({ "N/A", "1", "2", "3", "4" }), 0); 28 } 29 30 void MixGroupsController::loadParameterList (Array<AudioProcessorParameter*>& params) 31 { 32 // iterate over parameters 33 for (auto* param : params) 34 { 35 auto paramWithID = dynamic_cast<AudioProcessorParameterWithID*> (param); 36 37 if (paramWithID == nullptr) 38 continue; 39 40 auto paramID = paramWithID->paramID; 41 vts.addParameterListener (paramID, this); 42 43 if (paramID != mixGroupParamID) 44 paramList.addIfNotAlreadyThere (paramID); 45 } 46 } 47 48 void MixGroupsController::parameterChanged (const String& parameterID, float newValue) 49 { 50 if (parameterID == lastParameterChanged) // I just changed this param, don't want to get stuck in an endless loop... 51 { 52 lastParameterChanged = String(); 53 return; 54 } 55 56 int mixGroup = (int) mixGroupParam->load(); 57 58 if (parameterID == mixGroupParamID) // mix group was changed 59 { 60 sharedData->pluginGroupChanged (uuid.toString(), mixGroup); 61 62 if (mixGroup == 0) 63 return; 64 65 int numPluginsInGroup = sharedData->getNumPluginsInGroup (mixGroup); 66 if (numPluginsInGroup == 1) // I'm the only plugin in this group 67 { 68 sharedData->copyPluginState (mixGroup, vts); 69 } 70 else if (numPluginsInGroup > 1) // there are already plugins in this group 71 { 72 // copy shared state to me 73 for (const auto& paramID : paramList) 74 { 75 auto param = vts.getParameter (paramID); 76 auto value = sharedData->getParameter (paramID, mixGroup); 77 78 lastParameterChanged = paramID; 79 param->setValueNotifyingHost (param->convertTo0to1 (value)); 80 } 81 } 82 83 return; 84 } 85 86 if (mixGroup == 0) // no mix group, don't bother sending 87 return; 88 89 if (! paramList.contains (parameterID)) // parameter is not in list 90 return; 91 92 sharedData->setParameter (parameterID, mixGroup, newValue, uuid.toString()); 93 } 94 95 void MixGroupsController::mixGroupParamChanged (const String& paramID, int mixGroup, float value, String otherUuid) 96 { 97 if (uuid == otherUuid) // this message came from me! 98 return; 99 100 // load parameter 101 auto param = vts.getParameter (paramID); 102 if (param == nullptr) // invalid parameter 103 return; 104 105 if (mixGroup != (int) mixGroupParam->load()) // received message does not apply to this mix group 106 return; 107 108 // set parameter value 109 lastParameterChanged = paramID; 110 param->setValueNotifyingHost (param->convertTo0to1 (value)); 111 }