我在实施时遇到了一些问题FFTPitchDetector
。我真正想做的是从吉他输入中获取实时频率,我不太确定如何使用FftPitchDetector.cs
. 任何想法?
private void sourceStream_DataAvailable(object sender, NAudio.Wave.WaveInEventArgs e)
{
if (waveWriter == null) return;
byte[] buffer = e.Buffer;
float[] floats = new float[buffer.Length];
float sample32 = 0;
int bytesRecorded = e.BytesRecorded;
waveWriter.Write(buffer, 0, bytesRecorded);
for (int index = 0; index < e.BytesRecorded; index += 2)
{
short sample = (short)((buffer[index + 1] << 8) |
buffer[index + 0]);
sample32 = sample / 32768f;
sampleAggregator.Add(sample32);
}
floats = bytesToFloats(buffer);
FftPitchDetector PitchDetect = new FftPitchDetector(sample32);
PitchDetect.DetectPitch(floats, bytesRecorded);
Console.WriteLine("{0}",sample32);
}
private static float[] bytesToFloats(byte[] bytes)
{
float[] floats = new float[bytes.Length / 2];
for (int i = 0; i < bytes.Length; i += 2)
{
floats[i / 2] = bytes[i] | (bytes[i + 1] << 8);
}
return floats;
}
当我执行代码时,有一个错误IndexOutOfRangeException was unhandled
指向该行
fftBuffer[n * 2] = buffer[n-inFrames] * window(n, frames);
在fftPitchDetector.cs
. 我的代码有什么问题?
有没有 C# Guitar Tuner 的开源代码?我希望将其外包到我的项目中。