Skip to content

Decoder Class Reference

devongovett edited this page Jun 15, 2012 · 9 revisions

The Decoder class is an abstract class that subclasses extend to implement actual audio decoding for various codecs (e.g. MP3, AAC, FLAC, etc.). Decoders receive data from the Demuxer and implement the readChunk method to decode the audio data into Linear PCM to be passed to the audio hardware for playback, or other output destination.

When you implement your own decoders, remember to call Decoder.register(id, YourDecoder) to register your decoder with the framework.

Properties

Methods To Implement

Events

Registering and Finding

Properties

Decoder#stream

A Stream object containing the audio data.

Decoder#bitstream

A Bitstream object containing the audio data.

Decoder#receivedFinalBuffer

A boolean that keeps track of whether the decoder has been sent the final buffer from the Demuxer yet.

Methods To Implement

Decoder#setCookie(cookie)

This method is optional and is called when a 'cookie' event is emitted by the Demuxer. The cookie is passed as a Buffer object.

Decoder#readChunk()

readChunk is required, and is called by the framework whenever decoding is necessary. readChunk is responsible for decoding the audio data found in the stream or bitstream and emitting data events containing the decoded audio data as a Typed Array.

If not enough data is available in the stream at the time readChunk is called, schedule it to be called later by registering readChunk for the 'available' event once in the future, e.g. this.once('available', this.readChunk).

If there is an error in the decoding process, emit an error event to stop the whole decoding process entirely.

Events

'data', buffer

You should emit the data event whenever you have decoded some number of samples of Linear PCM audio data. You should include the data as a Typed Array.

'available'

This event is emitted by the Decoder class whenever new data is available from the Demuxer.

'error', err

You should emit the error event whenever there is an error in the decoder. Emitting this event will halt the rest of the decoding process.

Registering and Finding

Decoder.register(id, decoder)

All Decoder subclasses should be registered with the Decoder class using this class method. You can register your decoder for multiple ids if you know that different containers use different FOURCCs for the codec, for example.

Decoder.find(id)

Finds a registered decoder using the 4 (or less) character FOURCC code the decoder registered itself as.

Clone this wiki locally