opus

package
v0.3.9 Latest Latest
Warning

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

Go to latest
Published: Nov 7, 2026 License: MIT Imports: 8 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 (RFC 6716) for lys-shell's recordings: 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

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

This section is empty.

Functions

This section is empty.

Types

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