# deciBels in NormalisableRange using lambdas

**URL:** https://forum.juce.com/t/decibels-in-normalisablerange-using-lambdas/26379
**Category:** Audio Plugins
**Created:** [February 12, 2018, 6:39pm UTC](https://forum.juce.com/t/decibels-in-normalisablerange-using-lambdas/26379 "2018-02-12T18:39:16Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![meneervermeer](https://avatars.discourse-cdn.com/v4/letter/m/3da27b/32.png) [@meneervermeer](https://forum.juce.com/u/meneervermeer)
#### Post date: [February 12, 2018, 6:39pm UTC](https://forum.juce.com/t/decibels-in-normalisablerange-using-lambdas/26379/1 "2018-02-12T18:39:16Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![meneervermeer](https://avatars.discourse-cdn.com/v4/letter/m/3da27b/32.png) [@meneervermeer](https://forum.juce.com/u/meneervermeer)
#### Post date: [February 12, 2018, 9:05pm UTC](https://forum.juce.com/t/decibels-in-normalisablerange-using-lambdas/26379/2 "2018-02-12T21:05:01Z")

</div>

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

---

<div class="post-metadata">

### Author: ![DaveH](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/daveh/32/4924_2.png) [@DaveH](https://forum.juce.com/u/DaveH)
#### Post date: [February 19, 2019, 7:12pm UTC](https://forum.juce.com/t/decibels-in-normalisablerange-using-lambdas/26379/3 "2019-02-19T19:12:35Z")

</div>

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)

---

<div class="post-metadata">

### Author: ![meneervermeer](https://avatars.discourse-cdn.com/v4/letter/m/3da27b/32.png) [@meneervermeer](https://forum.juce.com/u/meneervermeer)
#### Post date: [February 19, 2019, 7:25pm UTC](https://forum.juce.com/t/decibels-in-normalisablerange-using-lambdas/26379/4 "2019-02-19T19:25:02Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![meneervermeer](https://avatars.discourse-cdn.com/v4/letter/m/3da27b/32.png) [@meneervermeer](https://forum.juce.com/u/meneervermeer)
#### Post date: [February 19, 2019, 7:27pm UTC](https://forum.juce.com/t/decibels-in-normalisablerange-using-lambdas/26379/5 "2019-02-19T19:27:02Z")

</div>

```
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(); 
};
```

---

<div class="post-metadata">

### Author: ![DaveH](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/daveh/32/4924_2.png) [@DaveH](https://forum.juce.com/u/DaveH)
#### Post date: [February 19, 2019, 7:28pm UTC](https://forum.juce.com/t/decibels-in-normalisablerange-using-lambdas/26379/6 "2019-02-19T19:28:55Z")

</div>

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

```auto
	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. 🙂
