naudio / NAudio

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

Proposal: new wave provider based on I/O pipelines #1158

Open wldevries opened 1 month ago

wldevries commented 1 month ago

All current building blocks of NAudio are built on or inspired by Stream and it's interface. There are several advantages of using a more modern approach, which is why I propose building blocks based on I/O pipelines.

public interface IWavePipeProvider
{
    WaveFormat Format { get; }
    PipeReader Reader { get; }
}

One advantage that spoke to me is that PipeWriter allows you to push data through. This allowed me to easily build a theremin like block with mouse input without having to worry about buffers. Backpressure is provided by pausing the writer on calls to its FlushAsync. Other advantage are better explained on the documentation page linked above that the API of pipelines or in its announcement blog post

Interoperability with current IWaveProvider can be achieved by converting PipeReader to a Stream. Ideally this would be an extension method similar to ToWaveProvider and ToSampleProvider.

WavePipeProvider generator = new();
RawSourceWaveStream waveStream = new(generator.Reader.AsStream(), generator.Format);

Similarly Channels could be a good fit as a modern variant of ISampleProvider.