Created
February 18, 2017 04:03
-
-
Save fuzzblob/168a11d4690d2267a98632e7e681e552 to your computer and use it in GitHub Desktop.
A little collection of handy conversions to do randomisation and control audio properties
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
public class BasicAudioConversions | |
{ | |
private static float twelfthRootOfTwo = Mathf.Pow(2, 1.0f / 12); | |
public static float St2pitch(float st) | |
{ | |
return Mathf.Clamp(Mathf.Pow(twelfthRootOfTwo, st), 0f, 4f); | |
} | |
public static float Pitch2st(float pitch) | |
{ | |
return Mathf.Log(pitch, twelfthRootOfTwo); | |
} | |
public static float DecibelToLinear(float dB) | |
{ | |
if (dB > -80) | |
return Mathf.Clamp01(Mathf.Pow(10.0f, dB / 20.0f)); | |
else | |
return 0; | |
} | |
public static float LinearToDecibel(float linear) | |
{ | |
if (linear > 0) | |
return Mathf.Clamp(20.0f * Mathf.Log10(linear), -80f, 0f); | |
else | |
return -80.0f; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment