fix(core): looped audio plays in sync with the picture in Safari (#8612)

* fix(core): looped audio plays in sync with the picture in Safari

A looped clip on WebKit now decodes and loops its buffer on the audio clock, like a plain clip, instead of playing from its element about 300 ms late.

* fix(core): a looped buffer wraps at the element's length, as media sync does

A decoded MP3 can run longer than the element reports, so looping at the buffer's end drifted a long loop off the picture each pass.
This commit is contained in:
Miguel Ángel
2026-11-23 20:31:30 +00:00
committed by GitHub
parent 457cc56926
commit 32afb984bd
4 changed files with 52 additions and 4 deletions
+14 -1
View File
@@ -6253,10 +6253,16 @@ describe("initSandboxRuntimeModular", () => {
const SAFARI =
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.0 Safari/605.1.15";
const startRouted = async (userAgent?: string, duration = 10, src = "/assets/track.mp3") => {
const startRouted = async (
userAgent?: string,
duration = 10,
src = "/assets/track.mp3",
loop = false,
) => {
if (userAgent) vi.spyOn(navigator, "userAgent", "get").mockReturnValue(userAgent);
const audio = mountAudio(src);
Object.defineProperty(audio, "duration", { value: duration });
audio.loop = loop;
const capture = vi
.spyOn(WebAudioTransport.prototype, "scheduleMediaElementPlayback")
.mockResolvedValue({ el: audio } as never);
@@ -6277,6 +6283,13 @@ describe("initSandboxRuntimeModular", () => {
expect(schedule).toHaveBeenCalledTimes(1);
});
// fails without the fix: a looped clip stayed on its element, heard ~300 ms behind the picture on iOS.
it("hands a looped clip to a decoded buffer too", async () => {
const { audio, schedule, release } = await startRouted(SAFARI, 10, "/assets/track.mp3", true);
await vi.waitFor(() => expect(release).toHaveBeenCalledWith(audio));
expect(schedule).toHaveBeenCalledTimes(1);
});
it("hands a 10 minute source to a decoded buffer", async () => {
const { audio, release } = await startRouted(SAFARI, 600);
await vi.waitFor(() => expect(release).toHaveBeenCalledWith(audio));
-1
View File
@@ -546,7 +546,6 @@ function decodesFirst(el: HTMLMediaElement, webAudio: WebAudioTransport): boolea
return (
isWebKit() &&
isAudioElement(el) &&
!el.loop &&
!isLongSource(el) &&
webAudio.fitsDecodeBudget(el) &&
typeof readElementRateSpec(el) === "number" &&
@@ -542,6 +542,31 @@ describe("WebAudioTransport", () => {
expect(mock.startFn).toHaveBeenCalledWith(108, 1.5);
});
// fails without the fix: the buffer played a looped clip's source once, from past its end.
it("loops a looping element's buffer over [mediaStart, end], from the pass the playhead is in", async () => {
const { transport, mock, gen } = setupTransport(100);
const looping = { ...mockEl, loop: true, duration: 10 } as HTMLMediaElement;
const buffer = { duration: 10 } as AudioBuffer;
// 20 s into a clip whose source loops 2..10 s: two passes and 4 s in, so 6 s into the file.
await transport.schedulePlayback(looping, buffer, 0, 2, () => 20, 1, gen, 1, 30);
expect(mock.sourceNode).toMatchObject({ loop: true, loopStart: 2, loopEnd: 10 });
expect(mock.startFn).toHaveBeenCalledWith(0, 6, 10);
});
it("loops at the element's length, which media sync wraps at, when the decoded buffer runs longer", async () => {
const { transport, mock, gen } = setupTransport(100);
const looping = { ...mockEl, loop: true, duration: 9.5 } as HTMLMediaElement;
const buffer = { duration: 10 } as AudioBuffer;
// Passes of 7.5 s (2..9.5): 20 s in is two passes and 5 s, so 7 s into the file.
await transport.schedulePlayback(looping, buffer, 0, 2, () => 20, 1, gen, 1, 30);
expect(mock.sourceNode).toMatchObject({ loop: true, loopStart: 2, loopEnd: 9.5 });
expect(mock.startFn).toHaveBeenCalledWith(0, 7, 10);
});
it("starts clips at exact composition start time immediately", async () => {
const { transport, mock, gen } = setupTransport(100);
+13 -2
View File
@@ -24,6 +24,7 @@ import { rateAt, sourceTimeAt, timeAtSourceTime } from "../speedRamp.js";
import { classifyWebAudioMediaRoute, reportWebAudioMediaRoute } from "./webAudioRoute.js";
import { assetUrl } from "./assetUrl.js";
import { mediaElementTime } from "./mediaClock.js";
import { sourceLengthOf } from "./sourceLength.js";
import { clampFadesToDuration, fadeGain, readElementFades } from "../audioFade.js";
const MASTER_FX_ATTRS: FxAttributes = { automation: null };
@@ -92,8 +93,11 @@ function startBoundedSource(
const remaining = (clipDuration - from) * mediaRate;
if (hasBound && remaining <= 0) return false;
const when = from > elapsed ? scheduledAt + (from - elapsed) / globalRate : 0;
if (hasBound) node.start(when, from * mediaRate + mediaStart, remaining);
else node.start(when, from * mediaRate + mediaStart);
let offset = from * mediaRate + mediaStart;
if (node.loop)
offset = node.loopStart + ((offset - node.loopStart) % (node.loopEnd - node.loopStart));
if (hasBound) node.start(when, offset, remaining);
else node.start(when, offset);
return true;
}
@@ -976,6 +980,13 @@ export class WebAudioTransport {
const sourceNode = this._ctx.createBufferSource();
sourceNode.buffer = buffer;
sourceNode.playbackRate.value = sourceRate;
const sourceLength = sourceLengthOf(el);
const loopEnd = Number.isFinite(sourceLength) ? sourceLength : buffer.duration;
if (el.loop && loopEnd > mediaStart) {
sourceNode.loop = true;
sourceNode.loopStart = mediaStart;
sourceNode.loopEnd = loopEnd;
}
const gainNode = this._ctx.createGain();
gainNode.gain.value = volume;