Skip to content

Instantly share code, notes, and snippets.

@fuzzblob
Created March 6, 2017 20:05
Show Gist options
  • Save fuzzblob/10a8854e485b0a077b82144f8dd426b0 to your computer and use it in GitHub Desktop.
Save fuzzblob/10a8854e485b0a077b82144f8dd426b0 to your computer and use it in GitHub Desktop.
A small helper class to help deal with midi information
public class MidiHelper
{
static string[] __notesShatp = new string[] { "C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B" };
static string[] __notesFlat = new string[] { "C", "Db", "D", "Eb", "E", "F", "Gb", "G", "Ab", "A", "Bb", "B" };
public static string MidiCodeToString(int midiCode, bool flats = false)
{
int octave = midiCode / 12 - 1;
if (octave < 0)
return "";
int absNote = midiCode % 12;
if (flats)
{
return string.Format("{0}-{1}", __notesFlat[absNote], octave.ToString());
}
return string.Format("{0}-{1}", __notesShatp[absNote], octave.ToString());
}
public static int FrequencyToClosestMidiCode(float frequency)
{
float mid = 69 + (12 * (Mathf.Log(frequency / 440, 2f)));
return Mathf.RoundToInt(mid);
}
public static float FrequencyToMidiCode(float frequency, float tuningA = 440f)
{
return 69 + (12 * (Mathf.Log(frequency / tuningA, 2f)));
}
public static float MidiCodeToFrequency(float midicode, float tuningA = 440f)
{
return (tuningA / 32) * (Mathf.Pow(2f, (midicode - 9f) / 12));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment