Adding parameters post AudioProcessorValueTreeState construction

I’m trying to add parameters programmatically (i.e. when configured to do so). But AudioProcessorValueTreeState::createAndAddParameter cannot add parameters for a “valid” object (jassert (! state.isValid()); ) and the state is set to valid on construction. So basically, createAndAddParameter is never valid to be called.
How should I go about to add parameters after the constructor of AudioProcessorValueTreeState?

You shouldn’t, really. That pattern is ‘two stage initialisation’ which is easy to get wrong, for example if you forget to signal that the initialisation has been completed.

The APVTS has two constructors: one which takes a ParameterLayout, and one which does not. The one which doesn’t also doesn’t set the state to valid, so you could use that (old) constructor, call createAndAddParameter a few times, and then finally set the state to valid. However, as I said above, this approach is not recommended, and createAndAddParameter may be removed in a future JUCE update. Instead, it’s better to write a helper function to create a ParameterLayout, and to pass the resulting layout to the constructor of the APVTS.

Ok, not too fond of that. Ignoring the jassert OTOH seems to work just fine, I get the parameters shown and all notifications seem to work.

Had a similar question recently:

Just posting for reference and to add those answers. I didn’t find a solution. But you seem to have though.

It’s worth pointing out that JUCE itself does not support adding/removing parameters after the construction of the AudioProcessor. This will probably not be handled gracefully by the plugin wrappers or by the plugin hosting interfaces. If you need ‘dynamic’ parameters you could create a large number of parameters up-front, when you create the APVTS, and then map their values onto different parts of your inner processing depending on the current configuration.

Adding/removing parameters on an AudioProcessor after construction is not recommended.

1 Like

I opted to create the ParameterLayout object based on a property file, so it works fine now according to the “correct” way to do it :slight_smile: