Skip to main content

AudioData

The AudioData class represents a container for audio sample data. It provides access to raw audio samples along with metadata such as sample format, sample rate, channel count, and timestamps.
This API follows the W3C WebCodecs AudioData specification. Refer to the spec for detailed behavior requirements.
Memory Management: AudioData objects hold references to potentially large audio buffers. You must call close() when finished with an AudioData instance to release the underlying memory. Failure to do so can lead to memory leaks.

Quick Example

Sample Formats

Planar vs Interleaved: In interleaved formats, samples alternate between channels (L, R, L, R…). In planar formats, each channel’s samples are stored contiguously in separate planes. Planar formats are often more efficient for audio processing, while interleaved formats are common for I/O operations.

Constructor

Creates a new AudioData instance from the provided initialization data.

Parameters

init
AudioDataInit
required
Configuration object for the audio data.

Properties

format
AudioSampleFormat | null
required
The sample format of the audio data. Returns null if the AudioData has been closed.
sampleRate
number
required
The sample rate in Hz. This value indicates how many samples represent one second of audio.
numberOfFrames
number
required
The number of audio frames in this AudioData. A frame consists of one sample per channel at a single point in time.
numberOfChannels
number
required
The number of audio channels. For example, 1 for mono audio, 2 for stereo.
duration
number
required
The duration in microseconds, calculated as (numberOfFrames / sampleRate) * 1_000_000.
timestamp
number
required
The presentation timestamp in microseconds, indicating when this audio should be played.

Methods

allocationSize()

Calculates the number of bytes needed to hold the audio data when copying with the specified options.

Parameters

options
AudioDataCopyToOptions
required
Options specifying which portion of audio to measure.

Returns

number - The number of bytes required.

copyTo()

Copies audio sample data to a destination buffer.

Parameters

destination
BufferSource
required
The destination buffer to copy audio data into. Must be large enough to hold the data.
options
AudioDataCopyToOptions
required
Options specifying which portion of audio to copy.

Returns

void

clone()

Creates a copy of this AudioData with its own data buffer.

Returns

AudioData - A new AudioData instance with copied data.

close()

Releases the resources held by this AudioData instance. After calling close(), the AudioData is no longer usable.

Returns

void

Interfaces

AudioDataInit

Configuration object passed to the AudioData constructor.
format
AudioSampleFormat
required
The sample format of the audio data.
sampleRate
number
required
The sample rate in Hz.
numberOfFrames
number
required
The number of audio frames.
numberOfChannels
number
required
The number of audio channels.
timestamp
number
required
The presentation timestamp in microseconds.
data
BufferSource
required
The raw audio sample data.

AudioDataCopyToOptions

Options for the allocationSize() and copyTo() methods.
planeIndex
number
required
The index of the plane to copy. For interleaved formats, this is always 0. For planar formats, this is the channel index.
frameOffset
number
The offset in frames from the start of the audio data. Defaults to 0.
frameCount
number
The number of frames to copy. Defaults to all remaining frames from the offset.
format
AudioSampleFormat
Optional target format for conversion during copy. If not specified, uses the source format.

Type Aliases

AudioSampleFormat

See Also

AudioEncoder

Encode raw audio data into compressed formats

AudioDecoder

Decode compressed audio into AudioData

EncodedAudioChunk

Container for encoded audio data