AudioBuffer

readonly struct NatML.Devices.AudioBuffer

Audio buffers contain audio data, along with corresponding information about aforementioned data. These buffers are produced by AudioDevice instances while streaming, but can also be created with client-owned data.

Creating an Audio Buffer

While clients will usually interact with audio buffers produced by audio devices, clients can also create audio buffers for use with audio device outputs. Audio buffers can be created in several different ways:

From a Sample Buffer

/// <summary>
/// Create an audio buffer.
/// </summary>
/// <param name="sampleBuffer">Linear PCM audio sample buffer interleaved by channel.</param>
/// <param name="sampleRate">Audio buffer sample rate.</param>
/// <param name="channelCount">Audio buffer channel count.</param>
/// <param name="timestamp">Audio buffer timestamp in nanoseconds.</param>
AudioBuffer (
    float[] sampleBuffer,
    int sampleRate,
    int channelCount,
    long timestamp
);

Audio buffers can be created by clients for use with existing audio outputs. The sampleBuffer should contain linear PCM audio data interleaved by channel (when channelCount > 1).

From a Native Array

/// <summary>
/// Create an audio buffer.
/// </summary>
/// <param name="sampleBuffer">Linear PCM audio sample buffer interleaved by channel.</param>
/// <param name="sampleRate">Audio buffer sample rate.</param>
/// <param name="channelCount">Audio buffer channel count.</param>
/// <param name="timestamp">Audio buffer timestamp in nanoseconds.</param>
AudioBuffer (
    NativeArray<float> sampleBuffer,
    int sampleRate,
    int channelCount,
    long timestamp
);

The audio buffer constructor has an overload that accepts a NativeArray<float> in place of a managed float[].

From a Native Buffer

/// <summary>
/// Create an audio buffer.
/// </summary>
/// <param name="sampleBuffer">Linear PCM audio sample buffer interleaved by channel.</param>
/// <param name="sampleRate">Audio buffer sample rate.</param>
/// <param name="channelCount">Audio buffer channel count.</param>
/// <param name="sampleCount">Audio buffer total sample count.</param>
/// <param name="timestamp">Audio buffer timestamp in nanoseconds.</param>
AudioBuffer (
    float* sampleBuffer,
    int sampleRate,
    int channelCount,
    int sampleCount,
    long timestamp
);

The audio buffer constructor has an overload that accepts a raw float* buffer. The sampleCount refers to the total number of samples within the buffer.

The sampleBuffer must remain valid for the duration of the audio buffer.

Inspecting the Buffer Format

Audio buffers declare the format of the audio data contained within them:

Sample Rate

/// <summary>
/// Audio buffer sample rate.
/// </summary>
int sampleRate { get; }

The sampleRate is defined in Hertz.

Channel Count

/// <summary>
/// Audio buffer channel count.
/// </summary>
int channelCount { get; }

The channelCount defines the number of interleaved channels within the audio data.

Inspecting the Buffer Timestamp

/// <summary>
/// Audio buffer timestamp in nanoseconds.
/// </summary>
long timestamp { get; }

The timestamp defines the buffer timestamp in nanoseconds.

For audio buffers generated by AudioDevice instances, the timestamp is based on the system media clock.

Accessing the Sample Buffer

/// <summary>
/// Audio sample buffer.
/// </summary>
NativeArray<float> sampleBuffer { get; }

The sampleBuffer contains linear PCM audio samples, interleaved by channel.

Inspecting the Audio Device

/// <summary>
/// Audio device that this buffer was generated from.
/// </summary>
IMediaDevice<AudioBuffer> device { get; }

The device identifies the audio device that the buffer was generated from.

Last updated