Hi JUCE community,
I’m in the process of building my first plugin and having a blast! This forum has been amazing at providing guidance, so thank you all kindly..
After reading a bunch of different posts on techniques for using the APVTS, I admit I still felt a little bothered by the amount of code I have to place in DSP classes which require multiple parameters (subscribing, filtering, setting locally, etc’).
I implemented an approach which utilizes template-based parameter binding of member pointers, and I’d appreciate your feedback on the design.
As this is my first experience with both JUCE and C++, I’m quite unsure if this is valid/robust.
The design aims to reduce boilerplate code as much as possible, as well as provide type-safe bulk registration using setting structs.
Core Components
- Settings Structs: Type-safe containers for related parameters:
struct MidiSettings {
float probability = 100.0f; // 0-100% chance of triggering a note
RhythmMode rhythmMode = RHYTHM_NORMAL;
float barProbability = 0.0f;
// ... other settings
};
- Parameter Descriptors: Define mappings between APVTS parameters and settings struct members:
template<typename SettingsType>
struct ParameterDescriptor {
juce::String paramID;
std::function<void(SettingsType &, float)> setter;
template<typename ValueType>
ParameterDescriptor(juce::String id,
ValueType SettingsType::* memberPtr,
std::function<ValueType(float)> converter)
: paramID(std::move(id)),
setter([memberPtr, converter](SettingsType &settings, float value) {
settings.*memberPtr = converter(value);
}) {}
};
- Parameter Binding Class: Connects APVTS to settings structs and handles updates:
template<typename SettingsType>
class ParameterBinding : public juce::AudioProcessorValueTreeState::Listener {
public:
ParameterBinding(SettingsType &settingsStruct, juce::AudioProcessorValueTreeState &apvts)
: settings(settingsStruct), audioParamsTree(apvts) {
}
~ParameterBinding() {
removeAllListeners();
}
void registerParameter(const ParameterDescriptor<SettingsType> &descriptor) {
parameterMap[descriptor.paramID] = descriptor.setter;
audioParamsTree.addParameterListener(descriptor.paramID, this);
// Initialize with current value
if (auto *param = audioParamsTree.getParameter(descriptor.paramID))
descriptor.setter(settings, param->getValue());
}
void registerParameters(const std::vector<ParameterDescriptor<SettingsType>> &descriptors) {
for (const auto &descriptor: descriptors)
registerParameter(descriptor);
}
void parameterChanged(const juce::String ¶meterID, float newValue) override {
auto it = parameterMap.find(parameterID);
if (it != parameterMap.end())
it->second(settings, newValue);
}
void removeAllListeners() {
for (const auto &mapping: parameterMap)
audioParamsTree.removeParameterListener(mapping.first, this);
parameterMap.clear();
}
private:
SettingsType &settings;
juce::AudioProcessorValueTreeState &audioParamsTree;
std::unordered_map<juce::String, std::function<void(SettingsType &, float)>> parameterMap;
};
- Param Helper Functions: Simplify the creation of common parameter types:
template<typename SettingsType>
ParameterDescriptor<SettingsType> createIntParam(const juce::String ¶mID, int SettingsType::* memberPtr) {
return ParameterDescriptor<SettingsType>(
paramID,
memberPtr,
std::function<int(float)>([](float value) { return static_cast<int>(value); })
);
}
template<typename SettingsType>
ParameterDescriptor<SettingsType> createPercentageParam(
const juce::String ¶mID,
float SettingsType::* memberPtr) {
return ParameterDescriptor<SettingsType>(
paramID,
memberPtr,
std::function<float(float)>([](float value) { return value / 100.0f; })
);
}
And so on for other types..
- Settings Helper Functions: Simplify the attachment of a settings struct to a group of parameters.
// Create a set of delay parameters
inline std::vector<ParameterDescriptor<Models::DelaySettings>> createDelayParameters() {
return {
createPercentageParam<Models::DelaySettings>(ID_DELAY_MIX, &Models::DelaySettings::delayMix),
createPercentageParam<Models::DelaySettings>(ID_DELAY_FEEDBACK, &Models::DelaySettings::delayFeedback),
createPercentageParam<Models::DelaySettings>(ID_DELAY_RATE, &Models::DelaySettings::delayRate),
createBoolParam<Models::DelaySettings>(ID_DELAY_PING_PONG, &Models::DelaySettings::delayPingPong),
createBoolParam<Models::DelaySettings>(ID_DELAY_BPM_SYNC, &Models::DelaySettings::delayBpmSync)
};
}
- Util for creating a typed ParameterBinding:
template<typename SettingsType>
std::unique_ptr<ParameterBinding<SettingsType>>
createParameterBinding(SettingsType &settings, juce::AudioProcessorValueTreeState &apvts) {
return std::make_unique<ParameterBinding<SettingsType>>(settings, apvts);
}
Initialization Flow
-
PluginProcessor initializes the APVTS (with params) in its constructor.
-
References to the PluginProcessor which exposes the APVTS via a getter are passed on to relevant classes
-
A ParameterBinding class member is created in the constructor of a DSP class, and immediately registers param(s) to listen to.
class Delay {
public:
Delay(PluginProcessor& processor) : processor(processor) {
paramBinding = AppState::createParameterBinding<DelaySettings>(settings, processor.getAPVTS());
paramBinding->registerParameters(AppState::createDelaySettings());
}
void doSomething() {
// Use settings.delayMix, settings.delayFeedback, etc..
}
private:
PluginProcessor& processor;
std::unique_ptr<AppState::ParameterBinding<DelaySettings>> paramBinding;
DelaySettings settings;
};
Data Flow:
-
GUI components modify APVTS parameter values
-
APVTS notifies ParameterBinding instances (as they are Listeners)
-
ParameterBinding updates the corresponding member in the settings struct using the member pointer.
-
Audio code reads values directly from the local settings struct
Questions for Feedback
-
Is this a good approach for handling the sync between GUI and audio thread in JUCE plugins? Any obvious pitfalls I’m missing here?
-
Is there a more idiomatic JUCE way to achieve the same goals?
-
Any suggestions for improvements to this pattern?
Thanks in advance for your insights!