Using WaveFileWriter with Streams...

Nov 12, 2009 at 8:52 AM


Was pleased with the "Trim Audio" code snippet. (
Tried it out with NAudio 1.3 (which was totally new to me) and worked perfectly on regular wav disk files.

My scenario involed in-memory Waves so I was pleased to see that (apparently starting with 1.3 release) WaveFileReader/Writer did support Streams.

Problem is that in the current WaveFileWriter class design, wave header and length is only updated when the object is disposed... which is ok when writing to a file, a little bit less when the stream is from another kind, because we might want to copy the result stream somewhere before disposing it.

Correct me if I'm wrong, but I was thinking of copying the block of code that updates the header from the Dispose method into the Flush method. Do you think that could do the job without breaking anything else?

UPDATE: Actually I did try this quick fix and it dit it, at least to me. Keep on the good work on this very helpful library.


Nov 13, 2009 at 1:39 PM

Hi Frederic,

Glad you got it working. I hope to make the WaveFileWriter class a bit more effective as a base class, so people can extend it for custom behaviour. If I made the Flush method virtual, and put the code that updated the header in a protected method, then you could get your desired behaviour without having to modify WaveFileWriter.



Jul 14, 2014 at 10:32 AM

I'm trying to do what the original poster described - created a complete wave in memory using the WaveFileWriter and then passing the memorystream to another component. I need the wave header and length updating so am trying override the WaveFileWriter.Flush() method to call UpdateHeader().

However, the BinaryWriter seems to be a private variable so I don't have access to this to pass it to the UpdateHeader method.

Am I missing something?


Jul 17, 2014 at 2:29 PM
UpdateHeader is virtual, so you can create your own class that inherits WaveFileWriter and do whatever you like in there
Jul 17, 2014 at 3:56 PM
Yeah but what I want to do involves the BinaryWriter member variable which is private.

I would suggest that this is made protected so that it is accessible to classes that inherit from WaveFileWriter.


Jul 17, 2014 at 4:03 PM
it's a parameter to UpdateHeader, so you can use it in there