问题是捕获原始音频数据并进行FFT。请帮忙

问题是捕获原始音频数据并进行FFT。请帮忙

问题描述:

我正在使用NAudio库来捕获音频信号并进行FFT。



它有点工作。它可以捕获音频并进行频率分析。但我想要的是:



播放5秒的曲调,在一秒钟内捕获原始数据并进行FFT。然后检查基频。并重复这一步。



然而,当我播放音调并使用thread.sleep(5000)确保声音播放至少5秒。并且在thread.sleep(5000)完成之前,音频捕获事件才会触发。在这1秒钟内它没有捕捉到任何东西。



请看代码:



first I在表单加载时初始化实例:

I am using NAudio library to capture the audio signal and do the FFT.

It sort of working. It can captured audio and do the frequency analysis. but what I want is :

playing a tune for 5 seconds, during a second, capture raw data and do FFT. and then check fundamental frequency. and repeat the step.

However, when I play the the tone and use thread.sleep(5000) to make sure the sound played at least 5 secs. and the audio capture events did not trigger until thread.sleep(5000) done. it did not capture anything during this 1 second time.

Please see the code:

first I initialize the instance while the form load:

private void Form1_Load(object sender, EventArgs e)
{

    IWaveIn waveIn = new WaveIn();
    waveIn.DataAvailable += waveIn_DataAvailable;

    sampleAggregator.PerformFFT = true;
    waveIn.DataAvailable += waveIn_DataAvailable;
    sampleAggregator.FftCalculated += new EventHandler<FftEventArgs>(FftCalculated);
}





秒写入事件处理函数以捕获原始音频数据



second write the event handler function for capture raw audio data

void waveIn_DataAvailable(object sender, WaveInEventArgs e)
{
            if (this.InvokeRequired)
            {
                this.BeginInvoke(new EventHandler<WaveInEventArgs>(waveIn_DataAvailable),    sender, e);
            }
            else
            {
                byte[] buffer = e.Buffer;
                int bytesRecorded = e.BytesRecorded;
                int bufferIncrement = waveIn.WaveFormat.BlockAlign;

                for (int index = 0; index < e.BytesRecorded; index += bufferIncrement)
                {
                    short sample = (short)((buffer[index + 1] << 8) |
                                            buffer[index + 0]);
                    float sample32 = sample / 32767f;
                    sampleAggregator.Add(sample32);   // will trigger FFT function
                }
            }
}





然后,FFT触发事件处理程序:



and then, FFT trigger event handler:

void FftCalculated(object sender, FftEventArgs e)
{

    int length = 0;
    int i;
    float[] Amp = new float[16384];
    float AmpMax = 0;
    int AmpMaxIndex = 0;

    FftBuffer = e.Result;

    length = FftBuffer.Length / 2;

    for (i = 0; i < length; i++)
    {
        Amp[i] = AmplitudeCalc(FftBuffer[i].X, FftBuffer[i].Y);
        if (Amp[i] > AmpMax)
        {
            AmpMax = Amp[i];
            AmpMaxIndex = i;
        }
    }

    frequency = AmpMaxIndex * 8000 / fftLength;
    //Debug.WriteLine("frequency: " + frequency);


}





和我的主要功能,播放音频一秒钟。 />



and in my main function to play audio for a second.

OnekHz_L.PlayLooping();
waveIn.StartRecording();


Thread.Sleep(5000);

OnekHz_L.Stop();
Stop();

if (frequency < 1050 && frequency > 950)
{
    // do sth.
}
else
{
    // do sth.
}





问题是在thread.sleep(5000)期间,它永远不会触发waveIn_DataAvailable(对象发送者,WaveInEventArgs e)和FftCalculated(对象发送者,FftEventArgs e)两个事件。



如何修复它?



请帮助



the problem is during thread.sleep(5000) period, it never trigger waveIn_DataAvailable(object sender, WaveInEventArgs e) and FftCalculated(object sender, FftEventArgs e) two events.

How do I fix it?

Please help