I am creating a tuner on android, I get the audio from the device's microphone and proccess it to get the frequency. However, once I get to transforming the frequency on a note, I wonder if there's a way to do it without having to use a bunch of "IF's", is there a calculation the does this in a more efficient way?
Calcute note based on frequency
1.4k Views Asked by hsteffano At
2
There are 2 best solutions below
0

The musical scale is logarithmic (base-2): An increase in pitch of an octave doubles the fundamental frequency, and decrease of an octave halves it.
In the western european music tradition, assuming equal temperament, we divide the octave into semitones. The frequencies of semitones form a geometric sequence whose ratio is ~1.06
(more precisely, it's the 12th root of 2 or pow(2, 1/12)
. Each semitone can be further divided into cents, yielding precisely 1200
per octave.
Current musical practice is for A4 (the A above middle C) to be precisely 440Hz
If you can't work out the maths from first principles, it's documented here.
Yes, this can be done. First however you must know the key in which to work. Assuming plain C major you'd start with the note a1 being 440Hz. Every octave up doubles the frequency, every octave down halves it, so you've got a nice logarithmic scale, with base 2.
Within a logarithmic scale, multiplications turn into additions. So if you take the intervals of the notes _ with respect to a the ratio between the frequencies remains the same for all octaves. So all you've to find out, which octave you're in, subtract or add that number and the residual is the logarithm of the frequency interval. Finding the octave is simple as well, just evaluate floor(log2(f/440Hz)).