Skip to main content

AudioDecoder

The AudioDecoder class decodes compressed audio data (EncodedAudioChunk) into raw audio samples (AudioData). It supports common audio codecs like AAC, MP3, Opus, and FLAC.

Quick Example

Always call audioData.close() in your output callback after processing. AudioData objects hold native memory that JavaScript’s garbage collector cannot reclaim automatically. Failing to close them will cause memory leaks.

Constructor

Creates a new AudioDecoder instance.
init
AudioDecoderInit
required
Initialization object containing callback functions.

Properties

state
CodecState
required
Current state of the decoder. One of:
  • 'unconfigured' - Initial state, call configure() before decoding
  • 'configured' - Ready to decode chunks
  • 'closed' - Decoder has been closed and cannot be used
decodeQueueSize
number
required
Number of chunks waiting to be decoded. Use this for backpressure management - if the queue grows too large, pause feeding new chunks until it decreases.

Static Methods

isConfigSupported()

Checks whether a given configuration is supported by the decoder without creating an instance.
config
AudioDecoderConfig
required
The configuration to test for support.
Returns: Promise<AudioDecoderSupport> - Object indicating whether the config is supported.

Instance Methods

configure()

Configures the decoder with codec parameters. Must be called before decode().
config
AudioDecoderConfig
required
Configuration object specifying the codec and audio parameters.
Throws: InvalidStateError if the decoder is closed.

decode()

Queues an encoded audio chunk for decoding. Decoded frames will be delivered asynchronously to the output callback.
chunk
EncodedAudioChunk
required
The encoded audio chunk to decode. Unlike video, most audio chunks are keyframes and can be decoded independently.
Throws:
  • InvalidStateError if the decoder is not configured
  • DataError if the chunk data is malformed

flush()

Completes all pending decode operations. Call this when you’ve submitted all chunks and need to ensure all output has been delivered.
Returns: Promise<void> - Resolves when all queued chunks have been decoded and output. Throws: InvalidStateError if the decoder is not configured.

reset()

Resets the decoder to the 'unconfigured' state, discarding any pending work. The decoder can be reconfigured after reset.
Throws: InvalidStateError if the decoder is closed.

close()

Closes the decoder and releases all resources. The decoder cannot be used after calling close().

Interfaces

AudioDecoderConfig

Configuration for the audio decoder.

AudioDecoderInit

Initialization object for creating an AudioDecoder.

AudioDecoderSupport

Result from isConfigSupported().

Common Codec Strings


Complete Decoding Example


See Also

AudioData

Raw audio sample data produced by decoding

AudioEncoder

Encode AudioData into EncodedAudioChunks

EncodedAudioChunk

Compressed audio data for decoding

Codec Registry

Full list of supported codecs