Documentation
¶
Overview ¶
Package opus provides a Pure Go implementation of the Opus audio codec. This implementation is based on the official libopus reference implementation and aims for complete compatibility without using CGO.
Index ¶
- Constants
- Variables
- type Application
- type Decoder
- type Encoder
- func (e *Encoder) Application() Application
- func (e *Encoder) Bandwidth() int
- func (e *Encoder) Bitrate() int
- func (e *Encoder) Complexity() int
- func (e *Encoder) DTX() bool
- func (e *Encoder) Encode(pcm []int16, frameSize int) ([]byte, error)
- func (e *Encoder) EncodeFloat(pcm []float64, frameSize int) ([]byte, error)
- func (e *Encoder) Reset() error
- func (e *Encoder) SetApplication(application Application)
- func (e *Encoder) SetBandwidth(bw int) error
- func (e *Encoder) SetBitrate(bitrate int) error
- func (e *Encoder) SetComplexity(complexity int) error
- func (e *Encoder) SetDTX(enabled bool)
- func (e *Encoder) SetMaxBandwidth(bw int) error
- func (e *Encoder) SetPacketPadding(n int)
- func (e *Encoder) SetSignalType(s SignalType)
- func (e *Encoder) SetVBR(vbr bool)
- func (e *Encoder) SetVBRConstraint(constrained bool)
- func (e *Encoder) SignalType() SignalType
- func (e *Encoder) VBR() bool
- type SignalType
Constants ¶
const ( Version = "0.1.0" VersionMajor = 0 VersionMinor = 1 VersionPatch = 0 )
Opus version constants
const ( SampleRate8kHz = 8000 SampleRate12kHz = 12000 SampleRate16kHz = 16000 SampleRate24kHz = 24000 SampleRate48kHz = 48000 )
Sample rates supported by Opus
const ( FrameSize2_5ms = 120 // 2.5ms at 48kHz FrameSize5ms = 240 // 5ms at 48kHz FrameSize10ms = 480 // 10ms at 48kHz FrameSize20ms = 960 // 20ms at 48kHz (most common) FrameSize40ms = 1920 // 40ms at 48kHz FrameSize60ms = 2880 // 60ms at 48kHz )
Frame sizes in samples (at 48kHz)
const ( ApplicationVOIP = 2048 // Voice over IP ApplicationAudio = 2049 // General audio ApplicationRestrictedLowDelay = 2051 // Lowest latency )
Application types
const ( BandwidthAuto = -1000 // automatic selection (default) BandwidthNarrowband = 1101 // 4kHz BandwidthMediumband = 1102 // 6kHz BandwidthWideband = 1103 // 8kHz BandwidthSuperWideband = 1104 // 12kHz BandwidthFullband = 1105 // 20kHz )
Bandwidth types
const ( ChannelsMono = 1 ChannelsStereo = 2 )
Channel modes
const ( ModeSILKOnly = 1000 ModeHybrid = 1001 ModeCELTOnly = 1002 )
Opus modes (internal)
const ( BitrateAuto = -1000 BitrateMax = -1 BitrateMin = 500 // 500 bps BitrateMaxVal = 512000 // 512 kbps )
Bitrate constants
const ( SetBitrateRequest = 4002 GetBitrateRequest = 4003 SetForceChannelsRequest = 4022 GetForceChannelsRequest = 4023 SetMaxBandwidthRequest = 4004 GetMaxBandwidthRequest = 4005 SetBandwidthRequest = 4008 GetBandwidthRequest = 4009 SetComplexityRequest = 4010 GetComplexityRequest = 4011 SetInbandFECRequest = 4012 GetInbandFECRequest = 4013 SetPacketLossPercRequest = 4014 GetPacketLossPercRequest = 4015 SetDTXRequest = 4016 GetDTXRequest = 4017 SetVBRRequest = 4006 GetVBRRequest = 4007 SetVBRConstraintRequest = 4020 GetVBRConstraintRequest = 4021 SetSignalRequest = 4024 GetSignalRequest = 4025 SetApplicationRequest = 4000 GetApplicationRequest = 4001 GetLookaheadRequest = 4027 SetExpertFrameDurationRequest = 4040 GetExpertFrameDurationRequest = 4041 SetPredictionDisabledRequest = 4042 GetPredictionDisabledRequest = 4043 ResetStateRequest = 4028 )
Encoder/Decoder control codes (CTL)
const ( ComplexityMin = 0 ComplexityMax = 10 ComplexityDefault = 9 )
Complexity (0-10)
const ( PacketLossPercMin = 0 PacketLossPercMax = 100 )
Packet loss percentage (0-100)
const ( MaxPacketSize = 1500 // bytes MaxFrameSize = 2880 // samples at 48kHz for 60ms )
Maximum packet size
Variables ¶
var ( // ErrBadArg indicates that one or more arguments are invalid ErrBadArg = errors.New("opus: bad argument") // ErrBufferTooSmall indicates that the provided buffer is too small ErrBufferTooSmall = errors.New("opus: buffer too small") // ErrInternalError indicates an internal error occurred ErrInternalError = errors.New("opus: internal error") // ErrInvalidPacket indicates the packet is invalid or corrupted ErrInvalidPacket = errors.New("opus: invalid packet") // ErrUnimplemented indicates a feature is not yet implemented ErrUnimplemented = errors.New("opus: unimplemented") // ErrInvalidState indicates the encoder/decoder is in an invalid state ErrInvalidState = errors.New("opus: invalid state") // ErrAllocFail indicates memory allocation failed ErrAllocFail = errors.New("opus: allocation failed") // ErrUnsupportedSampleRate indicates the sample rate is not supported ErrUnsupportedSampleRate = errors.New("opus: unsupported sample rate") // ErrUnsupportedChannels indicates the channel count is not supported ErrUnsupportedChannels = errors.New("opus: unsupported number of channels") // ErrUnsupportedFrameSize indicates the frame size is not supported ErrUnsupportedFrameSize = errors.New("opus: unsupported frame size") // ErrUnsupportedBandwidth indicates the bandwidth is not supported ErrUnsupportedBandwidth = errors.New("opus: unsupported bandwidth") )
Common Opus errors
Functions ¶
This section is empty.
Types ¶
type Application ¶
type Application = int
Application specifies the encoding mode (use constants from package)
type Decoder ¶
type Decoder struct {
// contains filtered or unexported fields
}
Decoder represents an Opus decoder instance
func NewDecoder ¶
NewDecoder creates a new Opus decoder
sampleRate must be one of: 8000, 12000, 16000, 24000, 48000 Hz channels must be 1 (mono) or 2 (stereo)
func (*Decoder) Decode ¶
Decode decodes an Opus packet to PCM samples
data is the compressed Opus packet pcm is the output buffer for 16-bit PCM samples Returns the number of samples per channel decoded (clamped to buffer size)
func (*Decoder) DecodeFEC ¶
DecodeFEC decodes forward error correction data This is used for packet loss concealment
func (*Decoder) DecodeFloat ¶
DecodeFloat decodes an Opus packet to floating-point PCM samples
data is the compressed Opus packet Returns float64 samples in range [-1.0, 1.0]
func (*Decoder) GetLastPacketDuration ¶
GetLastPacketDuration returns the duration of the last decoded packet in samples
type Encoder ¶
type Encoder struct {
// contains filtered or unexported fields
}
Encoder represents an Opus encoder instance
func NewEncoder ¶
func NewEncoder(sampleRate, channels int, application Application) (*Encoder, error)
NewEncoder creates a new Opus encoder
sampleRate must be one of: 8000, 12000, 16000, 24000, 48000 Hz channels must be 1 (mono) or 2 (stereo) application specifies the encoding mode
func (*Encoder) Application ¶ added in v1.1.1
func (e *Encoder) Application() Application
Application returns the current application mode.
func (*Encoder) Bandwidth ¶ added in v1.1.0
Bandwidth reports the coded bandwidth the encoder would currently use, as a public Bandwidth* constant.
func (*Encoder) Bitrate ¶ added in v1.1.1
Bitrate returns the current target bitrate in bits per second.
func (*Encoder) Complexity ¶ added in v1.1.1
Complexity returns the current complexity setting (0–10).
func (*Encoder) Encode ¶
Encode encodes PCM audio samples
pcm contains interleaved 16-bit PCM samples (left, right, left, right, ...) frameSize is the number of samples per channel (at the encoder's sample rate) Returns compressed Opus packet
func (*Encoder) EncodeFloat ¶
EncodeFloat encodes floating-point PCM samples
pcm contains interleaved float64 samples in range [-1.0, 1.0] frameSize is the number of samples per channel (at the encoder's sample rate)
func (*Encoder) SetApplication ¶
func (e *Encoder) SetApplication(application Application)
SetApplication changes the application mode. This re-derives the CELT content hint (voice for VOIP, music otherwise), which influences bandwidth selection and transient sensitivity; it does not affect already-emitted packets.
func (*Encoder) SetBandwidth ¶ added in v1.1.0
SetBandwidth forces a specific coded bandwidth, overriding the automatic selection (it is still clamped to the input sample rate's Nyquist limit). Pass BandwidthAuto to return to automatic selection (the default). bw must be BandwidthAuto or one of the public Bandwidth* constants. CELT has no medium-band mode, so BandwidthMediumband is rounded up to BandwidthWideband.
func (*Encoder) SetBitrate ¶
SetBitrate sets the target bitrate in bits per second
func (*Encoder) SetComplexity ¶
SetComplexity sets the computational complexity (0-10) Higher values use more CPU but may provide better quality
func (*Encoder) SetDTX ¶ added in v1.1.0
SetDTX enables or disables discontinuous transmission. When enabled, frames the encoder detects as silent are emitted as minimal packets (a few bytes) instead of being padded to the target size. This reduces bitrate during silence. The decoder reconstructs such frames as digital silence. DTX is off by default. The reduction is effective in any rate mode; in CBR it overrides the fixed-size padding for silent frames only.
func (*Encoder) SetMaxBandwidth ¶ added in v1.1.0
SetMaxBandwidth caps the automatically selected coded bandwidth. bw must be one of the public Bandwidth* constants (Narrowband..Fullband). The encoder never exceeds this cap, nor the input sample rate's Nyquist limit. The default is BandwidthFullband (no extra cap). Has no effect when an explicit bandwidth is forced via SetBandwidth.
func (*Encoder) SetPacketPadding ¶ added in v1.1.0
SetPacketPadding sets the number of code-3 padding-data bytes appended to each emitted packet (RFC 6716 §3.2.5). When n > 0, every packet is encoded as a code-3 packet with the padding flag set and n zero bytes appended at the end; the padding does not affect the decoded audio (the decoder strips it). This is useful for keeping a constant on-the-wire packet size or for obscuring the true payload length. n <= 0 disables padding (the default), restoring the compact code-0/1/2/3 selection.
func (*Encoder) SetSignalType ¶ added in v1.1.1
func (e *Encoder) SetSignalType(s SignalType)
SetSignalType overrides the content hint used by encoder heuristics. SignalAuto (the default) re-derives the hint from the current Application setting (VOIP → voice, otherwise music). Calling this with SignalVoice or SignalMusic pins the hint regardless of the Application value; a subsequent SetApplication call will overwrite it again.
func (*Encoder) SetVBR ¶
SetVBR enables or disables variable bitrate mode. When enabled, this sets constrained VBR (CVBR), which is the libopus default: the encoder produces variable-size packets but keeps the average bitrate close to the target. Use SetVBRConstraint(false) for unconstrained VBR.
func (*Encoder) SetVBRConstraint ¶ added in v1.1.0
SetVBRConstraint controls the VBR constraint. When true (default), CVBR is used; when false, unconstrained VBR is used. Has no effect if VBR is disabled.
func (*Encoder) SignalType ¶ added in v1.1.1
func (e *Encoder) SignalType() SignalType
SignalType reports the current content hint.
type SignalType ¶ added in v1.1.1
type SignalType = celt.SignalType
SignalType is a content hint that lets the encoder tune heuristics for the dominant signal type without changing the bitstream format.
const ( // SignalAuto lets the encoder derive a hint from the Application setting // (VOIP → voice, Audio/RestrictedLowDelay → music). This is the default. SignalAuto SignalType = celt.SignalUnknown // SignalVoice marks speech-leaning content. The encoder uses narrower // bandwidth tiers (matching ApplicationVOIP) and switches to short blocks // more eagerly on plosive onsets. SignalVoice SignalType = celt.SignalVoice // SignalMusic marks music or general audio content, applying wider // bandwidth tiers and standard transient sensitivity. SignalMusic SignalType = celt.SignalMusic )
Directories
¶
| Path | Synopsis |
|---|---|
|
toccheck
command
|
|
|
celt
Package celt implements the CELT (Constrained Energy Lapped Transform) codec.
|
Package celt implements the CELT (Constrained Energy Lapped Transform) codec. |
|
cgoref
Package cgoref is the libopus CGO reference wrapper.
|
Package cgoref is the libopus CGO reference wrapper. |
|
dsp
Package dsp provides digital signal processing utilities for the Opus codec.
|
Package dsp provides digital signal processing utilities for the Opus codec. |
|
entcode
Package entcode provides entropy coding (range coding) for Opus, bit-exact with the range coder in libopus 1.3.1 (celt/entcode.c, celt/entenc.c, celt/entdec.c).
|
Package entcode provides entropy coding (range coding) for Opus, bit-exact with the range coder in libopus 1.3.1 (celt/entcode.c, celt/entenc.c, celt/entdec.c). |
|
resampler
Package resampler provides high-quality sample rate conversion for Opus.
|
Package resampler provides high-quality sample rate conversion for Opus. |
|
silk
Package silk implements the SILK speech codec for Opus.
|
Package silk implements the SILK speech codec for Opus. |
|
testing
Package testing provides verification and comparison tools for Opus library development.
|
Package testing provides verification and comparison tools for Opus library development. |