fix: cap web playback latency at 300ms — prevents drift accumulation

When playback buffer drifts beyond 300ms ahead of real-time, reset
to 40ms. This prevents the unbounded latency growth over long sessions.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Siavash Sameni
2026-03-27 18:44:33 +04:00
parent 7fce83be82
commit 66f720f1ee

View File

@@ -190,11 +190,15 @@ function playAudio(pcmInt16) {
source.buffer = buffer;
source.connect(audioCtx.destination);
// Schedule seamlessly: each buffer starts exactly where the last one ended
const now = audioCtx.currentTime;
const MAX_LATENCY = 0.3; // 300ms max buffer — reset if we drift beyond this
if (nextPlayTime < now) {
// Fell behind — reset with small lookahead
nextPlayTime = now + 0.04; // 40ms initial buffer
// Fell behind — reset
nextPlayTime = now + 0.04;
} else if (nextPlayTime > now + MAX_LATENCY) {
// Too far ahead — skip to reduce latency
nextPlayTime = now + 0.04;
}
source.start(nextPlayTime);
nextPlayTime += buffer.duration;