Skip to content

Instantly share code, notes, and snippets.

@luke161
Last active October 14, 2016 15:52
Show Gist options
  • Save luke161/0979c5fcb518a741abe022711b19c2d2 to your computer and use it in GitHub Desktop.
Save luke161/0979c5fcb518a741abe022711b19c2d2 to your computer and use it in GitHub Desktop.
Util class for converting between a linear volume (0..1) and decibel volume in Unity.
/**
* VolumeUtils.cs
* Author: Luke Holland (http://lukeholland.me/)
*/
using UnityEngine;
public static class VolumeUtils
{
public static float ConvertLinearToDB(float linear)
{
return linear<=0 ? -144.0f : 20f * Mathf.Log10(linear);
}
public static float ConvertDBToLinear(float dB)
{
return Mathf.Clamp01( Mathf.Pow(10.0f, dB/20.0f) );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment