Music Programming

Frequency Calculation
'''Note: the following assumes that the MIDI note number for C3 is 48, which corresponds with Wikipedia. However, most software, including Ableton Live and GarageBand assumes that the Midi note number for C3 is 60.'''

The well-tempered tuning allows calculation of a frequency (f) from a note number (n) and an octave (o):
 * $$f(n,o) = \sqrt[4]{2} \cdot 27.5\ \mbox{Hz} \cdot 2^{n/12+o}$$,

where $$n=0$$ for a C, and the $$o=3$$ for the middle C (approx. 261.6 Hz)

Ruby example, calculating C3: n=0;o=3;2**(0.25)*27.5*2**(n/12+o)

In MIDI, C-1 is 0, and C3 is 48, so a midi frequency calculator could look like this: n=48;2**(0.25)*27.5*2**(n/12.0-1) Since $$\sqrt[4]{2} \cdot 27.5 \approx 32.70319566257483$$, we can simplify: n=48;32.7031956625748*2**(n/12.0-1)

In Python: note(note, octave): return 32.70319566257483 * 2**(note / 12. + octave)

In C: double midi2freq(int note, int octave) { return 32.70319566257483 * pow(2, note / 12. + octave); }
 * 1) include

Frequency table: C3    C#3      D3     D#2      E3      F3     F#3      G3     G#3     A3    A#3      B3 261.62  277.18  293.66  311.12  329.63  349.23  370.00  392.00  415.30   440  466.18  493.90

Examples
See the Python Music example. Note that this example uses $$o=0$$ for the middle C.