Web Audio playback queue (gapless, cancellable) #7

Closed
opened 2026-10-25 14:57:54 +00:00 by dsh · 1 comment
Owner

Goal

Gapless, cancellable playback of synthesized chunks through Web Audio — the part users judge instantly.

Scope

  • AudioQueue: lazily create one AudioContext; expose unlock() to be called from a user gesture (autoplay policy), resume()/suspend().
  • enqueue(AudioBuffer) schedules with source.start(when) where when = max(prevEnd, ctx.currentTime); track prevEnd; emit speaking/idle state.
  • stop() cancels scheduled sources and clears the queue; setRate(0.5–2.0) and setVolume(0–1) via playbackRate/GainNode; dispose() closes the context.
  • Decode helpers: decodeAudio(ArrayBuffer) for mp3/wav; PCM f32 → AudioBuffer at native sample rate (24 kHz for local engines).
  • Cap the scheduled queue (e.g. 3 chunks ahead) so a long answer does not allocate everything at once; release buffers on ended.
  • Pure timing-scheduler unit tests with an injected clock.
  • Manual: 20 sentences no audible gap/click; stop mid-sentence clears instantly; rate/volume apply live; second turn queues after the first.

Acceptance

  • No clicks between sentence chunks and no drift over a 3-minute reply.
  • Main thread stays responsive (no frames dropped > 50 ms) while playing.
  • Works in Chrome and Safari desktop after the unlock gesture.

Notes

  • Depends on #1; independent of the engine. Part of M1. Design notes in docs/research/tts-engines.md §7.
## Goal Gapless, cancellable playback of synthesized chunks through Web Audio — the part users judge instantly. ## Scope - [ ] `AudioQueue`: lazily create one `AudioContext`; expose `unlock()` to be called from a user gesture (autoplay policy), `resume()`/`suspend()`. - [ ] `enqueue(AudioBuffer)` schedules with `source.start(when)` where `when = max(prevEnd, ctx.currentTime)`; track `prevEnd`; emit `speaking`/`idle` state. - [ ] `stop()` cancels scheduled sources and clears the queue; `setRate(0.5–2.0)` and `setVolume(0–1)` via `playbackRate`/GainNode; `dispose()` closes the context. - [ ] Decode helpers: `decodeAudio(ArrayBuffer)` for mp3/wav; PCM f32 → `AudioBuffer` at native sample rate (24 kHz for local engines). - [ ] Cap the scheduled queue (e.g. 3 chunks ahead) so a long answer does not allocate everything at once; release buffers on `ended`. - [ ] Pure timing-scheduler unit tests with an injected clock. - [ ] Manual: 20 sentences no audible gap/click; stop mid-sentence clears instantly; rate/volume apply live; second turn queues after the first. ## Acceptance - No clicks between sentence chunks and no drift over a 3-minute reply. - Main thread stays responsive (no frames dropped > 50 ms) while playing. - Works in Chrome and Safari desktop after the unlock gesture. ## Notes - Depends on #1; independent of the engine. Part of M1. Design notes in `docs/research/tts-engines.md` §7.
Author
Owner

Done in 023e5a6 (queue) + ef4dae5 (decode helpers). 12 queue tests + 5 decode tests: monotonic start(when), look-ahead cap, rate/volume, stop/dispose, suspended state, WAV decode and PCM→AudioBuffer. CI run #3 and #4 green.

Follow-up owned by #10: the Piper adapter should feed decodeAudio(ctx, wavBuffer) into queue.enqueue(); unlock must happen in a user gesture.

**Done** in `023e5a6` (queue) + `ef4dae5` (decode helpers). 12 queue tests + 5 decode tests: monotonic `start(when)`, look-ahead cap, rate/volume, stop/dispose, suspended state, WAV decode and PCM→AudioBuffer. CI run #3 and #4 green. Follow-up owned by #10: the Piper adapter should feed `decodeAudio(ctx, wavBuffer)` into `queue.enqueue()`; unlock must happen in a user gesture.
dsh closed this issue 2026-10-25 17:43:50 +00:00
Sign in to join this conversation.
No description provided.