Class AudioBuffer
Represents an audio buffer that manages audio data in various formats and provides methods for manipulating and accessing the buffer.
Implements
Inherited Members
Namespace: FFmpeg.Audio
Assembly: FFmpeg.dll
Syntax
public sealed class AudioBuffer : IDisposable
Properties
View SourceAlignment
Gets the alignment value for the audio data in the buffer.
Declaration
public int Alignment { get; }
Property Value
| Type | Description |
|---|---|
| int |
BytesPerSample
Gets the number of bytes per sample for the current audio format.
Declaration
public int BytesPerSample { get; }
Property Value
| Type | Description |
|---|---|
| int |
Capacity
Gets the maximum capacity of the buffer in terms of samples.
Declaration
public int Capacity { get; }
Property Value
| Type | Description |
|---|---|
| int |
Channels
Gets the number of audio channels in the buffer.
Declaration
public int Channels { get; }
Property Value
| Type | Description |
|---|---|
| int |
Format
Gets the sample format of the audio buffer.
Declaration
public SampleFormat Format { get; }
Property Value
| Type | Description |
|---|---|
| SampleFormat |
Info
Gets the audio buffer metadata, including the sample format, number of channels, and alignment.
Declaration
public AudioBufferInfo Info { get; }
Property Value
| Type | Description |
|---|---|
| AudioBufferInfo |
IsPacked
Gets a value indicating whether the audio format is packed. In packed format, the audio data for all channels is stored interleaved.
Declaration
public bool IsPacked { get; }
Property Value
| Type | Description |
|---|---|
| bool |
IsPlanar
Gets a value indicating whether the audio format is planar. In planar format, each channel's data is stored separately.
Declaration
public bool IsPlanar { get; }
Property Value
| Type | Description |
|---|---|
| bool |
IsReadOnly
Gets a value indicating whether the buffer is read-only. If the buffer has more than one reference, true; otherwise, false.
Declaration
public bool IsReadOnly { get; }
Property Value
| Type | Description |
|---|---|
| bool |
Samples
Gets or sets the number of audio samples currently stored in the buffer.
Declaration
public int Samples { get; }
Property Value
| Type | Description |
|---|---|
| int |
Methods
View SourceAllocate(AudioBufferInfo, int)
Allocates a new AudioBuffer with the specified buffer info and sample capacity.
Declaration
public static AudioBuffer Allocate(AudioBufferInfo info, int sampleCapacity)
Parameters
| Type | Name | Description |
|---|---|---|
| AudioBufferInfo | info | The buffer info including format, channels, and alignment. |
| int | sampleCapacity | The number of samples the buffer should be able to hold. |
Returns
| Type | Description |
|---|---|
| AudioBuffer | A newly allocated AudioBuffer. |
Allocate(SampleFormat, int, int)
Allocates a new AudioBuffer with the specified format, number of channels, and sample capacity.
Declaration
public static AudioBuffer Allocate(SampleFormat format, int channels, int sampleCapacity)
Parameters
| Type | Name | Description |
|---|---|---|
| SampleFormat | format | The audio sample format, which defines the type of samples (e.g., integer or floating-point). |
| int | channels | The number of audio channels (e.g., 2 for stereo). |
| int | sampleCapacity | The number of audio samples the buffer should be able to hold per channel. |
Returns
| Type | Description |
|---|---|
| AudioBuffer | A newly allocated AudioBuffer with the specified format, channels, and sample capacity. |
AsReadOnlySpanPacked()
Returns a ReadOnlySpan<T> of packed audio data, where the samples are tightly packed in the buffer.
Declaration
public ReadOnlySpan<byte> AsReadOnlySpanPacked()
Returns
| Type | Description |
|---|---|
| ReadOnlySpan<byte> | A ReadOnlySpan<T> of byte representing the packed audio data. |
Remarks
This method will only return the span if the audio buffer is packed (i.e., the data is stored without padding). If the buffer is not packed, a NotSupportedException is thrown.
Exceptions
| Type | Condition |
|---|---|
| NotSupportedException | Thrown when the buffer is not packed (i.e., false for IsPacked). |
AsSpanPacked()
Returns a ReadOnlySpan<T> of packed audio data, where the samples are tightly packed in the buffer.
Declaration
public Span<byte> AsSpanPacked()
Returns
| Type | Description |
|---|---|
| Span<byte> | A ReadOnlySpan<T> of byte representing the packed audio data. |
Remarks
This method will only return the span if the audio buffer is packed (i.e., the data is stored without padding). If the buffer is not packed, a NotSupportedException is thrown.
Exceptions
| Type | Condition |
|---|---|
| NotSupportedException | Thrown when the buffer is not packed (i.e., false for IsPacked). |
Clear()
Clears the audio buffer, resetting the sample count to zero.
Declaration
public void Clear()
Clone()
Clones the current AudioBuffer by creating a new reference to the existing buffer.
Declaration
public AudioBuffer Clone()
Returns
| Type | Description |
|---|---|
| AudioBuffer | A new AudioBuffer instance sharing the same memory reference. |
Exceptions
| Type | Condition |
|---|---|
| OutOfMemoryException | Thrown if the buffer reference creation fails. |
Copy()
Creates a new copy of the current AudioBuffer with its own allocated memory.
Declaration
public AudioBuffer Copy()
Returns
| Type | Description |
|---|---|
| AudioBuffer | A new AudioBuffer instance with copied data. |
Exceptions
| Type | Condition |
|---|---|
| OutOfMemoryException | Thrown if memory allocation fails. |
CopyFrom(AudioBuffer, int)
Copies audio data from another AudioBuffer into this buffer.
Declaration
public int CopyFrom(AudioBuffer source, int startSrcSampleIndex = 0)
Parameters
| Type | Name | Description |
|---|---|---|
| AudioBuffer | source | The source AudioBuffer to copy data from. |
| int | startSrcSampleIndex | The starting sample index in the source buffer. |
Returns
| Type | Description |
|---|---|
| int | The number of samples copied. |
CopyFrom(AVFrame)
Copies audio data from an AVFrame into this buffer.
Declaration
public int CopyFrom(AVFrame source)
Parameters
| Type | Name | Description |
|---|---|---|
| AVFrame | source | The AVFrame containing the source audio data. |
Returns
| Type | Description |
|---|---|
| int | The number of samples copied. |
Exceptions
| Type | Condition |
|---|---|
| ArgumentException | Thrown when the channel count in the source frame does not match the destination buffer. |
CopyFromPlanarUncheckedType(ReadOnlySpan<byte>)
Copies audio data from a planar source without checking the data type.
Declaration
public int CopyFromPlanarUncheckedType(ReadOnlySpan<byte> source)
Parameters
| Type | Name | Description |
|---|---|---|
| ReadOnlySpan<byte> | source | The source buffer containing audio data to copy. |
Returns
| Type | Description |
|---|---|
| int | The number of samples copied. |
Exceptions
| Type | Condition |
|---|---|
| ArgumentException | Thrown when the source buffer length is not a multiple of BytesPerSample and Channels. |
CopyFromPlanar<T>(ReadOnlySpan<T>)
Copies audio data from a planar source with type checking.
Declaration
public int CopyFromPlanar<T>(ReadOnlySpan<T> source) where T : unmanaged
Parameters
| Type | Name | Description |
|---|---|---|
| ReadOnlySpan<T> | source | The source buffer containing audio data to copy. |
Returns
| Type | Description |
|---|---|
| int | The number of samples copied. |
Type Parameters
| Name | Description |
|---|---|
| T | The data type of the audio samples. |
CopyTo(AudioBuffer, int)
Copies audio data from this buffer to another AudioBuffer.
Declaration
public int CopyTo(AudioBuffer destination, int startSrcSampleIndex = 0)
Parameters
| Type | Name | Description |
|---|---|---|
| AudioBuffer | destination | The destination AudioBuffer to copy data to. |
| int | startSrcSampleIndex | The starting sample index in the source buffer. |
Returns
| Type | Description |
|---|---|
| int | The number of samples copied. |
Exceptions
| Type | Condition |
|---|---|
| ArgumentException | Thrown when the number of channels does not match between the source and destination buffers. |
CopyToPackedUncheckedType(Span<byte>, int)
Copies audio data from this buffer to a packed format buffer without checking the data type.
Declaration
public int CopyToPackedUncheckedType(Span<byte> destination, int startSrcSampleIndex = 0)
Parameters
| Type | Name | Description |
|---|---|---|
| Span<byte> | destination | The destination buffer to copy data to. |
| int | startSrcSampleIndex | The starting sample index in the source buffer. |
Returns
| Type | Description |
|---|---|
| int | The number of samples per channel that were copied. |
Exceptions
| Type | Condition |
|---|---|
| ArgumentException | Thrown when the destination buffer length is not a multiple of BytesPerSample and Channels. |
CopyToPacked<T>(Span<T>, int)
Copies audio data from this buffer to a packed format buffer with type checking.
Declaration
public int CopyToPacked<T>(Span<T> destination, int startSrcSampleIndex = 0) where T : unmanaged
Parameters
| Type | Name | Description |
|---|---|---|
| Span<T> | destination | The destination buffer to copy data to. |
| int | startSrcSampleIndex | The starting sample index in the source buffer. |
Returns
| Type | Description |
|---|---|
| int | The number of samples per channel that were copied. |
Type Parameters
| Name | Description |
|---|---|
| T | The type of the audio data. |
CreateFromPackedUncheckedType(AudioBufferInfo, ReadOnlySpan<byte>)
Creates an AudioBuffer from a packed audio source without performing type checking.
Declaration
public static AudioBuffer CreateFromPackedUncheckedType(AudioBufferInfo info, ReadOnlySpan<byte> source)
Parameters
| Type | Name | Description |
|---|---|---|
| AudioBufferInfo | info | The audio buffer info. |
| ReadOnlySpan<byte> | source | The source data in packed format. |
Returns
| Type | Description |
|---|---|
| AudioBuffer | An AudioBuffer initialized with the specified packed audio data. |
Exceptions
| Type | Condition |
|---|---|
| ArgumentException | Thrown if the length of the buffer is not a multiple of BytesPerSample*Channels. |
CreateFromPacked<T>(AudioBufferInfo, ReadOnlySpan<T>)
Creates an AudioBuffer from a packed source, performing type validation based on the sample format.
Declaration
public static AudioBuffer CreateFromPacked<T>(AudioBufferInfo info, ReadOnlySpan<T> source) where T : unmanaged
Parameters
| Type | Name | Description |
|---|---|---|
| AudioBufferInfo | info | The audio buffer info. |
| ReadOnlySpan<T> | source | The source data in packed format. |
Returns
| Type | Description |
|---|---|
| AudioBuffer | An AudioBuffer initialized with the specified packed audio data. |
Type Parameters
| Name | Description |
|---|---|
| T | The type of the audio data, which must be unmanaged. |
Dispose()
Publicly accessible dispose method. Disposes the current AudioBuffer and suppresses finalization.
Declaration
public void Dispose()
~AudioBuffer()
Finalizer for the AudioBuffer class. Ensures resources are freed when the object is garbage collected.
Declaration
protected ~AudioBuffer()
MakeWriteable()
Ensures that the buffer is writable. If the buffer cannot be made writable, throws an OutOfMemoryException.
Declaration
public void MakeWriteable()
Exceptions
| Type | Condition |
|---|---|
| OutOfMemoryException | Thrown when the buffer cannot be made writable. |
TryMakeWriteable()
Attempts to make the buffer writable by ensuring the reference count is 1. If the buffer is already writable, it does nothing.
Declaration
public bool TryMakeWriteable()
Returns
| Type | Description |
|---|---|
| bool |