Expand description
The audio output tail: decoded PCM to a playable file.
The codec hands back f32 samples in [-1, 1]; everything downstream of that is this module.
It is deliberately small and pure-Rust (AGENTS.md toolchain: no libsndfile, no FFmpeg FFI), and
it owns exactly two conversions that are easy to get subtly wrong:
- f32 to 16-bit PCM. Clamping happens before scaling. A sample of
1.2scaled first and clamped second wraps to a large negative value — an audible click that a duration check and a byte-count check both call success. - The RIFF header’s two length fields. They must describe the bytes actually present. A
header claiming more data than the file holds is corrupt, and players disagree about how to
fail on it, so
WavWriter::finishpatches both from the real sample count. The same rule is what makes a disk-full partial file still playable.
§Format
16-bit signed PCM, mono, 24 kHz — SAMPLE_RATE_HZ, the codec’s native rate (plan §2.7:
24 kHz, 1,920 samples per 80 ms frame). Nothing here resamples: emitting the codec’s own rate
keeps the output bit-faithful to what the model produced, and resampling is a separate,
explicitly-requested operation.
Structs§
Constants§
- BITS_
PER_ SAMPLE - Bits per sample in the emitted WAV.
- CHANNELS
- Channel count. The model is mono; stereo would be a fabrication.
- SAMPLES_
PER_ FRAME - Samples the codec emits per 80 ms frame:
SAMPLE_RATE_HZ / 12.5. - SAMPLE_
RATE_ HZ - The codec’s native output rate.
- WAV_
HEADER_ BYTES - Bytes in a canonical 44-byte RIFF/WAVE header for uncompressed PCM.
Functions§
- encode_
wav - Encode a whole PCM buffer as a WAV file in memory.
- mean_
square_ energy - Mean square energy of a PCM buffer, in
[0, 1]. - pcm_
f32_ to_ i16 - Convert a decoded
f32buffer to 16-bit PCM. - sample_
to_ i16 - Convert one
f32sample in[-1, 1]to signed 16-bit PCM. - samples_
for_ frames - Samples produced by
framescodec frames. - speech_
level - The loudest 10 ms window in
pcm, the reference “speech level” the tail rule compares against. - trailing_
noise_ range_ relative_ to - As
trailing_noise_samples_relative_to, but returning WHERE the noise run sits. - trailing_
noise_ samples - Samples of low-level, high-frequency junk at the very end of an utterance.
- trailing_
noise_ samples_ relative_ to trailing_noise_samplesagainst a caller-supplied speech level.- wav_
header - Build a 44-byte RIFF/WAVE header for
sample_countmono 16-bit samples.