How to perform the FFT to a wave-file using NAudio

爷,独闯天下 提交于 2020-04-15 13:03:28

问题


I'm working with the NAudio-library and would like to perform the fast fourier transformation to a WaveStream. I saw that NAudio has already built-in the FFT but how do I use it?

I heard i have to use the SampleAggregator class.


回答1:


You need to read this entire blog article to best understand the following code sample I lifted to ensure the sample is preserved even if the article isn't:

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);
    }
}

but in short, if you get the WAV file created you can use that sample to convert it to FFT.



来源:https://stackoverflow.com/questions/14479406/how-to-perform-the-fft-to-a-wave-file-using-naudio

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!