diff --git a/packages/core/src/runtime/init.test.ts b/packages/core/src/runtime/init.test.ts index d17d8efc83..4f038fe6a1 100644 --- a/packages/core/src/runtime/init.test.ts +++ b/packages/core/src/runtime/init.test.ts @@ -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)); diff --git a/packages/core/src/runtime/init.ts b/packages/core/src/runtime/init.ts index 96e7586052..b7c9ae29a9 100644 --- a/packages/core/src/runtime/init.ts +++ b/packages/core/src/runtime/init.ts @@ -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" && diff --git a/packages/core/src/runtime/webAudioTransport.test.ts b/packages/core/src/runtime/webAudioTransport.test.ts index da96a07dd2..e36ab16098 100644 --- a/packages/core/src/runtime/webAudioTransport.test.ts +++ b/packages/core/src/runtime/webAudioTransport.test.ts @@ -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); diff --git a/packages/core/src/runtime/webAudioTransport.ts b/packages/core/src/runtime/webAudioTransport.ts index 7b13f2bfcf..a6c5812f80 100644 --- a/packages/core/src/runtime/webAudioTransport.ts +++ b/packages/core/src/runtime/webAudioTransport.ts @@ -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;