deciBels in NormalisableRange using lambdas

#1

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?

0 Likes

#2

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

0 Likes

#3

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)

0 Likes

#4

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.

0 Likes

#5
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

#6

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. :slight_smile:

1 Like