Display params as percentage

Let say I have these params :

:AudioParameterFloat>("DRIVE", "Drive", -50.f, 3.f, -20.f))
:AudioParameterFloat>("RANGE", "Range", 0.f, 4.f, 1.f))

And I want the user to see the value as percentage,For example if range = 2, then present it as 50%,
I also need to export this value to the host in the same way, and when reading automation to do the opposite?

is there a Juce function I’m missing here? How would go about implementing something like that?

yep. so here’s the thing. APF has different constructors. one of them is this one:
https://docs.juce.com/master/classAudioParameterFloat.html#a965f23eef0d80c0407753d004cb5ce8f

it lets you specify functions for valueToString and stringToValue. in order to have a good percentage parameter you’d now let you parameter go from 0 to 1 (normalized), but have the valueToString function always paint String(value * 100.f) + " %" so that it looks like it goes from 0 to 100. then you’d map the output of that parameter in processBlock instead of in the parameter itself before using it in the given dsp architecture.

here’s what that function can look like (but not exactly the same because the lambda of AudioParameterFloat also expects a 2nd parameter int for the string’s length. i wrote my own parameter class, which doesn’t need that): Project/Param.cpp at master · Mrugalla/Project · GitHub

make sure to accompany every valueToString function also with a stringToValue function. those are mostly used when the user uses the textbox of a slider to input a value. there a string like “87%” would be converted to .87f

here’s a video i recently made about to construct such a lambda, but not for percentages, but for midi note related parameters, so that was a bit more complex but very fun to make:

1 Like

Thanks a lot! that’s exactly what I needed

1 Like