opus

package
v0.0.0-...-add2d34 Latest Latest
Warning

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

Go to latest
Published: Nov 14, 2018 License: GPL-3.0 Imports: 3 Imported by: 0

README

Documentation

Overview

Opus gives bindings to libopus https://www.opus-codec.org/

Index

Constants

View Source
const (
	//Auto/default setting
	Auto int = C.OPUS_AUTO
	//Maximum bitrate
	BitrateMax int = C.OPUS_BITRATE_MAX
)

Variables

This section is empty.

Functions

func GetDecoderSize

func GetDecoderSize(channels int) int

GetDecoderSize returns the size of a Decoder

func GetEncoderSize

func GetEncoderSize(channels int) int

GetEncoderSize returns the size of a Encoder

func GetError

func GetError(error int) error

GetError Converts an opus error code into a human readable string.

func GetRepacketizerSize

func GetRepacketizerSize() int

Gets the size of an OpusRepacketizer structure.

func GetVersionString

func GetVersionString() string

GetVersionString Gets the libopus version string.

func PacketGetNbChannels

func PacketGetNbChannels(data []byte) (int, error)

Gets the number of channels from a Opus packet.

func PacketGetNbFrames

func PacketGetNbFrames(data []byte) (int, error)

Gets the number of frames in an Opus packet.

func PacketGetNbSamples

func PacketGetNbSamples(data []byte, sampleRate int) (int, error)

Gets the number of samples of an Opus packet

func PacketGetSamplesPerFrame

func PacketGetSamplesPerFrame(data []byte, sampleRate int) int

Gets the number of samples per frame from an Opus packet

Types

type Application

type Application int
const (
	// Best for most VoIP/videoconference applications where listening quality and intelligibility matter most
	ApplicationVoip Application = C.OPUS_APPLICATION_VOIP
	// Best for broadcast/high-fidelity application where the decoded audio should be as close as possible to the input
	ApplicationAudio Application = C.OPUS_APPLICATION_AUDIO
	// Only use when lowest-achievable latency is what matters most. Voice-optimized modes cannot be used.
	ApplicationRestrictedLowdelay Application = C.OPUS_APPLICATION_RESTRICTED_LOWDELAY
)

type Bandwidth

type Bandwidth int
const (
	BandwidthAutoband Bandwidth = C.OPUS_AUTO
	//4kHz bandpass
	BandwidthNarrowband Bandwidth = C.OPUS_BANDWIDTH_NARROWBAND
	//6kHz bandpass
	BandwidthMediumband Bandwidth = C.OPUS_BANDWIDTH_MEDIUMBAND
	//8kHz bandpass
	BandwidthWideband Bandwidth = C.OPUS_BANDWIDTH_WIDEBAND
	//12kHz bandpass
	BandwidthSuperWideband Bandwidth = C.OPUS_BANDWIDTH_SUPERWIDEBAND
	//20kHz bandpass
	BandwidthFullband Bandwidth = C.OPUS_BANDWIDTH_FULLBAND
)

func PacketGetBandwidth

func PacketGetBandwidth(data []byte) (Bandwidth, error)

Gets the bandwidth of an Opus packet.

type Decoder

type Decoder C.struct_OpusDecoder

Decoder is a opus decoder

func NewDecoder

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

NewDecoder Allocates and initializes an decoder state.

func (*Decoder) Bandwidth

func (e *Decoder) Bandwidth() Bandwidth

Gets the decoder's configured bandpass or the decoder's last bandpass.

func (*Decoder) DecodeFloat32

func (e *Decoder) DecodeFloat32(data []byte, frameSize int, fec bool) ([]float32, error)

Decodes an Opus frame from floating point input.

func (*Decoder) DecodeInt16

func (e *Decoder) DecodeInt16(data []byte, frameSize int, fec bool) ([]int16, error)

Decodes a opus frame from pcm input

func (*Decoder) Gain

func (e *Decoder) Gain() int

Gets the decoder's configured gain adjustment.

