Reference

API reference

API reference

Everything exported from libopus-wasm. For the Discord-compatible adapter, see discord.js compatibility.

import {
  loadLibopus,
  createEncoder,
  createDecoder,
  getPacketInfo,
  Application,
  Signal,
  Bitrate,
  Bandwidth,
  EncoderCtl,
  DecoderCtl,
  OpusError,
  OpusErrorCode,
  isOpusError,
} from "libopus-wasm";

#Top-level functions

FunctionReturnsDescription
loadLibopus()Promise<{ version: string }>Loads the module and returns the bundled libopus version string.
createEncoder(options?)Promise<OpusEncoderHandle>Creates a raw-packet encoder. See EncoderOptions.
createDecoder(options?)Promise<OpusDecoderHandle>Creates a raw-packet decoder. See DecoderOptions.
getPacketInfo(packet, options?)Promise<OpusPacketInfo>Inspects a raw packet — duration, frames, channels, bandwidth — without decoding. See Packet inspection.

Both factories share one lazily-loaded WASM module, so the first call pays the load cost and later calls are cheap.

#Encoder

OpusEncoderHandle, returned by createEncoder.

#Properties

PropertyTypeDescription
applicationApplicationResolved encoding mode (read-only).
channels1 | 2Channel count (read-only).
frameSizenumberDefault frame size in samples/channel (read-only).
sampleRateSampleRateSample rate in Hz (read-only).

#Methods

MethodReturnsDescription
encode(pcm, options?)Uint8ArrayEncode one Int16 PCM frame (Int16Array | Uint8Array) to a packet.
encodeFloat(pcm, options?)Uint8ArrayEncode one Float32Array frame ([-1, 1]) to a packet.
encodeFrames(frames, options?)Uint8Array[]Encode several Int16 frames.
encodeFloatFrames(frames, options?)Uint8Array[]Encode several Float32 frames.
setBitrate(bitrate)voidSet bitrate (number | "auto" | "max").
getBitrate()numberRead the resolved bitrate.
setComplexity(n)voidSet complexity 010.
setSignal(signal)voidSet the Signal hint.
setMaxBandwidth(bw)voidCap coded Bandwidth.
setVbr(enabled)voidToggle variable bitrate.
setVbrConstraint(enabled)voidToggle constrained VBR.
setFec(enabled)voidToggle in-band FEC.
setPacketLossPercent(n)voidSet expected loss 0100.
setDtx(enabled)voidToggle discontinuous transmission.
getLookahead()numberEncoder algorithmic delay in samples.
getInDtx()booleanWhether the encoder is currently in a DTX period.
encoderCtl(request, value)voidInteger-setter CTL passthrough.
free()voidRelease the underlying encoder. Idempotent.
[Symbol.dispose]()voidCalls free(); enables using declarations.

encode accepts an Int16Array or a Uint8Array view of the same little-endian bytes. The frame must contain exactly frameSize * channels samples.

#Decoder

OpusDecoderHandle, returned by createDecoder.

#Properties

PropertyTypeDescription
channels1 | 2Channel count (read-only).
maxFrameSizenumberOutput capacity in samples/channel (read-only).
sampleRateSampleRateSample rate in Hz (read-only).

#Methods

MethodReturnsDescription
decode(packet, options?)Int16ArrayDecode one packet (or null for PLC) to Int16 PCM.
decodeFloat(packet, options?)Float32ArrayDecode one packet (or null) to Float32 PCM.
decodeFrames(packets, options?)Int16Array[]Decode several packets; null entries are concealed.
decodeFloatFrames(packets, options?)Float32Array[]Float32 batch variant.
decodePacketLoss(frameSize?)Int16ArraySynthesize one PLC frame (defaults to 20 ms).
decodePacketLossFloat(frameSize?)Float32ArrayFloat32 PLC variant.
decoderCtl(request, value)voidInteger-setter CTL passthrough.
free()voidRelease the underlying decoder. Idempotent.
[Symbol.dispose]()voidCalls free(); enables using declarations.

decode(null, { frameSize }) is equivalent to decodePacketLoss(frameSize).

#Options

#EncoderOptions

Passed to createEncoder. All fields are optional.

FieldTypeDefaultDescription
sampleRateSampleRate48000One of 8000, 12000, 16000, 24000, 48000.
channels1 | 22Mono or stereo.
applicationApplicationAudioEncoding mode.
bitratenumber | "auto" | "max"64000Target bits per second.
complexitynumber10010.
signalSignalAutoContent hint.
maxBandwidthBandwidthunsetCoded-bandwidth ceiling.
vbrbooleanunsetVariable bitrate.
vbrConstraintbooleanunsetConstrained VBR.
fecbooleanfalseIn-band FEC.
packetLossPercentnumber00100.
dtxbooleanfalseDiscontinuous transmission.
frameSizenumber20 msDefault samples/channel per frame.

#DecoderOptions

Passed to createDecoder. All fields are optional.

FieldTypeDefaultDescription
sampleRateSampleRate48000Must match the encoder.
channels1 | 22Must match the encoder.
maxFrameSizenumber120 msOutput capacity in samples/channel.

#EncodeOptions

Passed per encode / encodeFloat call.

FieldTypeDefaultDescription
frameSizenumberencoder defaultSamples/channel for this frame.
maxPacketBytesnumber4000Output buffer ceiling.

#DecodeOptions

Passed per decode / decodeFloat call.

FieldTypeDefaultDescription
decodeFecbooleanfalseRecover a lost frame from this packet's FEC data.
frameSizenumber20 msFrame size for PLC / FEC recovery.
maxFrameSizenumberdecoder defaultOutput capacity for this call.

#Enums and constants

#Application

MemberValue
Voip2048
Audio2049
RestrictedLowDelay2051

#Signal

MemberValue
Auto-1000
Voice3001
Music3002

#Bitrate

MemberValue
Auto-1000
Max-1

The string forms "auto" and "max" are accepted anywhere a Bitrate is.

#Bandwidth

MemberValue
Narrowband1101
Mediumband1102
Wideband1103
Superwideband1104
Fullband1105

#EncoderCtl / DecoderCtl

Integer request codes for the CTL passthrough. See the CTL reference for the full list and usage.

#Errors

OpusError extends Error is thrown when libopus itself returns an error code:

MemberTypeDescription
codenumberThe libopus error code.
codeNamestring | undefinedNamed libopus error code, such as "InvalidPacket", when known.
operationstring | undefinedThe call that failed (e.g. "decode").
messagestringHuman-readable libopus error text.

Use OpusErrorCode for named libopus error codes and isOpusError(error) for realm-safe checks.

Argument validation (wrong frame size, out-of-range option, empty packet, non-allow-listed CTL) throws a plain RangeError before reaching WASM. Using a handle after free() throws a plain Error. See Errors & validation for the full breakdown.

#Limits

ConstraintAllowed values
Sample rate8000, 12000, 16000, 24000, 48000 Hz
Channels1 (mono), 2 (stereo)
Encode frame duration2.5, 5, 10, 20, 40, 60 ms
Decode output capacityup to 120 ms
PLC / FEC frame sizemultiples of 2.5 ms, from 2.5 to 120 ms

#Types

These TypeScript types are exported for annotating your own code:

import type {
  OpusEncoderHandle,
  OpusDecoderHandle,
  EncoderOptions,
  DecoderOptions,
  EncodeOptions,
  DecodeOptions,
  PacketInfoOptions,
  OpusPacketInfo,
  SampleRate,
  ChannelCount,
} from "libopus-wasm";