Search code examples
c#wpfplaybacknaudiomemorystream

Play from a memory stream(which doesn't contain wav headers) in WPF


I try to concatenate some data in different durations but same sample rate. I concatenate the data with MemoryStream.CopyTo(memoryStream) method.

Is there any way to play a data from a memory stream which don't have a wav header in the start?

And if not, is there any way to append a wav headers to to start after the stream was appended?

(I want to avoid generating a file in the disk..)


Solution

  • To do this with NAudio, just use a RawSourceWaveStream and pass in the MemoryStream containing the raw concatenated audio to its constructor. You'll also need to explicitly specify that the WaveFormat is.

    memoryStream = ... // construct your audio
    memoryStream.Position = 0; // rewind to beginning
    var rs = new RawSourceWaveStream(memoryStream, new WaveFormat(sampleRate, 16, 1));