Western music notation and scale systems are based on "octave equivalence". Sounds related by integer multiples of an octave are perceived as distinct in terms of "register" but similar in pitch quality.
In cycles per second, or Hertz, octaves are related by multiples of 2. If we wanted to raise a given pitch by three octaves, we would multiply its frequency by the factor or 8. To lower a given pitch by three octaves, its frequency would be multiplied by or 1/8.
An Equal Tempered tuning system is based on twelve "equal" divisions of an octave. But because octaves are related by factors of 2, each equal division of the octave is given by a factor of .
MIDI note numbers range from 0 to 127. We can convert a MIDI note to a frequency value, given that MIDI note number 69 represents the equal tempered frequency 440 Hz, by the formula:
where is the given MIDI note number.
The figure below plots the frequency values for MIDI note numbers 16 - 95.
Figure 12:
Frequency values for MIDI note numbers 16-95.
Despite the relative simplicity of the MIDI-to-Frequency conversion formula given above, most software systems instead use a lookup table indexed by note number to retrieve the corresponding frequency values. This makes sense because there are only 128 MIDI note values and the calculation of exponential functions is relatively "expensive" computationally.
Given the relationship above, we can convert a frequency value to a MIDI note number via the formula:
This formula can be expressed in the C programming language as shown below. It is necessary to account for quantization inaccuracies in floating-point calculations when casting to an integer result.
int n = (int) ( ( 12 * log(f / 220.0) / log(2.0) ) + 57.01 );
Given the exponential spacing of music intervals, it is difficult to use a lookup table for conversions of this type because intermediate frequency values cannot simply be rounded to the nearest table value.
The mtof object peforms control-rate MIDI note to Frequency conversion.
The ftom object performs control-rate Frequency to MIDI note conversions.