func (*Decoder) GetFinalRange

func (e *Decoder) GetFinalRange() uint32

Gets the final state of the codec's entropy coder.

func (*Decoder) GetNbSamples

func (e *Decoder) GetNbSamples(data []byte) (int, error)

Gets the number of samples of an Opus packet.

func (*Decoder) Pitch

func (e *Decoder) Pitch() int

Gets the pitch of the last decoded frame, if available. This can be used for any post-processing algorithm requiring the use of pitch, e.g. time stretching/shortening. If the last frame was not voiced, or if the pitch was not coded in the frame, then zero is returned. This CTL is only implemented for decoder instances.

func (*Decoder) Reset

func (e *Decoder) Reset()

Resets the codec state to be equivalent to a freshly initialized state. This should be called when switching streams in order to prevent the back to back decoding from giving different results from one at a time decoding.

func (*Decoder) SetGain

func (e *Decoder) SetGain(v int)

Configures decoder gain adjustment. Scales the decoded output by a factor specified in Q8 dB units. This has a maximum range of -32768 to 32767 inclusive, and returns OPUS_BAD_ARG otherwise. The default is zero indicating no adjustment. This setting survives decoder reset. gain = pow(10, x/(20.0*256))

type Encoder

type Encoder C.struct_OpusEncoder

Encoder is a opus encoder

func NewEncoder

func NewEncoder(sampleRate int, channels int, application Application) (*Encoder, error)

NewEncoder Allocates and initializes an encoder state.

func (*Encoder) Application

func (e *Encoder) Application() Application

Gets the encoder's configured application.

func (*Encoder) Bandwidth

func (e *Encoder) Bandwidth() Bandwidth

Gets the encoder's configured bandpass or the decoder's last bandpass.

func (*Encoder) Bitrate

func (e *Encoder) Bitrate() int

Gets the encoder's bitrate configuration.

func (*Encoder) Complexity

func (e *Encoder) Complexity() int

Gets the encoder's complexity configuration.

func (*Encoder) DTX

func (e *Encoder) DTX() bool

Gets encoder's configured use of discontinuous transmission.

func (*Encoder) EncodeFloat32

func (e *Encoder) EncodeFloat32(pcm []float32, frameSize int, maxData int) ([]byte, error)

Encodes an Opus frame from floating point input.

func (*Encoder) EncodeInt16

func (e *Encoder) EncodeInt16(pcm []int16, frameSize int, maxData int) ([]byte, error)

Encodes a opus frame from pcm input

func (*Encoder) ForceChannels

func (e *Encoder) ForceChannels() int

Gets the encoder's forced channel configuration.

func (*Encoder) GetFinalRange

func (e *Encoder) GetFinalRange() uint32

Gets the final state of the codec's entropy coder.

func (*Encoder) InbandFEC

func (e *Encoder) InbandFEC() bool

Gets encoder's configured use of inband forward error correction.

func (*Encoder) LSBDepth

func (e *Encoder) LSBDepth() int

Gets the encoder's configured signal depth.

func (*Encoder) LastPacketDuration

func (e *Encoder) LastPacketDuration() int

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

func (*Encoder) Lookahead

func (e *Encoder) Lookahead() int

Gets the total samples of delay added by the entire codec. This can be queried by the encoder and then the provided number of samples can be skipped on from the start of the decoder's output to provide time aligned input and output. From the perspective of a decoding application the real data begins this many samples late. The decoder contribution to this delay is identical for all decoders, but the encoder portion of the delay may vary from implementation to implementation, version to version, or even depend on the encoder's initial configuration. Applications needing delay compensation should call this CTL rather than hard-coding a value.

func (*Encoder) MaxBandwidth

func (e *Encoder) MaxBandwidth() Bandwidth

Gets the encoder's configured maximum allowed bandpass.

func (*Encoder) PacketLossPerc

func (e *Encoder) PacketLossPerc() int

Gets the encoder's configured packet loss percentage.

func (*Encoder) Reset

