Simle test of my Compute Frequency function using some code citation tool...
public int[] ComputeFrequency(byte[] samples, bool stereo, bool bit16)
{
int stereoDiv = stereo ? 2 : 1;
int bitDiv = bit16 ? 2 : 1;
int sampleIterator; // stop iterating when second peak on each channel has been found
int[] firstPeaks = { 0, 0 }; // the first peak positions in the sample array
bool[] cleanPeaks = { false, false };// check whether we started the sample in the middle of some peak
int[] returnFrequency = { 0, 0 };
for (sampleIterator = 0; sampleIterator < SAMPLE_SIZE; sampleIterator++)
{
byte[] sampleData = { samples[stereoDiv * bitDiv * sampleIterator], 0 };
if (stereo)
{
sampleData[SECOND_CHANNEL] = samples[stereoDiv * bitDiv * sampleIterator + bitDiv];
}
for (int channel = 0; channel < stereoDiv; channel++)
{
// works for both int and byte because we want to check out we passed the middle
if (sampleData[channel] < 150 && sampleData[channel] > 20)
{
cleanPeaks[channel] = true;
}
if (sampleData[channel] >= PEAK_LEVEL_8 && cleanPeaks[channel])
{
if (firstPeaks[channel] == 0)
{
cleanPeaks[channel] = false;
firstPeaks[channel] = sampleIterator;
// make assumption about the frequency that is not lower than the sampling / 2000
sampleIterator += SAMPLING_RATE / 2000 * stereoDiv * bitDiv;
}
else // we found second peak, which is at position [k]
{
if (returnFrequency[channel] == 0)
{
// pitch as the sample frequency divided by the distance between two peaks
returnFrequency[channel] = SAMPLING_RATE / (sampleIterator - firstPeaks[channel]);
}
if (returnFrequency[FIRST_CHANNEL] > 0 && returnFrequency[SECOND_CHANNEL] > 0)
{
return returnFrequency;
}
}
}
}
}
return returnFrequency;
}