• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0

Pitch Estimation Algorithm

1 post in this topic


I'm trying to implement an algorithm that, given human singing samples captured from a mic, tell the singing note.
I'm no math expert, but I've read quite a lot of material on this topic the past weeks. I have an algorithm working but with a few problems, that I don't know how to solve them.

Basically what I do is:

1) Apply a hamming window to raw input data. (1024 samples from mono mic input at 44100Hz rate)
2) Apply FFT
3) Get magnitude and true frequency (frequency computed from the bin frequency + phase offset) for each bin
4) Return the frequency of the bin with greatest magnitude.

I'm using this tool to test my results: [url="http://www.seventhstring.com/tuningfork/tuningfork.html"]http://www.seventhst...tuningfork.html[/url]
I place the mic next to the speaker to capture input. I've noticed that this estimation depends on how far is the mic from my speakers. If I put it right in front of it I get right results for tones greater than D2 (~293Hz) even with speakers volumes at the minimum. Below that freq it gives me completely wrong values. If I move the mic 5 inches away of the speakers I start getting wrong values from below G2 (~392Hz) even if the speaker volumes are at the maximum.

It seems something is either wrong with my algorithm or my mic, or both.

The algorithm follows, pehaps you could shed some light on it:

// Using portaudio to capture mic Input. This callback is called each frame with inputBuffer filled with raw data.
int paCallback(const void* inputBuffer, void* outputBuffer, unsigned long framesPerBuffer,
const PaStreamCallbackTimeInfo* timeInfo, PaStreamCallbackFlags statusFlags,
void* userData)
float* input = (float*) inputBuffer;
double* data = (double*)userData;
if(input != NULL)
for(unsigned int i = 0; i < FFT_N; i++)
// apply hamming window
data[2 * i] = input[i] * fc.getWindow(i);
data[2 * i + 1] = 0.0;
double freq = 0.0;
double ampl = 0.0;

// fc is a global instance of Analyzer class
fc.analyze(data, FFT_N, SAMPLE_RATE, 1, &freq, &ampl);
double db = log10(ampl);
printf("%-10s | %8.3lfHz | %5.2lfdB | %lf\n", Note::noteName(freq), freq, db, ampl);

return 0;

[code]void FrequencyCounter::analyze(double* data, unsigned long nn, double sampleRate, int overlapFactor, double* outFreq, double* outMagnitude)
// daniel-lanzos algorithm (reverse binary reindexing dark magic)
fft(data, FFT_N);

// Precalculated constants
const double freqPerBin = sampleRate / FFT_N;
const double stepSize = FFT_N / overlapFactor;
const double expectPhaseDiff = 2.0 * M_PI * stepSize / FFT_N;
double real = 0.0;
double imag = 0.0;
double phase = 0.0;
double delta = 0.0;
long qpd = 0;

const size_t iMax = std::min(size_t(FFT_N / 2), size_t(FFT_MAXFREQ / freqPerBin));
for (size_t i = 0; i < iMax; ++i)
real = data[2*i];
imag = data[2*i+1];

phase = atan2(imag, real);

// process phase difference
delta = phase - mFFTLastPhase[i];
mFFTLastPhase[i] = phase;

// subtract expected phase difference
delta -= i * expectPhaseDiff;

/* map delta phase into +/- Pi interval */
qpd = delta / M_PI;
if (qpd >= 0)
qpd += qpd & 1;
qpd -= qpd & 1;
delta -= M_PI * static_cast<double>(qpd);

/* get deviation from bin frequency from the +/- Pi interval */
delta = overlapFactor * delta / (2.0 * M_PI);

// true frequency
data[2 * i] = (i + delta) * freqPerBin;

// magnitude
data[2 * i + 1] = 2.0 * sqrt(real * real + imag * imag);

unsigned int maxI = 0;
double maxMag = data[1];
for(unsigned int i = 0; i < iMax; i++)
if(data[2 * i + 1] > maxMag)
maxI = i;
maxMag = data[2 * i + 1];

if(outFreq != NULL)
*outFreq = data[2 * maxI];
if(outMagnitude != NULL)
*outMagnitude = data[2 * maxI + 1];

Thanks in advance.

Share this post

Link to post
Share on other sites
The bin with the largest magnitude is not necessarily the fundamental frequency. You probably need a better algorithm. Perhaps you can start here:

It's probably a good idea to make some pictures of the spectrum with real input, and then try several algorithms on that data.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  
Followers 0