Table of Contents

Class SampleProviderBase

Namespace
NAudio.Wave
Assembly
NAudio.Core.dll

Base class for creating an ISampleProvider by overriding a Read(float[], int, int) method instead of Read(Span<float>). Like WaveProvider32 it also implements IWaveProvider, so a derived class can be passed straight to an IWavePlayer.

public abstract class SampleProviderBase : ISampleProvider, IWaveProvider
Inheritance
SampleProviderBase
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 ISampleProvider and WaveProvider32 impossible to implement directly. It is also a convenient landing point when porting NAudio 2 code, whose Read had this exact signature.

The bridge rents a buffer from Shared and copies once per read, so C# code should prefer implementing ISampleProvider or deriving from WaveProvider32 directly and avoid the extra copy.

In VB.NET:
Public Class SineProvider
    Inherits SampleProviderBase

    Public Sub New()
        MyBase.New(44100, 1)
    End Sub

    Public Overrides Function Read(buffer() As Single, offset As Integer, count As Integer) As Integer
        For i = 0 To count - 1
            buffer(offset + i) = 0.0F
        Next
        Return count
    End Function
End Class

Constructors

SampleProviderBase()

Initializes a new instance of the SampleProviderBase class, defaulting to 44.1kHz mono.

protected SampleProviderBase()

SampleProviderBase(int, int)

Initializes a new instance of the SampleProviderBase class with the specified sample rate and channel count.

protected SampleProviderBase(int sampleRate, int channels)

Parameters

sampleRate int

Sample rate in Hz.

channels int

Number of channels.

Properties

WaveFormat

The WaveFormat of this provider, always 32 bit IEEE float.

public WaveFormat WaveFormat { get; }

Property Value

WaveFormat

Methods

Read(float[], int, int)

Method to override in derived classes. Fill count samples into buffer, starting at offset.

public abstract int Read(float[] buffer, int offset, int count)

Parameters

buffer float[]

The buffer to fill with 32 bit floating point samples.

offset int

The offset into buffer to start writing at.

count int

The number of samples to write.

Returns

int

The number of samples written. Return 0 to signal end of stream.

SetWaveFormat(int, int)

Allows you to specify the sample rate and channel count for this provider. Should be set before the provider is passed to a wave player or mixer.

public void SetWaveFormat(int sampleRate, int channels)

Parameters

sampleRate int

Sample rate in Hz.

channels int

Number of channels.