building an instrument tuner

2018

electronic tuners are useful devices that help musicians tune their instruments by telling them what pitch or note they're currently playing. Gadgets exists for this, as well as hundreds of amusingly skeuomorphic mobile apps. In this article I investigate what it takes to make one.

If you want to skip the discussion and just use the tool, it's at https://sa.lj.am/tuna.

Tone, pitch, and timbre

Before we dive into the mechanics of how to estimate the pitch of a note, let's revisit the fundamentals.

When a player plucks a string on a guitar, a sound comes out. Sound is a wave, and as such it has frequencies. In order to talk about them, musicians assigned these different frequencies names like Do, Re, Mi, etc. They also arranged them in a cyclic fashion, so that if you double the frequncy of any note, you wrap around to the same note an *octave* higher. In the same way, if you half the frequency of Do, you get Do again an octave lower.

Why double and not, say, use a period of 100Hz for the octave? It has to do with the fact that the human ear precieves difference in pitch logarithmically to the difference in frequency. Doubling a frequency, in particular, make another sounds that is distinctivly consonant with the original note, making it an easy separator for humans.

What frequency exactly each of these names has assigned to it changed over time, as different tuning systems came in and out of fashion. These days, equal temperament is the most common system of tuning.

The equal temprement tuning is called that because it divides the octave into 12 equal parts, along the same logarithmic scale of the octave. Since the ratio of a note and the same note an octave above is 2, the ratio of each of these notes to the next 12th of the octave is 2^(1/12).

first attempt: fourier transform

waveform of some wave, sampled at 44100Hz:

transformed into the frequency domain:

the peak is at hz.

more resiliant approach: auto-correlation

yin paper

same waveform as above:

the autocorrelation, stopping at the halfway point:

the fundamental frequency estimate is hz.

final piece: pitch frequency to note name

the estimated note is .