Click here to Skip to main content
14,036,509 members
Click here to Skip to main content
Add your own
alternative version


226 bookmarked
Posted 2 Jan 2009
Licenced MIT

FFT Guitar Tuner

, 10 Aug 2010
Rate this:
Please Sign up or sign in to vote.
Using a Fast Fourier Transform to calculate the fundamental frequency of the captured audio sound


This article shows how to use a Fast Fourier Transform (FFT) algorithm to calculate the fundamental frequency of a captured audio sound. Also, we will see how to apply the algorithm to analyze live sound to build a simple guitar tuner: the code provides a solution to the problem of calculation of the fundamental frequency of the played pitch.


The computer can capture live sound/music using a microphone that is connected to the sound card. Modern sound cards can capture digital signals. A digital signal is a set of quantized sound values that were taken in uniformly spaced times. The digital signal does not provide any information about frequencies that are present in the sound. To determine that, the data need to be analyzed.

The Short-Time Fourier Transform (STFT) makes representation of the phase and magnitude of the signal. The result of the STFT can be used to produce the spectrogram of the signal: the magnitude squared over time and frequencies. We will use a Fast Fourier Transform (FFT) to generate the spectrogram of the signal of short periods of time. After the spectrogram is calculated, the fundamental frequency can be determined by finding the index of the maximum value of the magnitude squared. The improved algorithm finds several such places, candidate frequency bins, with the magnitude squared in the top of the maximum values, and further analyzes them to verify the candidate fundamental frequencies by using the signal data.

When a note is played on a musical instrument, the sound waves are generated by strings, air, or the speaker - an instrument generates a musical note. One of the characteristics of a musical note is a pitch (fundamental frequency). Traditionally musical alphabet frequencies are divided by octaves, and then by semitones. An octave has 12 named pitches: C (prime), C#, D, D#, E, F, F#, G, G#, A, A#, and B. Octaves also have names: great, small, one-lined, two-lined, etc. The "standard pitch" (A one-lined or A4) has a fundamental frequency of its sound waves equals to 440 Hz. The frequencies of two neighboring notes are different by 21/12, and frequencies of the notes with the same name in two neighboring octaves are different by 2.

Table: Notes and Their Fundamental Frequencies
Note NameTraditional Octave Names (Scientific), Hz
Great (2)Small (3)One-lined (4)Two-lined (5)

The typical (six string) guitar normally plays pitches of great through two-lined octaves. The pitches of the open strings (E2, A2, D3, G3, B3, and E4) are selected in the table in bold.

Using the Code

The solution contains three projects: the main windows application (FftGuitarTuner), the sound analysis library (SoundAnalysis), and the sound capture library (SoundCapture). The heart of the solution and the SoundAnalysis project is the FFT algorithm (see the Calculate method of the SoundAnalysis.FftAlgorithm class):

// bit reversal
ComplexNumber[] data = new ComplexNumber[length];
for (int i = 0; i < x.Length; i++)
    int j = ReverseBits(i, bitsInLength);
    data[j] = new ComplexNumber(x[i]);

// Cooley-Tukey 
for (int i = 0; i < bitsInLength; i++)
    int m = 1 << i;
    int n = m * 2;
    double alpha = -(2 * Math.PI / n);

    for (int k = 0; k < m; k++)
        // e^(-2*pi/N*k)
        ComplexNumber oddPartMultiplier = 
           new ComplexNumber(0, alpha * k).PoweredE();

        for (int j = k; j < length; j += n)
            ComplexNumber evenPart = data[j];
            ComplexNumber oddPart = oddPartMultiplier * data[j + m];
            data[j] = evenPart + oddPart;
            data[j + m] = evenPart - oddPart;

// calculate spectrogram
double[] spectrogram = new double[length];
for (int i = 0; i < spectrogram.Length; i++)
    spectrogram[i] = data[i].AbsPower2();

The data for the algorithm is provided from the sound card capture buffer. The abstract SoundCapture.SoundCaptureBase utility class is an adapter for DirectSound's Capture and CaptureBuffer classes, that helps to encapsulate buffering and setting up the audio format parameters. The application requires Microsoft DirectX 9 runtime components for the live sound capture from the microphone.

