Tuning

CTL reference

CTL reference

libopus is configured through opus_encoder_ctl / opus_decoder_ctl — a varargs interface keyed by integer request codes. libopus-wasm exposes this as encoderCtl(request, value) and decoderCtl(request, value), with the request codes published as the EncoderCtl and DecoderCtl enums.

Most tuning has a named setter; reach for CTLs when you need a request that has no dedicated helper.

#How it works

import { createEncoder, EncoderCtl } from "libopus-wasm";

const encoder = await createEncoder();
encoder.encoderCtl(EncoderCtl.SetBitrate, 32000);
encoder.getBitrate(); // 32000
import { createDecoder, DecoderCtl } from "libopus-wasm";

const decoder = await createDecoder();
decoder.decoderCtl(DecoderCtl.SetGain, 256); // +3 dB, Q8 fixed-point

#A curated, safe subset

encoderCtl and decoderCtl accept integer setter requests only. This is a deliberate safety boundary, not a thin wrapper over the C varargs API:

  • Setters, not getters. Getter CTLs in libopus write through a pointer
  • argument. Passing a JS number where libopus expects an int* would corrupt WASM memory, so getters are exposed as explicit methods (getBitrate, getLookahead, getInDtx) instead.

  • Allow-listed requests. Only the codes in EncoderCtl / DecoderCtl are
  • accepted. Any other request — including pointer-style ones — throws a RangeError at the JS boundary before touching WASM.

  • Integers only. Both request and value must be integers, or the call
  • throws a RangeError.

encoder.encoderCtl(4003, 0); // RangeError: not an allow-listed integer setter

#Encoder requests

EncoderCtl contains the supported encoder setter codes:

Memberlibopus requestEquivalent helper
SetApplicationOPUS_SET_APPLICATION(construction-time application)
SetBitrateOPUS_SET_BITRATEsetBitrate
SetMaxBandwidthOPUS_SET_MAX_BANDWIDTHsetMaxBandwidth
SetVbrOPUS_SET_VBRsetVbr
SetBandwidthOPUS_SET_BANDWIDTH
SetComplexityOPUS_SET_COMPLEXITYsetComplexity
SetInBandFecOPUS_SET_INBAND_FECsetFec
SetPacketLossPercentOPUS_SET_PACKET_LOSS_PERCsetPacketLossPercent
SetDtxOPUS_SET_DTXsetDtx
SetVbrConstraintOPUS_SET_VBR_CONSTRAINTsetVbrConstraint
SetForceChannelsOPUS_SET_FORCE_CHANNELS
SetSignalOPUS_SET_SIGNALsetSignal
SetLsbDepthOPUS_SET_LSB_DEPTH
SetExpertFrameDurationOPUS_SET_EXPERT_FRAME_DURATION
SetPredictionDisabledOPUS_SET_PREDICTION_DISABLED
SetPhaseInversionDisabledOPUS_SET_PHASE_INVERSION_DISABLED
// Tell Opus the input only carries 16 meaningful bits.
encoder.encoderCtl(EncoderCtl.SetLsbDepth, 16);

// Force a stereo encode even from a mono-leaning signal.
encoder.encoderCtl(EncoderCtl.SetForceChannels, 2);

#Decoder requests

DecoderCtl covers the supported decoder setters:

Memberlibopus requestNotes
SetGainOPUS_SET_GAINOutput gain in Q8 dB (256 = +3 dB).
SetPhaseInversionDisabledOPUS_SET_PHASE_INVERSION_DISABLEDDisable stereo phase inversion.
decoder.decoderCtl(DecoderCtl.SetGain, -256); // attenuate by 3 dB

#Next