Skip to content

Instantly share code, notes, and snippets.

@made-indrayana
Last active August 6, 2021 15:10
Show Gist options
  • Save made-indrayana/c4155a4b052501edb05479c42c7b477f to your computer and use it in GitHub Desktop.
Save made-indrayana/c4155a4b052501edb05479c42c7b477f to your computer and use it in GitHub Desktop.
Small utilities to convert Linear value (0-1) to dB and vice versa - Yes I'm looking at you Unity - as well as FMODs Internal Bus Volume
// LinearDecibelConverter.cs
// Author: Made Indrayana
// MIT License
// Small utilities to convert Linear value (0-1) to dB and vice versa
public class LinearDecibelConverter
{
public float LinearToDecibel(float linear)
{
float dB;
if (linear != 0)
dB = 20.0f * Mathf.Log10(linear);
else
dB = -144.0f;
return dB;
}
public float DecibelToLinear(float dB)
{
float linear = Mathf.Pow(10.0f, dB / 20.0f);
return linear;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment