title: Delay (approx 200 ms) in streamed audio playback
body: <p>I have an application which plays the streamed audio data (like a chat client). The workflow involves three simple steps:</p>
  
  <ol>
  <li>The file header info (sample rate, bits per sample and num of channels) is sent first.</li>
  <li>Audio waveout device is initialized based on the above parameters.</li>
  <li>Audio (pcm) data is sent and is played on the above device.</li>
  </ol>
  
  <p>The data receiving code is native (C code). and it reads data on a socket. Then it calls the managed C# code, which uses Naudio library to initialize device and play audio.</p>
  
  <p>Now the problem is that, I am seeing some delay in audio playback. I have already instrumented the rest of my code (specifically: transferring data on socket and passing it back to managed code) and that seems to be okay. The whole transfer process is taking around 600 micro seconds, but after I assign the buffer to <code>Naudio</code>, it seems to start playing it after some time (around 200-250 milliseconds).</p>
  
  <p>Here is my C# class that handles the audio playing part:</p>
  
  <pre><code>class foo
  {
      static  IWavePlayer     s_WaveOut;
      static  WaveFormat      s_WaveOutFormat;
      static  BufferedWaveProvider    s_WaveProvider;
      static  byte[]          s_Samples       = new byte[10000];
  
      // called from native code to init deivce with specified sample rate and num of channels
      private static void DeviceInit(int rate, int bits, int channels)
      {
          s_WaveOut   = new WaveOut(WaveCallbackInfo.FunctionCallback());
          s_WaveOutFormat = new WaveFormat(rate, bits, channels);
          s_WaveProvider  = new BufferedWaveProvider(s_WaveOutFormat);
  
          s_WaveProvider.DiscardOnBufferOverflow      = true;
          s_WaveProvider.BufferLength         = 5 * 1024 * 1024;
  
          s_WaveOut.Init(s_WaveProvider);
          s_WaveOut.Play();
      }
  
      // called from native 'C' code upon receiving audio packates
      private unsafe static void PlayDataCallback(
          IntPtr buff,
          Int32 size) 
      {
          Marshal.Copy(buff, s_Samples, 0, size);
          s_WaveProvider.AddSamples(s_Samples, 0, size);
      }
  }
  </code></pre>
  
  <p>Anyone has any idea on what might be causing the delay or am I using it (Naudio) in some wrong way.</p>
  
  <p>I tried the same Naudio library to play a wav file and that seems to work perfect, the problem is coming only when I am adding samples after initialing the device myself.</p>
  
  <p><strong>[update]</strong> If I change <code>s_WaveOut   = new WaveOut(WaveCallbackInfo.FunctionCallback());</code> to <code>s_WaveOut = new DirectSound();</code>, the performance is much better. If after this, I modify the Naudio source to set playback thread priority to <code>Highest</code> (default is Normal), the performace improves further, but as expected, the process starts consuming high resources.</p>
  
  <p>Thank you,</p>
  
  <p>Vikram</p>