Expand description
Audio codec helpers: G.711 (μ-law / A-law) ↔ PCM16 and sample-rate conversion.
Telephony is G.711 μ-law @ 8 kHz; Gemini Live wants 16 kHz PCM in and emits
24 kHz PCM out (see DESIGN.md “Audio path”). G.711 via audio-codec-algorithms,
resampling via rubato.
§Streaming resampler design
Telephony delivers audio in small, frequently variable-size chunks (≈20 ms:
160 samples @ 8 kHz, 320 @ 16 kHz, 480 @ 24 kHz — and gateways may coalesce or
split frames). rubato’s async sinc resampler (rubato::Async) runs in one
of two fixed modes:
FixedAsync::Inputconsumes a fixed number of input frames perprocesscall and produces a variable number of output frames.FixedAsync::Outputproduces a fixed number of output frames and pulls a variable number of input frames.
A real-time media path naturally has a fixed input cadence (whatever the
carrier hands us) and an unconstrained output, so FixedAsync::Input is the
right base. But it still requires exactly chunk_size samples on every
call, which an arbitrary 20 ms frame will not satisfy. We therefore decouple
the carrier’s chunk size from the resampler’s block size with a small internal
carry buffer:
- Append the incoming samples to
in_buf. - While
in_bufholds at least one fullBLOCKof samples, feed exactlyBLOCKframes through the resampler (re-using pre-allocated scratch I/O buffers) and append the result to the output. - Keep the
< BLOCKremainder inin_buffor the next call.
This yields continuous, gap-free resampling across an arbitrary stream of
variable chunks: every input sample is consumed exactly once and in order, the
resampler’s internal filter state is preserved across calls (so block
boundaries are seamless), and the resampler plus its scratch buffers are
allocated once in Resampler::new — there is no per-call reallocation.
A pass-through (from_rate == to_rate) skips rubato entirely.
Structs§
- Resampler
- A mono sample-rate converter (e.g. 8k→16k, 24k→8k) backed by
rubato.
Functions§
- alaw_
to_ pcm16 - Decode a G.711 A-law byte stream into 16-bit PCM samples.
- pcm16_
to_ alaw - Encode 16-bit PCM samples into a G.711 A-law byte stream.
- pcm16_
to_ ulaw - Encode 16-bit PCM samples into a G.711 μ-law byte stream.
- ulaw_
to_ pcm16 - Decode a G.711 μ-law byte stream into 16-bit PCM samples.