opus

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 30, 2026 License: BSD-2-Clause Imports: 6 Imported by: 0

Documentation

Overview

Package opus wraps the Opus codec for jargo's audio path: it decodes Opus packets to PCM on the way in and encodes PCM to Opus packets on the way out.

The codec runs at 48 kHz — the rate WebRTC negotiates for Opus — so audio crosses the transport without resampling. Encoding produces 20 ms frames, the standard WebRTC packetization.

The decoder is pure Go. The Encoder has two builds selected by the `libopus` build tag (see encoder_pion.go / encoder_libopus.go): the default is the pure-Go encoder; `-tags libopus` links the C library for higher speech quality. Both expose the same NewEncoder/Encode API.

Index

Constants

View Source
const (
	// SampleRate is the codec sample rate in Hz. Opus runs at 48 kHz.
	SampleRate = 48000
	// FrameDuration is the duration of one encoded packet.
	FrameDuration = 20 * time.Millisecond
	// FrameSamples is the number of samples per channel in one 20 ms frame.
	FrameSamples = SampleRate / 1000 * 20 // 960

)

Variables

This section is empty.

Functions

func FrameBytes

func FrameBytes(channels int) int

FrameBytes is the size in bytes of one 20 ms S16LE frame for channels.

Types

type Decoder

type Decoder struct {
	// contains filtered or unexported fields
}

Decoder decodes Opus packets into signed 16-bit little-endian PCM.

func NewDecoder

func NewDecoder(channels int) (*Decoder, error)

NewDecoder builds a Decoder that outputs channels-channel 48 kHz PCM. It decodes all Opus modes (SILK, CELT and hybrid).

func (*Decoder) Decode

func (d *Decoder) Decode(packet []byte) ([]byte, error)

Decode decodes one Opus packet into interleaved S16LE PCM bytes.

type Encoder

type Encoder struct {
	// contains filtered or unexported fields
}

Encoder encodes 48 kHz S16LE PCM into Opus packets using the pure-Go pion SILK encoder. This is the default build — SILK is tuned for speech and needs no cgo. The 48 kHz input is downmixed to mono and resampled to 16 kHz with the pure-Go resampler, then encoded as wideband SILK. Build with `-tags libopus` for the C library instead; both expose the same NewEncoder/Encode API.

func NewEncoder

func NewEncoder(cfg EncoderConfig) (*Encoder, error)

NewEncoder builds a SILK Encoder for 48 kHz audio from cfg. EncoderConfig.InbandFEC is ignored: this encoder emits no FEC redundancy.

func (*Encoder) Encode

func (e *Encoder) Encode(pcm []byte) ([]byte, error)

Encode encodes exactly one 20 ms frame of interleaved S16LE PCM — that is FrameBytes(channels) bytes — into a single SILK-only Opus packet.

type EncoderConfig added in v0.1.0

type EncoderConfig struct {
	// Channels is the number of channels in the PCM handed to Encode.
	Channels int
	// Bitrate is the target bitrate in bits per second; 0 uses the codec
	// default. Speech is transparent well below the codec's fullband range, so
	// raising this past ~32 kbps for a single voice mostly costs bandwidth.
	Bitrate int
	// InbandFEC embeds a low-rate copy of the previous frame in each packet so
	// the receiver can reconstruct a dropped one instead of concealing it. It
	// trades a little of the bitrate budget for resilience, which is worth it
	// on lossy links (mobile radio, relayed media) and inert on clean ones,
	// where the redundancy is simply never decoded. Honored by the libopus
	// build; the pure-Go SILK encoder ignores it.
	InbandFEC bool
	// ExpectedPacketLoss is the loss percentage (0-100) the encoder sizes its
	// FEC redundancy for. Ignored unless InbandFEC is set.
	ExpectedPacketLoss int
}

EncoderConfig configures an Encoder.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL