Documentation
¶
Index ¶
- Constants
- func Version() string
- type Application
- type Bandwidth
- type Decoder
- func (dec *Decoder) Decode(data []byte, pcm []int16) (int, error)
- func (dec *Decoder) DecodeFEC(data []byte, pcm []int16) error
- func (dec *Decoder) DecodeFECFloat32(data []byte, pcm []float32) error
- func (dec *Decoder) DecodeFloat32(data []byte, pcm []float32) (int, error)
- func (dec *Decoder) DecodePLC(pcm []int16) error
- func (dec *Decoder) DecodePLCFloat32(pcm []float32) error
- func (dec *Decoder) Init(sample_rate int, channels int) error
- func (dec *Decoder) LastPacketDuration() (int, error)
- type Encoder
- func (enc *Encoder) Bitrate() (int, error)
- func (enc *Encoder) Complexity() (int, error)
- func (enc *Encoder) DTX() (bool, error)
- func (enc *Encoder) Encode(pcm []int16, data []byte) (int, error)
- func (enc *Encoder) EncodeFloat32(pcm []float32, data []byte) (int, error)
- func (enc *Encoder) InBandFEC() (bool, error)
- func (enc *Encoder) InDTX() (bool, error)
- func (enc *Encoder) Init(sample_rate int, channels int, application Application) error
- func (enc *Encoder) MaxBandwidth() (Bandwidth, error)
- func (enc *Encoder) PacketLossPerc() (int, error)
- func (enc *Encoder) Reset() error
- func (enc *Encoder) SampleRate() (int, error)
- func (enc *Encoder) SetBitrate(bitrate int) error
- func (enc *Encoder) SetBitrateToAuto() error
- func (enc *Encoder) SetBitrateToMax() error
- func (enc *Encoder) SetComplexity(complexity int) error
- func (enc *Encoder) SetDTX(dtx bool) error
- func (enc *Encoder) SetInBandFEC(fec bool) error
- func (enc *Encoder) SetMaxBandwidth(maxBw Bandwidth) error
- func (enc *Encoder) SetPacketLossPerc(lossPerc int) error
- type Error
- type Stream
- type StreamError
Constants ¶
const ( // 4 kHz passband Narrowband = Bandwidth(C.OPUS_BANDWIDTH_NARROWBAND) // 6 kHz passband Mediumband = Bandwidth(C.OPUS_BANDWIDTH_MEDIUMBAND) // 8 kHz passband Wideband = Bandwidth(C.OPUS_BANDWIDTH_WIDEBAND) // 12 kHz passband SuperWideband = Bandwidth(C.OPUS_BANDWIDTH_SUPERWIDEBAND) // 20 kHz passband Fullband = Bandwidth(C.OPUS_BANDWIDTH_FULLBAND) )
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
const ( // Optimize encoding for VoIP AppVoIP = Application(C.OPUS_APPLICATION_VOIP) // Optimize encoding for non-voice signals like music AppAudio = Application(C.OPUS_APPLICATION_AUDIO) // Optimize encoding for low latency applications AppRestrictedLowdelay = Application(C.OPUS_APPLICATION_RESTRICTED_LOWDELAY) )
const ( ErrStreamFalse = StreamError(C.OP_FALSE) ErrStreamEOF = StreamError(C.OP_EOF) ErrStreamHole = StreamError(C.OP_HOLE) ErrStreamRead = StreamError(C.OP_EREAD) ErrStreamFault = StreamError(C.OP_EFAULT) ErrStreamImpl = StreamError(C.OP_EIMPL) ErrStreamInval = StreamError(C.OP_EINVAL) ErrStreamNotFormat = StreamError(C.OP_ENOTFORMAT) ErrStreamBadHeader = StreamError(C.OP_EBADHEADER) ErrStreamVersion = StreamError(C.OP_EVERSION) ErrStreamNotAudio = StreamError(C.OP_ENOTAUDIO) ErrStreamBadPacked = StreamError(C.OP_EBADPACKET) ErrStreamBadLink = StreamError(C.OP_EBADLINK) ErrStreamNoSeek = StreamError(C.OP_ENOSEEK) ErrStreamBadTimestamp = StreamError(C.OP_EBADTIMESTAMP) )
Libopusfile errors. The names are copied verbatim from the libopusfile library.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Decoder ¶
type Decoder struct {
// contains filtered or unexported fields
}
func NewDecoder ¶
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 ¶
Decode encoded Opus data into the supplied buffer. On success, returns the number of samples correctly written to the target buffer.
func (*Decoder) DecodeFEC ¶
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 ¶
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 ¶
Decode encoded Opus data into the supplied buffer. On success, returns the number of samples correctly written to the target buffer.
func (*Decoder) DecodePLC ¶
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 ¶
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.
type Encoder ¶
type Encoder struct {
// contains filtered or unexported fields
}
Encoder contains the state of an Opus encoder for libopus.
func NewEncoder ¶
func NewEncoder(sample_rate int, channels int, application Application) (*Encoder, error)
NewEncoder allocates a new Opus encoder and initializes it with the appropriate parameters. All related memory is managed by the Go GC.
func (*Encoder) Bitrate ¶
Bitrate returns the bitrate of the Encoder
func (*Encoder) Complexity ¶
Complexity returns the computational complexity used by the encoder
func (*Encoder) DTX ¶
DTX reports whether this encoder is configured to use discontinuous transmission (DTX).
func (*Encoder) Encode ¶
Encode raw PCM data and store the result in the supplied buffer. On success, returns the number of bytes used up by the encoded data.
func (*Encoder) EncodeFloat32 ¶
Encode raw PCM data and store the result in the supplied buffer. On success, returns the number of bytes used up by the encoded data.
func (*Encoder) InBandFEC ¶
InBandFEC gets the encoder's configured inband forward error correction (FEC)
func (*Encoder) InDTX ¶
InDTX returns whether the last encoded frame was either a comfort noise update during DTX or not encoded because of DTX.
func (*Encoder) Init ¶
func (enc *Encoder) Init(sample_rate int, channels int, application Application) error
Init initializes a pre-allocated opus encoder. Unless the encoder has been created using NewEncoder, this method must be called exactly once in the life-time of this object, before calling any other methods.
func (*Encoder) MaxBandwidth ¶
MaxBandwidth gets the encoder's configured maximum allowed bandpass.
func (*Encoder) PacketLossPerc ¶
PacketLossPerc gets the encoder's configured packet loss percentage.
func (*Encoder) Reset ¶
Reset resets the codec state to be equivalent to a freshly initialized state.
func (*Encoder) SampleRate ¶
SampleRate returns the encoder sample rate in Hz.
func (*Encoder) SetBitrate ¶
SetBitrate sets the bitrate of the Encoder
func (*Encoder) SetBitrateToAuto ¶
SetBitrateToAuto will allow the encoder to automatically set the bitrate
func (*Encoder) SetBitrateToMax ¶
SetBitrateToMax causes the encoder to use as much rate as it can. This can be useful for controlling the rate by adjusting the output buffer size.
func (*Encoder) SetComplexity ¶
SetComplexity sets the encoder's computational complexity
func (*Encoder) SetDTX ¶
SetDTX configures the encoder's use of discontinuous transmission (DTX).
func (*Encoder) SetInBandFEC ¶
SetInBandFEC configures the encoder's use of inband forward error correction (FEC)
func (*Encoder) SetMaxBandwidth ¶
SetMaxBandwidth configures the maximum bandpass that the encoder will select automatically
type Stream ¶
type Stream struct {
// contains filtered or unexported fields
}
Stream wraps a io.Reader in a decoding layer. It provides an API similar to io.Reader, but it provides raw PCM data instead of the encoded Opus data.
This is not the same as directly decoding the bytes on the io.Reader; opus streams are Ogg Opus audio streams, which package raw Opus data.
This wraps libopusfile. For more information, see the api docs on xiph.org:
https://www.opus-codec.org/docs/opusfile_api-0.7/index.html
func NewStream ¶
NewStream creates and initializes a new stream. Don't call .Init() on this.
func (*Stream) Init ¶
Init initializes a stream with an io.Reader to fetch opus encoded data from on demand. Errors from the reader are all transformed to an EOF, any actual error information is lost. The same happens when a read returns succesfully, but with zero bytes.
func (*Stream) Read ¶
Read a chunk of raw opus data from the stream and decode it. Returns the number of decoded samples per channel. This means that a dual channel (stereo) feed will have twice as many samples as the value returned.
Read may successfully read less bytes than requested, but it will never read exactly zero bytes succesfully if a non-zero buffer is supplied.
The number of channels in the output data must be known in advance. It is possible to extract this information from the stream itself, but I'm not motivated to do that. Feel free to send a pull request.
type StreamError ¶
type StreamError int
StreamError represents an error from libopusfile.
func (StreamError) Error ¶
func (i StreamError) Error() string