Skip to content

Instantly share code, notes, and snippets.

@jacobjoaquin
Created November 27, 2010 17:28
Show Gist options
  • Save jacobjoaquin/718095 to your computer and use it in GitHub Desktop.
Save jacobjoaquin/718095 to your computer and use it in GitHub Desktop.
Generates a chart of MIDI notes and associated frequencies
/* MIDI to Frequency Chart
* Jacob Joaquin <jacobjoaquin@gmail.com>
* csoundblog.com
*
* Based on Listing 1.2 of The Audio Programming Book
* Edited by Richard Boulanger and Victor Lazzarini
* Chapter 1 - Programming in C
* Richard Dobson
*/
#include <math.h>
#include <stdio.h>
#include <string.h>
double midi_to_hz(int midi_note);
void midi_to_note_octave(int midi_note, char* string);
int main()
{
int i;
char note_octave[5];
for (i = 0; i <= 127; i++)
{
midi_to_note_octave(i, note_octave);
printf("%d\t%s\t%f\n", i, note_octave, midi_to_hz(i));
}
return 0;
}
double midi_to_hz(int midi_note)
{
static const double half_step = 1.0594630943592953;
static const double midi_c0 = 8.175798915643707;
return midi_c0 * pow(half_step, midi_note);
}
void midi_to_note_octave(int midi_note, char* string)
{
static const char* notes[] = {"c", "c#", "d", "d#", "e", "f", "f#", "g",
"g#", "a", "a#", "b"};
static const int tet_12 = 12;
char buffer[3];
strcpy(string, notes[midi_note % tet_12]);
sprintf(buffer, "%d", midi_note / tet_12);
strcat(string, buffer);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment