// The clip editor's live preview clock (LiveDeckPreview.tsx): from the // SOURCE second the editor's player is at, the CUT second the composition is // seeked to, and what the cut shows then -- the tally's counts, the stamp on // screen, whether the finale is up. Pure: no DOM, no React, so the arithmetic // is unit-tested (liveClock.test.mjs) and the page only applies it. // // The rule is the estimate's (deck.mjs estimateSchedule, playWindow): a clip's // segment starts at its play window's `from` -- the cut with the lead-in // breath before it, clamped into the extent -- and plays the source second for // second from there; a claim stamped at a source second (`claim.at`) lands // through the same `from` (factcheck.mjs stampSchedule). So the stamp lands on // its source second in the preview exactly as in the render. What the build // does that this does not: it snaps each edge to a nearby silence (up to // `render.snapWindow`), so the deck's handover at a join can land that much // off; the preview says so. // // A clip's GAPS (gaps.mjs: source seconds its segment leaves out) are on the // clock too: the footage is the kept seconds, a source second after a gap // plays that much earlier, and one inside a gap plays at its join. import { gappedLength, gapsWithin, offsetIn, sourceAtOffset } from "umtool-report-to-video/gaps"; /** @typedef {{ start: number, end: number, cutStart?: number | null, cutEnd?: number | null, gaps?: Array<{ from: number, to: number }> | null }} ClipSpan */ /** @typedef {{ id: string, start: number, duration: number, end?: number, hold?: number }} Segment */ /** * @typedef {{ claim: string, verdict: string, segment: string, at: number, landed: number, * out: [number, number], count?: number, stamp?: false, label?: string, finale?: boolean }} Stamp */ /** @typedef {{ segments: Segment[], factcheck?: { stamps: Stamp[] }, finale?: { from: number, to: number, segments?: string[] } }} Schedule */ const fin = (v) => typeof v === "number" && Number.isFinite(v); /** * The source seconds a clip plays (deck.mjs playWindow): the cut with the * lead-in before it, clamped into the extent, when it has a cut; else the * extent. The clip editor's draft cut is passed as `cutStart`/`cutEnd`, so an * unsaved nudge moves the clock at once. * * @param {ClipSpan} clip * @param {number} [leadIn] render.leadIn (0.4) */ export function livePlayWindow(clip, leadIn = 0.4) { const hasCut = fin(clip.cutStart) && fin(clip.cutEnd); return { from: hasCut ? Math.max(clip.start, /** @type {number} */ (clip.cutStart) - leadIn) : clip.start, to: hasCut ? Math.min(clip.end, /** @type {number} */ (clip.cutEnd)) : clip.end, }; } /** * Where a clip sits in the cut, for the live preview: its segment's start in * the cut's clock, the source second that start plays (`sourceFrom`), the * source second its footage ends (`sourceTo`), the cut second that last * frame plays at (`playEnd`), and its hold (a carrying clip's last frame * held for its posts, part of the segment). null when the schedule has no * segment for the clip (it is not in this cut). * * @param {Schedule} schedule * @param {ClipSpan & { id: string }} clip * @param {{ leadIn?: number }} [opts] */ export function liveClock(schedule, clip, { leadIn = 0.4 } = {}) { const i = (schedule?.segments ?? []).findIndex((s) => s.id === clip.id); if (i < 0) return null; const seg = schedule.segments[i]; const pw = livePlayWindow(clip, leadIn); const hold = fin(seg.hold) && /** @type {number} */ (seg.hold) > 0 ? /** @type {number} */ (seg.hold) : 0; const gaps = gapsWithin(Array.isArray(clip.gaps) ? clip.gaps : [], pw.from, pw.to); const playEnd = seg.start + Math.max(0, gappedLength(pw.from, pw.to, gaps)); return { id: clip.id, index: i, of: schedule.segments.length, start: seg.start, sourceFrom: pw.from, sourceTo: pw.to, playEnd: round(playEnd), hold, end: round(playEnd + hold), ...(gaps.length ? { gaps } : {}), }; } const round = (v) => Math.round(v * 1000) / 1000; /** * The cut second a source second plays at. Before the play window the clip's * first frame (`before`), after it its last (`after`): the preview holds * there, as the cut does not show those seconds. * * @param {ReturnType} clock * @param {number} sourceT * @returns {{ t: number, where: "before" | "inside" | "after" }} */ export function cutTimeAt(clock, sourceT) { if (!clock || !fin(sourceT)) return { t: clock?.start ?? 0, where: "before" }; if (sourceT < clock.sourceFrom) return { t: clock.start, where: "before" }; if (sourceT > clock.sourceTo) return { t: clock.playEnd, where: "after" }; return { t: round(clock.start + offsetIn(sourceT, clock.sourceFrom, clock.gaps)), where: "inside" }; } /** * The source second a cut second plays (the inverse, inside the clip): for a * "play the hold" or a seek from the cut's clock. null outside the footage. * * @param {ReturnType} clock * @param {number} t */ export function sourceTimeAt(clock, t) { if (!clock || !fin(t) || t < clock.start || t > clock.playEnd) return null; return round(sourceAtOffset(t - clock.start, clock.sourceFrom, clock.gaps)); } /** * The tally's counts at cut second `t`: every stamp that has LANDED by then * counts its `count` (1 unless the claim says) -- the deck's tallySteps rule. * One row per verdict the cut stamps, in the stamps' verdict order (the * order `order` gives, else first appearance). * * @param {Schedule} schedule * @param {number} t * @param {string[] | null} [order] the tally's cells (factcheck.mjs tallyVerdicts) * @returns {Array<{ verdict: string, count: number }>} */ export function tallyAt(schedule, t, order = null) { const stamps = schedule?.factcheck?.stamps ?? []; const verdicts = order ?? [...new Set(stamps.map((s) => s.verdict))]; const counts = new Map(verdicts.map((v) => [v, 0])); for (const s of stamps) { if (!counts.has(s.verdict) || !(s.landed <= t)) continue; counts.set(s.verdict, /** @type {number} */ (counts.get(s.verdict)) + (s.count ?? 1)); } return verdicts.map((v) => ({ verdict: v, count: /** @type {number} */ (counts.get(v)) })); } /** * The stamp on screen at cut second `t`, or null: a DRAWN stamp (not `stamp: * false`, not a finale's) from its `at` -- it slams in until `landed` -- until * the end of its leave (`out[1]`). `phase`: "in" (slamming), "on", "out" * (leaving). * * @param {Schedule} schedule * @param {number} t */ export function stampAt(schedule, t) { for (const s of schedule?.factcheck?.stamps ?? []) { if (s.stamp === false || s.finale) continue; if (!(t >= s.at && t < s.out[1])) continue; const phase = t < s.landed ? "in" : t < s.out[0] ? "on" : "out"; return { claim: s.claim, verdict: s.verdict, segment: s.segment, label: s.label ?? null, at: s.at, landed: s.landed, phase }; } return null; } /** * The stamps a clip carries -- one per mark it stamps (a clip may carry * several, `claims`) -- in landing order, each with its landing second in the * SOURCE's clock (where the editor's playhead would have to be), for the * readout and a "go to the stamp" seek. Empty when the clip stamps nothing in * this schedule. * * @param {Schedule} schedule * @param {ReturnType} clock */ export function clipStamps(schedule, clock) { if (!clock) return []; return (schedule?.factcheck?.stamps ?? []) .filter((x) => x.segment === clock.id) .sort((a, b) => a.landed - b.landed) .map((s) => ({ claim: s.claim, verdict: s.verdict, drawn: s.stamp !== false && !s.finale, landed: s.landed, sourceLanded: round(sourceAtOffset(s.landed - clock.start, clock.sourceFrom, clock.gaps)), })); } /** Is the finale's page up at cut second `t` (its window, `from`–`to`)? */ export function finaleUp(schedule, t) { const f = schedule?.finale; return !!f && fin(f.from) && fin(f.to) && t >= f.from && t <= f.to; } /** `m:ss.s` for a readout. */ export function clockText(t) { const s = Math.max(0, fin(t) ? t : 0); const m = Math.floor(s / 60); return `${m}:${(s - m * 60).toFixed(1).padStart(4, "0")}`; }