opus

package
v0.3.21 Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2026 License: MIT Imports: 10 Imported by: 0 modules, 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 is a pure-Go Opus encoder and decoder (RFC 6716).

The Decoder decodes SILK, hybrid and CELT packets of every frame size and bandwidth, in channel mapping families 0, 1 and 255, to 48 kHz PCM within one LSB of libopus, concealing discontinuous transmission as libopus does and soft-clipping as its 16-bit API does.

The Encoder, for lys-shell's recordings, writes CELT-only fullband 20 ms frames (configuration 31) at a constant or constrained variable bitrate, from lys-ui's media PCM layout (48 kHz, interleaved signed 16-bit samples, one or two channels). It is a port of the RFC's reference encoder with libopus's later analysis: a 3 Hz high-pass, transient detection with short blocks and anti-collapse, time-frequency resolution changes, the pitch pre-filter, inter/intra energy prediction, dynamic allocation, the full bit allocation, spreading, intensity and dual stereo. It leaves out libopus's tonality analysis, which steers some of those choices when it is compiled in.

enc, err := opus.NewEncoder(2, 128000, opus.CBR)
head := enc.AppendHead(nil)        // the stream's ID header
pkt, err := enc.Encode(nil, frame) // FrameSize*2 samples per call

dec, err := opus.NewDecoder(head)
pcm, err := dec.Decode(nil, pkt) // drop dec.PreSkip() frames first

Index

Constants

View Source
const (
	// SampleRate is the input and coding rate in Hz.
	SampleRate = 48000
	// FrameSize is the samples per channel in one packet (20 ms).
	FrameSize = 960
	// PreSkip is the encoder's lookahead in samples at 48 kHz: a
	// decoder drops this many samples from the start of the stream.
	PreSkip = overlap
	// MinBitrate and MaxBitrate bound NewEncoder's bitrate in bits per
	// second.
	MinBitrate = 6000
	MaxBitrate = 510000
)

The stream geometry the encoder produces.

Variables

View Source
var (
	// ErrInvalidPacket is a packet whose framing (RFC 6716 section
	// 3) is invalid.
	ErrInvalidPacket = errors.New("invalid Opus packet")
	// ErrUnsupportedMapping is a channel mapping family other than 0, 1
	// and 255 (RFC 7845 section 5.1.1).
	ErrUnsupportedMapping = errors.New("unsupported Opus channel mapping")
)

Decode fails with these, wrapped with what was being decoded.

Functions

This section is empty.

Types

type Decoder

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

Decoder decodes an Opus stream (RFC 6716) to 48 kHz interleaved signed 16-bit PCM: SILK, hybrid and CELT frames of every size, bandwidth and channel count, within channel mapping families 0, 1 and 255 (RFC 7845 section 5.1.1). A frame of at most one byte (discontinuous transmission) is concealed as libopus conceals it. A Decoder decodes one stream in order and belongs to one goroutine.

func NewDecoder

func NewDecoder(head []byte) (*Decoder, error)

NewDecoder reads the stream's OpusHead identification header (RFC 7845 section 5.1), as Ogg's first packet and Matroska's CodecPrivate carry it.

func (*Decoder) Channels

func (d *Decoder) Channels() int

Channels is the number of interleaved channels Decode appends.

func (*Decoder) Decode

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

Decode decodes one packet and appends its interleaved samples to dst. After an error the decoder's history is unchanged.

func (*Decoder) PreSkip

func (d *Decoder) PreSkip() int

PreSkip is how many samples per channel at the stream's start the caller drops: the encoder's priming.

func (*Decoder) Reset

func (d *Decoder) Reset()

Reset forgets the decoder's history, for decoding from a packet that does not follow the last one decoded (after a seek, which pre-rolls 80 ms to converge).

type Encoder

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

Encoder turns PCM frames into Opus packets. It keeps the signal's history between frames, so one Encoder codes one stream in order.

func NewEncoder

func NewEncoder(channels, bitrate int, rate RateControl) (*Encoder, error)

NewEncoder returns an encoder for channels (1 or 2) at bitrate bits per second with the given rate control. A CBR bitrate rounds down to whole bytes per packet.

func (*Encoder) AppendHead

func (e *Encoder) AppendHead(dst []byte) []byte

AppendHead appends the stream's OpusHead identification header (RFC 7845 section 5.1): channel mapping family 0, PreSkip, no gain. It is Ogg's first packet and Matroska's CodecPrivate.

func (*Encoder) Channels

func (e *Encoder) Channels() int

Channels is the number of interleaved channels Encode expects.

func (*Encoder) Encode

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

Encode appends to dst one packet coding pcm, FrameSize samples per channel interleaved.

func (*Encoder) MaxPacketBytes

func (e *Encoder) MaxPacketBytes() int

MaxPacketBytes is the largest packet Encode appends; with CBR every packet is this size.

type RateControl

type RateControl int

RateControl is how an Encoder spends its bitrate.

const (
	// CBR makes every packet the same size.
	CBR RateControl = iota
	// ConstrainedVBR sizes each packet by how hard its frame is to
	// code (transients and loud frames get more, correlated stereo
	// less) while a bit reservoir holds the stream to the bitrate: it
	// never runs more than one average packet ahead, so no packet
	// exceeds twice the average.
	ConstrainedVBR
)

Jump to

Keyboard shortcuts

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