Skip to content

Instantly share code, notes, and snippets.

@gmuller
Created March 3, 2011 15:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gmuller/852903 to your computer and use it in GitHub Desktop.
Save gmuller/852903 to your computer and use it in GitHub Desktop.
C Code snippet to generate midi to freq, and vice versa
#include <stdio.h>
#include <math.h>
int main(){
double semitone_ratio, c0, c5, frequency, fracmidi;
int midinote = 73;
semitone_ratio = pow(2, 1/12.0);
c5 = 220.0 * pow(semitone_ratio, 3);
c0 = c5 * pow(0.5, 5);
frequency = c0 * pow(semitone_ratio, midinote);
//and in reverse
fracmidi = log(frequency / c0) / log(semitone_ratio);
midinote = ( int)(fracmidi + 0.5);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment