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