// THE BENCH WAVEFORM'S ZOOM, as arithmetic (ClipBench.tsx, bench/GapsZoom.tsx): // which stretch of the cached window the waveform shows, moved by a zoom at a // point (the pointer's second stays under the pointer), a pan, a fit and a // zoom to a span; the ruler's ticks for what is shown; and the pauses between // words, drawn once the waveform is close enough to place a gap (or an edge) // in one. Pure: no DOM, no React, so it is unit-tested (benchZoom.test.mjs) // and the component only applies it. // // EVERYTHING IS IN ABSOLUTE SOURCE SECONDS, like the bench. `whole` is the // cached window (what the waveform shows at fit -- today's view); `vis` the // stretch of it shown now. import { pausePoints } from "umtool-report-to-video/pauses"; /** The closest the waveform zooms: a quarter of a second across its width. */ export const MIN_ZOOM_SPAN = 0.25; /** One step of + / − (and of a wheel notch, scaled by its delta). */ export const ZOOM_STEP = 1.6; /** At or under this many seconds shown, the measured silences and the words' boundaries are drawn. */ export const DETAIL_SPAN = 20; const fin = (v) => typeof v === "number" && Number.isFinite(v); const round4 = (v) => Math.round(v * 10000) / 10000; /** `vis` kept inside `whole`, at least MIN_ZOOM_SPAN (or the whole) wide. */ export function clampVis(vis, whole) { const W = Math.max(1e-6, whole.to - whole.from); const span = Math.min(W, Math.max(Math.min(MIN_ZOOM_SPAN, W), vis.to - vis.from)); let from = Math.max(whole.from, Math.min(vis.from, whole.to - span)); if (!fin(from)) from = whole.from; return { from: round4(from), to: round4(Math.min(whole.to, from + span)) }; } /** Is `vis` the whole window (no zoom)? */ export const isFit = (vis, whole) => !vis || (vis.from <= whole.from + 1e-3 && vis.to >= whole.to - 1e-3); /** * Zoom by `factor` (> 1 in, < 1 out) about source second `at`: the second * under the pointer stays under it. At fit and zooming out, fit. */ export function zoomAt(vis, whole, at, factor) { const v = vis ?? whole; const span = v.to - v.from; const next = Math.min(whole.to - whole.from, Math.max(MIN_ZOOM_SPAN, span / (factor > 0 ? factor : 1))); const a = fin(at) ? Math.min(v.to, Math.max(v.from, at)) : (v.from + v.to) / 2; const frac = span > 0 ? (a - v.from) / span : 0.5; return clampVis({ from: a - frac * next, to: a - frac * next + next }, whole); } /** Move what is shown by `dt` seconds (clamped to the window). */ export function panBy(vis, whole, dt) { const v = vis ?? whole; return clampVis({ from: v.from + dt, to: v.to + dt }, whole); } /** Show `range` with `margin` of its length either side (at least MIN_ZOOM_SPAN). */ export function zoomTo(range, whole, margin = 0.15) { const len = Math.max(MIN_ZOOM_SPAN, range.to - range.from); const pad = len * margin; return clampVis({ from: range.from - pad, to: range.to + pad }, whole); } /** Centre what is shown on `t`, keeping its width. */ export function centreOn(vis, whole, t) { const v = vis ?? whole; const span = v.to - v.from; return clampVis({ from: t - span / 2, to: t + span / 2 }, whole); } /** A wheel notch as a zoom factor: deltaY < 0 zooms in; a trackpad's small deltas zoom a little. */ export function wheelFactor(deltaY, deltaMode = 0) { const px = deltaMode === 1 ? deltaY * 16 : deltaMode === 2 ? deltaY * 400 : deltaY; return Math.exp(-px * 0.0025); } const STEPS = [0.01, 0.02, 0.05, 0.1, 0.2, 0.5, 1, 2, 5, 10, 15, 30, 60, 120, 300, 600]; /** * The ruler for what is shown: a round step that gives at most `maxTicks` * ticks, and each tick's second (a multiple of the step). */ export function rulerFor(vis, maxTicks = 10) { const span = Math.max(1e-6, vis.to - vis.from); const step = STEPS.find((s) => span / s <= maxTicks) ?? STEPS[STEPS.length - 1]; const ticks = []; for (let k = Math.ceil(vis.from / step - 1e-9); k * step <= vis.to + 1e-9; k += 1) ticks.push(round4(k * step) + 0); return { step, ticks }; } /** A tick's label: `m:ss` for a step of a second or more, else `m:ss.s` / `m:ss.ss`. */ export function tickLabel(t, step) { const s = Math.max(0, t); const m = Math.floor(s / 60); const sec = s - m * 60; const dp = step >= 1 ? 0 : step >= 0.1 ? 1 : 2; const text = sec.toFixed(dp).padStart(dp ? 3 + dp : 2, "0"); const h = Math.floor(m / 60); return h ? `${h}:${String(m % 60).padStart(2, "0")}:${text}` : `${m}:${text}`; } /** * The gaps between words, as places an edge or a gap's edge can go: for each * pause between consecutive words (or cues), its middle; and every measured * silence's middle. Sorted, deduplicated within 20 ms. In [from, to]. The * pause arithmetic is report-to-video/pauses.mjs's (pausePoints), the one the * trailer lands its crossfades by. * * @param {{ words?: Array<{ start: number, end: number }> | null, silences?: Array<{ from: number, to: number }> | null }} o * @param {number} from * @param {number} to */ export function quietPoints({ words = null, silences = null }, from, to) { return pausePoints({ words, silences }, from, to); }