# How to use AudioProcessorValueTreeState's SliderAttachment?

**URL:** <https://forum.juce.com/t/how-to-use-audioprocessorvaluetreestates-sliderattachment/17720>\
**Category:** Audio Plugins\
**Created:** [May 16, 2016, 6:48am UTC](https://forum.juce.com/t/how-to-use-audioprocessorvaluetreestates-sliderattachment/17720 "2016-05-16T06:48:44Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![mashu](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/mashu/32/579_2.png) [@mashu](https://forum.juce.com/u/mashu)\
**Post date:** [May 16, 2016, 6:48am UTC](https://forum.juce.com/t/how-to-use-audioprocessorvaluetreestates-sliderattachment/17720/1 "2016-05-16T06:48:44Z")

</div>

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 😥

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [May 16, 2016, 6:49am UTC](https://forum.juce.com/t/how-to-use-audioprocessorvaluetreestates-sliderattachment/17720/2 "2016-05-16T06:49:37Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mashu](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/mashu/32/579_2.png) [@mashu](https://forum.juce.com/u/mashu)\
**Post date:** [May 16, 2016, 6:52am UTC](https://forum.juce.com/t/how-to-use-audioprocessorvaluetreestates-sliderattachment/17720/3 "2016-05-16T06:52:28Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [May 16, 2016, 7:07am UTC](https://forum.juce.com/t/how-to-use-audioprocessorvaluetreestates-sliderattachment/17720/4 "2016-05-16T07:07:22Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mashu](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/mashu/32/579_2.png) [@mashu](https://forum.juce.com/u/mashu)\
**Post date:** [May 16, 2016, 7:41am UTC](https://forum.juce.com/t/how-to-use-audioprocessorvaluetreestates-sliderattachment/17720/5 "2016-05-16T07:41:59Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![mashu](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/mashu/32/579_2.png) [@mashu](https://forum.juce.com/u/mashu)\
**Post date:** [May 16, 2016, 1:39pm UTC](https://forum.juce.com/t/how-to-use-audioprocessorvaluetreestates-sliderattachment/17720/6 "2016-05-16T13:39:31Z")

</div>

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???

---

<div class="post-metadata">

**Author:** ![daniel](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/daniel/32/790_2.png) [@daniel](https://forum.juce.com/u/daniel)\
**Post date:** [May 16, 2016, 5:27pm UTC](https://forum.juce.com/t/how-to-use-audioprocessorvaluetreestates-sliderattachment/17720/7 "2016-05-16T17:27:13Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mashu](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/mashu/32/579_2.png) [@mashu](https://forum.juce.com/u/mashu)\
**Post date:** [May 17, 2016, 8:03am UTC](https://forum.juce.com/t/how-to-use-audioprocessorvaluetreestates-sliderattachment/17720/8 "2016-05-17T08:03:38Z")

</div>

thank you so much daniel! works perfectly now!

> [@daniel](#):
>
> 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.

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…

---

<div class="post-metadata">

**Author:** ![mashu](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/mashu/32/579_2.png) [@mashu](https://forum.juce.com/u/mashu)\
**Post date:** [July 13, 2016, 2:41pm UTC](https://forum.juce.com/t/how-to-use-audioprocessorvaluetreestates-sliderattachment/17720/9 "2016-07-13T14:41:45Z")

</div>

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