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 ¶
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 ¶
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 ¶
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) Decode ¶
Decode decodes one packet and appends its interleaved samples to dst. After an error the decoder's history is unchanged.
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 ¶
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) Encode ¶
Encode appends to dst one packet coding pcm, FrameSize samples per channel interleaved.
func (*Encoder) MaxPacketBytes ¶
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 )