AudioBuffer

namespace VideoKit {
    /// <summary>
    /// View of linear PCM audio data in memory.
    /// </summary>
    readonly struct AudioBuffer : IDisposable { ... }
}

The AudioBuffer stores linear PCM audio data and provides properties for accessing the buffer's sample rate, channel count, and more.


Creating an Audio Buffer

The audio buffer must be created with floating point linear PCM audio data interleaved by channel:

From a Managed Buffer

/// <summary>
/// Create an audio buffer from a linear PCM sample buffer.
/// </summary>
/// <param name="sampleRate">Sample rate.</param>
/// <param name="channelCount">Channel count.</param>
/// <param name="sampleBuffer">Sample buffer.</param>
/// <param name="timestamp">Timestamp in nanoseconds.</param>
AudioBuffer (
    int sampleRate,
    int channelCount,
    float[] sampleBuffer,
    long timestamp = 0L
);

This constructor creates a new AudioBuffer from an array of linear PCM samples.

From a Native Array

/// <summary>
/// Create an audio buffer from a linear PCM sample buffer.
/// </summary>
/// <param name="sampleRate">Sample rate.</param>
/// <param name="channelCount">Channel count.</param>
/// <param name="sampleBuffer">Sample buffer.</param>
/// <param name="timestamp">Timestamp in nanoseconds.</param>
AudioBuffer (
    int sampleRate,
    int channelCount,
    NativeArray<float> sampleBuffer,
    long timestamp = 0L
);

This constructor initializes an AudioBuffer from a Unity NativeArray<float> of samples. This overload avoids copying the sample buffer.

From a Native Buffer

/// <summary>
/// Create an audio buffer from a linear PCM sample buffer.
/// </summary>
/// <param name="sampleRate">Sample rate.</param>
/// <param name="channelCount">Channel count.</param>
/// <param name="sampleBuffer">Sample buffer.</param>
/// <param name="sampleCount">Total number of samples in sample buffer.</param>
/// <param name="timestamp">Timestamp in nanoseconds.</param>
unsafe AudioBuffer (
    int sampleRate,
    int channelCount,
    float* sampleBuffer,
    int sampleCount,
    long timestamp = 0L
);

This constructor initializes an AudioBuffer from a native sample buffer.


Inspecting the Sample Rate

/// <summary>
/// Sample rate.
/// </summary>
int sampleRate { get; }

The sampleRate refers to the audio sample rate in Hertz.


Inspecting the Channel Count

/// <summary>
/// Channel count.
/// </summary>
int channelCount { get; }

The channelCount refers to the audio channel count.


Inspecting the Audio Timestamp

/// <summary>
/// Timestamp for the audio buffer in nanoseconds.
/// </summary>
long timestamp { get; }

The timestamp refers to the generation time for the audio buffer based on the system's high resolution media clock.


Accessing the Audio Data

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

The sampleBuffer contains floating point linear PCM audio data interleaved by channel.


Disposing the Audio Buffer

/// <summary>
/// Dispose the audio buffer and release resources.
/// </summary>
void Dispose ();

Because the AudioBuffer holds native resources, it must explicitly be disposed when it is no longer needed.


Was this page helpful?