"use client"; import { useEffect, useState } from "react"; import { renderCut } from "umtool-report-to-video/snap"; // WHERE THE RENDER WILL CUT THIS CLIP, over the bench's waveform: a faint // ghost at each edge the build will actually cut at -- the play window (the // cut with its lead-in, else the extent, as drafted) snapped to the nearest // silence (snap.mjs, the build's own arithmetic) -- with how far it is from // the edge drawn ("render +0.52 s"). The silences come from the cut-points // route (lib/report/cutPoints.mjs: the build's file, the build's // silencedetect, measured once and cached); the snap runs here, so a ghost // follows a drag with no request. A draft that reaches past what was measured // snaps there as unsnapped, until it is saved and measured again. type Silence = { s: number; e: number }; type Row = { id: string; silences?: Silence[]; fetchStart?: number; sound?: [number, number]; montage?: boolean; /** Which edges are the author's (mute.mjs lockedEdges): each snaps only outward, inside its own silence. */ locked?: { start: boolean; end: boolean }; missing?: boolean; error?: string; }; type Got = { knobs: Record; leadIn: number; clips: Row[] }; /** The play window (deck.mjs playWindow) of a drafted extent and the saved cut. */ function playWindowOf(sel: { from: number; to: number }, cut: { cutStart: number | null; cutEnd: number | null }, leadIn: number) { const has = cut.cutStart != null && cut.cutEnd != null; return { from: has ? Math.max(sel.from, cut.cutStart! - leadIn) : sel.from, to: has ? Math.min(sel.to, cut.cutEnd!) : sel.to, }; } /** One edge as the render will cut it: where (source s), how far from the edge drawn, and whether a silence moved it. */ export type GhostEdge = { at: number; offset: number; snapped: boolean }; /** The two edges, and the silences they were snapped to (source seconds: the zoomed waveform draws them). */ export type RenderEdges = { start: GhostEdge; end: GhostEdge; silences: { from: number; to: number }[] } | null; /** "render: +0.52 s" (or "render: here"): a ghost's words. */ export const ghostLabel = (g: GhostEdge) => Math.abs(g.offset) < 0.005 ? "render: here" : `render: ${g.offset > 0 ? "+" : "−"}${Math.abs(g.offset).toFixed(2)} s`; /** * Where the render will cut this clip's drafted selection: the silences * measured once per saved extent (the cut-points route), snapped here on * every drag. null until measured, or when the route cannot say (no source on * disk, a montage's phrase cut). */ export function useRenderEdges( project: string, clip: { id: string; start: number; end: number; cutStart: number | null; cutEnd: number | null; gaps?: { from: number; to: number }[] }, sel: { from: number; to: number }, /** Walking a take: the clip is the take's (the cut-points route reads its manifest). */ take: string | null = null, /** The cut as drafted (the bench's cut mode drags it), in place of the saved one; `sel` is then the extent as drafted. */ cutDraft: { cutStart: number | null; cutEnd: number | null } | null = null, ): RenderEdges { const [got, setGot] = useState<{ key: string; row: Row | null; knobs: Got["knobs"]; leadIn: number } | null>(null); // Measured again when what was SAVED moves (the extent picks the file and the span). const key = `${project}|${take ?? ""}|${clip.id}|${clip.start}|${clip.end}|${clip.cutStart}|${clip.cutEnd}`; useEffect(() => { let live = true; const q = new URLSearchParams({ project, clip: clip.id, ...(take ? { take } : {}) }); fetch(`/api/report/cut-points?${q}`, { cache: "no-store" }) .then((r) => (r.ok ? (r.json() as Promise) : null)) .then((j) => { if (live && j) setGot({ key, row: j.clips.find((c) => c?.id === clip.id) ?? null, knobs: j.knobs, leadIn: j.leadIn }); }) .catch(() => {}); return () => { live = false; }; }, [key, project, clip.id, take]); const row = got?.row; if (!got || !row || !row.silences || row.fetchStart == null || row.montage) return null; const pw = playWindowOf(sel, cutDraft ?? clip, got.leadIn); const render = { snapWindow: got.knobs.snapWindow ?? undefined, snapLead: got.knobs.snapLead ?? undefined, snapTail: got.knobs.snapTail ?? undefined, }; // The clip's gaps: no edge snaps across one (snap.mjs, as the build). const gaps = clip.gaps?.length ? clip.gaps : null; const cut = renderCut({ sil: row.silences, fetchStart: row.fetchStart, playFrom: pw.from, playTo: pw.to, render, locked: row.locked ?? null, gaps }); const fs = row.fetchStart; return { start: { at: cut.start, offset: Number((cut.start - pw.from).toFixed(3)), snapped: cut.snapped.start }, end: { at: cut.end, offset: Number((cut.end - pw.to).toFixed(3)), snapped: cut.snapped.end }, silences: row.silences.map((x) => ({ from: fs + x.s, to: fs + x.e })), }; } /** The ghosts over the desk's waveform: a faint line at each edge the render cuts at, with its offset. */ export default function RenderGhosts({ edges, view }: { edges: RenderEdges; view: { from: number; to: number } }) { if (!edges) return null; const span = Math.max(1e-6, view.to - view.from); const pct = (t: number) => `${((t - view.from) / span) * 100}%`; return ( <> {(["start", "end"] as const).map((edge) => { const g = edges[edge]; if (g.at < view.from || g.at > view.to) return null; const label = ghostLabel(g); return (
{label}
); })} ); }