Skip to content

Instantly share code, notes, and snippets.

@mkgin
Created May 14, 2020 10:27
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 mkgin/f932d766e1d3b09b08241ed025bb4c86 to your computer and use it in GitHub Desktop.
Save mkgin/f932d766e1d3b09b08241ed025bb4c86 to your computer and use it in GitHub Desktop.
Calculate and print table of notes and frequencies for MIDI note numbers
#include <stdio.h>
#include <math.h>
/***********
* Calculate and print note names, octaves and frequency for MIDI notes
*
* gcc -lm -o midi_note_list midi_note_list.c
*
* UNLICENCE (see end)
*
************/
void main()
{
float frequency;
int midinote ;
//char notes[12][20] = {"A","A#","B","C","C#","D","D#","E","F","F#","G","G#"};
char notes[12][20] = {"A","B♭","B","C","D♭","D","E♭","E","F","G♭","G","A♭"};
int noteindex ;
int noteoctave ;
printf("midi note frequency \n");
for ( midinote = 0; midinote < 128 ; midinote++)
{
//calc frequency
// f = 440 * 2 ^ (n -69)/12
frequency = 440 * powf( 2, ( ((float) midinote - 69)/12 )) ;
noteoctave = (( midinote - 12) / 12); // calculate octave #
noteindex = (( midinote + 3) % 12); // calculate name of note
if (midinote >= 12)
{
printf("%3d %2s%d %8.2f\n", midinote, notes[noteindex],noteoctave,frequency);
}
else
{
printf("%3d --- %8.2f\n", midinote, notes[noteindex], noteoctave,frequency);
}
}
}
/*
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to <https://unlicense.org>
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment