views:

2158

answers:

6

Hello,

I'm trying to do real time pitch detection of a users singing, but I'm running into alot of problems. I've tried lots of methods, including FFT (FFT Problem (Returns random results)) and autocorrelation (Autocorrelation pitch detection returns random results with mic input), but I can't seem to get any methods to give a good result. Can anyone suggest a method for real-time pitch tracking or how to improve on a method I already have? I can't seem to find any good C / C++ methods for real time pitch detection.

Thanks,

Niall.

Edit: Just to note, i've checked that the mic input data is correct, and that when using a sine wave the results are more or less the correct pitch.

Edit: Sorry this is late, but at the moment, im visualizing the autocolleration by taking the values out of the results array, and each index, and plotting the index on the X axis and the value on the Y axis (both are divided by 100000 or something, and im using OpenGL), plugging the data into a VST host and using VST plugins isn't an option to me. At the moment, it just looks like some random dots. Am i doing it correctly, or can you please point me torwards some code for doing it or help me understand how to visualize the raw audio data and autocorrelation data.

A: 

Can you adapt anything from instrument tuners? My delightfully compact guitar tuner is able to detect the pitch of the strings pretty well. I see this reference to a piano tuner which explains an algorithm to some extent.

djna
I've tried adapting stuff from some guitar tuner code (which used FFT), but the results were all over the place.
Niall
+3  A: 

I had a similar problem with microphone input on a project I did a few years back - turned out to be due to a DC offset.

Make sure you remove any bias before attempting FFT or whatever other method you are using.

It is also possible that you are running into headroom or clipping problems.

Graphs are the best way to diagnose most problems with audio.

Dipstick
Sorry if i sound stupid, but how do i visualize the result of the FFT / Autocorrelation? Would i take each value in the result array, and plot that and the magnitude of that value?
Niall
You can remove DC bias with a high pass filter set to a very low cutoff. I usually go with 25-30 hertz, based on the lowest result from extended string (5- or 6-) bass guitars.
Nosredna
I suggest running your input through a host and using the free VSTs Fre(a)koscope and s(M)exoscope to see the frequency response and the waveform graphically.
Nosredna
Is there any other way to do it? VSTs Fre(a)koscope and s(M)exoscope is for windows and im on a mac.
Niall
I think there's a plugin adaper that lets you use PC VSTs on Intel Macs. The vast majority of free plugins are PC (which is why I still do music on my PC rather than my Mac). There are some similar Mac tools, but most of them are not free. Try BlueCat's stuff. He has a spectrum analyzer and an oscilloscope. Or search the audio plugin database at kvraudio. Or just ask on a forum there.
Nosredna
http://www.bluecataudio.com/Download/Home/
Nosredna
The spectrum analyzer will allow you to compare your FFT results with someone else's, which will let you know if you have a bug. The oscilloscope will let you see if you are clipping.
Nosredna
Im now using a high pass filter to remove the DC offset (see http://stackoverflow.com/questions/1353368/autocorrelation-returns-random-results-with-mic-input-using-a-high-pass-filter), but it's still not working, any idea why?
Niall
+12  A: 

Taking a step back... To get this working you MUST figure out a way to plot intermediate steps of this process. What you're trying to do is not particularly hard, but it is error prone and fiddly. Clipping, windowing, bad wiring, aliasing, DC offsets, reading the wrong channels, the weird FFT frequency axis, impedance mismatches, frame size errors... who knows. But if you can plot the raw data, and then plot the FFT, all will become clear.

tom10
A: 

Check out aubio, and open source library which includes several state-of-the-art methods for pitch tracking.

piem
A: 

It looks like duplicate of the question is here Real-time pitch detection using FFT with some additional answers.

mloskot
A: 

Take a look at this sample application:

http://www.codeproject.com/KB/audio-video/SoundCatcher.aspx

I realize the app is in C# and you need C++, and I realize this is .Net/Windows and you're on a mac... But I figured his FFT implementation might be a starting reference point. Try to compare your FFT implementation to his. (His is the iterative, breadth-first version of Cooley-Tukey's FFT). Are they similar?

Also, the "random" behavior you're describing might be because you're grabbing data returned by your sound card directly without assembling the values from the byte-array properly. Did you ask your sound card to sample 16 bit values, and then gave it a byte-array to store the values in? If so, remember that two consecutive bytes in the returned array make up one 16-bit audio sample.

Christian Pedersen