naudio / NAudio

Audio and MIDI library for .NET
MIT License
5.38k stars 1.09k forks source link

WaveFileWriter Dispose disposing output stream #1040

Open geferon opened 1 year ago

geferon commented 1 year ago

Whenever WaveFileWriter is Disposed it's disposing the output stream attached to it, when it shouldn't, as seen here: https://github.com/naudio/NAudio/blob/master/NAudio.Core/Wave/WaveOutputs/WaveFileWriter.cs#L388

I'm currently using a memory stream to save data to the memory and I need to dispose of this stream so I can save everything to the memory stream and use it afterwards, but I cannot do this as the WaveFileWriter is disposing the underlying output stream. The output stream should be disposed by the consumer, or there should be some sort of setting that allows you to choose if the attached output stream will be disposed or not.

geferon commented 1 year ago

As I imagine, this is done because of the constructor that creates the file stream. This should only really be handled with an internal variable such as "_disposeOutput" which only gets set to true whenever using such constructor. For example, NAudio.Lame handles this correctly: https://github.com/Corey-M/NAudio.Lame/blob/master/NAudio.Lame/LameMP3FileWriter.cs#L108

markheath commented 1 year ago

There is a convenience helper class called IgnoreDisposeStream that you can wrap your stream which was intended for exactly this scenario. Changing the current behaviour would unfortunately be a breaking change for existing consumers of the library.

geferon commented 1 year ago

Oh huh, I didn't see it... Still unintuitive and not the appropriate way of streams handling output streams but, if you say this would break existing consumers then so be it. Should I close the issue?

markheath commented 1 year ago

NAudio was initially created over 20 years ago. There's a lot I would do differently today if I had the time to rework it (which is unfortunately not at the moment).