[SOLVED] Slider TextBox does not update until clicked

there is probably a very simple answer to this…

i have a regular Slider called sliderFeedback.
in the PluginEditor constructor, i re-wrote textFromValueFunction & valueFromTextFunction so i could display some nice percentage values.

//parameter value ranges from 0.0 - 0.6
sliderFeedback.textFromValueFunction = [](double value) //values between 0 - 0.6
{
    double feedback = jmap(value, 0.0, 0.6, 0.0, 100.0); //value between 0 - 100
    return juce::String(feedback); //prints 0 - 100%
};

sliderFeedback.valueFromTextFunction = [](const String &text) //text between 0 - 100
{
    double feedbackPercent = text.getDoubleValue(); //value between 0 - 100
    double feedback = jmap(feedbackPercent, 0.0, 100.0, 0.0, 0.6); //value between 0 - 0.6
    return feedback; //returns 0 - 0.6
};

it works perfectly - once i click on the sliders to change their value.
until then, the text displayed in the TextBox is the default value the slider receives from the parameter (displays 0.3% instead of 50%)…

i was wondering how can they show the correct percentage value since the start of the plugin?

thanks in advance!

If you use the AudioValueTreeState and the SliderAttachment, simply move your textFromValueFunction lambda into the AudioParameterFloat. It will be propagated automatically to the Slider.

That way the initial update of the Attachment will also set the text correctly

1 Like

yes!!! thank you, daniel! it works perfectly. i didn’t know about lambda functions and what not, it’s indeed quite handy.

i’ll just post the code here so it (hopefully) helps people.

Lf_delayAudioProcessor::Lf_delayAudioProcessor()
    // ifndef's & endif's .....
    AudioProcessor(),
    //instantiate parameters
    tree(*this, nullptr, "PARAMETERS", {
        //Feedback
        std::make_unique<AudioParameterFloat>(FEEDBACK_ID, FEEDBACK_NAME, NormalisableRange<float>(0.0f, FEEDBACK_MAX), 0.28f, String(), AudioProcessorParameter::genericParameter,
            //textFromValue
            [](float value, int) //values between 0 - MAX
            {
                float feedback = (int) jmap(value, 0.0f, FEEDBACK_MAX, 0.0f, 100.0f); //value between 0 - 100
                return juce::String(feedback, 0); //returns value between 0 - MAX
            },
            //valueFromText
            [](const String &text) //text between 0 - 100
            {
                float feedbackPercent = text.getDoubleValue(); //value between 0 - 100
                float feedback = jmap(feedbackPercent, 0.0f, 100.0f, 0.0f, FEEDBACK_MAX); //value between 0 - MAX
                return feedback; //returns value between 0 - MAX
            } 
        ),

         //other parameters here...
    })
#endif
{
   //....
}

the only thing i had to change from the code i initially posted was the function parameters received (for the textFromValue functions), to match with the ones that JUCE Documentation provides for AudioParameterFloat (link)

cheers!