title: How to calculate FFT using NAudio in realtime (ASIO out)
body: <p>I am programming clone of guitar (violin)  Hero as a final project for this school year.</p>
  
  <p>The idea is to take input from my electric violin, analyse it via FFT, do some logic and drawing and output it through speakers. Perhaps some steps in parallel threads.</p>
  
  <p>I already have Asio low latency input-output implemented but I am having a great problem implementing realtime FFT.</p>
  
  <p>This is a code that sets up asioOut along with sampleAggregator. Sample aggregator should store samples that are added each time AudioAvailable() is called and trigger FFT calculation when the number of samples exceeds fftLength.</p>
  
  <pre><code>private static int fftLength = 8192;
  private SampleAggregator sampleAggregator = new SampleAggregator(fftLength);
  
  void asioStartPlaying(object sender, EventArgs e)
  {
      sampleAggregator.PerformFFT = true;
      sampleAggregator.FftCalculated += new EventHandler&lt;FftEventArgs&gt;(FftCalculated);
      var asioOut = new AsioOut();
      BufferedWaveProvider wavprov = new BufferedWaveProvider(new WaveFormat(48000, 1));
      asioOut.AudioAvailable += new EventHandler&lt;AsioAudioAvailableEventArgs&gt; (asio_DataAvailable);
      asioOut.InitRecordAndPlayback(wavprov, 1, 25);
      asioOut.Play();
  }
  
  void asio_DataAvailable(object sender, AsioAudioAvailableEventArgs e)
  {
      byte[] buf = new byte[e.SamplesPerBuffer*4];
  
      for (int i = 0; i &lt; e.InputBuffers.Length; i++)
      {
          Marshal.Copy(e.InputBuffers[i], buf, 0, e.SamplesPerBuffer*4);
          Marshal.Copy(buf, 0, e.OutputBuffers[i], e.SamplesPerBuffer*4);
      }
  
      for (int i = 0; i &lt; buf.Length; i=i+4)
      {
          float sample32 = BitConverter.ToSingle(buf, i);
          sampleAggregator.Add(sample32);
      }
  
      e.WrittenToOutputBuffers = true;
  }
  </code></pre>
  
  <p>SampleAggregator is class taken from <a href="https://stackoverflow.com/a/20414331">NAudio fft result gives intensity on all frequencies C#</a>. </p>
  
  <p>Asio outputs data in Int32LSB sample type. In buf there are values from 0 to 255.</p>
  
  <p>This is function that should be called when fft is calculated (triggered from SampleAggregator class).</p>
  
  <pre><code>void FftCalculated(object sender, FftEventArgs e)
  {
      for (var i = 0; i &lt; e.Result.Length; i++)
      {
          Debug.WriteLine("FFT output.");
          Debug.WriteLine(e.Result[i].X);
          Debug.WriteLine(e.Result[i].Y);
      }
  }
  </code></pre>
  
  <p>But the FFT always outputs NaN as a result.</p>
  
  <p>I think there is a problem with the conversion to float.</p>
  
  <p>Could someone point me in the right direction?</p>
  
  <p><strong>EDIT_1:</strong> I changed the loop in DataAvailable() to </p>
  
  <pre><code>for (int i = 0; i &lt; e.SamplesPerBuffer * 4; i++)
  {
      float sample32 = Convert.ToSingle(buf[i]);
      sampleAggregator.Add(sample32);
  }
  </code></pre>
  
  <p>And FFT now outputs data. But I think they are not correct. <strong>The mistake must be in</strong> the conversion between asio samples and float values. But I am not much comfortable around byte operations.</p>
  
  <p>Could e.GetAsInterleavedSamples somehow help? </p>
  
  <p>Sample of raw data from FFT: 
  X: -5,304741
  Y: -0,7160959
  X: 6,270798
  Y: -0,4169312
  X: -8,851931
  Y: -0,4485725</p>
  
  <p>I noticed, that first few and last few values in raw data from FFT are somehow bigger then other data. Making calculation of magnitude tricky.</p>