2

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

我听说我必须使用 SampleAggregator 类。

4

1 回答 1

3

您需要阅读整篇博客文章,以最好地理解我提取的以下代码示例,以确保即使文章没有保留该示例:

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。

于 2013-01-23T12:13:32.407 回答