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
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 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
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.
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.
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.
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