codec

package
v0.11.2 Latest Latest
Warning

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

Go to latest
Published: Jul 15, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

View Source
const RTPBufSize = 1500

RTPBufSize is the maximum RTP packet buffer size.

Variables

This section is empty.

Functions

func Downsample16to8

func Downsample16to8(samples16k []int16) []byte

Downsample16to8 converts 16kHz int16 samples to 8kHz 16-bit LE PCM bytes by taking every other sample.

func Downsample48to8

func Downsample48to8(samples48k []int16) []byte

Downsample48to8 converts 48kHz int16 samples to 8kHz 16-bit LE PCM bytes by taking every 6th sample.

func Upsample8to16

func Upsample8to16(pcm8k []byte) []int16

Upsample8to16 converts 8kHz 16-bit LE PCM bytes to 16kHz int16 samples by duplicating each sample (zero-order hold).

func Upsample8to48

func Upsample8to48(pcm8k []byte) []int16

Upsample8to48 converts 8kHz 16-bit LE PCM bytes to 48kHz int16 samples by duplicating each sample 6 times (zero-order hold).

Types

type CodecType

type CodecType int

CodecType identifies a supported audio codec.

const (
	CodecUnknown CodecType = iota
	CodecPCMU              // PT=0, 8kHz
	CodecPCMA              // PT=8, 8kHz
	CodecG722              // PT=9, 16kHz internal / 8kHz SDP clock (RFC 3551)
	CodecOpus              // PT=111 (dynamic), 48kHz
	CodecAMRWB             // PT=96 (dynamic), 16kHz (G.722.2, RFC 4867)
	CodecAMRNB             // PT=97 (dynamic), 8kHz (AMR narrowband, RFC 4867)
)

func CodecTypeFromName

func CodecTypeFromName(name string) CodecType

CodecTypeFromName looks up a CodecType by name (case-insensitive).

func CodecTypeFromPT

func CodecTypeFromPT(pt uint8) CodecType

CodecTypeFromPT looks up a CodecType by RTP payload type number.

func (CodecType) ClockRate

func (c CodecType) ClockRate() int

ClockRate returns the SDP clock rate. Per RFC 3551, G.722 uses 8000 in SDP despite encoding at 16kHz.

func (CodecType) PayloadType

func (c CodecType) PayloadType() uint8

PayloadType returns the RTP payload type number for this codec.

func (CodecType) SampleRate

func (c CodecType) SampleRate() int

SampleRate returns the actual internal sample rate.

func (CodecType) String

func (c CodecType) String() string

type Decoder

type Decoder interface {
	Decode(data []byte) ([]int16, error)
	Reset()
}

Decoder decodes compressed codec data to PCM samples.

func NewAMRNBDecoder added in v0.9.0

func NewAMRNBDecoder(octetAligned bool) Decoder

NewAMRNBDecoder creates an AMR-NB decoder for the given payload format.

func NewAMRWBDecoder added in v0.9.0

func NewAMRWBDecoder(octetAligned bool) Decoder

NewAMRWBDecoder creates an AMR-WB decoder for the given payload format.

func NewDecoder

func NewDecoder(ct CodecType) (Decoder, error)

NewDecoder creates a Decoder for the given codec type.

type Encoder

type Encoder interface {
	Encode(samples []int16) ([]byte, error)
	Reset()
}

Encoder encodes PCM samples to compressed codec data.

func NewAMRNBEncoder added in v0.9.0

func NewAMRNBEncoder(mode int, octetAligned bool) (Encoder, error)

NewAMRNBEncoder creates an AMR-NB encoder for the given speech mode (0..7) and payload format (octetAligned=false selects bandwidth-efficient framing).

func NewAMRWBEncoder added in v0.9.0

func NewAMRWBEncoder(mode int, octetAligned bool) (Encoder, error)

NewAMRWBEncoder creates an AMR-WB encoder for the given speech mode (0..8) and payload format (octetAligned=false selects bandwidth-efficient framing).

func NewEncoder

func NewEncoder(ct CodecType) (Encoder, error)

NewEncoder creates an Encoder for the given codec type.

type G722Decoder

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

G722Decoder decodes G.722 encoded data to 16kHz 16-bit PCM samples.

func NewG722Decoder

func NewG722Decoder() *G722Decoder

NewG722Decoder creates a new G.722 decoder with initial state.

func (*G722Decoder) Decode

func (d *G722Decoder) Decode(data []byte) ([]int16, error)

Decode decodes G.722 data to 16kHz PCM samples. Each input byte produces 2 output samples.

func (*G722Decoder) Reset

func (d *G722Decoder) Reset()

Reset resets the decoder to its initial state.

type G722DecoderReader

type G722DecoderReader struct {
	Source io.Reader
	// contains filtered or unexported fields
}

G722DecoderReader reads G.722 encoded data from Source and produces 8kHz 16-bit LE PCM. Internally decodes to 16kHz then downsamples.