Main Application Form

Figure: Main Application Form

After the application is started, select the sound device and play a note. The application will capture the live sound and will calculate the current fundamental frequency of the signal. The information can be used to tune the guitar.

Points of Interest

To calculate the Fast Fourier Transform, the Cooley-Tukey algorithm was used. It gives good performance for the required task. To challenge the algorithm, the application analyses about 22,000 sample blocks in real time: the sound is captured at a 44,100 Hz rate and a 16 bits sample size, and the analysis is performed twice a second.

The sound analysis library can be used for tone, background noise, sound, or speech detection. Series of the spectrogram of the continued sound can be displayed as a 2D (or 3D) image to present it visually.


  1. "Musical Note", Wikipedia
  2. "Short-Time Fourier Transform", Wikipedia
  3. "Fast Fourier Transform", Wikipedia
  4. "Cooley-Tukey FFT Algorithm", Wikipedia


  • 1st January, 2009: Initial version
  • 2nd January, 2009: Added algorithm code snippet
  • 3rd August, 2010: Corrected article typos; new frequency detection algorithm


This article, along with any associated source code and files, is licensed under The MIT License


About the Author

Software Developer
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

QuestionSetup Pin
Member 1295200216-Jan-17 0:13
memberMember 1295200216-Jan-17 0:13 
AnswerRe: Setup Pin
ProcopioPi29-Oct-18 21:16
memberProcopioPi29-Oct-18 21:16 
QuestionPoint FFT Pin
Member 1167218922-May-15 21:12
memberMember 1167218922-May-15 21:12 
QuestionFFT Guitar Tuner not work in win7 64bit & visual studio 2010 Pin
lpln85211-Aug-14 21:00
memberlpln85211-Aug-14 21:00 
AnswerRe: FFT Guitar Tuner not work in win7 64bit & visual studio 2010 Pin
Member 106723433-Jun-15 9:28
memberMember 106723433-Jun-15 9:28 
QuestionProject within a project Pin
Member 1053139926-Apr-14 6:07
memberMember 1053139926-Apr-14 6:07 
GeneralMy vote of 1 Pin
Fabrizio Stellato12-Jan-14 3:28
memberFabrizio Stellato12-Jan-14 3:28 
QuestionHow hard it would be to redo this project for java ? Pin
YuriyKravets25-Nov-13 6:00
memberYuriyKravets25-Nov-13 6:00 
AnswerGot it working on XP x64 under .NET 4.0 Pin
bmac20-Jul-13 5:54
memberbmac20-Jul-13 5:54 
Here's how I got it to run on XP x64:

A couple of notes first:
. Big shout-out to notmasteryet for this truly excellent project.
. I don't use Visual Studio, so all compiling is done by calling msbuild on the three csprojs from the command-line.
. No changes were made to the SoundAnalysis or SoundCapture projects or to the solution file.
. I used the 64bit version of msbuild but I'd be surprised if the 32bit version didn't work, too.
. This very purposefully generates a 32bit exe because Managed DirectX does not appear to have a 64bit version; if you open both the 32bit and 64bit DirectX Control Panel utilities (under "Programs/DirectX SDK June 2010/DirectX Utilities" and "[same]/DirectX Utilities (64-bit)"), the 5th and final tab is "Managed" which only has entries in the 32bit version.
. The 32bit issue is separate from the fact that the DirectSound DLL is dependent upon v1.1 of .NET, which requires the BindAsLegacyV2Runtime rigmorole in Program.cs and the addition of the specially-crafted app.config file -- actually I've left it an exercise for the reader to determine if both are actually needed.
. For now, all I've got is XP x64.

1) Add this after the last using in Program.cs
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

