Script snippet - convert linear parameter to decibels / db

Hey y’all!
Sharing a little helper today. When controlling gain parameters from inside scripts, one has to specify a linear float value which GP will internally convert to a logarithmic dB scale.
So let’s say you want to set a GP gain control to 0 dB, you first have to look up the linear value for that which is about 0.71.

I was tired of it, so I figured out the math and wrote utility functions for converting between “param” value and dB. So instead of setting a gain slider to 0db, connecting a widget and checking out the linear param equivalent, this scripts it as dbToFloat(0.0).

Function floatToDb( val:Double ) Returns Double
  result = ((Log(val)+0.3) * 40.0) - 6.0
End
Function dbToFloat( val:Double ) Returns Double
  result = Power(10.0, (((val + 6)/40) - 0.3))
End

Happy scripting!

2 Likes

Always nice to have this somewhere… no need to think about it anymore. Thank you for that! :wink:

1 Like

Great idea. These are useful enough that I’ve added them to the system function library - they’ll appear in the next release.

I did rename the functions and added a test to make sure that we don’t get a crash if you pass in a value of 0 or less causing a crash since one can’t take the log of a value <= 0

screenshot_8275

4 Likes