func (e *Encoder) Reset()

Resets the codec state to be equivalent to a freshly initialized state. This should be called when switching streams in order to prevent the back to back decoding from giving different results from one at a time decoding.

func (*Encoder) SampleRate

func (e *Encoder) SampleRate() int

Gets the sampling rate the encoder or decoder was initialized with. This simply returns the frameSize value passed to NewEncoder()

func (*Encoder) SetApplication

func (e *Encoder) SetApplication(v Application)

Configures the encoder's intended application. The initial value is a mandatory argument to the encoder_create function.

func (*Encoder) SetBandwidth

func (e *Encoder) SetBandwidth(v Bandwidth)

Sets the encoder's bandpass to a specific value. This prevents the encoder from automatically selecting the bandpass based on the available bitrate. If an application knows the bandpass of the input audio it is providing, it should normally use OPUS_SET_MAX_BANDWIDTH instead, which still gives the encoder the freedom to reduce the bandpass when the bitrate becomes too low, for better overall quality.

func (*Encoder) SetBitrate

func (e *Encoder) SetBitrate(v int)

Configures the bitrate in the encoder. Rates from 500 to 512000 bits per second are meaningful, as well as the special values OPUS_AUTO and OPUS_BITRATE_MAX. The value OPUS_BITRATE_MAX can be used to cause the codec to use as much rate as it can, which is useful for controlling the rate by adjusting the output buffer size.

func (*Encoder) SetComplexity

func (e *Encoder) SetComplexity(v int)

Configures the encoder's computational complexity. The supported range is 0-10 inclusive with 10 representing the highest complexity.

func (*Encoder) SetDTX

func (e *Encoder) SetDTX(vb bool)

Configures the encoder's use of discontinuous transmission (DTX).

func (*Encoder) SetForceChannels

func (e *Encoder) SetForceChannels(v int)

Configures mono/stereo forcing in the encoder. This can force the encoder to produce packets encoded as either mono or stereo, regardless of the format of the input audio. This is useful when the caller knows that the input signal is currently a mono source embedded in a stereo stream. Auto is applicable

func (*Encoder) SetInbandFEC

func (e *Encoder) SetInbandFEC(vb bool)

Configures the encoder's use of inband forward error correction (FEC). Note: This is only applicable to the LPC layer

func (*Encoder) SetLSBDepth

func (e *Encoder) SetLSBDepth(v int)

Configures the depth of signal being encoded. This is a hint which helps the encoder identify silence and near-silence.

func (*Encoder) SetMaxBandwidth

func (e *Encoder) SetMaxBandwidth(v Bandwidth)

Configures the maximum bandpass that the encoder will select automatically. Applications should normally use this instead of OPUS_SET_BANDWIDTH (leaving that set to the default, OPUS_AUTO). This allows the application to set an upper bound based on the type of input it is providing, but still gives the encoder the freedom to reduce the bandpass when the bitrate becomes too low, for better overall quality.

func (*Encoder) SetPacketLossPerc

func (e *Encoder) SetPacketLossPerc(v int)

Configures the encoder's expected packet loss percentage. Higher values with trigger progressively more loss resistant behavior in the encoder at the expense of quality at a given bitrate in the lossless case, but greater quality under loss.

func (*Encoder) SetSignal

func (e *Encoder) SetSignal(v Signal)

Configures the type of signal being encoded. This is a hint which helps the encoder's mode selection.

func (*Encoder) SetVBR

func (e *Encoder) SetVBR(v int)

Enables or disables variable bitrate (VBR) in the encoder. The configured bitrate may not be met exactly because frames must be an integer number of bytes in length. Warning: Only the MDCT mode of Opus can provide hard CBR behavior.

func (*Encoder) SetVBRConstraint

func (e *Encoder) SetVBRConstraint(v int)

Enables or disables constrained VBR in the encoder. This setting is ignored when the encoder is in CBR mode. Warning: Only the MDCT mode of Opus currently heeds the constraint. Speech mode ignores it completely, hybrid mode may fail to obey it if the LPC layer uses more bitrate than the constraint would have permitted.

