Search code examples
mathclojuresignal-processing

Sound Frequency to Octave and Key Number


Given the Frequency of a wave, determine the octave from 0 to 7 and the key number, where 0 is C, 6 is B, C# is 7 and A# is 11.

You could solve it with a series of if statements, but there has to be a better way to do it. The language my project uses is Clojure, but I my guess is that any solution can easily be ported to any language.


Solution

  • The following assumes you are using a tempered chromatic scale. Musical notes have a logarithmic scale of frequency. Every octave up multiplies frequencies by two. Within an octave, there are twelve equal proportional steps for notes C, C#, D, D#, E, F, F#, G, G#, A, A#, B, which are numbered from zero to eleven.

    To get the octave number and the note number from the frequency:

    • Take the log of the frequency to base 2.
    • Subtract the log of the frequency of middle C.
      • The whole number part is the octave number;
      • the (positive) remainder, multiplied by twelve, is the note number. This will not be exact.

    Notes below middle C have a negative octave number.