mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-09-05 17:30:50 +00:00
* 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>
137 lines
4.7 KiB
TypeScript
137 lines
4.7 KiB
TypeScript
// User edits to the detected beat grid. All times are in AUDIO-FILE coordinates
|
||
// (offsets into the music source), matching MusicBeatAnalysis.beatTimes, so edits
|
||
// survive moving/trimming the music clip on the timeline.
|
||
|
||
export interface UserBeat {
|
||
time: number; // audio-file seconds
|
||
strength: number; // 0–1, measured from audio
|
||
}
|
||
|
||
export interface BeatEditState {
|
||
/** Music src these edits apply to; edits reset when the src changes. */
|
||
src: string;
|
||
/** Beats the user added (audio-file coords). */
|
||
added: UserBeat[];
|
||
/** Audio-file times of detected beats the user removed. */
|
||
removed: number[];
|
||
}
|
||
|
||
// Two beat times within this many seconds are treated as the same beat.
|
||
const MATCH_EPS = 0.015;
|
||
|
||
function near(a: number, b: number): boolean {
|
||
return Math.abs(a - b) < MATCH_EPS;
|
||
}
|
||
|
||
function activeEdits(edits: BeatEditState | null, src: string | null): BeatEditState | null {
|
||
return edits && src && edits.src === src ? edits : null;
|
||
}
|
||
|
||
/** Merge detected beats with user edits → effective beats (audio-file coords). */
|
||
export function mergeUserBeats(
|
||
detectedTimes: number[],
|
||
detectedStrengths: number[],
|
||
edits: BeatEditState | null,
|
||
src: string | null,
|
||
): { times: number[]; strengths: number[] } {
|
||
const e = activeEdits(edits, src);
|
||
const removed = e?.removed ?? [];
|
||
const merged: UserBeat[] = [];
|
||
for (let i = 0; i < detectedTimes.length; i++) {
|
||
const t = detectedTimes[i]!;
|
||
if (removed.some((r) => near(r, t))) continue;
|
||
merged.push({ time: t, strength: detectedStrengths[i] ?? 0.5 });
|
||
}
|
||
if (e) {
|
||
// Skip added beats that land on an already-present (detected) beat so an
|
||
// "add" near an existing beat doesn't create a near-duplicate.
|
||
for (const b of e.added) {
|
||
if (!merged.some((m) => near(m.time, b.time))) merged.push(b);
|
||
}
|
||
}
|
||
merged.sort((a, b) => a.time - b.time);
|
||
return { times: merged.map((b) => b.time), strengths: merged.map((b) => b.strength) };
|
||
}
|
||
|
||
function base(edits: BeatEditState | null, src: string): BeatEditState {
|
||
const e = activeEdits(edits, src);
|
||
return e
|
||
? { ...e, added: [...e.added], removed: [...e.removed] }
|
||
: { src, added: [], removed: [] };
|
||
}
|
||
|
||
/**
|
||
* Add a beat at an audio-file time. `detectedTimes` lets us no-op when the beat
|
||
* lands on an existing (non-removed) detected beat — otherwise the merge would
|
||
* drop it anyway and we'd record a phantom edit/undo/write. Returns the SAME
|
||
* reference when nothing changed so callers can skip persisting.
|
||
*/
|
||
export function addUserBeat(
|
||
edits: BeatEditState | null,
|
||
src: string,
|
||
beat: UserBeat,
|
||
detectedTimes: number[] = [],
|
||
): BeatEditState | null {
|
||
const active = activeEdits(edits, src);
|
||
// Already covered by a surviving detected beat → nothing to do.
|
||
const onLiveDetected =
|
||
detectedTimes.some((t) => near(t, beat.time)) &&
|
||
!(active?.removed ?? []).some((r) => near(r, beat.time));
|
||
if (onLiveDetected) return edits;
|
||
// Already an added beat here → nothing to do.
|
||
if ((active?.added ?? []).some((b) => near(b.time, beat.time))) return edits;
|
||
|
||
const next = base(edits, src);
|
||
// If a detected beat here was previously removed, drop the removal instead of stacking.
|
||
const ri = next.removed.findIndex((r) => near(r, beat.time));
|
||
if (ri >= 0) {
|
||
next.removed.splice(ri, 1);
|
||
return next;
|
||
}
|
||
next.added.push(beat);
|
||
return next;
|
||
}
|
||
|
||
/**
|
||
* Remove the beat nearest `time` — drops a user-added beat or hides a detected
|
||
* one. Returns the SAME reference when nothing changed (no added beat near
|
||
* `time`, and no live detected beat to hide) so callers can skip persisting a
|
||
* phantom edit/undo/write.
|
||
*/
|
||
export function removeUserBeat(
|
||
edits: BeatEditState | null,
|
||
src: string,
|
||
detectedTimes: number[],
|
||
time: number,
|
||
): BeatEditState | null {
|
||
const active = activeEdits(edits, src);
|
||
const hasAdded = (active?.added ?? []).some((b) => near(b.time, time));
|
||
const detected = detectedTimes.find((t) => near(t, time));
|
||
const alreadyHidden =
|
||
detected !== undefined && (active?.removed ?? []).some((r) => near(r, detected));
|
||
if (!hasAdded && (detected === undefined || alreadyHidden)) return edits;
|
||
|
||
const next = base(edits, src);
|
||
const ai = next.added.findIndex((b) => near(b.time, time));
|
||
if (ai >= 0) {
|
||
next.added.splice(ai, 1);
|
||
return next;
|
||
}
|
||
if (detected !== undefined && !next.removed.some((r) => near(r, detected))) {
|
||
next.removed.push(detected);
|
||
}
|
||
return next;
|
||
}
|
||
|
||
/** Move the beat at `fromTime` to `toBeat` (delete original, add new). */
|
||
export function moveUserBeat(
|
||
edits: BeatEditState | null,
|
||
src: string,
|
||
detectedTimes: number[],
|
||
fromTime: number,
|
||
toBeat: UserBeat,
|
||
): BeatEditState | null {
|
||
const removed = removeUserBeat(edits, src, detectedTimes, fromTime);
|
||
return addUserBeat(removed, src, toBeat, detectedTimes) ?? removed;
|
||
}
|