This project has moved. For the latest updates, please go here.

Writing / Playing MemoryStream from WasapiLoopback

Jul 13, 2014 at 3:59 PM
I used a WasapiLoopbackCapture and on data available wrote to a MemoryStream (I tried a WaveStream but it seems its an abstract class). Should I be using the RawSourceWaveStream ?

After finishing the recording, I want to do two things:
  1. Play the MemoryStream
    If the MemoryStream sounds okay:
  2. Save the MemoryStream to a WAVE file.
I've managed to figure out how to play the MemoryStream, though I've no idea if it's the proper way to do it: (waveStream is the Memory Stream, and waveOut is NAudio Wave Out)
waveStream.Position = 0;
using (RawSourceWaveStream NewReader = new RawSourceWaveStream(waveStream, captureDevice.WaveFormat))
{
    waveOut.Init(NewReader);
    waveOut.Play();
    NewReader.Close();
}
That seems to work for playing.

I'm also using WaveFileWriter with a raw stream again and I just read from it and write to the wave file. I just want to make sure that these are both "best practice" when using NAudio.

I'd also love to be able to trim/clip seconds off the front or end of the wave stream (before writing to a file) but I don't think NAudio can do this out of the box.


Thanks!
Coordinator
Jul 17, 2014 at 2:31 PM
well if you have a memory stream, then trimming is trivial. Just position to where you want to start the WAV file from, then copy the bytes for the duration you want directly into a WaveFileWriter's Write method. Just be sure to only position to a multiple of BlockAlign.