How to use AudioProcessorValueTreeState's SliderAttachment?

I’m having trouble figuring out how to use AudioProcessorValueTreeState::SliderAttachment

Can somebody who knows, please list the steps needed to set this up?

i’m very lost here :disappointed_relieved:

It’s easier than you think - just create one, and while it exists, it’ll connect the slider to that value.

1 Like

do i need to add a parameter to the AudioProcessorValueTreeState, or anything else like that?

(so far, just creating the sliderAttachment with the correct args doesn’t seem to work for me)

edit: rereading the documentation, it seems obvious i have to create a parameter in the AudioProcessorValueTree…yeah?

Well, yeah, it’d be tricky to tell it which parameter to control if there isn’t one…

this is my first time trying to use any of the audioProcessorParameter classes, so obviously i’m doing something really wrong here. Here’s what i have so far:
(and sorry, it’s a button i’m trying to connect at the moment, not a slider)

in my PluginProcessor.h, as a public variable:

AudioProcessorValueTreeState mainValueTree;

and then here are the first lines of my PluginProcessor’s constructor:

PluginProcessor::PluginProcessor()
:   mainValueTree (*this, nullptr)

{
    mainValueTree.state = ValueTree ("mainValueTree");

    NormalisableRange<float> buttonRange (0, 1, 1);
    mainValueTree.createAndAddParameter ("clockForwards", "ClockForwards", "clock forwards", buttonRange, 0, nullptr, nullptr);

and then in my GUIcomponent.h

AudioProcessorValueTreeState::ButtonAttachment* clockForwardsButtonAttachment;

and then finally, in the constructor of my GUIcomponent.cpp, after the clockForwards button is added, i have:

clockForwardsButtonAttachment = new AudioProcessorValueTreeState::ButtonAttachment (p.mainValueTree, "clockForwards", *clockForwards);

this doesn’t work, and gives me a seg fault. Obviously i’m doing it wrong, but quite in the dark here how to do it right.

(btw: clockFowards is just an ordinary text button, created with the projucer)

ok, so an update here…if anyone can help me i am very grateful.

in my PluginProcessor.h, i have:

AudioProcessorValueTreeState mainValueTree;

as a public variable (will make it private with get/set methods later)
and then:

AudioProcessorParameter* clockForwards;

as a private variable.

in my PluginProcessor.cpp, i have this in the constructor:

PluginProcessor::PluginProcessor()
:   mainValueTree (*this, nullptr)

{
    NormalisableRange<float> buttonRange (0, 1, 1);
    clockForwards = mainValueTree.createAndAddParameter ("clockForwards", "ClockForwards", "clock forwards", buttonRange, 0, nullptr, nullptr);

in my MainGUIcomponent.h, i have this as a private:

AudioProcessorValueTreeState::ButtonAttachment* clockForwardsButtonAttachment;

and then this in the constructor, after the clockForwards button is created with addAndMakeVisible:

clockForwardsButtonAttachment = new AudioProcessorValueTreeState::ButtonAttachment (p.mainValueTree, "clockForwards", *clockForwards);

it all works ok if i comment out that bit. So seems i am not attaching it correctly.

please! any ideas???

It looks actually ok, the only thing to note is not to use raw pointers but a ScopedPointer, so
AudioProcessorValueTreeState::ButtonAttachment* clockForwardsButtonAttachment;
becomes
ScopedPointer<AudioProcessorValueTreeState::ButtonAttachment> clockForwardsButtonAttachment;
This is how you avoid using delete and the object is destroyed automatically.

Another thing to note is, if by chance you have already listener callbacks for your buttons, you can romove them, because the ButtonAttachment already serves as listener.

Good luck

thank you so much daniel! works perfectly now!

i wonder how this will work with the combo box’s…as the projucer doesn’t give an option to set/remove their listeners. Will try now, and report back…

removing the listeners is not compulsory here. Things still work ok if they are there…but yeah, more optimal to remove them if possible