title: NAudio frequency analyser giving inconsistant results
body: <p>I'm developping a simple program that analyses frequencies of audio files. 
  Using an fft length of 8192, samplerate of 44100, if I use as input a constant frequency wav file - say 65Hz, 200Hz or 300Hz - the output is a constant graph at that value.</p>
  
  <p>If I use a recording of someone speaking, the frequencies has peaks as high as 4000Hz, with an average at 450+ish on a 90 seconds file. 
  At first I thought it was because of the recording being stereo sound, but converting it to mono with the exact same bitrate as the test files doesn't change much. (average goes down from 492 to 456 but that's still way too high)</p>
  
  <p>Has anyone got an idea as to what could cause this ?</p>
  
  <p>I think I shouldn't find the highest value but perhaps take either an average or a median value ?</p>
  
  <p>EDIT : using the average of the magnitudes per 8192 bytes buffer and getting the index that's closest to that magnitude messes everything up.</p>
  
  <hr>
  
  <p>This is the code for the handler of the event the Sample Aggregator fires when it has calculated fft for current buffer</p>
  
  <pre><code>void FftCalculated(object sender, FftEventArgs e)
      {
          int length = e.Result.Length;
          float[] magnitudes = new float[length];
  
          for (int i = 0; i &lt; length / 2; i++)
          {
              float real = e.Result[i].X;
              float imaginary = e.Result[i].Y;
              magnitudes[i] = (float)(10 * Math.Log10(Math.Sqrt((real * real) + (imaginary * imaginary))));
          }
  
          float max_mag = float.MinValue;
          float max_index = -1;
          for (int i = 0; i &lt; length / 2; i++)
              if (magnitudes[i] &gt; max_mag)
              {
                  max_mag = magnitudes[i];
                  max_index = i;
              }
  
          var currentFrequency = max_index * SAMPLERATE / 8192; 
  
          Console.WriteLine("frequency be " + currentFrequency);
      }
  </code></pre>
  
  <hr>
  
  <p>ADDITION : this is the code that reads and sends the file to the analysing part</p>
  
  <pre><code>using (var rdr = new WaveFileReader(audioFilePath))
              {
                  var newFormat = new WaveFormat(Convert.ToInt32(SAMPLERATE/*44100*/), 16, 1);
                  byte[] buffer = new byte[8192];
                  var audioData = new AudioData(); //custom class for project
                  using (var conversionStream = new WaveFormatConversionStream(newFormat, rdr))
                  {
                      // Used to send audio in realtime, it's a timestamps issue for the graphs 
                      // I'm working on fixing this, but it has lower priority so disregard it :p
                      TimeSpan audioDuration = conversionStream.TotalTime;
                      long audioLength = conversionStream.Length;
                      int waitTime = (int)(audioDuration.TotalMilliseconds / audioLength * 8192);
                      while (conversionStream.Read(buffer, 0, buffer.Length) != 0)
                      {
                          audioData.AudioDataBase64 = Utils.Base64Encode(buffer);
                          Thread.Sleep(waitTime);
                          SendMessage("AudioData", Utils.StringToAscii(AudioData.GetJSON(audioData)));
                      }
                      Console.WriteLine("Reached End of File");
                  }
              }
  </code></pre>
  
  <hr>
  
  <p>This is the code that receives the audio data</p>
  
  <pre><code>{
      var audioData = new AudioData();
      audioData = 
      AudioData.GetStateFromJSON(Utils.AsciiToString(receivedMessage));
      QueueAudio(Utils.Base64Decode(audioData.AudioDataBase64)));
  }
  </code></pre>
  
  <p>followed by </p>
  
  <pre><code>var waveFormat = new WaveFormat(Convert.ToInt32(SAMPLERATE/*44100*/), 16, 1);
  _bufferedWaveProvider = new BufferedWaveProvider(waveFormat);
  _bufferedWaveProvider.BufferDuration = new TimeSpan(0, 2, 0);
  
  
  {
      void QueueAudio(byte[] data)
      {
          _bufferedWaveProvider.AddSamples(data, 0, data.Length);
          if (_bufferedWaveProvider.BufferedBytes &gt;= fftLength)
          {
              byte[] buffer = new byte[_bufferedWaveProvider.BufferedBytes];
              _bufferedWaveProvider.Read(buffer, 0, _bufferedWaveProvider.BufferedBytes);
              for (int index = 0; index &lt; buffer.Length; index += 2)
              {
                  short sample = (short)((buffer[index] | buffer[index + 1] &lt;&lt; 8));
                  float sample32 = (sample) / 32767f;
                  sampleAggregator.Add(sample32);
              }
          }
      }
  }
  </code></pre>
  
  <p>And then the SampleAggregator fires the event above when it's done with the fft.</p>