Skip to main content

Module codec

Module codec 

Source
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::Input consumes a fixed number of input frames per process call and produces a variable number of output frames.
  • FixedAsync::Output produces 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:

  1. Append the incoming samples to in_buf.
  2. While in_buf holds at least one full BLOCK of samples, feed exactly BLOCK frames through the resampler (re-using pre-allocated scratch I/O buffers) and append the result to the output.
  3. Keep the < BLOCK remainder in in_buf for 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.