Class WaveProviderBase
Base class for creating an IWaveProvider of any format by overriding a
Read(byte[], int, int) method instead of Read(Span<byte>).
public abstract class WaveProviderBase : IWaveProvider
- Inheritance
-
WaveProviderBase
- Implements
- Inherited Members
- Extension Methods
Remarks
This exists for callers that cannot express Span<T> in a method signature.
The most common case is VB.NET, whose compiler rejects any source that names a ref struct
("Types with embedded references are not supported in this version of your compiler"), making
IWaveProvider impossible to implement directly. It is also a convenient landing
point when porting NAudio 2 code, whose Read had this exact signature.
If your provider produces 32 bit float samples, prefer SampleProviderBase, which also implements ISampleProvider so it can be used in a sample pipeline. If you need seeking and a length, derive from WaveStream instead — its inherited Read(byte[], int, int) overload is likewise overridable without naming a span.
The bridge rents a buffer from Shared and copies once per read, so C# code should prefer implementing IWaveProvider directly and avoid the extra copy.
Public Class SilenceProvider
Inherits WaveProviderBase
Public Sub New()
MyBase.New(New WaveFormat(44100, 16, 2))
End Sub
Public Overrides Function Read(buffer() As Byte, offset As Integer, count As Integer) As Integer
Array.Clear(buffer, offset, count)
Return count
End Function
End Class
Constructors
WaveProviderBase(WaveFormat)
Initializes a new instance of the WaveProviderBase class.
protected WaveProviderBase(WaveFormat waveFormat)
Parameters
waveFormatWaveFormatThe WaveFormat of the audio this provider produces.
Properties
WaveFormat
The WaveFormat of this provider.
public WaveFormat WaveFormat { get; }
Property Value
Methods
Read(byte[], int, int)
Method to override in derived classes. Fill count bytes into
buffer, starting at offset.
public abstract int Read(byte[] buffer, int offset, int count)
Parameters
bufferbyte[]The buffer to fill with audio data.
offsetintThe offset into
bufferto start writing at.countintThe number of bytes to write.
Returns
- int
The number of bytes written. Return 0 to signal end of stream.