__Introduction__

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*

__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?

The code for this tuner works as follows:

- Perform an FFT of the audio signal
- Search through the resulting array for frequencies for the loudest (largest amplitude) frequency
- Determine which equal tempered note the frequency is closest to
- We assume that the frequencies for an equal tempered scale have been saved in a list when the program was launched
- We loop through the list and look for the note with the closest frequency
- Determine how many cents off the frequency is
- If the note is flat (i.e. the frequency is less than the closest tone)
- Start from the note below the reference note and calculate the frequency for each cent
- Loop through each cent and find the one that is closest to the frequency we're tuning
- If the note is sharp (i.e. the frequency is greater than the closest tone)
- Start from the reference note and calculate the frequency for each cent
- 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
- Draw the spectrum diagram and the tuning information to the screen

__Code:__

__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.

Have fun!

## No comments :

## Post a Comment