2) Add this before static class Program in Program.cs
    // This dominating code worked the first time (let's not quibble on the formatting today :-) and is courtesy:
    public static class RuntimePolicyHelper
        public static bool LegacyV2RuntimeEnabledSuccessfully { get; private set; }
        static RuntimePolicyHelper()
            ICLRRuntimeInfo clrRuntimeInfo =
                LegacyV2RuntimeEnabledSuccessfully = true;
            catch (COMException)
                // This occurs with an HRESULT meaning
                // "A different runtime was already bound to the legacy CLR version 2 activation policy."
                LegacyV2RuntimeEnabledSuccessfully = false;
        private interface ICLRRuntimeInfo
            void xGetVersionString();
            void xGetRuntimeDirectory();
            void xIsLoaded();
            void xIsLoadable();
            void xLoadErrorString();
            void xLoadLibrary();
            void xGetProcAddress();
            void xGetInterface();
            void xSetDefaultStartupFlags();
            void xGetDefaultStartupFlags();

            [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
            void BindAsLegacyV2Runtime();

3) Create FftGuitarTuner\app.config (in the same folder as FftGuitarTuner.csproj), making sure to create it as UTF8 to match the XML declaration encoding
<?xml version ="1.0" encoding="utf-8"?>
  <startup useLegacyV2RuntimeActivationPolicy="true">
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0" />

4) Add the following to FftGuitarTuner.csproj before the <Import Project ... line
  <None Include="app.config" />

5) Edit FftGuitarTuner.csproj as such:
a) On line #2, change the ToolsVersion value from "3.5" to "4.0"
b) On line #13, change "v3.5" to "v4.0"
c) Delete the three lines {36,39,42} that contain "<requiredtargetframework>3.5"

6) Call msbuild from a cmd prompt as follows to force the exe to be treated strictly as a 32-bit exe, after, of course, you cd into the folder where FftGuitarTuner.csproj is
"C:\WINDOWS\Microsoft.NET\Framework64\v4.0.30319\MSBuild.exe" /property:PlatformTarget=x86 FftGuitarTuner.csproj

Robert's Robot Double
"Lasting peace and happiness for *ALL* human beings."

modified 20-Jul-13 12:15pm.

GeneralRe: Got it working on XP x64 under .NET 4.0 Pin
Achmad Rahman M10-Apr-17 7:30
memberAchmad Rahman M10-Apr-17 7:30 
GeneralMy vote of 4 Pin
saddam Escape2-Apr-13 10:51
membersaddam Escape2-Apr-13 10:51 
GeneralMy vote of 5 Pin
Abdullatif M. Abu Al Rub2-Mar-13 0:31
memberAbdullatif M. Abu Al Rub2-Mar-13 0:31 
GeneralMy vote of 5 Pin
vard9912-Jan-13 13:06
membervard9912-Jan-13 13:06 
QuestionBuilding as 4.0 Pin
theney11-Dec-12 7:04
membertheney11-Dec-12 7:04 
AnswerRe: Building as 4.0 Pin
Member 869236731-Jan-13 8:52
memberMember 869236731-Jan-13 8:52 
GeneralRe: Building as 4.0 Pin
bmac20-Jul-13 5:58
memberbmac20-Jul-13 5:58 
QuestionProblem with Fft Algorithm Pin
Member 963903729-Nov-12 1:14
memberMember 963903729-Nov-12 1:14 
Question? Windows 7 Pin
rspercy6528-Nov-12 4:19
memberrspercy6528-Nov-12 4:19 
AnswerRe: ? Windows 7 Pin
lalitsrana3-Jan-13 20:46
memberlalitsrana3-Jan-13 20:46 
GeneralRe: ? Windows 7 Pin
Member 869236731-Jan-13 8:41
memberMember 869236731-Jan-13 8:41 
GeneralRe: ? Windows 7 Pin
bmac20-Jul-13 5:58
memberbmac20-Jul-13 5:58 
QuestionRequest Help on using this code to measure bicycle spoke tesnion Pin
Member 934224119-Aug-12 15:37
memberMember 934224119-Aug-12 15:37 
QuestionNot working as expected Pin
ZeAleks24-May-12 9:39
memberZeAleks24-May-12 9:39 
GeneralMy vote of 1 Pin
vartika sharma25-Apr-12 2:23
membervartika sharma25-Apr-12 2:23 
QuestionHELP please: How to modify this to work for 2 channels (stereo) Pin
Member 813676915-Feb-12 4:02
memberMember 813676915-Feb-12 4:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web04 | 2.8.190424.1 | Last Updated 10 Aug 2010
Article Copyright 2009 by notmasteryet
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid