title: Streaming non-PCM raw audio using NAudio
body: <p>I'm hell bent on making this work with NAudio, so please tell me if there's a way around this. I have streaming raw audio coming in from a serial device, which I'm trying to play through WaveOut.</p>
  
  <p><strong>Attempt 1:</strong></p>
  
  
  
  <pre class="lang-vb prettyprint-override"><code>'Constants 8000, 1, 8000 * 1, 1, 8
  Dim CustomWaveOutFormat = WaveFormat.CreateCustomFormat(WaveFormatEncoding.Pcm, SampleRate, Channels, AverageBPS, BlockAlign, BitsPerSample)    
  Dim rawStream = New RawSourceWaveStream(VoicePort.BaseStream, CustomWaveOutFormat)
  
  'Run in background
  Dim waveOut = New WaveOut(WaveCallbackInfo.FunctionCallback())
  
  'Play stream
  waveOut.Init(rawStream)
  waveOut.Play()
  </code></pre>
  
  <p>This code works, but there's a tiny problem - the actual audio stream isn't raw PCM, it's raw MuLaw. It plays out the companding like a Beethoven's 5th on cheese-grater. If I change the <code>WaveFormat</code> to <code>WaveFormatEncoding.MuLaw</code>, I get a bad format exception because it's raw audio and there are no RIFF headers.</p>
  
  <p>So I moved over to converting it to PCM:</p>
  
  <p><strong>Attempt 2:</strong></p>
  
  <pre><code>Dim reader = New MuLawWaveStream(VoicePort.BaseStream, SampleRate, Channels)
  Dim pcmStream = WaveFormatConversionStream.CreatePcmStream(reader)
  Dim waveOutStream = New BlockAlignReductionStream(pcmStream)
  
  waveOut.Init(waveOutStream)
  </code></pre>
  
  <p>Here, <code>CreatePcmStream</code> tries to get the length of the stream (even though CanSeek = false) and fails.</p>
  
  <p><strong>Attempt 3</strong></p>
  
  <pre><code>waveOutStream = New BufferedWaveProvider(WaveFormat.CreateMuLawFormat(SampleRate, Channels))
  *add samples when OnDataReceived()*
  </code></pre>
  
  <p>It too seems to suffer from lack of having a header.</p>
  
  <p>I'm hoping there's something minor I missed in all of this. The device only streams audio when in use, and no data is received otherwise - a case which is handled by (1).</p>