Skip to content

Encoder

Encodes raw PCM audio frames into Opus packets.

The encoder state is an opaque libopus pointer that persists across calls - do not create a new Encoder per frame. One Encoder instance per audio stream is the correct usage.

Call destroy when the encoder is no longer needed to free the native state.

Encoder.handle  :: FFIPointer
Encoder.pcmPointer  :: FFIPointer

Native buffer that each PCM frame is copied into before encoding, reused for every call.

Encoder.outputPointer  :: FFIPointer

Native buffer that libopus writes the encoded packet into, reused for every call.

Encoder.sampleRate  :: number
Encoder.channels  :: number
Encoder.application  :: number
Encoder : encode ( pcmFrame )  -> buffer

Encodes a raw PCM frame into an Opus packet. The frame must be exactly one Opus frame duration of samples.

Parameters

pcmFrame: buffer

Returns

buffer
Encoder : setBitrate ( bitsPerSecond )  -> ()

Sets the target bitrate in bits per second. Raises outside 500 to 510000, and after destroy.

Parameters

bitsPerSecond: number
Encoder : getBitrate ( )  -> number

Returns the bitrate libopus is using, in bits per second. Raises after destroy.

Returns

number
Encoder : setSignal ( signal )  -> ()

Hints what the audio is: "voice", "music", or "auto" to let libopus decide. Raises on any other value, and after destroy.

Parameters

signal: Signal
Encoder : getSignal ( )  -> Signal

Returns the signal hint in effect. Raises after destroy.

Returns

Signal
Encoder : destroy ( )  -> ()

Frees the native encoder state. No-op if already destroyed.

Encoder . checkBitrate ( bitsPerSecond )  -> ()

Raises unless bitsPerSecond is a whole number from 500 to 510000, including when it is not a number at all, so it can check a bitrate taken straight from user options.

libopus calls 500 to 512000 the meaningful range. The cap is 510000 because, after encoding a 20 ms stereo frame, libopus reports at most 510400, so the value read back equals the value set across the whole range.

Parameters

bitsPerSecond: unknown
Encoder . new ( sampleRate ,  channels ,  application )  -> Encoder

Responsible for constructing this class.

Parameters

sampleRate: number
channels: number
application: number

Returns

Encoder