MCPcopy
hub / github.com/mudler/LocalAI / floatToPCM16LE

Function floatToPCM16LE

backend/go/omnivoice-cpp/audio.go:42–55  ·  view source on GitHub ↗

floatToPCM16LE clamps each sample to [-1,1] and encodes it as little-endian signed 16-bit PCM.

(samples []float32)

Source from the content-addressed store, hash-verified

40// floatToPCM16LE clamps each sample to [-1,1] and encodes it as little-endian
41// signed 16-bit PCM.
42func floatToPCM16LE(samples []float32) []byte {
43 out := make([]byte, len(samples)*2)
44 for i, s := range samples {
45 if s > 1 {
46 s = 1
47 } else if s < -1 {
48 s = -1
49 }
50 v := int16(s * 32767)
51 out[i*2] = byte(v)
52 out[i*2+1] = byte(v >> 8)
53 }
54 return out
55}
56
57// writeWAV24k writes samples as a finalized 24 kHz mono 16-bit WAV at dst.
58func writeWAV24k(dst string, samples []float32) error {

Callers 2

streamCallbackFunction · 0.70

Calls 1

int16Function · 0.85

Tested by

no test coverage detected