// TIGHTEN A CLIP TO ITS SENTENCE, and an edge into the nearest pause (UI notes // 2026-10-30, item 4: "punchy tightening meant silencedetect by hand and // en-orig word times read from VTT"). // // Two existing rules, composed -- nothing here detects a pause or a sentence // of its own: // // the sentence lib/report/sentenceEdges.mjs (the phone walk's strip, the // pre-cut's rule): a start edge lands on a sentence START, // an end edge on a sentence END, read off the cues; // the pause report-to-video/pauses.mjs (L1's helper, the trailer's // snap): the measured silences and the gaps between timed // words, and `snapEdge` -- a start back into the pause // before its first word, an end forward into the one after // its last, "nearest" to the middle of the closest one. // // WHICH SENTENCE ("the sentence around the playhead or the current window"): // // at the playhead, when it is inside the clip (more than AT_MARGIN from // either edge: a playhead parked on the start, where the bench puts it // on arrival, is not a place somebody chose): the one sentence it is in // cut else the clip's cut (the quote's span): every sentence it touches, // whole -- the quote finished, nothing more // window else the whole sentences inside the window: a partial sentence at // either edge is dropped // middle a window inside one sentence: that sentence // // then each edge into its pause (a hard cut's SNAP_HARD_ROOM of quiet, at most // SNAP_MAX_SHIFT away, never past `lo`/`hi`: the cached window on the bench). // What moved and why comes back to be said; nothing here writes. The bench // writes through the window writer (PUT /api/report/window, the token, the cite // that follows the edge), and so does each Apply of the batch's proposals // (tightPropose.mjs). // // PURE: no fs, no DOM -- the bench imports it. import { sentenceBoundaries } from "./sentenceEdges.mjs"; import { pauseSpans, snapEdge } from "umtool-report-to-video/pauses"; /** Two seconds closer than this are the same boundary (sentenceEdges' SAME). */ const SAME = 0.05; /** A playhead this close to an edge is parked there, not placed (seconds). */ export const AT_MARGIN = 0.25; /** How far past the sentence the pauses are read (seconds): a snap moves at most SNAP_MAX_SHIFT. */ const PAUSE_REACH = 2; const fin = (v) => typeof v === "number" && Number.isFinite(v); const r2 = (v) => Math.round(v * 100) / 100; /** * The sentence(s) around the span [a, b]: the last sentence start at or * before `a` and the first sentence end at or after `b`. null when the cues * hold no such boundary on either side. * * @param {Array<{ start: number, end: number, text: string }>} cues * @param {number} a * @param {number} b * @returns {{ start: number, end: number } | null} */ export function sentenceAround(cues, a, b) { const { starts, ends } = sentenceBoundaries(cues); let start = null; for (const s of starts) if (s <= a + SAME) start = s; const end = ends.find((e) => e >= b - SAME && e > (start ?? -Infinity) + SAME) ?? null; return start !== null && end !== null ? { start, end } : null; } /** * The whole sentences inside [from, to]: the first sentence start at or after * `from`, the last sentence end at or before `to`. null when no whole sentence * fits. * * @param {Array<{ start: number, end: number, text: string }>} cues * @param {number} from * @param {number} to */ export function sentencesInside(cues, from, to) { const { starts, ends } = sentenceBoundaries(cues); const start = starts.find((s) => s >= from - SAME) ?? null; let end = null; for (const e of ends) if (e <= to + SAME) end = e; return start !== null && end !== null && end > start + SAME ? { start, end } : null; } /** * The pauses around [from, to] (pauses.mjs pauseSpans): the measured silences * and the gaps between timed words. * * @param {{ words?: Array<{ start: number, end?: number }> | null, silences?: Array<{ from: number, to: number }> | null }} o * @param {number} from * @param {number} to */ export function pausesNear({ words = null, silences = null }, from, to) { return pauseSpans({ words, silences }, { from: from - PAUSE_REACH, to: to + PAUSE_REACH }); } /** One snap in words: "start 12.40 → 12.95, into the pause before “So”". */ export function snapSaid(edge, snap) { if (!snap) return null; if (!snap.moved) { const why = snap.reason === "fits" ? "already in a pause" : snap.reason === "far" ? "the nearest pause is more than a second away" : snap.reason === "bound" ? "the pause is past the cached window" : "no pause near it"; return `${edge} stays at ${snap.at.toFixed(2)} (${why})`; } const word = snap.word ? ` ${snap.side === "start" ? "before" : snap.side === "end" ? "after" : "by"} “${snap.word}”` : ""; return `${edge} ${snap.from.toFixed(2)} → ${snap.at.toFixed(2)}, into the pause${word}${snap.bounded ? " (as far as the cached window)" : ""}`; } /** * An edge into the pause nearest it (pauses.mjs snapEdge "nearest"): what the * bench's "Snap edge to nearest pause" does. * * @param {number} t * @param {{ words?: Array<{ start: number, end?: number, text?: string }> | null, silences?: Array<{ from: number, to: number }> | null, lo?: number, hi?: number }} o */ export function nearestPause(t, { words = null, silences = null, lo = -Infinity, hi = Infinity }) { const pauses = pausesNear({ words, silences }, t, t); return snapEdge(t, "nearest", { pauses, words, lo, hi }); } /** * Tighten a clip to its sentence (see the header). * * @param {{ * clip: { start: number, end: number, cutStart?: number | null, cutEnd?: number | null }, * cues: Array<{ start: number, end: number, text: string }>, * words?: Array<{ start: number, end?: number, text?: string }> | null, * silences?: Array<{ from: number, to: number }> | null, * at?: number | null, * lo?: number, hi?: number, * snap?: boolean, * }} o * @returns {{ ok: true, start: number, end: number, how: "at" | "cut" | "window" | "middle", * sentence: { start: number, end: number }, snaps: { start: any, end: any } | null, changed: boolean, said: string[] } * | { ok: false, why: string }} */ export function tightWindow({ clip, cues, words = null, silences = null, at = null, lo = -Infinity, hi = Infinity, snap = true }) { if (!clip || !fin(clip.start) || !fin(clip.end)) return { ok: false, why: "the clip has no window" }; const cs = (cues ?? []).filter((c) => c && fin(c.start) && fin(c.end)); if (!cs.length) return { ok: false, why: "no cues for this recording: nothing says where a sentence is" }; const hasCut = fin(clip.cutStart) && fin(clip.cutEnd); /** @type {"at" | "cut" | "window" | "middle"} */ let how; let s = null; if (fin(at) && at > clip.start + AT_MARGIN && at < clip.end - AT_MARGIN) { how = "at"; s = sentenceAround(cs, /** @type {number} */ (at), /** @type {number} */ (at)); } else if (hasCut) { how = "cut"; s = sentenceAround(cs, /** @type {number} */ (clip.cutStart), /** @type {number} */ (clip.cutEnd)); } else { how = "window"; s = sentencesInside(cs, clip.start, clip.end); if (!s) { how = "middle"; const mid = (clip.start + clip.end) / 2; s = sentenceAround(cs, mid, mid); } } if (!s) return { ok: false, why: "the cues around it hold no sentence boundary" }; // The cached window bounds the sentence itself, not only the snap: an edge // past it is clamped to it (and said). const said = []; let start = s.start; let end = s.end; if (start < lo - 1e-6) { said.push(`the sentence starts at ${start.toFixed(2)}, before the cached window: from ${lo.toFixed(2)}`); start = lo; } if (end > hi + 1e-6) { said.push(`the sentence ends at ${end.toFixed(2)}, past the cached window: to ${hi.toFixed(2)}`); end = hi; } let snaps = null; if (snap) { const pauses = pausesNear({ words, silences }, start, end); if (pauses.length) { const a = snapEdge(start, "start", { pauses, words, lo, hi: end }); const b = snapEdge(end, "end", { pauses, words, lo: start, hi }); snaps = { start: a, end: b }; if (a.moved) start = a.at; if (b.moved) end = b.at; for (const [edge, x] of /** @type {const} */ ([["start", a], ["end", b]])) if (x.moved) said.push(/** @type {string} */ (snapSaid(edge, x))); } } start = r2(start); end = r2(end); if (!(end - start >= 0.5)) return { ok: false, why: "the sentence is shorter than half a second" }; const changed = Math.abs(start - clip.start) > 0.02 || Math.abs(end - clip.end) > 0.02; return { ok: true, start, end, how, sentence: { start: r2(s.start), end: r2(s.end) }, snaps, changed, said }; } /** What the anchor was, in words. */ export const HOW_SAID = Object.freeze({ at: "the sentence at the playhead", cut: "the sentences the cut (its quote) touches", window: "the whole sentences inside the window", middle: "the sentence the window is inside", });