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.
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:
Notes below middle C have a negative octave number.