This project has moved and is read-only. For the latest updates, please go here.

Weird issue with Waveprovider16

Oct 15, 2015 at 1:24 AM
I'm using WasapiOut with a class derived from WaveProvider16, since I'm getting streamed data in from the network and playing it out.

Problem is, when my Read routine gets called, what is 'supposed' to be a short[] buffer is actually a byte[] buffer. I was trying to use Array.Copy to slam some PCM 16 data into the buffer, but it throws an exception due to the incompatible types.

Any idea how to get an actual short[] buffer passed to my Read routine?
Oct 17, 2015 at 10:30 AM
The reason you are seeing this is because NAudio uses a trick under the covers to prevent unnecessary coping of memory. This is the WaveBuffer class, and uses unions to make a byte[] look like its a short[]. Unfortunately, Array.Copy uses reflection under the hood and gets confused. I think you'll find Buffer.BlockCopy will do the trick instead, but remember to pass the count parameter as a number of bytes.