Skip to content

Instantly share code, notes, and snippets.

@mazbox
Created September 18, 2012 08:43
Show Gist options
  • Save mazbox/3742083 to your computer and use it in GitHub Desktop.
Save mazbox/3742083 to your computer and use it in GitHub Desktop.
Midi to Frequency, Frequency to Midi, stolen from Puredata + freq to note name
float mtof(float f)
{
return (8.17579891564 * exp(.0577622650 * f));
}
float ftom(float f)
{
return (17.3123405046 * log(.12231220585 * f));
}
string freqToMidiString(float hz) {
static char *names[12] = {"C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"};
int midinote = (int) ftom(hz);
int note = midinote % 12;
int octave = midinote / 12;
octave -= 1;
char name[5];
sprintf(name, "%s%d", names[note], octave);
string out = name;
return out;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment