title: Microphone stops providing data with NAudio
body: <p>I'm using <code>WaveInEvent</code> of <code>NAudio</code> to record microphone data. It works fine for a while, but after a few times, it stops providing input data- the <code>DataAvailable</code> callback is never called with new data.</p>
  
  <p>I have tried creating a new <code>WaveInEvent</code> each time, but this did not resolve the problem. I've also tried using the WASAPI input, which always called <code>DataAvailable</code> - with zero bytes of data.</p>
  
  <p>How can I record audio from the microphone reliably with <code>NAudio</code>?</p>
  
  <p>Currently, my code looks like this:</p>
  
  <pre><code>StartRecording() {
      microphone = new WaveInEvent();
      microphone.DeviceNumber = 0;
      microphone.WaveFormat = outformat;
      microphone.BufferMilliseconds = 50;
      microphone.DataAvailable += (_, recArgs) =&gt;
      {
          session.OnAudioData(recArgs.Buffer, recArgs.BytesRecorded);
      };
      microphone.StartRecording();
  }
  StopRecording() {
      if (microphone != null)
      {
          microphone.StopRecording();
          microphone.Dispose();
          microphone = null;
      }        
  }
  </code></pre>
  
  <p>There's no other naudio code in the project except using WaveFormat to describe wave formats.</p>
  
  <p>NAudio throws an access violation exception trying to call WaveInBuffer.Reuse() from a threadpool worker. I'm not sure why this doesn't do something more serious than just drop audio data.</p>
  
  <p>For the condition where I did not recreate the WaveInEvent, I get an MmException instead- invalid handle calling waveInPrepareHeader, in the same place.</p>
  
  <p>Frankly, the fact that I get different results heavily implies that NAudio is doing some funky shit it shouldn't to share state between instances, and looking at the source on Codeplex, I'm not really sure WTF is going on.</p>