trouble in pitch detection

Oct 11, 2013 at 6:54 AM
Hi,
I am working on audio to detect pitch. I have used NAudio to get Mp3 Frames, now i want to calculate pitch of every frame.

In this article I have found some way to do but could not do it. Article: http://channel9.msdn.com/coding4fun/articles/AutotuneNET

As you described code which is:


public AutoCorrelator(int sampleRate)
{
this.sampleRate = (float)sampleRate;
int minFreq = 85;
int maxFreq = 255;

this.maxOffset = sampleRate / minFreq;
this.minOffset = sampleRate / maxFreq;
}
public float DetectPitch(float[] buffer, int frames)
{
if (prevBuffer == null)
{
    prevBuffer = new float[frames];
}

float maxCorr = 0;
int maxLag = 0;

// starting with low frequencies, working to higher
for (int lag = maxOffset; lag >= minOffset; lag--)
{
    float corr = 0; //  sum of squares
    for (int i = 0; i < frames; i++)
    {
        int oldIndex = i - lag;
        float sample = ((oldIndex < 0) ? prevBuffer[frames + 
        corr += (sample * buffer[i]);
    }
    if (corr > maxCorr)
    {
        maxCorr = corr;
        maxLag = lag;
    }

}
for (int n = 0; n < frames; n++)
{ 
    prevBuffer[n] = buffer[n]; 
}
float noiseThreshold = frames / 1000f;

if (maxCorr < noiseThreshold || maxLag == 0) return 0.0f;
return this.sampleRate / maxLag;
}


I did not understand this line:
int oldIndex = i - lag;
        float sample = ((oldIndex < 0) ? prevBuffer[frames + 
        corr += (sample * buffer[i]);
further more what is actually the pitch is?
Any little help will be appreciated.
Thank you.