How to set an initial value to an Slider?

I want to set my Gain Slider to an initial value of 1.0 but if I used the ‘setValue()’ method I cannot drag/move that slider anymore.
How should I set that slider’s initial value?

Thanks so much

Set a default value for your parameter, attach the parameter to your Slider, don’t set a value directly-let the attachment handle it.

Rail

1 Like

I have set the parameter to a default value. The problem is that parameter lives in the Audio Processor class and the slider lives in the Editor class.

My code in the Editor is:

Blockquote
void CircularAudioBufferAudioProcessorEditor::sliderValueChanged(Slider* slider)
{
if (slider == &sldrDelayGain)
{
audioProcessor.setDelayGain(sldrDelayGain.getValue());
}
}

In the Audio Processor class I set that value as my delayGain parameter by implementing the function setDelayGain().
Thus, the default value of that parameter doesn’t affect the slider.

https://docs.juce.com/master/classAudioProcessorValueTreeState_1_1SliderAttachment.html

Rail

2 Likes

I typically give the slider its own variable within the editor and set it to said variable upon class initialization. Then, on the AudioProcessor side, I have a complimentary variable and then have a get() and set() function to communicate between the editor and the processor.

For example, if it’s a volume slider, on the editor’s side I would have

private:
    juce::Slider volumeSlider;
    float sliderVol = 1.f;

    PluginAudioProcessor& audioProcessor; 

with the initialization as

PluginProcessorEditor::PluginProcessorEditor(PluginAudioProcessor& p)
{

    /*
    *
    * All other slider initalization
    *
    */

    volumeSlider.setValue(sliderVol, juce::NotificationType::dontSendNotification);
}

then on the processor side

    float& getVolume() { return volume; }
    void setVolume(const float vol) { volume = vol; }

private:
    float volume = 1.f;

which will then allow you to do

void PluginProcessorEditor::sliderValueChanged(Slider* slider)
{
    if (slider == &volumeSlider)
    {
         sliderVol = volumeSlider.getValue();
         audioProcessor.setVolume(sliderVol);
    }
}

Obvs this is a very simplified example. As @railjonrogut said, the correct way to do it is with an AudioProcessorValueTreeState as not only will this keep everything in sync but also will allow you to save the parameter in between sessions in your DAW as well as allow for parameter automation from the DAW.

The Audio Programmer has a great tutorial on sliders, slider attachments, and the AudioProcessorValueTreeState in his Sampler VST tutorial.

However, if you want to keep it simple just for quick learning/experimenting, the above example will work.