Skip to content

Commit d9f69f6

Browse files
feat(studio,cli): music beat detection with timeline guides + headless beats CLI (#1424)
* feat(studio,cli): music beat detection with timeline guides + headless beats CLI Beat detection for music tracks: the Studio draws beat guides on the active track, beats are user-editable and persist to a project file, and a new `hyperframes beats` CLI generates that file headlessly before the Studio opens. Detection lives in @hyperframes/core/beats (shared by Studio + CLI): an energy onset detector cross-validated with bpm-detective, regularized to an octave- aligned grid, silence-gated, with per-beat loudness. Music-only — an <audio data-timeline-role="music"> is analyzed; voiceover is excluded. Studio: green beat lines + draggable dots on the selected track; add at playhead, drag to move, double-click to delete (audio scrubs); edits persist to beats/<audio>.json and are undoable (interleaved with file history). CLI: `hyperframes beats [dir]` runs the same detection in headless Chrome (prebuilt browser bundle in dist) and writes the beat file. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> Co-authored-by: Miguel Ángel <miguel07alm@protonmail.com> * feat(studio): timeline beat-grid + zoom UX refinements - Center-anchored magnify: zooming via the toolbar/slider keeps the time at the viewport center fixed instead of anchoring at the left. Pinch still anchors at the cursor. - Move-snap to beats: dragging a clip snaps whichever edge (start or end) is nearest a beat, matching the existing resize-edge snapping. - Beat lines on track backgrounds: faint full-height beat lines now paint behind the clips on every track lane (brightness scales with loudness); the green dots stay on the active track's top bar. - Waveform follows zoom: bars fill the full clip width and resample the windowed peaks, so the waveform stretches with zoom instead of stopping partway across a widened clip. - Beat dots centered in the top bar: align the dot band to the clip top (CLIP_Y) so the dots sit centered in the dark bar instead of being bisected by the clip's top border. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> Co-authored-by: Miguel Ángel <miguel07alm@protonmail.com> * fix(studio): preserve media sourceDuration across element re-derivation Moving a non-music clip re-derived the timeline elements into fresh objects whose sourceDuration the DOM scan hadn't loaded yet. The async probe skips srcs already in its cache, so the value was silently dropped — trimFractions then returned no window and the trimmed music waveform reset to the full source pinned at the track start. Re-apply the cached probe duration synchronously on every derivation (applyCachedSourceDurations) and extract the async probe loop into probeMissingSourceDurations to keep useTimelinePlayer within the file size limit. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> Co-authored-by: Miguel Ángel <miguel07alm@protonmail.com> * feat(studio): skip beat-snap on the music track, highlight move-snap target The music track defines the beats, so moving or trimming it no longer snaps to its own beats (isMusicTrack guard on both the move and resize snap paths). Moving another clip snapped only on drop with no cue. snapMoveStartToBeat now also returns the beat it will snap to; BeatBackgroundLines draws that beat's line as a bright neon-green glow while the clip's edge is within the snap region, so the target is visible before drop. Also drops .commitmsg.tmp, accidentally committed via git add -A. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> Co-authored-by: Miguel Ángel <miguel07alm@protonmail.com> * feat(studio): hide playhead while dragging a beat; default beat dots to music track - Dragging a beat dot now hides the playhead guideline (new beatDragging store flag set on beat pointer down/up) so its line doesn't track the scrub and clutter the beat being moved. - Beat dots render on the selected track, falling back to the music track when nothing is selected. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> Co-authored-by: Miguel Ángel <miguel07alm@protonmail.com> * fix(core): remove polynomial-ReDoS regex from audioRelPathForSrc CodeQL js/polynomial-redos: the lazy `.+?` followed by an optional trailing `[?#].*$` backtracks polynomially on crafted `/preview/...` inputs. Parse the preview-relative path with indexOf/slice instead, and strip the query/hash with a single linear char-class search. Behavior is unchanged for all preview/absolute/blob/data/bare inputs. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> Co-authored-by: Miguel Ángel <miguel07alm@protonmail.com> * fix(studio,core,cli): review hardening for beat detection + timeline UX - playerStore.reset() now clears beat state (analysis, edits, undo/redo, persist) so a project switch can't apply the previous project's beats, undo stack, or file-writer to the new one. - removeUserBeat returns the same reference on a no-op, and delete/move beat actions skip committing when nothing changed — no more phantom undo entries / debounced writes for no-op edits. - regularizeBeats bails to raw onsets when the (octave-misread) tempo would produce a sub-125ms grid, avoiding a tens-of-thousands-of-beats freeze. - parseBeats clamps strength to [0,1] and rejects non-finite time/strength, so a hand-edited file can't feed NaN into the gamma curve (Math.pow on a negative base) and blank out beat markers. - Start-edge beat-snap now also requires duration >= minDuration, matching the end-edge guard, so a rightward snap can't collapse the clip. - Center-anchor zoom effect always consumes its skip flag, so a pinch that produced no pps change can't leave it stranded and skip the next zoom. - Headless beats analyzer projects to {beatTimes,beatStrengths,bpm,confidence} before returning, so page.evaluate no longer serializes the full decoded PCM (channelData) across the CDP boundary. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> Co-authored-by: Miguel Ángel <miguel07alm@protonmail.com> * fix(core): gate parseBeats on schema version parseBeats accepted any object with a beats array, so a future v2 beat file (with changed semantics) would be parsed silently as v1. Reject anything whose version is not 1, treating an unknown version like an absent/invalid file. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> Co-authored-by: Miguel Ángel <miguel07alm@protonmail.com> --------- Co-authored-by: Miguel Ángel <miguel07alm@protonmail.com>
1 parent a95e49d commit d9f69f6

33 files changed

Lines changed: 1945 additions & 73 deletions

bun.lock

Lines changed: 14 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/packages/cli.mdx

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,33 @@ This is suppressed in CI environments, non-TTY shells, and when `HYPERFRAMES_NO_
512512

513513
The linter detects missing attributes, missing adapter libraries (GSAP, Lottie, Three.js), structural problems, and more. See [Common Mistakes](/guides/common-mistakes) for details on each rule.
514514

515+
### `beats`
516+
517+
Detect the beats in a composition's music track and write them to a beat file the Studio uses to draw beat guides on the timeline:
518+
519+
```bash
520+
npx hyperframes beats [dir]
521+
npx hyperframes beats [dir] --json # machine-readable JSON output
522+
```
523+
524+
The command finds the music track (an `<audio>` element with `data-timeline-role="music"`, or an id like `music`/`bgm`/`soundtrack`), runs the **same** detection the Studio uses inside a headless Chrome (identical decode + BPM analysis), and writes `beats/<audio-path>.json`:
525+
526+
```json
527+
{
528+
"version": 1,
529+
"audio": "music.wav",
530+
"beats": [{ "time": 2.027, "strength": 0.924 }]
531+
}
532+
```
533+
534+
Run it when authoring a composition so the beat file exists **before** the Studio is opened — the Studio loads this file as-is (it only auto-generates one when none exists). `time` is in seconds into the audio file; `strength` (0–1) is the beat's relative loudness. Beats edited in the Studio (add/move/delete) persist back to the same file.
535+
536+
| Flag | Description |
537+
|------|-------------|
538+
| `--json` | Output `{ ok, file, count, bpm }` as JSON |
539+
540+
Requires a local Chrome (the same one used by `render`; run `npx hyperframes browser ensure` if missing). Detection runs the **same** algorithm the Studio uses; results are near-identical (a different headless-Chrome audio sample rate can shift beat times by a frame or two).
541+
515542
### `inspect`
516543

517544
Inspect rendered visual layout across the composition timeline:

packages/cli/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@
1717
"scripts": {
1818
"test": "vitest run",
1919
"dev": "tsx src/cli.ts",
20-
"build": "bun run build:fonts && tsup && bun run build:runtime && bun run build:copy",
20+
"build": "bun run build:fonts && tsup && bun run build:runtime && bun run build:beat-analyzer && bun run build:copy",
2121
"build:fonts": "node scripts/build-fonts.mjs",
2222
"build:runtime": "tsx scripts/build-runtime.ts",
23+
"build:beat-analyzer": "node scripts/build-beat-analyzer.mjs",
2324
"build:copy": "node scripts/build-copy.mjs",
2425
"typecheck": "tsc --noEmit"
2526
},
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Prebuild the beat-detection browser bundle into dist so `hyperframes beats`
2+
// works in the published CLI (which ships only dist, not source). Mirrors how
3+
// the runtime IIFE is shipped. headlessAnalyzer.ts loads this at runtime and
4+
// injects it into a headless page.
5+
import { build } from "esbuild";
6+
import { createRequire } from "node:module";
7+
import { dirname, join } from "node:path";
8+
9+
const require = createRequire(import.meta.url);
10+
const coreRoot = dirname(require.resolve("@hyperframes/core/package.json"));
11+
const entry = join(coreRoot, "src/beats/beatDetection.ts");
12+
13+
await build({
14+
stdin: {
15+
contents:
16+
`import { analyzeMusicFromBuffer } from ${JSON.stringify(entry)};\n` +
17+
`globalThis.__hfAnalyze = analyzeMusicFromBuffer;`,
18+
resolveDir: coreRoot,
19+
loader: "ts",
20+
},
21+
bundle: true,
22+
format: "iife",
23+
platform: "browser",
24+
target: "es2020",
25+
outfile: "dist/beat-analyzer.global.js",
26+
});
27+
28+
console.log("built dist/beat-analyzer.global.js");
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
// Run the shared beat detection (@hyperframes/core/beats) in a headless Chrome
2+
// so results match the Studio exactly — same Web Audio decode + same
3+
// bpm-detective. Used by the `beats` CLI command to write the beat file before
4+
// the Studio is ever opened.
5+
6+
import { existsSync, readFileSync } from "node:fs";
7+
import { createRequire } from "node:module";
8+
import { dirname, join } from "node:path";
9+
import { fileURLToPath } from "node:url";
10+
import type { Browser, Page } from "puppeteer-core";
11+
12+
const require = createRequire(import.meta.url);
13+
14+
// The detection is browser code. We need it as an IIFE that exposes
15+
// analyzeMusicFromBuffer on the page. Prefer the artifact prebuilt at CLI build
16+
// time (shipped in dist); fall back to bundling from core source at runtime
17+
// (dev/monorepo, where core's src is on disk).
18+
let bundlePromise: Promise<string> | null = null;
19+
20+
function findPrebuiltBundle(): string | null {
21+
const here = dirname(fileURLToPath(import.meta.url));
22+
const candidates = [
23+
join(here, "beat-analyzer.global.js"), // dist root (tsup-bundled cli)
24+
join(here, "../beat-analyzer.global.js"), // dist/beats → dist
25+
join(here, "../dist/beat-analyzer.global.js"),
26+
];
27+
for (const p of candidates) {
28+
if (existsSync(p)) return p;
29+
}
30+
return null;
31+
}
32+
33+
async function buildFromCoreSource(): Promise<string> {
34+
const esbuild = await import("esbuild");
35+
const coreRoot = dirname(require.resolve("@hyperframes/core/package.json"));
36+
const entry = join(coreRoot, "src/beats/beatDetection.ts");
37+
const result = await esbuild.build({
38+
stdin: {
39+
contents:
40+
`import { analyzeMusicFromBuffer } from ${JSON.stringify(entry)};\n` +
41+
`globalThis.__hfAnalyze = analyzeMusicFromBuffer;`,
42+
resolveDir: coreRoot,
43+
loader: "ts",
44+
},
45+
bundle: true,
46+
format: "iife",
47+
platform: "browser",
48+
target: "es2020",
49+
write: false,
50+
});
51+
const out = result.outputFiles?.[0];
52+
if (!out) throw new Error("Failed to bundle beat analyzer");
53+
return out.text;
54+
}
55+
56+
function buildAnalyzerBundle(): Promise<string> {
57+
if (bundlePromise) return bundlePromise;
58+
bundlePromise = (async () => {
59+
const prebuilt = findPrebuiltBundle();
60+
if (prebuilt) return readFileSync(prebuilt, "utf8");
61+
return buildFromCoreSource();
62+
})().catch((err) => {
63+
bundlePromise = null; // don't poison the process with a cached rejection
64+
throw err;
65+
});
66+
return bundlePromise;
67+
}
68+
69+
export interface HeadlessBeatResult {
70+
beatTimes: number[];
71+
beatStrengths: number[];
72+
bpm: number | null;
73+
bpmConfidence: string;
74+
}
75+
76+
// Guard against pathological inputs that would blow CDP message limits when
77+
// transferred to the page as base64 (≈ +33% over the raw bytes).
78+
const MAX_AUDIO_BYTES = 80 * 1024 * 1024;
79+
80+
// Runs inside the headless page: decode the base64 audio and analyze it.
81+
function inPageAnalyze(data: string) {
82+
const bin = atob(data);
83+
const bytes = new Uint8Array(bin.length);
84+
for (let i = 0; i < bin.length; i++) bytes[i] = bin.charCodeAt(i);
85+
const win = window as unknown as {
86+
AudioContext: typeof AudioContext;
87+
webkitAudioContext?: typeof AudioContext;
88+
__hfAnalyze?: (buffer: AudioBuffer) => Promise<HeadlessBeatResult>;
89+
};
90+
if (typeof win.__hfAnalyze !== "function") throw new Error("beat analyzer not loaded");
91+
const ctx = new (win.AudioContext || win.webkitAudioContext!)();
92+
return (
93+
ctx
94+
.decodeAudioData(bytes.buffer)
95+
.then((buf) => win.__hfAnalyze!(buf))
96+
// analyzeMusicFromBuffer also returns the decoded PCM (channelData) + sampleRate;
97+
// project to only the fields we need so page.evaluate doesn't serialize an
98+
// ~8-million-element Float32Array back across the CDP boundary.
99+
.then((r) => ({
100+
beatTimes: r.beatTimes,
101+
beatStrengths: r.beatStrengths,
102+
bpm: r.bpm,
103+
bpmConfidence: r.bpmConfidence,
104+
}))
105+
.finally(() => ctx.close())
106+
);
107+
}
108+
109+
// Load the analyzer bundle into the page, run analysis, and surface in-page
110+
// errors (decode/codec failures, missing global) instead of an opaque rejection.
111+
async function detectOnPage(page: Page, bundle: string, b64: string): Promise<HeadlessBeatResult> {
112+
const pageErrors: string[] = [];
113+
page.on("pageerror", (e) => {
114+
pageErrors.push((e as Error).message);
115+
});
116+
page.on("console", (m) => {
117+
if (m.type() === "error") pageErrors.push(m.text());
118+
});
119+
await page.setContent("<!doctype html><html><body></body></html>");
120+
await page.addScriptTag({ content: bundle });
121+
try {
122+
return (await page.evaluate(inPageAnalyze, b64)) as HeadlessBeatResult;
123+
} catch (err) {
124+
const detail = pageErrors.length ? ` (${pageErrors.join("; ")})` : "";
125+
throw new Error(`${err instanceof Error ? err.message : String(err)}${detail}`);
126+
}
127+
}
128+
129+
/** Decode + analyze the given audio bytes in headless Chrome. */
130+
export async function analyzeBeatsHeadless(audioBytes: Buffer): Promise<HeadlessBeatResult> {
131+
if (audioBytes.length > MAX_AUDIO_BYTES) {
132+
const mb = Math.round(audioBytes.length / 1e6);
133+
throw new Error(
134+
`Audio file too large for headless analysis (${mb}MB > ${MAX_AUDIO_BYTES / 1e6}MB).`,
135+
);
136+
}
137+
const bundle = await buildAnalyzerBundle();
138+
const { ensureBrowser } = await import("../browser/manager.js");
139+
const puppeteer = await import("puppeteer-core");
140+
const browser = await ensureBrowser();
141+
const chrome: Browser = await puppeteer.default.launch({
142+
headless: true,
143+
executablePath: browser.executablePath,
144+
args: ["--no-sandbox", "--disable-dev-shm-usage", "--autoplay-policy=no-user-gesture-required"],
145+
});
146+
try {
147+
const page = await chrome.newPage();
148+
return await detectOnPage(page, bundle, audioBytes.toString("base64"));
149+
} finally {
150+
await chrome.close();
151+
}
152+
}

packages/cli/src/cli.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ const subCommands = {
117117
publish: () => import("./commands/publish.js").then((m) => m.default),
118118
render: () => import("./commands/render.js").then((m) => m.default),
119119
lint: () => import("./commands/lint.js").then((m) => m.default),
120+
beats: () => import("./commands/beats.js").then((m) => m.default),
120121
inspect: () => import("./commands/inspect.js").then((m) => m.default),
121122
layout: () => import("./commands/layout.js").then((m) => m.default),
122123
info: () => import("./commands/info.js").then((m) => m.default),

0 commit comments

Comments
 (0)