fix(core): play bounded WebAudio clips full-length at non-1x playback rate (#1494)

* fix(core): play bounded WebAudio clips full-length at non-1x playback rate

startBoundedSource passed `clipDuration * rate` as start()'s duration arg, but
that arg is buffer-content seconds while clipDuration is composition seconds.
Media advances 1:1 with composition (the global rate scales the transport clock
and the source playbackRate together), so the content to play is exactly
clipDuration. Multiplying by rate truncated the clip at rate < 1 (audio cut out
at the midpoint on half-speed playback) and overran it at rate > 1. Drop the
multiply — playbackRate alone stretches the fixed content to the right wall time.

Adds a half-speed regression test and corrects the prior test that asserted the
rate-scaled (overrunning) bound.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* fix(core): restore the mediaRate scaling on the WebAudio clip bound

The bound this branch removed was correct. `start()`'s duration argument is
buffer seconds, and an element with `data-playback-rate="2"` consumes two
buffer seconds per composition second, so a `clipDuration`-second clip needs
`clipDuration * mediaRate` of buffer. Wall time then works out as
`(clipDuration * mediaRate) / (mediaRate * globalRate) = clipDuration /
globalRate`, which is the transport duration that was wanted. Dropping the
factor truncated authored 2x clips at their midpoint and overran authored
0.5x ones — and the sibling line still scaled `sourceElapsed` by mediaRate,
so `remaining` mixed buffer with composition seconds and only landed right at
mediaRate = 1.

The branch's half-speed regression could not have caught this: it changed the
GLOBAL rate on an element whose authored rate is 1, and the global rate
cancels out (it scales the transport clock and the source's playbackRate
together). Both formulas return 10 there, so the test passed before the
change it was meant to justify.

Replaced with the two cases that do discriminate — a clip authored at 2x and
one at 0.5x, each asserting the buffer-second bound. Both fail if the factor
is dropped again, as does the pre-existing authored-2x/global-0.5x contract
test the removal was breaking.

---------

Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
Vance Ingalls
2026-08-20 23:37:30 -07:00
committed by GitHub
co-authored by Claude Opus 4.8
parent 865f7fec52
commit 1ff99a50f5
@@ -386,6 +386,36 @@ describe("WebAudioTransport", () => {
expect(mock.startFn).toHaveBeenCalledWith(0, 3, 7);
});
// The bound is in BUFFER seconds, and an authored `data-playback-rate`
// is the only thing that makes buffer seconds differ from composition
// seconds — the global rate scales the transport clock and the source's
// playbackRate together, so it cancels out. Dropping the `* mediaRate`
// therefore looks harmless at the default rate and truncates every
// authored-fast clip, which is why these two cases exist.
it("bounds an authored 2x clip by its buffer length, not its clip length", async () => {
const { transport, mock, gen } = setupTransport(100);
const fast = {
muted: false,
getAttribute: (name: string) => (name === "data-playback-rate" ? "2" : null),
} as unknown as HTMLMediaElement;
// A 10s clip authored at 2x consumes 20 buffer seconds; playing from the
// start must hand `start()` all 20, or the audio stops at the halfway mark.
await transport.schedulePlayback(fast, mockBuffer, 5, 0, 5, 1, gen, 1, 10);
expect(mock.startFn).toHaveBeenCalledWith(0, 0, 20);
});
it("bounds an authored 0.5x clip by its buffer length, not its clip length", async () => {
const { transport, mock, gen } = setupTransport(100);
const slow = {
muted: false,
getAttribute: (name: string) => (name === "data-playback-rate" ? "0.5" : null),
} as unknown as HTMLMediaElement;
// The mirror case: 10 composition seconds at 0.5x consume 5 buffer
// seconds, so an unscaled bound of 10 would run 5s past the clip's end.
await transport.schedulePlayback(slow, mockBuffer, 5, 0, 5, 1, gen, 1, 10);
expect(mock.startFn).toHaveBeenCalledWith(0, 0, 5);
});
it("plays unbounded when clipDuration is omitted (legacy behavior)", async () => {
const { transport, mock, gen } = setupTransport(100);
await transport.schedulePlayback(mockEl, mockBuffer, 5, 0, 8, 1, gen);