PCM Audio Converter (audio/rtp.go)

streamcore-server · internal/audio/rtp.go · go · Apache-2.0
voice, lang:go, tag:pcm

Converts int16 PCM samples to little-endian byte slices for Deepgram STT, with an allocation-free variant for per-frame hot paths. Reuse when streaming audio to speech-to-text services.

// Source: https://github.com/streamcoreai/streamcore-server | License: Apache-2.0 | Copyright You must retain, in the Source form of any Derivative Works
// Provided via justclaw.it.com; keep this notice with the code.
package audio

import (
	"encoding/binary"
	"log"
)

// PCMToLinear16Bytes converts int16 PCM samples to little-endian byte slice
// suitable for Deepgram STT.
func PCMToLinear16Bytes(pcm []int16) []byte {
	return PCMToLinear16BytesInto(nil, pcm)
}

// PCMToLinear16BytesInto is the allocation-free variant: it reuses buf when
// its capacity suffices (allocating only otherwise) and returns the encoded
// slice. Callers on per-frame hot paths keep one buffer and pass it back in
// every call — safe as long as the previous result is no longer referenced,
// which holds for the

... (truncated -- full source via MCP)

See the full source, get the GitHub permalink, and search for more like it.

Get a free API key