// THE PAUSES IN A STRETCH OF SPEECH, and an edge moved into one (2026-11-02). // // A cut that lands on a word is heard as a cut-off sentence; a crossfade that // lands on one fades it out. This is the one place that says where the quiet // is and where an edge goes in it, for every caller that moves an edge to a // pause: the trailer (trailer.mjs, each beat's crossfade landed in silence), // the bench waveform's marks between words (lib/report/benchZoom.mjs // quietPoints) and the clip editor's "snap edge to the nearest pause". // // A PAUSE is a span `{ from, to }` (seconds, any clock -- the caller's) where // nobody speaks: a measured silence (cut-points.mjs detectSilence, `{ s, e }`, // or `{ from, to }`), or the gap between two TIMED words (a words file, a // whisper transcript) at least PAUSE_MIN_GAP long. Overlapping ones merge. // // WORDS `{ start, end?, text }`: a timed word has an `end`; a caption's word // (YouTube's en-orig `` timings) has only its `start`. Both say where a // word starts and what it says -- what decides that an edge finishes a // sentence, and names the word an edge was moved past. Only timed words add // pauses: a start-only word list has no gaps to read. // // AN EDGE INTO A PAUSE (snapEdge). `room` is how much quiet the edge wants on // its own side: a crossfade's length (its fade runs over the beat's last // `room` seconds, or its first), or SNAP_HARD_ROOM for a hard cut. // // end forward, into the first pause after the words it should finish, // at `pause.from + room` (the whole fade in the quiet) or the pause's // end when it is shorter -- never past the next word's start. When // the words are known, the words it finishes are the one in progress // at the edge, or -- when a sentence ends within `maxShift` of the // edge (. ? !) -- through that sentence's last word. // start back, into the last pause that starts before it, at `pause.to − // room` (the whole fade-in in the quiet) or the pause's start. // nearest the middle of the pause nearest the edge. // // Bounded: never more than `maxShift` (SNAP_MAX_SHIFT), never past `lo`/`hi` // (the caller's span: a segment's, a clip's). An edge already where it should // be does not move. What moved and why comes back to be said. // // PURE: no fs, no ffmpeg, no DOM -- the browser imports it (benchZoom.mjs). /** A gap between two timed words this long is a pause (seconds): the build's silencedetect minimum. */ export const PAUSE_MIN_GAP = 0.09; /** The furthest an edge is moved to reach a pause (seconds). */ export const SNAP_MAX_SHIFT = 1; /** The quiet a hard cut (no crossfade) wants on its side of the edge (seconds). */ export const SNAP_HARD_ROOM = 0.1; /** A word sounds for at least this long after its start (seconds): a pause that starts sooner is the one before it. */ export const MIN_WORD = 0.1; const fin = (v) => typeof v === "number" && Number.isFinite(v); const round4 = (v) => Math.round(v * 10000) / 10000; const TERMINAL = /[.?!]["')\]”’]*\s*$/; /** Does this text close a sentence by its punctuation (. ? !, closing quotes or brackets allowed after)? */ export function endsSentence(text) { return TERMINAL.test(String(text ?? "")); } /** A word list as `{ start, end?, text }`, finite starts only, in order. Accepts `w` / `word` for the text. */ export function normWords(words) { return (words ?? []) .map((w) => ({ start: Number(w?.start), ...(fin(Number(w?.end)) && w?.end !== null && w?.end !== undefined ? { end: Number(w.end) } : {}), text: String(w?.text ?? w?.w ?? w?.word ?? ""), })) .filter((w) => fin(w.start)) .sort((a, b) => a.start - b.start); } /** * The gaps between consecutive TIMED words at least `minGap` long (0: every * gap, a zero-width one where two words touch). Words without an end add none. * * @param {Array<{ start: number, end?: number }>} words * @param {number} [minGap] * @returns {Array<{ from: number, to: number }>} */ export function wordGaps(words, minGap = 0) { const ws = (words ?? []).filter((w) => fin(w?.start) && fin(w?.end)).sort((a, b) => a.start - b.start); const out = []; for (let i = 1; i < ws.length; i += 1) { const gap = ws[i].start - ws[i - 1].end; if (gap >= 0 && gap >= minGap) out.push({ from: ws[i - 1].end, to: ws[i].start }); } return out; } /** A silence as `{ from, to }`: detectSilence's `{ s, e }` or already `{ from, to }`; null when it is not one. */ function spanOf(x) { const from = fin(x?.from) ? x.from : fin(x?.s) ? x.s : null; const to = fin(x?.to) ? x.to : fin(x?.e) ? x.e : null; return from !== null && to !== null && to > from ? { from, to } : null; } /** * The pauses: measured silences and the gaps between timed words, merged * where they overlap or touch, in order, those meeting [from, to]. * * @param {{ words?: Array<{ start: number, end?: number }> | null, silences?: Array<{ s?: number, e?: number, from?: number, to?: number }> | null }} o * @param {{ from?: number, to?: number, minGap?: number }} [range] * @returns {Array<{ from: number, to: number }>} */ export function pauseSpans({ words = null, silences = null }, { from = -Infinity, to = Infinity, minGap = PAUSE_MIN_GAP } = {}) { const all = [...(silences ?? []).map(spanOf).filter(Boolean), ...wordGaps(words ?? [], minGap).filter((g) => g.to > g.from)] .sort((a, b) => a.from - b.from); const out = []; for (const p of all) { const last = out[out.length - 1]; if (last && p.from <= last.to + 1e-6) last.to = Math.max(last.to, p.to); else out.push({ ...p }); } return out.filter((p) => p.to >= from && p.from <= to).map((p) => ({ from: round4(p.from), to: round4(p.to) })); } /** * The places an edge or a gap's edge can go, as points: the middle of every * pause between consecutive words (or cues; touching ones too) and of every * measured silence. Sorted, deduplicated within 20 ms. In [from, to]. (The * bench waveform's marks: benchZoom.mjs quietPoints.) * * @param {{ words?: Array<{ start: number, end: number }> | null, silences?: Array<{ from: number, to: number }> | null }} o * @param {number} from * @param {number} to */ export function pausePoints({ words = null, silences = null }, from, to) { const pts = wordGaps(words ?? [], 0).map((g) => (g.from + g.to) / 2); for (const s of silences ?? []) if (fin(s?.from) && fin(s?.to)) pts.push((s.from + s.to) / 2); const out = []; for (const p of pts.filter((x) => x >= from && x <= to).sort((a, b) => a - b)) { if (!out.length || p - out[out.length - 1] > 0.02) out.push(round4(p)); } return out; } /** * @typedef {{ at: number, from: number, moved: boolean, side: "start" | "end" | "nearest", * pause?: { from: number, to: number }, word?: string, sentence?: boolean, bounded?: boolean, * reason?: "fits" | "none" | "far" | "bound" }} Snap */ /** * One edge moved into a pause (see the header). `at` is where it goes; with * `moved` false it stays, and `reason` says why: "fits" (already in quiet * enough), "none" (no pause on that side), "far" (the pause is more than * `maxShift` away), "bound" (only past `lo`/`hi`). `word` names the word the * edge now sits after (an end) or before (a start); `sentence` says an end * was carried to a sentence's last word; `bounded` that it stopped at `lo`/`hi` * inside the pause. * * @param {number} t the edge * @param {"start" | "end" | "nearest"} side * @param {{ pauses: Array<{ from: number, to: number }>, words?: Array<{ start: number, end?: number, text?: string }> | null, * room?: number, maxShift?: number, lo?: number, hi?: number }} o * @returns {Snap} */ export function snapEdge(t, side, { pauses, words = null, room = SNAP_HARD_ROOM, maxShift = SNAP_MAX_SHIFT, lo = -Infinity, hi = Infinity }) { const ps = [...(pauses ?? [])].filter((p) => fin(p?.from) && fin(p?.to) && p.to > p.from).sort((a, b) => a.from - b.from); const ws = normWords(words); const stay = (reason, extra = {}) => ({ at: t, from: t, moved: false, side, reason, ...extra }); if (side === "nearest") { let best = null; for (const p of ps) { const mid = (p.from + p.to) / 2; if (mid < lo - 1e-9 || mid > hi + 1e-9) continue; if (!best || Math.abs(mid - t) < Math.abs(best.mid - t)) best = { p, mid }; } if (!best) return stay("none"); if (t >= best.p.from && t <= best.p.to) return stay("fits", { pause: best.p }); if (Math.abs(best.mid - t) > maxShift + 1e-9) return stay("far", { pause: best.p }); return { at: round4(best.mid), from: t, moved: true, side, pause: best.p }; } if (side === "end") { // The words the edge finishes: the one in progress, or through a sentence // that ends within maxShift. A pause that starts before that word has // sounded (MIN_WORD) is the quiet before it, not after. let after = -Infinity; let sentence = false; const k = lastIndex(ws, (w) => w.start < t - 1e-6); if (k >= 0) { let target = k; if (!endsSentence(ws[k].text)) { for (let j = k + 1; j < ws.length && ws[j].start <= t + maxShift; j += 1) { if (endsSentence(ws[j].text)) { target = j; sentence = true; break; } } } after = ws[target].start + MIN_WORD; } // Already in quiet enough -- the whole fade in one pause -- and no // sentence to finish: it stays. if (!sentence) { const holds = ps.find((x) => x.from <= t - room + 1e-6 && x.to >= t - 1e-6); if (holds) return stay("fits", { pause: holds, ...(lastWordBefore(ws, holds.from) ? { word: lastWordBefore(ws, holds.from) } : {}) }); } const p = ps.find((x) => x.to > t + 1e-6 && x.from >= after - 1e-6); if (!p) return stay("none"); const next = ws.find((w) => w.start > p.from + 1e-6); const cap = Math.min(p.to, next ? next.start : Infinity); const target = Math.max(t, Math.min(p.from + room, cap)); const word = lastWordBefore(ws, p.from); const said = { pause: p, ...(word ? { word } : {}), ...(sentence ? { sentence } : {}) }; if (target - t <= 1e-3) return stay("fits", said); if (target - t > maxShift + 1e-9) return stay("far", said); if (target > hi + 1e-9) { if (hi > t + 1e-3 && hi >= p.from - 1e-9) return { at: round4(hi), from: t, moved: true, side, ...said, bounded: true }; return stay("bound", said); } return { at: round4(target), from: t, moved: true, side, ...said }; } // start: back into the last pause that starts before it. const i = lastIndex(ps, (x) => x.from < t - 1e-6); if (i < 0) return stay("none"); const p = ps[i]; const target = Math.min(t, Math.max(p.to - room, p.from)); const first = ws.find((w) => w.start >= p.from - 1e-6); const said = { pause: p, ...(first ? { word: first.text } : {}) }; if (t - target <= 1e-3) return stay("fits", said); if (t - target > maxShift + 1e-9) return stay("far", said); if (target < lo - 1e-9) { if (lo < t - 1e-3 && lo <= p.to + 1e-9) return { at: round4(lo), from: t, moved: true, side, ...said, bounded: true }; return stay("bound", said); } return { at: round4(target), from: t, moved: true, side, ...said }; } function lastIndex(list, pred) { for (let i = list.length - 1; i >= 0; i -= 1) if (pred(list[i])) return i; return -1; } function lastWordBefore(ws, t) { const i = lastIndex(ws, (w) => w.start < t - 1e-6); return i >= 0 ? ws[i].text : null; }