Friday, October 5, 2012

Tuner: Fun with FFTs

I'm very interested in computers, electronics, and music, and one of the many way to analyze music and sound is with a Fourier Transform. A simple way to think about Fourier Transforms is that it converts a signal into its constituent frequencies, effectively showing the spectrum of frequencies the signal produces at any given time.

The only problem with Fourier Transforms is that to do them symbolically, that is, to create an equation that describes the frequency spectrum, is tricky and computationally intensive. The Fast Fourier Transform (FFT) algorithm solves this problem. Similar to the how a computer can perform numeric integration, a FFT estimates the frequency spectrum of an audio signal and produces values rather than an equation.

The result of an FFT

Processing includes minim, a fantastic library for jumping into audio analysis, which has a robust FFT algorithm built in! To look more in depth about what we can do with FFTs, I decided to make an electronic tuner.

How Do Most Tuners Work?
Most instruments, such as pianos, trumpets, violins, etc., produce more that one frequency at once--even when you play just one note. The reason for this is that they all produce sound by vibrating something (usually a string or a column of air) which produces overtones that are also known as harmonics. These overtones are spaced apart by octaves (harmonics) so you don't typically hear them, however, these harmonics are what distinguishes between two instruments playing the same note (think about how you can hear the difference between clarinet and a violin). Therefore, in order to tune a note, we need to look at the loudest (the frequency with the highest amplitude) frequency that we see in the spectrum.

Once we know the frequency that we're tuning, we need to compare it to the ideal frequency for the note. The frequencies of notes for the traditional chromatic (equal-tempered) scale can be calculated with this equation. Note that this equation is not linear. This means that the number of hertz between each note is not constant. This will become more important when we try to calculate how many cents sharp or flat a note is.

How Does This Tuner Work?

Screen Shot 2012-10-05 at 10.13.25 AM
The code for this tuner works as follows:
  1. Perform an FFT of the audio signal
  2. Search through the resulting array for frequencies for the loudest (largest amplitude) frequency
  3. Determine which equal tempered note the frequency is closest to
    1. We assume that the frequencies for an equal tempered scale have been saved in a list when the program was launched
    2. We loop through the list and look for the note with the closest frequency 
  4. Determine how many cents off the frequency is
    1. If the note is flat (i.e. the frequency is less than the closest tone)
      1. Start from the note below the reference note and calculate the frequency for each cent 
      2. Loop through each cent and find the one that is closest to the frequency we're tuning
    2. If the note is sharp (i.e. the frequency is greater than the closest tone)
      1. Start from the reference note and calculate the frequency for each cent
      2. Loop through the cents from the reference note to the note above it and find the one that is closest to the frequency we're tuning
  5. Draw the spectrum diagram and the tuning information to the screen

Closing Thoughts:
This tuner is not perfect. One bug I found is that seems to be consistently be ~40 cents off in its analysis. I tested this by creating a 440hz sine wave using SuperCollider and then routing the audio internally using SoundFlower to the tuner. Additionally, this code can be optimized to run faster. The logic can be cleaned up a bit so that you don't have loop through every frequency. Faster code means more sampling which means better tuning.

Screen Shot 2012-10-05 at 10.36.51 AM
Have fun!