codec

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Jan 24, 2026 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package codec provides audio codec implementations for telephony.

This package implements G.711 codecs (mu-law and A-law) which are standard audio compression formats used in telephony systems worldwide.

  • mu-law (μ-law): Used in North America and Japan
  • A-law: Used in Europe and most other countries

Both codecs compress 16-bit linear PCM samples to 8-bit companded samples, providing 2:1 compression while maintaining good voice quality.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AlawDecode

func AlawDecode(alaw []byte) []int16

AlawDecode decodes 8-bit A-law samples to 16-bit linear PCM.

func AlawDecodeBytes

func AlawDecodeBytes(alaw []byte) []byte

AlawDecodeBytes decodes A-law to raw PCM bytes (little-endian int16). This is a convenience function for working with byte slices directly.

func AlawEncode

func AlawEncode(pcm []int16) []byte

AlawEncode encodes 16-bit linear PCM samples to 8-bit A-law.

A-law (G.711 A-law) is the standard audio codec for telephony in Europe and most countries outside North America/Japan. It compresses 16-bit PCM to 8-bit samples.

func AlawEncodeBytes

func AlawEncodeBytes(pcm []byte) []byte

AlawEncodeBytes encodes raw PCM bytes (little-endian int16) to A-law. This is a convenience function for working with byte slices directly.

func AlawSilence

func AlawSilence() byte

AlawSilence returns a byte representing silence in A-law encoding. This is useful for generating silence or padding.

func AlawSilenceBuffer

func AlawSilenceBuffer(length int) []byte

AlawSilenceBuffer returns a buffer of silence in A-law encoding.

func AlawToMulaw

func AlawToMulaw(alaw []byte) []byte

AlawToMulaw converts A-law encoded audio to mu-law encoding. This is useful when bridging between European and North American systems.

func BytesToInt16

func BytesToInt16(data []byte, bigEndian bool) []int16

BytesToInt16 converts bytes to int16 samples with the specified endianness.

func CalculateDBFS

func CalculateDBFS(samples []int16) float64

CalculateDBFS calculates the level in dB relative to full scale. 0 dBFS is the maximum possible level, negative values indicate quieter audio.

func CalculateRMS

func CalculateRMS(samples []int16) float64

CalculateRMS calculates the Root Mean Square of PCM samples. This is a measure of audio loudness.

func Float32ToInt16

func Float32ToInt16(samples []float32) []int16

Float32ToInt16 converts float32 samples (range -1.0 to 1.0) to int16.

func Float64ToInt16

func Float64ToInt16(samples []float64) []int16

Float64ToInt16 converts float64 samples (range -1.0 to 1.0) to int16.

func GenerateSilence

func GenerateSilence(numSamples int) []int16

GenerateSilence generates a buffer of PCM silence (zeros).

func GenerateSilenceBytes

func GenerateSilenceBytes(numSamples int) []byte

GenerateSilenceBytes generates a buffer of PCM silence as bytes.

func GenerateSineWave

func GenerateSineWave(frequency float64, sampleRate SampleRate, duration float64, amplitude int16) []int16

GenerateSineWave generates a sine wave tone. Useful for testing audio pipelines.

func Int16ToBytes

func Int16ToBytes(samples []int16, bigEndian bool) []byte

Int16ToBytes converts int16 samples to bytes in the specified endianness.

func Int16ToFloat32

func Int16ToFloat32(samples []int16) []float32

Int16ToFloat32 converts int16 samples to float32 (range -1.0 to 1.0).

func Int16ToFloat64

func Int16ToFloat64(samples []int16) []float64

Int16ToFloat64 converts int16 samples to float64 (range -1.0 to 1.0).

func MonoToStereo

func MonoToStereo(mono []int16) []int16

MonoToStereo converts mono audio to stereo by duplicating samples.

func MulawDecode

func MulawDecode(mulaw []byte) []int16

MulawDecode decodes 8-bit mu-law samples to 16-bit linear PCM.

func MulawDecodeBytes

func MulawDecodeBytes(mulaw []byte) []byte

MulawDecodeBytes decodes mu-law to raw PCM bytes (little-endian int16). This is a convenience function for working with byte slices directly.

func MulawEncode

func MulawEncode(pcm []int16) []byte

MulawEncode encodes 16-bit linear PCM samples to 8-bit mu-law.

mu-law (G.711 μ-law) is the standard audio codec for telephony in North America and Japan. It compresses 16-bit PCM to 8-bit samples.

func MulawEncodeBytes

func MulawEncodeBytes(pcm []byte) []byte

MulawEncodeBytes encodes raw PCM bytes (little-endian int16) to mu-law. This is a convenience function for working with byte slices directly.

func MulawSilence

func MulawSilence() byte

MulawSilence returns a byte representing silence in mu-law encoding. This is useful for generating silence or padding.

func MulawSilenceBuffer

func MulawSilenceBuffer(length int) []byte

MulawSilenceBuffer returns a buffer of silence in mu-law encoding.

func MulawToAlaw

func MulawToAlaw(mulaw []byte) []byte

MulawToAlaw converts mu-law encoded audio to A-law encoding. This is useful when bridging between North American and European systems.

func NormalizePCM

func NormalizePCM(samples []int16) []int16

NormalizePCM normalizes PCM samples to use the full dynamic range. This finds the peak amplitude and scales all samples proportionally.

func Resample

func Resample(samples []int16, fromRate, toRate SampleRate) []int16

Resample performs simple linear interpolation resampling. For production use, consider using a proper resampling library.

func ResampleBytes

func ResampleBytes(data []byte, fromRate, toRate SampleRate) []byte

ResampleBytes resamples raw PCM bytes (little-endian int16).

func StereoToMono

func StereoToMono(stereo []int16) []int16

StereoToMono converts stereo audio to mono by averaging channels.

Types

type PCMFormat

type PCMFormat struct {
	SampleRate SampleRate
	Channels   int  // 1 for mono, 2 for stereo
	BitDepth   int  // 8, 16, 24, or 32
	BigEndian  bool // true for big-endian, false for little-endian
}

PCMFormat describes the format of PCM audio data.

func TelephonyFormat

func TelephonyFormat() PCMFormat

TelephonyFormat returns the standard telephony PCM format. This is 8kHz, mono, 16-bit, little-endian.

type SampleRate

type SampleRate int

SampleRate represents common audio sample rates.

const (
	// SampleRate8kHz is the standard telephony sample rate.
	SampleRate8kHz SampleRate = 8000

	// SampleRate16kHz is common for speech recognition.
	SampleRate16kHz SampleRate = 16000

	// SampleRate22050Hz is a common intermediate rate.
	SampleRate22050Hz SampleRate = 22050

	// SampleRate44100Hz is CD quality audio.
	SampleRate44100Hz SampleRate = 44100

	// SampleRate48kHz is professional audio/video standard.
	SampleRate48kHz SampleRate = 48000
)

Jump to

Keyboard shortcuts

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