Documentation
¶
Overview ¶
Package opus provides a Opus Audio Codec RFC 6716 implementation
Index ¶
- type Application
- type Bandwidth
- type Configuration
- type Decoder
- func (d *Decoder) Decode(in, out []byte) (bandwidth Bandwidth, isStereo bool, err error)
- func (d *Decoder) DecodeFloat32(in []byte, out []float32) (bandwidth Bandwidth, isStereo bool, err error)
- func (d *Decoder) DecodePLC(out []int16) error
- func (d *Decoder) DecodeToFloat32(in []byte, out []float32) (int, error)
- func (d *Decoder) DecodeToInt16(in []byte, out []int16) (int, error)
- func (d *Decoder) Init(sampleRate, channels int) error
- type Encoder
- func (e *Encoder) Application() Application
- func (e *Encoder) Bandwidth() Bandwidth
- func (e *Encoder) Complexity() int
- func (e *Encoder) ConstrainedVBR() bool
- func (e *Encoder) Encode(in []byte, out []byte) (int, error)
- func (e *Encoder) EncodeFloat32(in []float32, out []byte) (int, error)
- func (e *Encoder) EncodeSILK(pcm []int16, bandwidth Bandwidth, out []byte) (int, error)
- func (e *Encoder) LossRate() int
- func (e *Encoder) MaxBandwidth() Bandwidth
- func (e *Encoder) SetApplication(app Application) error
- func (e *Encoder) SetBandwidth(bw Bandwidth) error
- func (e *Encoder) SetBitrate(bps int) error
- func (e *Encoder) SetComplexity(complexity int) error
- func (e *Encoder) SetConstrainedVBR(cvbr bool)
- func (e *Encoder) SetLossRate(rate int) error
- func (e *Encoder) SetMaxBandwidth(bw Bandwidth) error
- func (e *Encoder) SetVBR(vbr bool)
- func (e *Encoder) VBR() bool
- type EncoderOption
- func WithApplication(app Application) EncoderOption
- func WithBandwidth(bw Bandwidth) EncoderOption
- func WithBitrate(bps int) EncoderOption
- func WithChannels(channels int) EncoderOption
- func WithComplexity(complexity int) EncoderOption
- func WithConstrainedVBR(cvbr bool) EncoderOption
- func WithMaxBandwidth(bw Bandwidth) EncoderOption
- func WithSampleRate(rate int) EncoderOption
- func WithVBR(vbr bool) EncoderOption
- type OpusDecoder
- type OpusDecoderConfig
- type OpusEncoder
- type OpusEncoderConfig
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Application ¶
type Application int
Application selects the encoder's tuning profile, mirroring libopus's OPUS_APPLICATION_* control values (opus_defines.h) and their numeric IDs. RFC 6716 does not define per-application behavior as part of the bitstream; it only describes the underlying control parameters — bitrate mode, frame duration, DTX — that each profile is meant to bias (see RFC 6716 Section 2.1, "Control Parameters"). Selecting an Application here only records the chosen profile, retrievable via Application(); it does not change VBR, frame duration, or DTX on its own — pass WithVBR, WithConstrainedVBR, etc. explicitly.
const ( // ApplicationAudio tunes the encoder for music and general audio. This // is the default application. ApplicationAudio Application = 2049 // ApplicationVoIP tunes the encoder for voice over a lossy, // latency-sensitive network. In libopus this profile defaults to VBR // (RFC 6716 Section 2.1.8) and DTX (RFC 6716 Section 2.1.9); this // encoder does not wire those defaults automatically. ApplicationVoIP Application = 2048 // ApplicationRestrictedLowDelay tunes the encoder for the lowest // possible algorithmic delay by skipping mode-switching analysis // between the SILK and CELT layers. Frame duration and look-ahead // trade-offs are described in RFC 6716 Section 2.1.4; this encoder // does not vary either by application. ApplicationRestrictedLowDelay Application = 2051 )
type Bandwidth ¶
type Bandwidth byte
The Bandwidth the Opus codec scales from 6 kbit/s narrowband mono speech to 510 kbit/s fullband stereo music, with algorithmic delays ranging from 5 ms to 65.2 ms. At any given time, either the LP layer, the MDCT layer, or both, may be active. It can seamlessly switch between all of its various operating modes, giving it a great deal of flexibility to adapt to varying content and network conditions without renegotiating the current session. The codec allows input and output of various audio bandwidths, defined as follows:
+----------------------+-----------------+-------------------------+ | Abbreviation | Audio Bandwidth | Sample Rate (Effective) | +----------------------+-----------------+-------------------------+ | NB (narrowband) | 4 kHz | 8 kHz | | | | | | MB (medium-band) | 6 kHz | 12 kHz | | | | | | WB (wideband) | 8 kHz | 16 kHz | | | | | | SWB (super-wideband) | 12 kHz | 24 kHz | | | | | | FB (fullband) | 20 kHz (*) | 48 kHz | +----------------------+-----------------+-------------------------+
https://datatracker.ietf.org/doc/html/rfc6716#section-2
const ( BandwidthAuto Bandwidth = 0 // let the encoder select based on bitrate BandwidthNarrowband Bandwidth = 1 BandwidthMediumband Bandwidth = 2 BandwidthWideband Bandwidth = 3 BandwidthSuperwideband Bandwidth = 4 BandwidthFullband Bandwidth = 5 )
Bandwidth constants. Numbered explicitly, not with iota, because BandwidthAuto's 0 value doesn't belong to the Narrowband..Fullband sequence it's leading.
func (Bandwidth) SampleRate ¶
SampleRate returns the effective SampleRate for a given bandwidth.
type Configuration ¶
type Configuration byte
Configuration numbers in each range (e.g., 0...3 for NB SILK- only) correspond to the various choices of frame size, in the same order. For example, configuration 0 has a 10 ms frame size and configuration 3 has a 60 ms frame size.
+-----------------------+-----------+-----------+-------------------+ | Configuration | Mode | Bandwidth | Frame Sizes | | Number(s) | | | | +-----------------------+-----------+-----------+-------------------+ | 0...3 | SILK-only | NB | 10, 20, 40, 60 ms | | | | | | | 4...7 | SILK-only | MB | 10, 20, 40, 60 ms | | | | | | | 8...11 | SILK-only | WB | 10, 20, 40, 60 ms | | | | | | | 12...13 | Hybrid | SWB | 10, 20 ms | | | | | | | 14...15 | Hybrid | FB | 10, 20 ms | | | | | | | 16...19 | CELT-only | NB | 2.5, 5, 10, 20 ms | | | | | | | 20...23 | CELT-only | WB | 2.5, 5, 10, 20 ms | | | | | | | 24...27 | CELT-only | SWB | 2.5, 5, 10, 20 ms | | | | | | | 28...31 | CELT-only | FB | 2.5, 5, 10, 20 ms | +-----------------------+-----------+-----------+-------------------+
type Decoder ¶
type Decoder struct {
// contains filtered or unexported fields
}
Decoder decodes the Opus bitstream into PCM.
func NewDecoderWithOutput ¶
NewDecoderWithOutput creates a new Opus Decoder with the requested output sample rate and channel count.
func (*Decoder) DecodeFloat32 ¶
func (d *Decoder) DecodeFloat32(in []byte, out []float32) (bandwidth Bandwidth, isStereo bool, err error)
DecodeFloat32 decodes the Opus bitstream into F32LE PCM.
func (*Decoder) DecodeToFloat32 ¶
DecodeToFloat32 decodes Opus data into float32 PCM and returns the sample count per channel.
func (*Decoder) DecodeToInt16 ¶
DecodeToInt16 decodes Opus data into signed 16-bit PCM and returns the sample count per channel.
type Encoder ¶
type Encoder struct {
// contains filtered or unexported fields
}
Encoder encodes PCM into Opus packets.
func NewEncoder ¶
func NewEncoder(opts ...EncoderOption) (*Encoder, error)
NewEncoder creates a new Opus encoder with the supplied options.
Defaults: 48 kHz, mono, 24 kbit/s, complexity 5. Pass options to override any of these. The current implementation supports 48 kHz, 1 or 2 channels, 20 ms CELT-only packets, plus SILK-only encoding via EncodeSILK. Transient detection is a follow-up.
func (*Encoder) Application ¶
func (e *Encoder) Application() Application
Application returns the current encoder application mode.
func (*Encoder) Complexity ¶
func (*Encoder) ConstrainedVBR ¶
ConstrainedVBR returns whether constrained VBR is enabled.
func (*Encoder) Encode ¶
Encode encodes S16LE PCM into a single Opus packet.
The input must contain exactly one 20 ms mono 48 kHz frame.
func (*Encoder) EncodeFloat32 ¶
EncodeFloat32 encodes float PCM into a single Opus packet.
The input must contain one 20 ms 48 kHz frame.
func (*Encoder) EncodeSILK ¶
EncodeSILK encodes one 20, 40, or 60 ms mono SILK frame into a SILK-only Opus packet. pcm must hold exactly one frame of mono s16 samples at the bandwidth's internal rate: 160/320/480 (Narrowband/8 kHz), 240/480/720 (Mediumband/12 kHz), or 320/640/960 (Wideband/16 kHz) samples for 20/40/60 ms. Durations longer than 20 ms are coded as multiple 20 ms SILK coding units in a single SILK header, per RFC 6716 Section 4.2.1. This is a separate entry point from Encode/EncodeFloat32 — bitrate-based auto-selection always picks CELT bandwidths (Wideband and up); SILK is for callers who specifically want a SILK-only voice packet (VoIP/narrowband use cases), not an automatic CELT/SILK/hybrid switch. Superwideband and Fullband aren't SILK bandwidths and are rejected. Applies a fixed DC-removal high-pass before encoding (libopus's dc_reject applied to the shared PCM path); the pitch-adaptive VoIP cutoff (hp_cutoff) is not implemented. Covers voiced/LTP prediction, noise shaping and NLSF interpolation (see internal/silk); the delayed-decision NSQ, stereo, hybrid mode, and the bitrate-control loop are not yet implemented.
func (*Encoder) MaxBandwidth ¶
MaxBandwidth returns the maximum bandwidth the auto-select algorithm may choose.
func (*Encoder) SetApplication ¶
func (e *Encoder) SetApplication(app Application) error
SetApplication updates the encoder application mode.
func (*Encoder) SetBandwidth ¶
SetBandwidth sets the encoder bandwidth, overriding auto-selection.
func (*Encoder) SetBitrate ¶
SetBitrate updates the target bitrate in bits per second.
func (*Encoder) SetComplexity ¶
SetComplexity updates the encoder complexity on the standard Opus 0..10 scale.
func (*Encoder) SetConstrainedVBR ¶
SetConstrainedVBR enables or disables constrained VBR (RFC 6716 Section 2.1.8).
func (*Encoder) SetLossRate ¶
SetLossRate sets the expected packet loss rate (0-100 percent), the control parameter behind the packet loss resilience trade-off described in RFC 6716 Section 2.1.6.
func (*Encoder) SetMaxBandwidth ¶
SetMaxBandwidth sets the maximum bandwidth the auto-select algorithm may choose. Only affects encoding when bandwidth is set to BandwidthAuto (the default).
func (*Encoder) SetVBR ¶
SetVBR enables or disables variable bitrate encoding (RFC 6716 Section 2.1.8).
type EncoderOption ¶
EncoderOption configures an Encoder during construction.
Options are applied in the order they are passed to NewEncoder. Each option returns an error if the requested value is unsupported by the current encoder slice, so callers can detect unsupported configurations at construction time rather than at first encode.
func WithApplication ¶
func WithApplication(app Application) EncoderOption
WithApplication sets the encoder application mode.
func WithBandwidth ¶
func WithBandwidth(bw Bandwidth) EncoderOption
WithBandwidth sets the encoder bandwidth explicitly (Narrowband through Fullband; Mediumband is SILK-only and not supported here). Use WithMaxBandwidth instead to cap auto-selection rather than fixing it.
func WithBitrate ¶
func WithBitrate(bps int) EncoderOption
WithBitrate sets the target bitrate in bits per second. Valid range is 6000 to 510000.
func WithChannels ¶
func WithChannels(channels int) EncoderOption
WithChannels sets the channel count (1 for mono, 2 for stereo).
func WithComplexity ¶
func WithComplexity(complexity int) EncoderOption
WithComplexity sets the encoder complexity on the standard Opus 0..10 scale. Higher values enable more analysis (pitch detection, spreading, dynalloc) for better quality at the cost of CPU.
func WithConstrainedVBR ¶
func WithConstrainedVBR(cvbr bool) EncoderOption
WithConstrainedVBR enables or disables constrained VBR. When enabled, the encoder simulates a "bit reservoir" to bound short-term bitrate variation instead of producing plain VBR — recommended for low-latency links over a constrained connection (RFC 6716 Section 2.1.8).
func WithMaxBandwidth ¶
func WithMaxBandwidth(bw Bandwidth) EncoderOption
WithMaxBandwidth sets the maximum bandwidth the auto-select algorithm may choose. Has no effect when an explicit bandwidth is set via WithBandwidth.
func WithSampleRate ¶
func WithSampleRate(rate int) EncoderOption
WithSampleRate sets the input sample rate in Hz. The current encoder only supports 48 kHz (the CELT internal rate).
func WithVBR ¶
func WithVBR(vbr bool) EncoderOption
WithVBR enables or disables variable bitrate encoding. VBR is the more efficient mode and is the Opus default; CBR is reserved for transports that require a fixed frame size or for highly sensitive streams (RFC 6716 Section 2.1.8).
type OpusDecoder ¶
type OpusDecoder struct {
// contains filtered or unexported fields
}
OpusDecoder wraps Decoder with a qrtc-compatible method surface.
func CreateOpusDecoder ¶
func CreateOpusDecoder(config *OpusDecoderConfig) (*OpusDecoder, error)
CreateOpusDecoder creates a pure-Go Opus decoder through the compatibility API.
func (*OpusDecoder) Close ¶
func (d *OpusDecoder) Close() error
Close releases decoder resources. It is a no-op for the pure-Go decoder.
type OpusDecoderConfig ¶
OpusDecoderConfig configures the compatibility decoder API used by github.com/qrtc/opus-go callers.
type OpusEncoder ¶
type OpusEncoder struct {
// contains filtered or unexported fields
}
OpusEncoder wraps Encoder with a qrtc-compatible method surface.
func CreateOpusEncoder ¶
func CreateOpusEncoder(config *OpusEncoderConfig) (*OpusEncoder, error)
CreateOpusEncoder creates a pure-Go Opus encoder through the compatibility API.
In addition to the upstream 48 kHz/20 ms CELT API, it supports the xiaozhi wire format of 8/12/16/24/48 kHz PCM, one or two channels, and 60 ms packets. That path resamples each 20 ms segment to 48 kHz and packs three CBR CELT frames into a single Opus Code 3 packet.
func (*OpusEncoder) Close ¶
func (e *OpusEncoder) Close() error
Close releases encoder resources. It is a no-op for the pure-Go encoder.
type OpusEncoderConfig ¶
type OpusEncoderConfig struct {
SampleRate int
MaxChannels int
Application Application
FrameDuration time.Duration
Bitrate int
}
OpusEncoderConfig configures the compatibility encoder API used by github.com/qrtc/opus-go callers.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
examples
|
|
|
decode
command
Package main is an example of an Opus decoder that save the output PCM to disk
|
Package main is an example of an Opus decoder that save the output PCM to disk |
|
internal
|
|
|
bitdepth
Package bitdepth provides utilities to convert between different audio bitdepths
|
Package bitdepth provides utilities to convert between different audio bitdepths |
|
celt
Package celt implements the MDCT layer of the Opus decoder.
|
Package celt implements the MDCT layer of the Opus decoder. |
|
rangecoding
Package rangecoding provides a Range coder for the Opus bitstream
|
Package rangecoding provides a Range coder for the Opus bitstream |
|
resample/silk
Package silkresample ports the RFC 6716 SILK resampler.
|
Package silkresample ports the RFC 6716 SILK resampler. |
|
silk
Package silk provides a Silk coder
|
Package silk provides a Silk coder |
|
slicetools
Package slicetools provides shared helpers for reusing scratch slices.
|
Package slicetools provides shared helpers for reusing scratch slices. |
|
pkg
|
|
|
oggreader
Package oggreader implements the Ogg media container reader
|
Package oggreader implements the Ogg media container reader |