naudio / NAudio

Audio and MIDI library for .NET
MIT License
5.52k stars 1.1k forks source link

How to detect pitch out of voice singing (through microphone) #996

Open PeterHevesi opened 1 year ago

PeterHevesi commented 1 year ago

Hi guys, I've asked: This Question on Reddit, because I needed to find some C# library for detecting pitch out of human voice singing (through microphone). It should work real time, but I don't mind some short latency :)

And here in this Reddit thread, some guys recommended me this NAudio library for this task. I've found, that it can also do changing of pitch, but I don't actually need that. I need just detection of pitch from voice singing sound.

I've also found, that it can do FFT (Fast Furier Transformation). And I understand, that FFT will surely be the first step of this pitch detection. But after I do FFT, what should I do to get the pitch information out of this spectrum of frequencies after FFT? Should I somehow make an average frequency out of it, or what algorithm should be applied to frequency spectrum after FFT to get the pitch information into like 1 final frequency?

(To explain exactly what I want to achieve: I am developing a game in Unity, in which I want to play some melody to the user, and then I let the user sing (or whiste or hum) that melody. Then I want to measure how successfully (how clearly) he sang it.
So let's say the melody is C# 2 Dis 2 Fis# 2 C# 2. And then I measure user singing in kHz, so I know how far he is singing from the supposed frequencies...)

Thanks sooo much for any help, I will greatly appreciate it, even a small hint where should I look :)

markheath commented 1 year ago

Pitch detection is quite challenging. Although it's technically possible with FFT, it may not be the best for this particular problem. FFT returns the magnitude and phase of frequency "bins", which means it does not necessarily accurately point at a single note. The approach taken by things like guitar tuners is more often an "autocorrelation" algorithm, and often you explicitly ignore the octave and just try to detect the musical pitch. Autocorrelation is also the approach that auto-tune algorithms typically use, as they first need to pitch detect before they can correct. Check out the later slides in this presentation about autotune for some diagrams of autocorrelation: http://www.xavierriley.co.uk/assets/autotune/index.html#0

PeterHevesi commented 1 year ago

Hi Mark, Thanks so much for your tip, I thought, that it should be done with FFT, so I am very glad, that you pointed me to better approach of autocorrelation, I will check it out! Thanks again ! :)