opus

package
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Jul 2, 2026 License: BSD-2-Clause Imports: 4 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 encoder. This is the default build. The encoder is currently CELT-only, which is weaker on speech than libopus; build with `-tags libopus` for the C library instead (see encoder_libopus.go).

func NewEncoder

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

NewEncoder builds an Encoder for channels-channel 48 kHz audio at the given bitrate in bits per second; pass 0 for the codec default.

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

Jump to

Keyboard shortcuts

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