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, or semitones, of an octave. But because octaves are related by factors of 2, each equal division of the octave is given by a factor of .
The frequencies of semitones in equal-tempered tuning must be determined with respect to a reference frequency, the most common one being A4 = 440 Hz (the A above middle C on a piano keyboard).
Tuning is often specified in terms of cents, with 100 cents per semitone and 1200 cents per octave. To determine the number of cents a given frequency value () in Hertz is from a reference frequency (
), the following formula would be used:
As an example, a frequency of 453 Hz is 50.4 cents above A4, which is a quarter-tone sharp. But while a quarter-tone always corresponds to 50 cents above or below an equal-tempered semitone, the number of Hz corresponding to a quarter-tone increases with frequency (13 Hz above A4 is a quarter tone sharp but only an eighth tone sharp in the next octave).
MIDI note numbers, which correspond to equal-tempered semitones, 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, using the formula:
where is the given MIDI note number.
The figure below plots the frequency values for MIDI note numbers 16 - 95.
Figure 8:
Frequency values for MIDI note numbers 16-95.
Despite the relative simplicity of the MIDI-to-frequency conversion formula given above, software systems often 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.