Skip to main content

Module audio

Module audio 

Source
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:

  1. f32 to 16-bit PCM. Clamping happens before scaling. A sample of 1.2 scaled 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.
  2. 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::finish patches 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§

WavWriter

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 f32 buffer to 16-bit PCM.
sample_to_i16
Convert one f32 sample in [-1, 1] to signed 16-bit PCM.
samples_for_frames
Samples produced by frames codec 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_samples against a caller-supplied speech level.
wav_header
Build a 44-byte RIFF/WAVE header for sample_count mono 16-bit samples.