# How to sync two sliders in Audio Plugin?

**URL:** <https://forum.juce.com/t/how-to-sync-two-sliders-in-audio-plugin/53512>\
**Category:** Audio Plugins\
**Created:** [October 25, 2022, 3:30pm UTC](https://forum.juce.com/t/how-to-sync-two-sliders-in-audio-plugin/53512 "2022-10-25T15:30:40Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![PP02](https://avatars.discourse-cdn.com/v4/letter/p/c2a13f/32.png) [@PP02](https://forum.juce.com/u/PP02)\
**Post date:** [October 25, 2022, 3:30pm UTC](https://forum.juce.com/t/how-to-sync-two-sliders-in-audio-plugin/53512/1 "2022-10-25T15:30:41Z")

</div>

Hello All,

I am working on an Audio plugin, all the parameters are added using AudioProcessorValueTreeState --createParameterLayout() procedure.

on GUI, there are two sliders, which are needed to be in sync, if any of it moves, other should move on UI in real time,  
how to achieve this?

(I had implemented ValueTree & setProperty() once in GUI app, but not sure how to do that in Audio Plugin with AudioProcessorValueTreeState)

Any help/suggestion is appreciated, TIA!

Thanks

---

<div class="post-metadata">

**Author:** ![Rincewind](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/rincewind/32/10645_2.png) [@Rincewind](https://forum.juce.com/u/Rincewind)\
**Post date:** [October 25, 2022, 8:58pm UTC](https://forum.juce.com/t/how-to-sync-two-sliders-in-audio-plugin/53512/2 "2022-10-25T20:58:02Z")

</div>

Are both Sliders altering the same AudioParameter? If so, you can add a SliderAttachment from the APVTS to both Sliders with the same AudioParameter.

If not, you could attach a SliderListener to both sliders and every time the vale change event occurs, call the other sliders setValue function. But be careful not to run in any callback loops.

---

<div class="post-metadata">

**Author:** ![PP02](https://avatars.discourse-cdn.com/v4/letter/p/c2a13f/32.png) [@PP02](https://forum.juce.com/u/PP02)\
**Post date:** [October 26, 2022, 10:36pm UTC](https://forum.juce.com/t/how-to-sync-two-sliders-in-audio-plugin/53512/3 "2022-10-26T22:36:50Z")

</div>

Thanks @Rincewind , adding slider attachment solved the issue.  
But I need slider 2 disabled in One working mode, (Where Slider 1 is not present on the UI)  
That fails due to same Audio Parameter used in APVTS

---

<div class="post-metadata">

**Author:** ![refusesoftware](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/refusesoftware/32/20561_2.png) [@refusesoftware](https://forum.juce.com/u/refusesoftware)\
**Post date:** [October 27, 2022, 5:05pm UTC](https://forum.juce.com/t/how-to-sync-two-sliders-in-audio-plugin/53512/4 "2022-10-27T17:05:53Z")

</div>

By “disabled”, do you mean you are calling `Component::setEnabled (false)` on slider 2?

What is it that “fails” about that?

---

<div class="post-metadata">

**Author:** ![PP02](https://avatars.discourse-cdn.com/v4/letter/p/c2a13f/32.png) [@PP02](https://forum.juce.com/u/PP02)\
**Post date:** [October 27, 2022, 5:09pm UTC](https://forum.juce.com/t/how-to-sync-two-sliders-in-audio-plugin/53512/5 "2022-10-27T17:09:30Z")

</div>

@refusesoftware Something similar to it, I don’t want Slider 2 working in Mode A(say).  
Mode A has Slider 2 on UI, and slider 1 in setVisible(false),  
But, since Slider1 and Slider2 have same Audio Parameter altering, slider2 moves too (slider1 is working backend, even though setVisible(false))

---

<div class="post-metadata">

**Author:** ![refusesoftware](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/refusesoftware/32/20561_2.png) [@refusesoftware](https://forum.juce.com/u/refusesoftware)\
**Post date:** [October 27, 2022, 5:20pm UTC](https://forum.juce.com/t/how-to-sync-two-sliders-in-audio-plugin/53512/6 "2022-10-27T17:20:25Z")

</div>

OK, I think I understand better. So you want to add or remove the SliderAttachment dynamically, depending on the mode?

I haven’t tried it, but I’d guess that if the Editor had a `std::unique_ptr<AttachmentClass>` member, you could set that to `nullptr` to remove the attachment, and call `std::make_unique` to add the attachment, when switching modes?

---

<div class="post-metadata">

**Author:** ![PP02](https://avatars.discourse-cdn.com/v4/letter/p/c2a13f/32.png) [@PP02](https://forum.juce.com/u/PP02)\
**Post date:** [October 27, 2022, 5:27pm UTC](https://forum.juce.com/t/how-to-sync-two-sliders-in-audio-plugin/53512/7 "2022-10-27T17:27:47Z")

</div>

@refusesoftware That’s correct, SliderAttachment use depends on the mode.  
Can you explain more about

> [@refusesoftware](#):
>
> call `std::make_unique` to add the attachment, when switching modes?

?

---

<div class="post-metadata">

**Author:** ![refusesoftware](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/refusesoftware/32/20561_2.png) [@refusesoftware](https://forum.juce.com/u/refusesoftware)\
**Post date:** [October 27, 2022, 8:42pm UTC](https://forum.juce.com/t/how-to-sync-two-sliders-in-audio-plugin/53512/8 "2022-10-27T20:42:33Z")

</div>

@PP02 Sorry if that was a little vague. I can try to fill in the blanks a bit. Before getting more specific, it would help to know that I understand how your plugin’s Editor class is currently set up, so here’s a couple questions:

Does your Editor currently have a pair of `SliderAttachment` class members, one for each of the Sliders?

And, do you have a change listener of some kind where you are detecting changing from Mode A to Mode B?

---

<div class="post-metadata">

**Author:** ![PP02](https://avatars.discourse-cdn.com/v4/letter/p/c2a13f/32.png) [@PP02](https://forum.juce.com/u/PP02)\
**Post date:** [November 14, 2022, 8:20am UTC](https://forum.juce.com/t/how-to-sync-two-sliders-in-audio-plugin/53512/9 "2022-11-14T08:20:04Z")

</div>

@refusesoftware Apologies for delayed response, I was distracted due to some other issues and had to pause this one.  
getting back to it, SliderAttachment class members for 2 sliders – one is in Editor and other one is different Component (which is included in Editor)

And for changing from Mode A to B the button is used. (onClick)

Since, the attachments are passed in Editor constructor, it doesn’t allow anything to change when button is operated for mode change

---

<div class="post-metadata">

**Author:** ![refusesoftware](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/refusesoftware/32/20561_2.png) [@refusesoftware](https://forum.juce.com/u/refusesoftware)\
**Post date:** [November 14, 2022, 10:22pm UTC](https://forum.juce.com/t/how-to-sync-two-sliders-in-audio-plugin/53512/10 "2022-11-14T22:22:59Z")

</div>

> [@PP02](#):
>
> Since, the attachments are passed in Editor constructor, it doesn’t allow anything to change when button is operated for mode change

OK I think I understand your setup, and you’re right that a SliderAttachment can only be initialized once.

So my thought is that, instead of having the SliderAttachment be a class member, you instead have the class member be a smart pointer to a SliderAttachment, and then you can create and destroy it when you switch between modes.

So the class member would be declared as:

`std::unique_ptr<SliderAttachment> sliderTwoAttachment;`

And then in your onClick lambda for the mode button (with some pseudocode obviously, since I don’t know your other variable names):

```
if (mode == A) 
  sliderTwoAttachment = nullptr;
else
  sliderTwoAttachment = std::make_unique<SliderAttachment> (apvtsReference, "parameterFoo", sliderTwoReference);

```

I haven’t tried that myself but I think that would work.

---

<div class="post-metadata">

**Author:** ![Rincewind](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/rincewind/32/10645_2.png) [@Rincewind](https://forum.juce.com/u/Rincewind)\
**Post date:** [December 7, 2022, 1:55pm UTC](https://forum.juce.com/t/how-to-sync-two-sliders-in-audio-plugin/53512/11 "2022-12-07T13:55:26Z")

</div>

@PP02 I don’t know if that applies to you, but in case that is happening to you or anyone else:  
Reusing the same std::unique\_ptr to switch the same component to different parameters imposes a hard to catch bug. As a practical example: I’m dealing with a preset editor for an ADSR envelope. Four sliders altering a certain preset (1 of 16 selected by a combo box). So every time the user changes the preset he wants to edit, this method is called to reattach the four sliders:

```cpp
void ADSRPresetEditor::attachPresetParameters(AdsrParameterSet parameters)
{
  attackAttachment = nullptr;
  attackCurvAttachment = nullptr;
  releaseAttachment = nullptr;
  releaseCurvAttachment = nullptr;
  
  attackAttachment = std::make_unique<juce::SliderParameterAttachment>(*parameters.attack, sliderBar.sliderAttack.slider, undo);
  attackCurvAttachment = std::make_unique<juce::SliderParameterAttachment>(*parameters.attackCurve, sliderBar.sliderDecay.slider, undo);
  releaseAttachment = std::make_unique<juce::SliderParameterAttachment>(*parameters.release, sliderBar.sliderRelease.slider, undo);
  releaseCurvAttachment = std::make_unique<juce::SliderParameterAttachment>(*parameters.releaseCurve, sliderBar.sliderSustain.slider, undo);
}

```

You need to zero out the current attachments first. Otherwise the new attachment for the new parameter will be created (and by that setting the current parameter value to the slider) while the old attachment is still active - picking up on the slider value change and altering the previous parameter to the current value of the new parameter.

---

<div class="post-metadata">

**Author:** ![refusesoftware](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/refusesoftware/32/20561_2.png) [@refusesoftware](https://forum.juce.com/u/refusesoftware)\
**Post date:** [December 7, 2022, 5:10pm UTC](https://forum.juce.com/t/how-to-sync-two-sliders-in-audio-plugin/53512/12 "2022-12-07T17:10:34Z")

</div>

That’s a great point, and I can see how that would be a tricky bug to catch. I haven’t tried this myself, but it looks like if you didn’t zero out the old attachments first, then you would momentarily have two attachments in existence, between the time `std::make_unique` constructed a new SliderParameterAttachment, and assigning that to the `std::unique_ptr` member.

In @PP02 's case, I was suggesting switching the same `std::unique_ptr` between nullptr and a new attachment object, so they wouldn’t have to worry about that particular bug. But thank you for that caveat.
