AudioData
TheAudioData 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.
Quick Example
Sample Formats
Constructor
Creates a newAudioData instance from the provided initialization data.
Parameters
AudioDataInit
required
Configuration object for the audio data.
Properties
AudioSampleFormat | null
required
The sample format of the audio data. Returns
null if the AudioData has been closed.number
required
The sample rate in Hz. This value indicates how many samples represent one second of audio.
number
required
The number of audio frames in this AudioData. A frame consists of one sample per channel at a single point in time.
number
required
The number of audio channels. For example, 1 for mono audio, 2 for stereo.
number
required
The duration in microseconds, calculated as
(numberOfFrames / sampleRate) * 1_000_000.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.Example: Calculate buffer size
Example: Calculate buffer size
Parameters
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.Example: Copy audio data to a buffer
Example: Copy audio data to a buffer
Example: Copy a portion of audio data
Example: Copy a portion of audio data
Parameters
BufferSource
required
The destination buffer to copy audio data into. Must be large enough to hold the data.
AudioDataCopyToOptions
required
Options specifying which portion of audio to copy.
Returns
void
clone()
Creates a copy of this AudioData with its own data buffer.Example: Clone audio data
Example: Clone audio data
Returns
AudioData - A new AudioData instance with copied data.
close()
Releases the resources held by this AudioData instance. After callingclose(), the AudioData is no longer usable.
Example: Proper resource cleanup
Example: Proper resource cleanup
Returns
void
Interfaces
AudioDataInit
Configuration object passed to theAudioData constructor.
AudioSampleFormat
required
The sample format of the audio data.
number
required
The sample rate in Hz.
number
required
The number of audio frames.
number
required
The number of audio channels.
number
required
The presentation timestamp in microseconds.
BufferSource
required
The raw audio sample data.
AudioDataCopyToOptions
Options for theallocationSize() and copyTo() methods.
number
required
The index of the plane to copy. For interleaved formats, this is always 0. For planar formats, this is the channel index.
number
The offset in frames from the start of the audio data. Defaults to 0.
number
The number of frames to copy. Defaults to all remaining frames from the offset.
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