1

I'm using NAudio to capture 15 seconds of audio. Like this:

MemoryStream bufferedVoice = new MemoryStream();
voiceCapturer = new WasapiLoopbackCapture(OutputDevice);
voiceCapturer.DataAvailable += onVoiceOutputReceived;
voiceCapturer.StartRecording();

private void onVoiceOutputReceived(object sender, WaveInEventArgs e)
{
    bufferedVoice.Write(e.Buffer, 0, e.BytesRecorded);    
}

And after 15 seconds I want to save it to a file, and exit. I tried it like this but it didn't work:

var ResourceWaveStream = new RawSourceWaveStream(bufferedVoice, voiceCapturer.WaveFormat);
var SampleProvider = new WaveToSampleProvider(ResourceWaveStream).ToWaveProvider16();
var fileWriter = new WaveFileWriter("output.mp3", SampleProvider.WaveFormat);

byte[] buf = new byte[8192];
while(SampleProvider.Read(buf, 0, buf.Length) > 0)
{
    fileWriter.Write(buf, 0, buf.Length);
}
fileWriter.Dispose();

How can I save the memorystream into a file?

Clarification: I only want to store x seconds of audio in memory. So when the max size is reached, some of the oldest part is removed. Then if I press a button, I want to save the 15 seconds of audio into a file.

Now, my question is how should I store the audio in memory, and then write it to a file?

botiapa
  • 353
  • 2
  • 13
  • Is this what you are looking for? https://stackoverflow.com/questions/19722028/convert-wasapiloopbackcapture-wav-audio-stream-to-mp3-file – Lee Toffolo May 21 '18 at 19:58
  • If not, the issue may be arising from the fact that WAV and MP3 are different, one is lossless and the other is compressed, and if i remember correctly from tinkering with mp3s there can be headers to the data file as well. Could be a 1!=1 compatibility issue. – Lee Toffolo May 21 '18 at 20:00
  • i meant to write wav instead of mp3. But that wasn't the problem. – botiapa May 22 '18 at 16:28
  • And no unfortunately that isn't what I'm looking for. – botiapa May 22 '18 at 16:29

2 Answers2

1

Try this:

using(var fileWriter = new WaveFileWriter("yourOutputFile", SampleProvider.WaveFormat)
{
    ResourceWaveStream.CopyTo(fileWriter);
}

Btw, the "using" block is good for you here because it will automatically dispose the writer, allowing the WaveFileWriter to write headers to the file.

Lee Toffolo
  • 134
  • 1
  • 1
  • 10
  • I've created a WaveFileWriter(new WaveFileWriter(new IgnoreDisposeStream(ms), voiceCapturer.WaveFormat)) and wrote the sound data to it. After that I copied the memorystream to a proper WaveFileWriter, but it didn't work. – botiapa May 22 '18 at 17:00
  • modified answer, try this out? – Lee Toffolo May 22 '18 at 17:46
  • Wait, do I need to pass the memorystream to a ResourceWaveStream before I copy it to a filewriter? – botiapa May 22 '18 at 18:53
  • 1
    Yes, if you use ResourseWaveStream you need to convert to it from the memoryStream, this im assuming adds Wave-type formatting and I think you should do this. You could also try to directly copy the MemoryStream to the WaveFileWriter too (skip the new RawSourceWaveStream() part) , but im not sure if the WaveWriter will like that. If you want to try to copy the MemoryStream directly to the writer, be sure to rewind the stream (seek 0) before copying it. – Lee Toffolo May 22 '18 at 18:56
0

Okay, I finally have the solution. First, I copy the sound data directly to the memory stream. And then when I need it, I read the whole memorystream into a RawSourceWaveStream and then I pass that to the final WaveFileWriter. If anybody is interested in how I did it exactly, then message me.

Thanks.

botiapa
  • 353
  • 2
  • 13