APVTS Design pattern review

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

  1. 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

};
  1. 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);
              }) {}
};
  1. 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 &parameterID, 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;
};
  1. Param Helper Functions: Simplify the creation of common parameter types:
template<typename SettingsType>
ParameterDescriptor<SettingsType> createIntParam(const juce::String &paramID, 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 &paramID,
        float SettingsType::* memberPtr) {
    return ParameterDescriptor<SettingsType>(
            paramID,
            memberPtr,
            std::function<float(float)>([](float value) { return value / 100.0f; })
    );
}

And so on for other types..

  1. 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)
      };
}
  1. 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

  1. PluginProcessor initializes the APVTS (with params) in its constructor.

  2. References to the PluginProcessor which exposes the APVTS via a getter are passed on to relevant classes

  3. 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

  1. Is this a good approach for handling the sync between GUI and audio thread in JUCE plugins? Any obvious pitfalls I’m missing here?

  2. Is there a more idiomatic JUCE way to achieve the same goals?

  3. Any suggestions for improvements to this pattern?

Thanks in advance for your insights!

  1. You pass a reference of PluginProcessor to a DSP class. I don’t think it will compile. And packing parameter creation & parameter listening into a DSP class is not a good design from my point of view.
  2. Use pure AudioProcessorValueTreeState::Listener

Thanks for reading.

a) Not sure why you say this won’t compile, it does and works (I might have some issue there, just pseudo-code i wrote for the example). Can you elaborate please?

b) Parameter creation is not done from the DSP class - just registering as a listener. If a DSP class should not be a listener, the only alternative I can think of would be passing the settings as args, i.e:

dspClass.process(....., CustomSettings settings);

which would in turn force familiarity of internal parameters to the invoker, something I’d like to avoid ideally. Why should a DSP class not be a listener?

  1. By purely I assume you mean the DSP class should inherit from it instead of ParameterBinding? Could you please explain your reasoning?
  1. Ha, I haven’t compiled it. But it seems that the class Delay is a member of PluginProcessor. I would guess there is a circular dependency. You could pass a reference of juce::AudioProcessoror juce::AudioProcessorValueTreeState instead, which does not have a method called getAPVTS().
  2. I would suggest adding a class, e.g., DelayAttach inherited from AudioProcessorValueTreeState::Listener. It should listen to the according APVTS and adjust the parameters of class Delay. In such way, you can handle thread safety issues (or other complex things, e.g., change latency) in the DelayAttach and let Delay do pure dsp processing.