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
Configuration object for the audio data.
Properties
The sample format of the audio data. Returns
null if the AudioData has been closed.The sample rate in Hz. This value indicates how many samples represent one second of audio.
The number of audio frames in this AudioData. A frame consists of one sample per channel at a single point in time.
The number of audio channels. For example, 1 for mono audio, 2 for stereo.
The duration in microseconds, calculated as
(numberOfFrames / sampleRate) * 1_000_000.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
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
The destination buffer to copy audio data into. Must be large enough to hold the data.
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.
The sample format of the audio data.
The sample rate in Hz.
The number of audio frames.
The number of audio channels.
The presentation timestamp in microseconds.
The raw audio sample data.
AudioDataCopyToOptions
Options for theallocationSize() and copyTo() methods.
The index of the plane to copy. For interleaved formats, this is always 0. For planar formats, this is the channel index.
The offset in frames from the start of the audio data. Defaults to 0.
The number of frames to copy. Defaults to all remaining frames from the offset.
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