Skip to main content

AudioEncoder

The AudioEncoder class encodes raw AudioData objects into compressed EncodedAudioChunk objects. It supports common audio codecs like AAC, MP3, Opus, FLAC, and Vorbis through FFmpeg.

Quick Example

Always call audioData.close() after passing it to encode(). Failing to close AudioData objects will cause memory leaks as the underlying buffers are not automatically released.

Supported Audio Codecs


Constructor

Creates a new AudioEncoder with output and error callbacks.
init
AudioEncoderInit
required
Initialization callbacks for the encoder.
Throws: TypeError if callbacks are not functions.

Properties

state
CodecState
required
Current encoder state. One of:
  • 'unconfigured' - Not configured yet, or reset() was called
  • 'configured' - Ready to encode audio data
  • 'closed' - Encoder has been closed and cannot be used
encodeQueueSize
number
required
Number of pending encode operations. Useful for backpressure management when encoding large amounts of audio data.

Static Methods

isConfigSupported()

Check if an audio encoder configuration is supported on this platform.
config
AudioEncoderConfig
required
Configuration to test for support.
Returns: Promise<AudioEncoderSupport> - Resolves with support status and normalized config.

Instance Methods

configure()

Configure the encoder with codec parameters. Must be called before encoding.
config
AudioEncoderConfig
required
Encoder configuration specifying codec and audio parameters.
Throws:
  • InvalidStateError if encoder is closed
  • NotSupportedError if codec is not supported

encode()

Queue an AudioData object for encoding.
data
AudioData
required
The raw audio data to encode. Must have a valid format and timestamp.
Throws:
  • InvalidStateError if encoder is not configured
  • TypeError if data is invalid
Always call audioData.close() after encoding to prevent memory leaks. The encoder makes an internal copy of the data, so closing the source is safe.

addEventListener()

Add an event listener for encoder events.
type
string
required
Event type. Currently supports 'dequeue' which fires when the encode queue size decreases.
listener
() => void
required
Callback function to invoke when the event fires.
options
object
Event listener options.

removeEventListener()

Remove a previously added event listener.
type
string
required
Event type (e.g., 'dequeue').
listener
() => void
required
The callback function to remove. Must be the same function reference that was added.

flush()

Wait for all pending encode operations to complete.
Returns: Promise<void> - Resolves when all queued audio data has been encoded. Throws:
  • InvalidStateError if encoder is not configured
  • Rejects if encoding fails

reset()

Reset the encoder to the unconfigured state.
Aborts all pending encode operations and resets to the 'unconfigured' state. You must call configure() again before encoding more audio. Throws: InvalidStateError if encoder is closed

close()

Close the encoder and release all resources.
The encoder cannot be used after calling close(). Any pending encode operations are aborted and the state transitions to 'closed'.

Type Definitions

AudioEncoderConfig

Configuration options for the audio encoder.

AudioEncoderInit

Initialization callbacks for creating an AudioEncoder.

AudioEncoderOutputMetadata

Metadata provided with encoded audio chunks.

AudioEncoderSupport

Result from isConfigSupported() indicating codec support.

AudioBitrateMode

Bitrate control mode for audio encoding.

See Also

AudioData

Raw audio sample data for encoding

AudioDecoder

Decode EncodedAudioChunks back to AudioData

EncodedAudioChunk

Encoded audio chunk output