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.
Summary
Properties
Encoder.handle :: FFIPointerEncoder.pcmPointer :: FFIPointerEncoder.outputPointer :: FFIPointerEncoder.sampleRate :: numberEncoder.channels :: numberEncoder.application :: numberProperties
Section titled “Properties”handle
Section titled “handle”pcmPointer
Section titled “pcmPointer”Native buffer that each PCM frame is copied into before encoding, reused for every call.
outputPointer
Section titled “outputPointer”Native buffer that libopus writes the encoded packet into, reused for every call.
sampleRate
Section titled “sampleRate”channels
Section titled “channels”application
Section titled “application”Methods
Section titled “Methods”encode
Section titled “encode”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 |
setBitrate
Section titled “setBitrate”Encoder : setBitrate ( bitsPerSecond ) -> ()
Sets the target bitrate in bits per second. Raises outside 500 to 510000, and after destroy.
Parameters
| bitsPerSecond: number |
getBitrate
Section titled “getBitrate”Encoder : getBitrate ( ) -> number
Returns the bitrate libopus is using, in bits per second. Raises after destroy.
Returns
| number |
setSignal
Section titled “setSignal”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 |
getSignal
Section titled “getSignal”Encoder : getSignal ( ) -> Signal
Returns the signal hint in effect. Raises after destroy.
Returns
| Signal |
destroy
Section titled “destroy”Encoder : destroy ( ) -> ()
Frees the native encoder state. No-op if already destroyed.
Functions
Section titled “Functions”checkBitrate
Section titled “checkBitrate”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.