This project has moved. For the latest updates, please go here.

Pitch Detection

Feb 16, 2013 at 6:15 PM

Firstly, I'd just like to say thank you for NAudio, it's a brilliant library.

I'm having difficulty in trying to extract pitches from a single channel wave file. I have taken a look at Autotune.NET and used the piece of code to read the wave file into a buffer
using (WaveFileReader reader = new WaveFileReader(file))
                IWaveProvider stream32 = new Wave16ToFloatProvider(reader);

                byte[] buffer = new byte[4096];
                int bytesRead;

                    bytesRead = stream32.Read(buffer, 0, buffer.Length);
                } while (bytesRead != 0);
However, I am not sure what the next step would be to go ahead and get pitches from it.

Any help or advice would be much appreciated.

Feb 18, 2013 at 8:37 AM
well in I use an FFT for pitch detection. There are other (and possibly better) ways, but for an explanation of how to use FFT with NAudio, read this article:

You will need some patience if you are new to DSP, as I found it took me a long time to understand FFTs.
Feb 21, 2013 at 1:55 PM
Thanks for the reply.

I have discovered a problem with the code, but I am not sure why the error is happening.

The byte[] buffer is always filled with 0s and never any content. Is this right?

Also, am I right in believing that the byte[] should be filled with sample values after the read command?

Thanks again for any advice or clarification.
Feb 21, 2013 at 4:10 PM
Yes, after each call to read there should be data in the buffer. Does bytesRead return a non-zero value?
Feb 22, 2013 at 3:13 PM
Edited Feb 25, 2013 at 11:49 AM
Yes, bytesRead does return a non-zero value until it reaches the end of the file, where its zero.
Feb 25, 2013 at 11:51 AM
Is this the correct result?

I editted the code slightly to write out to the console when a value in the console is greater than zero, but the values barely go above 200, which for a constant 500Hz tone I'm not sure is right...
Feb 25, 2013 at 4:25 PM
my autotune code is just looking for the most likely musical note - it is not interested in what octave that note is in, so the range of pitches it outputs may not include the test tones you are using. You need to examine the FFT output directly yourself.