Created
September 18, 2012 08:43
-
-
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
This file contains 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
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