Documentation
¶
Overview ¶
Package opus wraps the Opus codec for jargo's audio path: it decodes Opus packets to PCM on the way in and encodes PCM to Opus packets on the way out.
The codec runs at 48 kHz, the rate WebRTC negotiates for Opus. Decoding produces 48 kHz PCM, while the encoder takes PCM at whatever rate it was configured for and converts it itself. Encoding produces 20 ms frames, the standard WebRTC packetization.
Both the decoder and the encoder are pure Go, so the package needs no cgo: the decoder handles every Opus mode, and the encoder emits wideband SILK (see encoder.go), which is the mode tuned for speech.
Index ¶
Constants ¶
const ( // SampleRate is the codec sample rate in Hz. Opus runs at 48 kHz. SampleRate = 48000 // FrameDuration is the duration of one encoded packet. FrameDuration = 20 * time.Millisecond // FrameSamples is the number of samples per channel in one 20 ms frame. FrameSamples = SampleRate / 1000 * 20 // 960 )
Variables ¶
This section is empty.
Functions ¶
func FrameBytes ¶
FrameBytes is the size in bytes of one 20 ms S16LE frame for channels.
func FrameBytesAt ¶ added in v0.3.0
FrameBytesAt is the size in bytes of one 20 ms S16LE frame for channels at sampleRate, which is what Encode takes from an Encoder fed at that rate.
Types ¶
type Decoder ¶
type Decoder struct {
// contains filtered or unexported fields
}
Decoder decodes Opus packets into signed 16-bit little-endian PCM.
func NewDecoder ¶
NewDecoder builds a Decoder that outputs channels-channel 48 kHz PCM. It decodes all Opus modes (SILK, CELT and hybrid).
type Encoder ¶
type Encoder struct {
// contains filtered or unexported fields
}
Encoder encodes S16LE PCM into Opus packets using the pure-Go pion SILK encoder. SILK is tuned for speech and needs no cgo. The input, 48 kHz unless the config names another rate, is downmixed to mono and resampled to 16 kHz with the pure-Go resampler, then encoded as wideband SILK.
func NewEncoder ¶
func NewEncoder(cfg EncoderConfig) (*Encoder, error)
NewEncoder builds a SILK Encoder from cfg.
type EncoderConfig ¶ added in v0.1.0
type EncoderConfig struct {
// Channels is the number of channels in the PCM handed to Encode.
Channels int
// SampleRate is the rate in Hz of the PCM handed to Encode; 0 means
// SampleRate (48 kHz). The encoder converts it to the rate the codec runs
// at, so a stream produced at its own rate is encoded without first being
// brought to 48 kHz. It must divide into whole 20 ms frames.
SampleRate int
// Bitrate is the target bitrate in bits per second; 0 uses the codec
// default. Speech is transparent well below the codec's fullband range, so
// raising this past ~32 kbps for a single voice mostly costs bandwidth.
Bitrate int
}
EncoderConfig configures an Encoder.