Created
March 3, 2011 15:15
-
-
Save gmuller/852903 to your computer and use it in GitHub Desktop.
C Code snippet to generate midi to freq, and vice versa
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
#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