# deciBels in NormalisableRange using lambdas

I’m trying to create a NormalisableRange which converts decibels to gain, used in a master volume slider. However, my current code produces junk values…

``````NormalisableRange<float> decibelRange = NormalisableRange<float>(-144.0f, 6.0f,
[](float start, float end, float gain) { return Decibels::gainToDecibels(gain, start); },
[](float start, float end, float dB) { return Decibels::decibelsToGain(dB, start); }
);

addParameter (gainParam = new AudioParameterFloat("volume", "Volume" , decibelRange,	-6.0f));
``````

What am I doing wrong?

Turns out I’m an idiot. Didn’t convert the decibels to normalised value in applyGain call…

How do you make the range go to +6 dB with this?
Never mind - you just multiply and divide the gain or result by 2.0f or (end/3.0f)

I’ve actually made my own conversion code, but that’s pretty much the same formula. Can’t remember why I did that, but hey.

``````auto decibelToGainLambda = [](auto min, auto end, auto dB) {
return dB > min ? std::pow(10.0f, dB * 0.05f) / Decibels::decibelsToGain(end) : 0.0;
};

auto gainToDecibelLambda = [](auto min, auto end, auto gain) {
return gain > 0.0f ? 20.0f * std::log10(gain * Decibels::decibelsToGain(end) ) : -std::numeric_limits<float>::infinity();
};``````
1 Like

Yeah I just found it thanks! You did it quite correctly by something like:-

``````	decibelNorm(-144.0f, 6.0f,
[](float start, float end, float gain) { return Decibels::gainToDecibels(gain * Decibels::decibelsToGain(end) , start); },
[](float start, float end, float dB) { return Decibels::decibelsToGain(dB, start) / Decibels::decibelsToGain(end); }
``````

edit oops same time posting.

1 Like