title: C# Frequency Retrieval
body: <p>What I need to do is calculate the frequency of the microphone input. I'm using <code>IWaveProvider</code> for this and its implemented <code>Read()</code>. The buffer always has a size of 8820 elements and something seems to be going wrong with the conversion from byte array to float array as well (the <code>FloatBuffer</code> property part).</p>
  
  <p>Here are some of the important bits...</p>
  
  <p>This is where I start my recording:</p>
  
  <pre><code>private void InitializeSoundRecording()
  {
      WaveIn waveIn = new WaveIn();
      waveIn.DeviceNumber = 0;   
      waveIn.DataAvailable += (s, e) =&gt; this.waveIn_DataAvailable(s, e); 
      waveIn.RecordingStopped += (s, e) =&gt; this.waveIn_RecordingStopped(s, e);
      waveIn.WaveFormat = new WaveFormat(44100, 1);
      waveIn.StartRecording();
  }
  </code></pre>
  
  <p>When the DataAvailable event handler is called, the following is executed:</p>
  
  <pre><code>private void waveIn_DataAvailable(object sender, WaveInEventArgs e)
  {
      WaveBuffer wb = new WaveBuffer(e.Buffer.Length);
  
      IWaveProvider iWaveProvider = new PitchDetector(new WaveInProvider(sender as WaveIn), new WaveBuffer(e.Buffer));
      iWaveProvider.Read(wb, 0, e.Buffer.Length);
  
      PitchDetector pd = iWaveProvider as PitchDetector;
  
      this.ShowPitch(pd.Pitch);
  }
  </code></pre>
  
  <p>And lastly, this is the "actual" important bit:</p>
  
  <pre><code>private const int FLOAT_BUFFER_SIZE = 8820;
  private IWaveProvider source;
  private WaveBuffer waveBuffer;
  private int sampleRate;
  private float[] fftBuffer;
  private float[] prevBuffer;
  public float Pitch { get; private set; }
  
  public WaveFormat WaveFormat { get { return this.source.WaveFormat; } }
  
  internal PitchDetector(IWaveProvider waveProvider, WaveBuffer waveBuffer = null)
  {
      this.source = waveProvider;
      this.sampleRate = waveProvider.WaveFormat.SampleRate;
      this.waveBuffer = waveBuffer;
  }
  
  /// &lt;summary&gt;
  /// UNSAFE METHOD! 
  /// &lt;/summary&gt;
  /// &lt;param name="input"&gt;&lt;/param&gt;
  /// &lt;returns&gt;&lt;/returns&gt;
  private unsafe float[] ByteArrayToFloatArray(byte[] input)
  {
      float[] fb = new float[FLOAT_BUFFER_SIZE];
      unsafe
      {
          fixed (byte* ptrBuffer = input)
          {
              float* ptrFloatBuffer = (float*)ptrBuffer;
              for (int i = 0; i &lt; FLOAT_BUFFER_SIZE; i++)
              {
                  fb[i] = *ptrFloatBuffer;
                  ptrFloatBuffer++;
              }
          }
      }
      return fb;
  }
  
  public int Read(byte[] buffer, int offset = 0, int count = 0)
  {
      if (this.waveBuffer == null || this.waveBuffer.MaxSize &lt; count)
          this.waveBuffer = new WaveBuffer(count);
  
      int readBytes = this.source.Read(this.waveBuffer, 0, count);
  
      if (readBytes &gt; 0) readBytes = count;
  
      int frames = readBytes / sizeof(float);
  
      this.Pitch = this.DeterminePitch(this.waveBuffer.FloatBuffer, frames);
  
      return frames * 4;
  }
  </code></pre>
  
  <p>Strangely enough, when it enters the constructor, waveBuffer contains some data (255, 1, 0, etc.), but when I check the "buffer" parameter of <code>Read()</code>, it's entirely 0. Every element.</p>
  
  <p>Out of curiosity also, why does <code>Read()</code> have a buffer parameter, but isn't actually used in the method at all (I got that piece of code from one of your articles)?</p>
  
  <p>Any help to resolve this issue would be greatly appreciated! I've been at this for quite a while already, but can make no sense out of it.</p>
  
  <p>Thanks, 
  Alain</p>