How to calculate FFT using NAudio in realtime (ASIO out)

后端 未结 2 1005
别那么骄傲
别那么骄傲 2021-01-06 11:14

I am programming clone of guitar (violin) Hero as a final project for this school year.

The idea is to take input from my electric violin, analyse it via FFT, do so

相关标签:
2条回答
  • 2021-01-06 11:36

    I haven't used NAudio, but we have implemented pretty much similar thing with DirectSound. There's tools for this in LightningChart Ultimate SDK. AudioInput component captures waveform data from sound device, and the data is forwarded to FFT calculation (SpectrumCalculator component) and waveform monitors at same time. FFT data is then visualized as spectrograms in 2D or 3D. AudioOutput writes the data to sound device to be audible through speakers.

    Overall the audio input/output, FFT calculation and visualization run with very low CPU load.

    Spectrogram example in LightningChart demo application

    Our libraries are commercial, but I think even if you were not looking for any additional components, it might be a good idea to take a look at our audio examples, source code is visible in the demo application Visual Studio projects. You may get fresh ideas, at least :-) You can apply some methods for NAudio, I believe.

    Download LightningChart demo from LightningChart web site, running it costs you nothing.

    [I'm CTO of LightningChart components]

    0 讨论(0)
  • 2021-01-06 11:44

    The problem was as I thought in conversion between data about samples from Asio (4 bytes in a row in buf array) to float for fft. BitConvertor should do the trick but it somehow makes fft output NaN in my case. So I tried this conversion instead.

    for (int i = 0; i < e.SamplesPerBuffer * 4; i=i+4)
    {
        float sample = Convert.ToSingle(buf[i] + buf[i+1] + buf[i+2] + buf[i+3]);
        sampleAggregator.Add(sample);
    }
    

    And it works very well. Even with 192 000 sample rate.

    0 讨论(0)
提交回复
热议问题