Skip to content

feat: send /live voice audio as binary WebSocket frames - #37

Merged
Alorse merged 7 commits into
mainfrom
feat/binary-audio-frames
Sep 29, 2026
Merged

Alorse merged 7 commits into
mainfrom
feat/binary-audio-frames

Conversation

@Alorse

@Alorse Alorse commented Sep 29, 2026

Copy link
Copy Markdown
Owner

Voice audio on /live now travels as raw binary WebSocket frames in both directions, instead of being turned into base64 text inside JSON. Control messages (start, state, transcripts, reviews, errors) are still JSON. The same conversation now sends about a quarter less data each way.

What changed

  • Browser: the mic's PCM16 chunks are sent as binary frames, and the tutor's audio is played straight from the binary frames it arrives in. The browser no longer does any base64 work. The app says binary: true in its start.
  • Server: the codec is parseClientFrame and encodeServerMessage in server/protocol.ts, and every /live message goes through it. The Gemini upstream still gets base64 in JSON, so the server converts once at the browser-facing edge.
  • Rollout compatibility: a stale cached app (the v12 shell) doesn't send binary: true. The server still takes its JSON/base64 mic frames and answers it in JSON/base64. I checked this in a real browser by running the old public/ against the new server (below).
  • Cache version: bumped to 13 (?v=13, parley-v13, sw.js?v=13).
  • Scripts and docs: scripts/e2e-live.mjs uses the binary protocol. scripts/perf-server.mjs streams binary by default (--audio json for an old client) and now reports bytes per second in both directions. docs/DEVELOPING.md describes the protocol.
  • Browser repro: new audio-wire scenario (/live KB/s each way). Every scenario now checks for playback overlaps and underruns.

Tests

  • 192 → 210 tests. npm test and /usr/bin/node (v20) both pass: 209 pass, 1 todo. The todo is the unrelated ACCESS_TOKENS test.
  • Protocol codec tests cover both formats in both directions, plus the size claim.
  • Harness tests:
  • New test/live-client.test.js, plus new capture and player tests for the ArrayBuffer path.
  • Also fixed a flaky wait in the Close tutor sessions nobody is listening to #20 idle test: it matched the greeting's empty transcript instead of the learner's words.

Measurements (before → after)

before after
perf-server, 1 session: mic payload, client → server 41.4 KB/s (1394 B per 1024 B frame) 30.0 KB/s (1024 B)
perf-server, 1 session: tutor audio, server → client 28.2 KB/s 21.2 KB/s
perf-server, 12 sessions: server CPU 14.3 / 15.0 % 15.0 / 14.0 % (no change, within noise)
repro audio-wire: up / down 19.7 / 28.3 KB/s 14.6 / 21.2 KB/s
repro: mic frame rate (double-tap / server-restart) 31.5 / 31.5 per s 30.5–32 / 32 per s
repro: playback overlaps / underruns (audio-wire, echo-window, echo-bluetooth) 0 / 0 0 / 0
stale v12 client on the new server (audio-wire) — 4 turns, 18.8 / 28.2 KB/s (JSON sizes), 0 overlaps, 0 underruns

The full repro-browser run shows no other differences beyond run-to-run noise.

Known issue, not changed here: when the tutor's reply is streamed in real time rather than sent in a burst, the player leaves small gaps equal to the network jitter (up to 43 ms in the fake). This is because audio-player.js re-applies its 120 ms look-ahead whenever less than 120 ms is queued. The real API sends replies in bursts, and in burst mode there are no gaps. The behaviour is the same before and after this change.

Closes #24

🤖 Generated with Claude Code

Alorse and others added 7 commits September 29, 2026 13:14
Adds an audio-wire scenario (payload KB/s each way on the /live socket)
and counts, per AudioContext, buffers that overlap or leave a short hole
mid-reply. Mic frames are counted whether sent as JSON or binary, ahead
of moving audio to binary frames (#24).

Co-authored-by: Claude Code <noreply@anthropic.com>
A client that says binary: true in its start gets the tutor's audio as
raw PCM binary frames instead of base64 inside JSON, and binary frames
from any client are taken as mic audio. JSON/base64 audio still works
both ways for a client that doesn't announce it (a stale cached app
during rollout). Control messages stay JSON.

Refs #24

Co-authored-by: Claude Code <noreply@anthropic.com>
The mic's PCM16 chunks go to the server as binary WebSocket frames and
the tutor's audio comes back the same way, announced with binary: true
in the start; no more base64 in the browser. About a quarter less data
each way on the /live socket (browser repro: 19.7 -> 14.6 KB/s up,
28.3 -> 21.2 KB/s down). The e2e script speaks the same protocol.

Bumps the shell cache version to 13.

Closes #24

Co-authored-by: Claude Code <noreply@anthropic.com>
perf-server now streams mic audio the way the app does (binary frames,
binary: true in the start) and reports payload KB/s both ways; --audio
json measures an app from before #24.

Co-authored-by: Claude Code <noreply@anthropic.com>
Co-authored-by: Claude Code <noreply@anthropic.com>
…test helpers

index.ts sends control messages and audio alike through one send()
that calls encodeServerMessage; the harness decodes each audio event
once (tagOfChunk takes a Buffer too) and builds mic frames with one
helper; the repro reuses a single TextEncoder.

Co-authored-by: Claude Code <noreply@anthropic.com>
The wait also matched the greeting's empty final input-text, so the
test timed the wrong moment and failed now and then under a loaded
full-suite run.

Co-authored-by: Claude Code <noreply@anthropic.com>
@Alorse
Alorse merged commit 072ebcc into main Sep 29, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Send voice audio more compactly

1 participant