func NewG722DecoderReader

func NewG722DecoderReader(source io.Reader) *G722DecoderReader

NewG722DecoderReader creates a reader that decodes G.722 to 8kHz PCM.

func (*G722DecoderReader) Read

func (d *G722DecoderReader) Read(b []byte) (int, error)

type G722Encoder

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

G722Encoder encodes 16kHz 16-bit PCM samples to G.722.

func NewG722Encoder

func NewG722Encoder() *G722Encoder

NewG722Encoder creates a new G.722 encoder with initial state.

func (*G722Encoder) Encode

func (e *G722Encoder) Encode(samples []int16) ([]byte, error)

Encode encodes 16kHz PCM samples to G.722 data. Input samples must be in pairs (2 samples per encoded byte).

func (*G722Encoder) Reset

func (e *G722Encoder) Reset()

Reset resets the encoder to its initial state.

type G722EncoderWriter

type G722EncoderWriter struct {
	Writer io.Writer
	// contains filtered or unexported fields
}

G722EncoderWriter accepts 8kHz 16-bit LE PCM and writes G.722 encoded data.

func NewG722EncoderWriter

func NewG722EncoderWriter(writer io.Writer) *G722EncoderWriter

NewG722EncoderWriter creates a writer that encodes 8kHz PCM to G.722.

func (*G722EncoderWriter) Write

func (e *G722EncoderWriter) Write(pcm []byte) (int, error)

type OpusDecoder

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

OpusDecoder wraps a gopus Decoder for 48kHz mono.

func NewOpusDecoder

func NewOpusDecoder() (*OpusDecoder, error)

NewOpusDecoder creates a new Opus decoder configured for 48kHz mono.

func (*OpusDecoder) Decode

func (d *OpusDecoder) Decode(data []byte) (out []int16, err error)

Decode decodes an Opus packet to 48kHz int16 PCM samples. Mirrors Encode's panic-to-error conversion.

func (*OpusDecoder) Reset

func (d *OpusDecoder) Reset()

Reset resets the decoder state.

type OpusEncoder

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

OpusEncoder wraps a gopus Encoder for 48kHz mono VoIP.

func NewOpusEncoder

func NewOpusEncoder() (*OpusEncoder, error)

NewOpusEncoder creates a new Opus encoder configured for 48kHz mono VoIP.

func (*OpusEncoder) Encode

func (e *OpusEncoder) Encode(samples []int16) (out []byte, err error)

Encode encodes 48kHz int16 PCM samples to an Opus packet. Panics in the underlying codec are converted to errors so one bad frame fails just the leg, not the process.

func (*OpusEncoder) Reset

func (e *OpusEncoder) Reset()

Reset resets the encoder state.

func (*OpusEncoder) SetBitrate added in v0.6.0

func (e *OpusEncoder) SetBitrate(bps int) error

SetBitrate adjusts the target bitrate in bits per second (gopus accepts 6_000..510_000). Used by transports that want a tighter rate than the library default.

type PCMADecoder

type PCMADecoder struct{}

PCMADecoder decodes G.711 A-law to 16-bit linear PCM. Stateless — one sample per byte.

func (*PCMADecoder) Decode

func (d *PCMADecoder) Decode(data []byte) ([]int16, error)

func (*PCMADecoder) Reset

func (d *PCMADecoder) Reset()

type PCMAEncoder

type PCMAEncoder struct{}

PCMAEncoder encodes 16-bit linear PCM to G.711 A-law. Stateless — one byte per sample.

func (*PCMAEncoder) Encode

func (e *PCMAEncoder) Encode(samples []int16) ([]byte, error)

func (*PCMAEncoder) Reset

func (e *PCMAEncoder) Reset()

type PCMUDecoder

type PCMUDecoder struct{}

PCMUDecoder decodes G.711 mu-law to 16-bit linear PCM. Stateless — one sample per byte.

func (*PCMUDecoder) Decode

func (d *PCMUDecoder) Decode(data []byte) ([]int16, error)

func (*PCMUDecoder) Reset

func (d *PCMUDecoder) Reset()

type PCMUEncoder

type PCMUEncoder struct{}

PCMUEncoder encodes 16-bit linear PCM to G.711 mu-law. Stateless — one byte per sample.

func (*PCMUEncoder) Encode

func (e *PCMUEncoder) Encode(samples []int16) ([]byte, error)

func (*PCMUEncoder) Reset

func (e *PCMUEncoder) Reset()

Directories

Path Synopsis
Package t140 implements ITU-T T.140 real-time text packetization with the RFC 4103 RTP payload format and optional RFC 2198 redundancy (text/red).
Package t140 implements ITU-T T.140 real-time text packetization with the RFC 4103 RTP payload format and optional RFC 2198 redundancy (text/red).

Jump to

Keyboard shortcuts

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