func (*Encoder) Signal

func (e *Encoder) Signal() Signal

Gets the encoder's configured signal type.

func (*Encoder) VBR

func (e *Encoder) VBR() int

Determine if variable bitrate (VBR) is enabled in the encoder.

func (*Encoder) VBRConstraint

func (e *Encoder) VBRConstraint() int

Configures the encoder's intended application. The initial value is a mandatory argument to the encoder_create function.

type Error

type Error int

Error represents a Opus Error

const (
	//No Error
	OK Error = C.OPUS_OK
	//One or more invalid/out of range arguments
	BadArg Error = C.OPUS_BAD_ARG
	// The mode struct passed is invalid
	BufferToSmall Error = C.OPUS_BUFFER_TOO_SMALL
	// An internal error was detected
	InternalError Error = C.OPUS_INTERNAL_ERROR
	// The compressed data passed is corrupted
	InvalidPacket Error = C.OPUS_INVALID_PACKET
	// Invalid/unsupported request number
	Unimplemented Error = C.OPUS_UNIMPLEMENTED
	// An encoder or decoder structure is invalid or already freed
	InvalidState Error = C.OPUS_INVALID_STATE
	// Memory allocation has failed
	AllocFail Error = C.OPUS_ALLOC_FAIL
)

type Repacketizer

type Repacketizer C.struct_OpusRepacketizer

The repacketizer can be used to merge multiple Opus packets into a single packet or alternatively to split Opus packets that have previously been merged.

func NewRepacketizer

func NewRepacketizer() *Repacketizer

Allocates memory and initializes the new repacketizer with opus_repacketizer_init().

func (*Repacketizer) Cat

func (e *Repacketizer) Cat(data []byte) error

Add a packet to the current repacketizer state. This packet must match the configuration of any packets already submitted for repacketization since the last call to opus_repacketizer_init(). This means that it must have the same coding mode, audio bandwidth, frame size, and channel count. This can be checked in advance by examining the top 6 bits of the first byte of the packet, and ensuring they match the top 6 bits of the first byte of any previously submitted packet. The total duration of audio in the repacketizer state also must not exceed 120 ms, the maximum duration of a single packet, after adding this packet. The contents of the current repacketizer state can be extracted into new packets using opus_repacketizer_out() or opus_repacketizer_out_range(). In order to add a packet with a different configuration or to add more audio beyond 120 ms, you must clear the repacketizer state by calling opus_repacketizer_init(). If a packet is too large to add to the current repacketizer state, no part of it is added, even if it contains multiple frames, some of which might fit. If you wish to be able to add parts of such packets, you should first use another repacketizer to split the packet into pieces and add them individually.

func (*Repacketizer) GetNBFrames

func (e *Repacketizer) GetNBFrames() int

Return the total number of frames contained in packet data submitted to the repacketizer state so far via opus_repacketizer_cat() since the last call to opus_repacketizer_init() or opus_repacketizer_create(). This defines the valid range of packets that can be extracted with opus_repacketizer_out_range() or opus_repacketizer_out().

func (*Repacketizer) Out

func (e *Repacketizer) Out(maxlen int) ([]byte, error)

Construct a new packet from data previously submitted to the repacketizer state via opus_repacketizer_cat(). This is a convenience routine that returns all the data submitted so far in a single packet. It is equivalent to calling OutRange(0,GetNBFrames(),maxlen)

func (*Repacketizer) OutRange

func (e *Repacketizer) OutRange(begin, end, maxlen int) ([]byte, error)

Construct a new packet from data previously submitted to the repacketizer state via opus_repacketizer_cat().

type Signal

type Signal int
const (
	//Signal being encoded is voice
	SignalVoice Signal = C.OPUS_SIGNAL_VOICE
	//Signal being encoded is music
	SignalMusic Signal = C.OPUS_SIGNAL_MUSIC
)

Jump to

Keyboard shortcuts

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