Swapping AudioProcessorValueTreeState::SliderAttachments


#1

I have a bunch of Sliders and Buttons that are connected to my AudioProcessorValueTreeState with SliderAttachments, etc, and I’d like to be able to change the parameters that the sliders & buttons are attached to based on another button.

So:

Switch = off
Slider1 attached to Osc1 Volume
Slider2 attached to Osc1 Pan

Switch = on
Slider1 attached to Osc2 Volume
Slider2 attached to Osc2 Pan

I’m guessing that when the user presses Switch, I would delete the current Attachments and create new ones and vice versa. Is that correct? Am I missing any gotchas?

As I think about it a little more, I’d prefer if there was a way to switch without deleting and creating new Attachments every time.

Thanks.


#2

Correct, You don’t need to create a new set of attachments with every button click.
Instead you can leave the attachments connected to the sliders and on each button click just

  • deregister the attachment’s ParameterID
  • swap the ParameterID within the attachment
  • register the new ParameterID
  • then reinitialize the slider with the current parameter value

I have done something very similar with a custom label attachment:
https://github.com/Soundbytes/Juce-Attachments
Check out the SliderMonitor example. To enable Parameter swapping I added the AttachedControlBase::replaceListener() member function in juce_AudioProcessorValueTreeState.h (that is the version of the file that comes with my examples.)

Note that I have made some further changes to the AudioProcessorValueTreeState class that will break compatability with existing presets. If you want to preserve the Preset compatability and still create your own attachments You need to modify the factory supplied versions of the juce_AudioProcessorValueTreeState.cpp/h files.
Just move the class/struct definitions of AudioProcessorValueTreeState::Parameter and AttachedControlBase over to the header file and you are done.
You will then be able to create your own Attachments within your user code.


#3

Thanks for that, but now, after a little more thought, I’m thinking that it just might be cleaner to make 2 sets of sliders with their proper attachments and just have my switch control the visibility of the 2 sets…turning one set visible and the other hidden.