opus

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Sep 18, 2026 License: BSD-2-Clause Imports: 6 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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.

Both the decoder and the encoder are pure Go, so the package needs no cgo: the decoder handles every Opus mode, and the encoder emits wideband SILK (see encoder.go), which is the mode tuned for speech.

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. 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.

func NewEncoder

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

NewEncoder builds a SILK Encoder for 48 kHz audio from cfg.

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
}

EncoderConfig configures an Encoder.

Jump to

Keyboard shortcuts

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