如何使用NAudio对波形文件执行FFT

时间:2013-01-23 12:10:27

标签: c# fft naudio

我正在使用NAudio-library,并希望对WaveStream执行快速傅里叶变换。我看到NAudio已经内置了FFT,但我该如何使用呢?

我听说我必须使用SampleAggregator类。

1 个答案:

答案 0 :(得分:3)

您需要阅读整个blog article,以便最好地理解我提取的以下代码示例,以确保即使文章不是,也会保留样本:

using (WaveFileReader reader = new WaveFileReader(fileToProcess))
{
    IWaveProvider stream32 = new Wave16toFloatProvider(reader);
    IWaveProvider streamEffect = new AutoTuneWaveProvider(stream32, autotuneSettings);
    IWaveProvider stream16 = new WaveFloatTo16Provider(streamEffect);
    using (WaveFileWriter converted = new WaveFileWriter(tempFile, stream16.WaveFormat))
    {
        // buffer length needs to be a power of 2 for FFT to work nicely
        // however, make the buffer too long and pitches aren't detected fast enough
        // successful buffer sizes: 8192, 4096, 2048, 1024
        // (some pitch detection algorithms need at least 2048)
        byte[] buffer = new byte[8192]; 
        int bytesRead;
        do
        {
            bytesRead = stream16.Read(buffer, 0, buffer.Length);
            converted.WriteData(buffer, 0, bytesRead);
        } while (bytesRead != 0 && converted.Length < reader.Length);
    }
}

但简而言之,如果您创建了WAV文件,则可以使用该示例将其转换为FFT。