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 ¶
- func AlawDecode(alaw []byte) []int16
- func AlawDecodeBytes(alaw []byte) []byte
- func AlawEncode(pcm []int16) []byte
- func AlawEncodeBytes(pcm []byte) []byte
- func AlawSilence() byte
- func AlawSilenceBuffer(length int) []byte
- func AlawToMulaw(alaw []byte) []byte
- func BytesToInt16(data []byte, bigEndian bool) []int16
- func CalculateDBFS(samples []int16) float64
- func CalculateRMS(samples []int16) float64
- func Float32ToInt16(samples []float32) []int16
- func Float64ToInt16(samples []float64) []int16
- func GenerateSilence(numSamples int) []int16
- func GenerateSilenceBytes(numSamples int) []byte
- func GenerateSineWave(frequency float64, sampleRate SampleRate, duration float64, amplitude int16) []int16
- func Int16ToBytes(samples []int16, bigEndian bool) []byte
- func Int16ToFloat32(samples []int16) []float32
- func Int16ToFloat64(samples []int16) []float64
- func MonoToStereo(mono []int16) []int16
- func MulawDecode(mulaw []byte) []int16
- func MulawDecodeBytes(mulaw []byte) []byte
- func MulawEncode(pcm []int16) []byte
- func MulawEncodeBytes(pcm []byte) []byte
- func MulawSilence() byte
- func MulawSilenceBuffer(length int) []byte
- func MulawToAlaw(mulaw []byte) []byte
- func NormalizePCM(samples []int16) []int16
- func Resample(samples []int16, fromRate, toRate SampleRate) []int16
- func ResampleBytes(data []byte, fromRate, toRate SampleRate) []byte
- func StereoToMono(stereo []int16) []int16
- type PCMFormat
- type SampleRate
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AlawDecode ¶
AlawDecode decodes 8-bit A-law samples to 16-bit linear PCM.
func AlawDecodeBytes ¶
AlawDecodeBytes decodes A-law to raw PCM bytes (little-endian int16). This is a convenience function for working with byte slices directly.
func AlawEncode ¶
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 ¶
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 ¶
AlawSilenceBuffer returns a buffer of silence in A-law encoding.
func AlawToMulaw ¶
AlawToMulaw converts A-law encoded audio to mu-law encoding. This is useful when bridging between European and North American systems.
func BytesToInt16 ¶
BytesToInt16 converts bytes to int16 samples with the specified endianness.
func CalculateDBFS ¶
CalculateDBFS calculates the level in dB relative to full scale. 0 dBFS is the maximum possible level, negative values indicate quieter audio.
func CalculateRMS ¶
CalculateRMS calculates the Root Mean Square of PCM samples. This is a measure of audio loudness.
func Float32ToInt16 ¶
Float32ToInt16 converts float32 samples (range -1.0 to 1.0) to int16.
func Float64ToInt16 ¶
Float64ToInt16 converts float64 samples (range -1.0 to 1.0) to int16.
func GenerateSilence ¶
GenerateSilence generates a buffer of PCM silence (zeros).
func GenerateSilenceBytes ¶
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 ¶
Int16ToBytes converts int16 samples to bytes in the specified endianness.
func Int16ToFloat32 ¶
Int16ToFloat32 converts int16 samples to float32 (range -1.0 to 1.0).
func Int16ToFloat64 ¶
Int16ToFloat64 converts int16 samples to float64 (range -1.0 to 1.0).
func MonoToStereo ¶
MonoToStereo converts mono audio to stereo by duplicating samples.
func MulawDecode ¶
MulawDecode decodes 8-bit mu-law samples to 16-bit linear PCM.
func MulawDecodeBytes ¶
MulawDecodeBytes decodes mu-law to raw PCM bytes (little-endian int16). This is a convenience function for working with byte slices directly.
func MulawEncode ¶
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 ¶
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 ¶
MulawSilenceBuffer returns a buffer of silence in mu-law encoding.
func MulawToAlaw ¶
MulawToAlaw converts mu-law encoded audio to A-law encoding. This is useful when bridging between North American and European systems.
func NormalizePCM ¶
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 ¶
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 )