fix(runtime): schedule future WebAudio clips with delay instead of playing immediately

Clips whose compositionStart is ahead of the current timeline position
were starting immediately because sourceNode.start() always received
when=0. Use the AudioContext scheduling API to defer future clips:
sourceNode.start(ctx.currentTime + delay, mediaStart).

Closes #674

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Miguel Ángel
2026-05-08 14:56:33 +00:00
co-authored by Claude Opus 4.6
parent 8de7ad7f61
commit 38522f996e
2 changed files with 89 additions and 2 deletions
@@ -91,10 +91,16 @@ export class WebAudioTransport {
sourceNode.connect(gainNode);
gainNode.connect(this._masterGain);
const offset = Math.max(0, compositionTime - compositionStart + mediaStart);
const elapsed = compositionTime - compositionStart;
const scheduledAt = this._ctx.currentTime;
this._scheduleOffset = scheduledAt - compositionTime;
sourceNode.start(0, offset);
if (elapsed >= 0) {
sourceNode.start(0, elapsed + mediaStart);
} else {
const delay = -elapsed;
sourceNode.start(scheduledAt + delay, mediaStart);
}
const priorMuted = el.muted;
el.muted = true;