Skip to content

Commit 9abf65a

Browse files
authored
fix(player): correct playback rate for direct-timeline and audio-clock paths (#849)
## Summary - **Direct-timeline path** (GSAP compositions with `window.__timelines`): The player drives these via `DirectTimelineAdapter`, bypassing postMessage entirely. Rate changes sent `set-playback-rate` to the iframe but had no receiver — GSAP's `timeScale()` was never called. Fix: add optional `timeScale?` to `DirectTimelineAdapter` and call `this._directTimelineAdapter?.timeScale?.(rate)` in `attributeChangedCallback`. GSAP timelines expose `timeScale` natively, no composition changes required. - **Audio-clock path** (compositions with audio): Three bugs caused `TransportClock` to always run at 1x when an audio element or WebAudio context drove the clock: 1. `schedulePlayback` was called without the `playbackRate` arg (defaulted to 1). 2. `onSetPlaybackRate` and `player.setPlaybackRate` didn't call `webAudio.setRate()`. 3. `TransportClock.attachAudioSource` divided by `this._rate` instead of `el.playbackRate`, cancelling the rate multiplier. - Adds 2 regression tests to `clock.test.ts` covering the corrected audio-clock formula. ## Test plan - [ ] Unit tests: `bun run --cwd packages/core test` — 861/861 pass - [ ] Browser verification (Playwright headless, GSAP direct-timeline composition): - 1x speed → ratio 0.972 ✓ - 2x speed → ratio 1.965 ✓ - 0.5x speed → ratio 0.490 ✓ 🤖 Generated with [Claude Code](https://claude.com/claude-code)
1 parent 23dd1aa commit 9abf65a

5 files changed

Lines changed: 34 additions & 1 deletion

File tree

packages/core/src/runtime/clock.test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,5 +327,29 @@ describe("TransportClock", () => {
327327
audioEl.currentTime = 3.5;
328328
expect(clock.now()).toBe(3.5);
329329
});
330+
331+
it("at 2x rate, audio-derived time advances at 2x (not 1x)", () => {
332+
// el.playbackRate=2 means el.currentTime advances at 2x wall-clock speed.
333+
// With rate=2, composition time should also advance at 2x — so
334+
// composition_time = (el.currentTime - mediaStart) + compositionStart.
335+
// The old bug divided by rate instead, yielding 1x speed.
336+
const { clock } = createClock({ rate: 2, duration: 20 });
337+
const audioEl = { currentTime: 4, paused: false, playbackRate: 2 } as HTMLMediaElement;
338+
clock.play();
339+
clock.attachAudioSource({ el: audioEl, compositionStart: 0, mediaStart: 0 });
340+
// After 1 wall-clock second at 2x: el.currentTime=4, composition should be at 4.
341+
expect(clock.now()).toBe(4);
342+
});
343+
344+
it("composition time is correct when el.playbackRate differs from clock rate", () => {
345+
// General formula: wall_elapsed = (el.currentTime - mediaStart) / el.playbackRate
346+
// composition_time = compositionStart + wall_elapsed * clockRate
347+
const { clock } = createClock({ rate: 2, duration: 20 });
348+
// Audio at 1x, clock at 2x: after 1s wall, el.currentTime=1, comp should be 2.
349+
const audioEl = { currentTime: 1, paused: false, playbackRate: 1 } as HTMLMediaElement;
350+
clock.play();
351+
clock.attachAudioSource({ el: audioEl, compositionStart: 0, mediaStart: 0 });
352+
expect(clock.now()).toBe(2);
353+
});
330354
});
331355
});

packages/core/src/runtime/clock.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,10 @@ export class TransportClock {
4848
} else {
4949
const { el, compositionStart, mediaStart } = this._audioSource;
5050
if (!el.paused && Number.isFinite(el.currentTime)) {
51-
audioTime = (el.currentTime - mediaStart) / this._rate + compositionStart;
51+
audioTime =
52+
((el.currentTime - mediaStart) / (el.playbackRate > 0 ? el.playbackRate : 1)) *
53+
this._rate +
54+
compositionStart;
5255
}
5356
}
5457
if (audioTime !== null) {

packages/core/src/runtime/init.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1586,6 +1586,7 @@ export function initSandboxRuntimeModular(): void {
15861586
onSetPlaybackRate: (rate) => {
15871587
applyPlaybackRate(rate);
15881588
if (state.transportClock) state.transportClock.setRate(state.playbackRate);
1589+
webAudio.setRate(state.playbackRate);
15891590
},
15901591
onTick: () => {
15911592
if (state.tornDown || !clock.isPlaying()) return;
@@ -1927,6 +1928,7 @@ export function initSandboxRuntimeModular(): void {
19271928
clock.now(),
19281929
vol * state.bridgeVolume,
19291930
gen,
1931+
state.playbackRate,
19301932
);
19311933
});
19321934
}
@@ -1997,6 +1999,7 @@ export function initSandboxRuntimeModular(): void {
19971999
player.setPlaybackRate = (rate: number) => {
19982000
applyPlaybackRate(rate);
19992001
clock.setRate(state.playbackRate);
2002+
webAudio.setRate(state.playbackRate);
20002003
};
20012004

20022005
// Sync clock duration from any captured timeline

packages/player/src/hyperframes-player.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ class HyperframesPlayer extends HTMLElement {
177177
const rate = parseFloat(val || "1");
178178
this._media.updatePlaybackRate(rate);
179179
this._sendControl("set-playback-rate", { playbackRate: rate });
180+
this._directTimelineAdapter?.timeScale?.(rate);
180181
this.controlsApi?.updateSpeed(rate);
181182
this.dispatchEvent(new Event("ratechange"));
182183
break;

packages/player/src/timeline-adapters.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ export interface DirectTimelineAdapter {
2424
seek: (timeInSeconds: number) => unknown;
2525
play: () => unknown;
2626
pause: () => unknown;
27+
/** Optional: set playback rate (e.g. GSAP's timeScale). Called when the player's playbackRate changes. */
28+
timeScale?: (scale: number) => unknown;
2729
}
2830

2931
export type PlaybackDurationAdapter =

0 commit comments

Comments
 (0)