// WHERE THE RENDER CUTS A CLIP, as arithmetic: a desired edge moved to the // nearest silence (`snap`), and the two edges of a clip kept from inverting // or collapsing (`snapCut`). Pure -- no ffmpeg, no fs -- so the build // (build-video.mjs buildClipSegment), umtool's cut-points route (the Play view // plays these edges) and the clip bench's ghost markers (drawn live while an // edge is dragged, from the silences the route measured once) run ONE // implementation, in the browser as on the server. The silences themselves are // measured by cut-points.mjs `detectSilence`, the build's own. // // EVERYTHING HERE IS IN THE SOURCE FILE'S SECONDS (0 = the window's first // sample, `fetchStart` in the recording) unless a name says `source`: that is // the clock the build snaps on, and the same float arithmetic on the same // clock is what makes the route's edges the build's to the millisecond. import { GAP_LIMITS } from "./gaps.mjs"; /** The render's defaults for the snap knobs (render.snapWindow, snapLead, snapTail, silence*). */ export const SNAP_DEFAULTS = Object.freeze({ snapWindow: 1.6, silenceMinDur: 0.09, silenceRelDb: 6, lead: 0.10, tail: 0.18, }); /** * Snap a desired cut to the nearest silence, so the clip begins and ends * between words instead of through one. Returns the desired point unchanged * when no silence is close enough -- better a tight cut than a cut in the * wrong place. * * How much of the pause is kept: 0.10 s before speech resumes, 0.18 s after it * stops, unless the render says `snapLead` / `snapTail` -- and then never more * than the pause holds, so a long one keeps its breath (room for the * crossfade to fade over silence, not the last word) without reaching the * next word. * * @param {number} desired * @param {Array<{ s: number, e: number }>} intervals the silences, file seconds * @param {"start" | "end"} kind * @param {number} window how far a silence may be (render.snapWindow) * A PINNED edge (a locked one, mute.mjs `lockedEdges`) never moves across a * sound and never INTO the window: it may only move outward, inside the * silence it already sits in (an end later, a start earlier) -- breath for * the crossfade, which the locked edge's mute keeps silent. Anything else * (no silence around it, or a snap that would cut into what the author kept: * the silence threshold is relative, and quiet last words read as silence) * leaves it exactly where it is. * * @param {{ snapLead?: number, snapTail?: number }} [render] * @param {{ pinned?: boolean }} [opts] * @returns {{ at: number, snapped: boolean }} */ export function snap(desired, intervals, kind, window, render = {}, { pinned = false } = {}) { let best = null; for (const iv of intervals) { if (pinned && !(iv.s <= desired + 1e-9 && desired <= iv.e + 1e-9)) continue; // Starting: we want to resume just before speech does -> the silence's END. // Ending: we want to stop just after speech does -> the silence's START. const point = kind === "start" ? iv.e : iv.s; const d = Math.abs(point - desired); if (d > window) continue; if (!best || d < best.d) best = { d, point, iv }; } if (!best) return { at: desired, snapped: false }; const set = kind === "start" ? render.snapLead : render.snapTail; let lead = kind === "start" ? -SNAP_DEFAULTS.lead : SNAP_DEFAULTS.tail; if (Number.isFinite(set) && set >= 0) { const room = best.iv.e - best.iv.s; lead = (kind === "start" ? -1 : 1) * Math.min(set, room); } const at = Math.max(0, best.point + lead); if (pinned && (kind === "start" ? at > desired : at < desired)) return { at: desired, snapped: false }; return { at, snapped: true }; } /** * How close (seconds) a snapped edge may come to a clip's gap: the writer's * own rule for the material kept beside one (gaps.mjs GAP_LIMITS.keep). */ export const GAP_KEEP = GAP_LIMITS.keep; /** * A clip's two edges as the render cuts them: each snapped (`snap`), then * never inverted or collapsed -- the start at least a second before the * desired end, the end at least a second after the start. * * @param {{ sil: Array<{ s: number, e: number }>, wantA: number, wantB: number, render?: Record, * locked?: { start?: boolean, end?: boolean } | null, gaps?: Array<{ from: number, to: number }> | null, * endAt?: number | null }} o * `wantA`/`wantB`: the play window's edges in the file's seconds; `locked`: * which of them are the author's, exact (mute.mjs lockedEdges) -- each * snapped only outward, inside its own silence; `gaps`: the clip's gaps * (gaps.mjs) in the file's seconds -- a gap's edges are exact, so neither * edge snaps across one (nor nearer it than GAP_KEEP): such a snap is not * taken and the edge stays where it was asked */ export function snapCut({ sil, wantA, wantB: asked, render = {}, locked = null, gaps = null, endAt = null }) { const win = render.snapWindow ?? SNAP_DEFAULTS.snapWindow; // THE RECORDING'S END (lane window-end): a file that ends the recording // (`endAt`, its last second on the file's clock) is never cut past it -- the // end is held there and does not snap beyond it, so nothing is padded with // black or silence for seconds that do not exist. const end = Number.isFinite(endAt) ? Number(endAt) : Infinity; const wantB = Math.min(asked, end); let a = snap(wantA, sil, "start", win, render, { pinned: !!locked?.start }); let b = snap(wantB, sil, "end", win, render, { pinned: !!locked?.end }); if (b.at > end) b = { at: end, snapped: false }; if (gaps?.length) { const first = gaps.reduce((m, g) => Math.min(m, g.from), Infinity); const last = gaps.reduce((m, g) => Math.max(m, g.to), -Infinity); if (a.at > wantA && a.at > first - GAP_KEEP) a = { at: wantA, snapped: false }; if (b.at < wantB && b.at < last + GAP_KEEP) b = { at: wantB, snapped: false }; } const cutA = Math.min(a.at, wantB - 1); const cutB = Math.min(Math.max(b.at, cutA + 1), Math.max(end, cutA)); return { win, a, b, cutA, cutB }; } /** A source second as the cut record writes it (`.cut.json`, three decimals). */ export const recordSecond = (fetchStart, t) => Number((fetchStart + t).toFixed(3)); /** * The render's cut of one clip in SOURCE seconds, from the silences of the * file it is cut from: what `.cut.json` records (`start`, `end`, * `snapped`) for a clip with no room and no montage. `playFrom`/`playTo` are * the play window (deck.mjs playWindow); `fetchStart` the file's first * second in the recording; `gaps` the clip's (gaps.mjs), source seconds; * `sourceEnd` the source second the file's footage ends at when it ends the * recording (onDiskClipSource) -- the cut never passes it. * * @param {{ sil: Array<{ s: number, e: number }>, fetchStart: number, playFrom: number, playTo: number, render?: Record, * locked?: { start?: boolean, end?: boolean } | null, gaps?: Array<{ from: number, to: number }> | null, * sourceEnd?: number | null }} o */ export function renderCut({ sil, fetchStart, playFrom, playTo, render = {}, locked = null, gaps = null, sourceEnd = null }) { const c = snapCut({ sil, wantA: playFrom - fetchStart, wantB: playTo - fetchStart, render, locked, ...(Number.isFinite(sourceEnd) ? { endAt: Number(sourceEnd) - fetchStart } : {}), ...(gaps?.length ? { gaps: gaps.map((g) => ({ from: g.from - fetchStart, to: g.to - fetchStart })) } : {}), }); return { start: recordSecond(fetchStart, c.cutA), end: recordSecond(fetchStart, c.cutB), snapped: { start: c.a.snapped, end: c.b.snapped }, file: { cutA: c.cutA, cutB: c.cutB }, }; } /** * The render knobs that move a snapped edge, and nothing else: what a cache of * cut points is keyed by, so a change of any other knob reuses it. * * @param {Record} render */ export function snapKnobs(render = {}) { return { snapWindow: render.snapWindow ?? SNAP_DEFAULTS.snapWindow, silenceMinDur: render.silenceMinDur ?? SNAP_DEFAULTS.silenceMinDur, silenceRelDb: render.silenceRelDb ?? SNAP_DEFAULTS.silenceRelDb, snapLead: Number.isFinite(render.snapLead) ? render.snapLead : null, snapTail: Number.isFinite(render.snapTail) ? render.snapTail : null, }; }