mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-24 16:06:34 +00:00
* fix(core): the playhead no longer jumps back when an audio clip starts While no Web Audio buffer plays, the transport clock follows an audio element, and each frame it re-picked the first in-window <audio> in page order. A sound effect that started during a voice line took the clock over, and since a clip starts late (seeked, asked to play, not yet producing sound) the playhead jumped back by that delay, 30 to 450 ms in real films, dragging every other track with it. The followed clip now keeps the clock while it plays. And audio may pull the playhead forward or hold it while it catches up (starting, seeking, buffering), but never push it back; a jump back past a hard sync, such as a native loop wrap, is ignored. The clock never runs ahead of its source, so media sync never chases it. Also: a playing element with a media error no longer leads the clock, and a speed change sets the clock's rate before the elements'. * style(core): trim the handover fix's comments to what the code cannot say * style(core): name the clock's no-rewind rule instead of commenting it * style(core): format the clock * test(core): pin the clock's hold while seeking and its skip of a failed clip * fix(core): the playhead keeps its place on a speed change, a short loop or a failed clip * fix(core): a late looping bed holds the playhead, and a clip with no source never leads it * refactor(core): split the clock's behind-the-playhead rule into its own method * fix(core): an audio clip with no source never leads or freezes the playhead
216 lines
7.2 KiB
TypeScript
216 lines
7.2 KiB
TypeScript
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
import { initSandboxRuntimeModular } from "./init";
|
|
import type { RuntimeTimelineLike } from "./types";
|
|
|
|
type GraphNode = {
|
|
kind: string;
|
|
outputs: Set<GraphNode>;
|
|
gain?: { value: number };
|
|
connect(dest: GraphNode): GraphNode;
|
|
disconnect(dest?: GraphNode): void;
|
|
};
|
|
|
|
function makeParam(value: number) {
|
|
return {
|
|
value,
|
|
cancelScheduledValues() {},
|
|
cancelAndHoldAtTime() {},
|
|
setValueAtTime() {},
|
|
linearRampToValueAtTime() {},
|
|
setTargetAtTime() {},
|
|
};
|
|
}
|
|
|
|
function makeNode(kind: string, extra: Partial<GraphNode> = {}): GraphNode {
|
|
const node: GraphNode = {
|
|
kind,
|
|
outputs: new Set(),
|
|
connect(dest) {
|
|
node.outputs.add(dest);
|
|
return dest;
|
|
},
|
|
disconnect(dest) {
|
|
if (dest) node.outputs.delete(dest);
|
|
else node.outputs.clear();
|
|
},
|
|
...extra,
|
|
};
|
|
return node;
|
|
}
|
|
|
|
const sources = new Map<HTMLMediaElement, GraphNode>();
|
|
|
|
class GraphAudioContext {
|
|
state = "running";
|
|
currentTime = 0;
|
|
destination = makeNode("destination");
|
|
resume() {
|
|
return Promise.resolve();
|
|
}
|
|
close() {
|
|
return Promise.resolve();
|
|
}
|
|
createGain() {
|
|
return makeNode("gain", { gain: makeParam(1) });
|
|
}
|
|
createMediaElementSource(el: HTMLMediaElement) {
|
|
const node = makeNode("media-element");
|
|
sources.set(el, node);
|
|
return node;
|
|
}
|
|
}
|
|
|
|
function createTimeline(duration: number): RuntimeTimelineLike {
|
|
const s = { time: 0, paused: true };
|
|
return {
|
|
play: () => void (s.paused = false),
|
|
pause: () => void (s.paused = true),
|
|
seek: (t?: number) => (t === undefined ? s.time : (s.time = t)),
|
|
totalTime: (t?: number) => (t === undefined ? s.time : (s.time = t)),
|
|
time: () => s.time,
|
|
duration: () => duration,
|
|
add: () => {},
|
|
paused: (v?: boolean) => (typeof v === "boolean" ? (s.paused = v) : s.paused),
|
|
timeScale: () => {},
|
|
set: () => {},
|
|
getChildren: () => [],
|
|
};
|
|
}
|
|
|
|
function mount(bodyHtml: string): void {
|
|
document.body.innerHTML =
|
|
`<div data-composition-id="main" data-root="true" data-start="0" data-duration="10"` +
|
|
` data-width="1920" data-height="1080">${bodyHtml}</div>`;
|
|
for (const el of document.querySelectorAll("audio")) {
|
|
el.load = () => {};
|
|
el.play = vi.fn(() => Promise.resolve());
|
|
el.pause = vi.fn();
|
|
for (const [key, value] of Object.entries({ paused: true, readyState: 4, currentTime: 0 })) {
|
|
Object.defineProperty(el, key, { value, writable: true, configurable: true });
|
|
}
|
|
}
|
|
window.__timelines = { main: createTimeline(10) };
|
|
}
|
|
|
|
async function flush() {
|
|
for (let i = 0; i < 4; i++) await Promise.resolve();
|
|
}
|
|
|
|
let frameCallbacks: FrameRequestCallback[] = [];
|
|
let nowMs = 0;
|
|
function stepFrames(count: number) {
|
|
for (let i = 0; i < count; i++) {
|
|
nowMs += 1000 / 60;
|
|
const pending = frameCallbacks;
|
|
frameCallbacks = [];
|
|
for (const cb of pending) cb(nowMs);
|
|
}
|
|
}
|
|
|
|
const originalAudioContext = (globalThis as Record<string, unknown>).AudioContext;
|
|
const originalRaf = window.requestAnimationFrame;
|
|
const originalCaf = window.cancelAnimationFrame;
|
|
|
|
beforeEach(() => {
|
|
sources.clear();
|
|
nowMs = 0;
|
|
frameCallbacks = [];
|
|
(globalThis as Record<string, unknown>).AudioContext = GraphAudioContext;
|
|
(globalThis as typeof globalThis & { CSS?: { escape?: (v: string) => string } }).CSS ??= {};
|
|
globalThis.CSS.escape ??= (v: string) => v;
|
|
window.requestAnimationFrame = ((cb: FrameRequestCallback) => {
|
|
frameCallbacks.push(cb);
|
|
return frameCallbacks.length;
|
|
}) as typeof window.requestAnimationFrame;
|
|
window.cancelAnimationFrame = (() => {}) as typeof window.cancelAnimationFrame;
|
|
vi.spyOn(performance, "now").mockImplementation(() => nowMs);
|
|
});
|
|
|
|
afterEach(() => {
|
|
window.__hfRuntimeTeardown?.();
|
|
document.body.innerHTML = "";
|
|
document.documentElement.removeAttribute("style");
|
|
document.body.removeAttribute("style");
|
|
window.__timelines = {} as Record<string, RuntimeTimelineLike>;
|
|
delete window.__player;
|
|
delete window.__hf;
|
|
vi.restoreAllMocks();
|
|
(globalThis as Record<string, unknown>).AudioContext = originalAudioContext;
|
|
window.requestAnimationFrame = originalRaf;
|
|
window.cancelAnimationFrame = originalCaf;
|
|
});
|
|
|
|
describe("the audio the playhead follows", () => {
|
|
it("keeps following a playing voice when a clip earlier in the page starts late", async () => {
|
|
mount(
|
|
`<audio id="sfx" data-start="1" data-duration="2" src="/assets/sfx.mp3"></audio>` +
|
|
`<audio id="vo" data-start="0" data-duration="10" src="/assets/vo.mp3"></audio>`,
|
|
);
|
|
const sfx = document.getElementById("sfx") as HTMLAudioElement;
|
|
const vo = document.getElementById("vo") as HTMLAudioElement;
|
|
initSandboxRuntimeModular();
|
|
await flush();
|
|
window.__player?.play();
|
|
await flush();
|
|
const playedAt = nowMs;
|
|
Object.assign(vo, { paused: false });
|
|
let lastTime = 0;
|
|
for (let frame = 0; frame < 90; frame++) {
|
|
stepFrames(1);
|
|
const t = (nowMs - playedAt) / 1000;
|
|
vo.currentTime = t; // the voice plays in step with the wall clock
|
|
if (t >= 1) Object.assign(sfx, { paused: false, currentTime: 0 }); // started, not moving yet
|
|
const time = window.__player!.getTime();
|
|
expect(time).toBeGreaterThanOrEqual(lastTime);
|
|
lastTime = time;
|
|
}
|
|
expect(lastTime).toBeGreaterThan(1.4);
|
|
});
|
|
|
|
it("follows the next clip when one earlier in the page failed to load", async () => {
|
|
mount(
|
|
`<audio id="broken" data-start="0" data-duration="10" src="/assets/missing.mp3"></audio>` +
|
|
`<audio id="vo" data-start="0" data-duration="10" src="/assets/vo.mp3"></audio>`,
|
|
);
|
|
const broken = document.getElementById("broken") as HTMLAudioElement;
|
|
const vo = document.getElementById("vo") as HTMLAudioElement;
|
|
initSandboxRuntimeModular();
|
|
await flush();
|
|
window.__player?.play();
|
|
await flush();
|
|
const playedAt = nowMs;
|
|
Object.defineProperty(broken, "error", { value: { code: 4 } });
|
|
Object.assign(broken, { paused: false, currentTime: 0 });
|
|
Object.assign(vo, { paused: false });
|
|
for (let frame = 0; frame < 90; frame++) {
|
|
stepFrames(1);
|
|
vo.currentTime = (nowMs - playedAt) / 1000 + 0.2; // ahead of the wall clock, so only the voice explains it
|
|
}
|
|
expect(window.__player!.getTime()).toBeGreaterThan((nowMs - playedAt) / 1000 + 0.1);
|
|
});
|
|
|
|
it("is not frozen by a clip that has no playable source", async () => {
|
|
mount(
|
|
`<audio id="empty" data-start="0" data-duration="10"></audio>` +
|
|
`<audio id="vo" data-start="0" data-duration="10" src="/assets/vo.mp3"></audio>`,
|
|
);
|
|
const empty = document.getElementById("empty") as HTMLAudioElement;
|
|
const vo = document.getElementById("vo") as HTMLAudioElement;
|
|
initSandboxRuntimeModular();
|
|
await flush();
|
|
window.__player?.play();
|
|
await flush();
|
|
const playedAt = nowMs;
|
|
// What Chrome reports for an <audio> with no source once play() is called on it.
|
|
Object.defineProperty(empty, "networkState", { value: 0 });
|
|
Object.defineProperty(empty, "readyState", { value: 0 });
|
|
Object.assign(empty, { paused: false });
|
|
Object.assign(vo, { paused: false });
|
|
for (let frame = 0; frame < 90; frame++) {
|
|
stepFrames(1);
|
|
vo.currentTime = (nowMs - playedAt) / 1000;
|
|
}
|
|
expect(window.__player!.getTime()).toBeGreaterThan(1.4);
|
|
});
|
|
});
|