opus

package
v0.8.1 Latest Latest
Warning

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

Go to latest
Published: Oct 29, 2025 License: MIT, BSD-3-Clause Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ErrOK             = Error(C.OPUS_OK)
	ErrBadArg         = Error(C.OPUS_BAD_ARG)
	ErrBufferTooSmall = Error(C.OPUS_BUFFER_TOO_SMALL)
	ErrInternalError  = Error(C.OPUS_INTERNAL_ERROR)
	ErrInvalidPacket  = Error(C.OPUS_INVALID_PACKET)
	ErrUnimplemented  = Error(C.OPUS_UNIMPLEMENTED)
	ErrInvalidState   = Error(C.OPUS_INVALID_STATE)
	ErrAllocFail      = Error(C.OPUS_ALLOC_FAIL)
)

Libopus errors

Variables

This section is empty.

Functions

This section is empty.

Types

type Decoder

type Decoder struct {
	// contains filtered or unexported fields
}

func NewDecoder

func NewDecoder(sample_rate int, channels int) (*Decoder, error)

NewDecoder allocates a new Opus decoder and initializes it with the appropriate parameters. All related memory is managed by the Go GC.

func (*Decoder) Decode

func (dec *Decoder) Decode(data []byte, pcm []int16) (int, error)

Decode encoded Opus data into the supplied buffer. On success, returns the number of samples correctly written to the target buffer.

func (*Decoder) DecodeFEC

func (dec *Decoder) DecodeFEC(data []byte, pcm []int16) error

DecodeFEC encoded Opus data into the supplied buffer with forward error correction.

It is to be used on the packet directly following the lost one. The supplied buffer needs to be exactly the duration of audio that is missing

When a packet is considered "lost", DecodeFEC can be called on the next packet in order to try and recover some of the lost data. The PCM needs to be exactly the duration of audio that is missing. `LastPacketDuration()` can be used on the decoder to get the length of the last packet. Note also that in order to use this feature the encoder needs to be configured with SetInBandFEC(true) and SetPacketLossPerc(x) options.

Note that DecodeFEC automatically falls back to PLC when no FEC data is available in the provided packet.

func (*Decoder) DecodeFECFloat32

func (dec *Decoder) DecodeFECFloat32(data []byte, pcm []float32) error

DecodeFECFloat32 encoded Opus data into the supplied buffer with forward error correction. It is to be used on the packet directly following the lost one. The supplied buffer needs to be exactly the duration of audio that is missing

func (*Decoder) DecodeFloat32

func (dec *Decoder) DecodeFloat32(data []byte, pcm []float32) (int, error)

Decode encoded Opus data into the supplied buffer. On success, returns the number of samples correctly written to the target buffer.

func (*Decoder) DecodePLC

func (dec *Decoder) DecodePLC(pcm []int16) error

DecodePLC recovers a lost packet using Opus Packet Loss Concealment feature.

The supplied buffer needs to be exactly the duration of audio that is missing. When a packet is considered "lost", `DecodePLC` and `DecodePLCFloat32` methods can be called in order to obtain something better sounding than just silence. The PCM needs to be exactly the duration of audio that is missing. `LastPacketDuration()` can be used on the decoder to get the length of the last packet.

This option does not require any additional encoder options. Unlike FEC, PLC does not introduce additional latency. It is calculated from the previous packet, not from the next one.

func (*Decoder) DecodePLCFloat32

func (dec *Decoder) DecodePLCFloat32(pcm []float32) error

DecodePLCFloat32 recovers a lost packet using Opus Packet Loss Concealment feature. The supplied buffer needs to be exactly the duration of audio that is missing.

func (*Decoder) Init

func (dec *Decoder) Init(sample_rate int, channels int) error

func (*Decoder) LastPacketDuration

func (dec *Decoder) LastPacketDuration() (int, error)

LastPacketDuration gets the duration (in samples) of the last packet successfully decoded or concealed.

type Error

type Error int

func (Error) Error

func (e Error) Error() string

Error string (in human readable format) for libopus errors.

type Latency

type Latency time.Duration

Latency is a type of OPUS codec frame duration.

const (
	Latency2500us Latency = Latency(2500 * time.Microsecond)
	Latency5ms    Latency = Latency(5 * time.Millisecond)
	Latency10ms   Latency = Latency(10 * time.Millisecond)
	Latency20ms   Latency = Latency(20 * time.Millisecond)
	Latency40ms   Latency = Latency(40 * time.Millisecond)
	Latency60ms   Latency = Latency(60 * time.Millisecond)
)

Latency values available in OPUS codec.

func (Latency) Duration

func (l Latency) Duration() time.Duration

Duration returns latency in time.Duration.

func (Latency) Validate

func (l Latency) Validate() bool

Validate that the Latency is allowed in OPUS.

type Params

type Params struct {
	codec.BaseParams
	// ChannelMixer is a mixer to be used if number of given and expected channels differ.
	ChannelMixer mixer.ChannelMixer

	// Expected latency of the codec.
	Latency Latency
}

Params stores opus specific encoding parameters.

func NewParams

func NewParams() (Params, error)

NewParams returns default opus codec specific parameters.

func (*Params) BuildAudioEncoder

func (p *Params) BuildAudioEncoder(r audio.Reader, property prop.Media) (codec.ReadCloser, error)

BuildAudioEncoder builds opus encoder with given params

func (*Params) RTPCodec

func (p *Params) RTPCodec() *codec.RTPCodec

RTPCodec represents the codec metadata

Jump to

Keyboard shortcuts

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