// Pure helpers of the cited-clip player and the audition deck (release 32 R1): // a fetch failure in plain words, and the LIT LINE -- each word of a quote with // the second it is spoken, so the line lights as playback reaches it. /** * A fetch failure as the person reading it needs it: a rate limit says the * platform is rate-limiting, an audio-check refusal says the source served * corrupt audio. `detail` keeps the editor's own words. */ export function fetchFailureSentence(message: string): { message: string; detail?: string } { const m = String(message ?? "").trim(); if (/\b429\b|rate[- ]?limit|too many requests|cooling down/i.test(m)) { const platform = /^(\w+) is cooling down/i.exec(m)?.[1] ?? /\b(youtube|rumble|odysee|twitch|kick)\b/i.exec(m)?.[1] ?? "the platform"; return { message: `${platform} is rate-limiting fetches: try again later.`, detail: m }; } if (/audio check found .* malformed|malformed/i.test(m)) { return { message: "The source served corrupt audio for these seconds (the audio check refused it twice); nothing was kept.", detail: m }; } if (/did not answer|ECONNREFUSED|no editor|ARCHILYZER_EDITOR_URL/i.test(m)) { return { message: "The editor is not answering, so nothing can be fetched right now.", detail: m }; } return { message: m || "the fetch failed" }; } export type SpokenWord = { text: string; t: number }; const norm = (w: string) => w.toLowerCase().replace(/[^\p{L}\p{N}']+/gu, "").replace(/'/g, ""); /** * Each word of `quote` with the second it is spoken: the cues in [from, to] * spread their words evenly across their own span, and the quote's words are * found in that sequence in order; a word not found takes the time of the one * before it (or the span's start). With no cues, or fewer than half the words * found, the quote spreads evenly across the span. Word-level offsets are * not read: no transcript shape in the corpus carries them. */ export function spokenWords( quote: string, cues: readonly { start: number; end?: number; text: string }[], from: number, to: number, ): SpokenWord[] { const words = String(quote ?? "").split(/\s+/).filter(Boolean); if (!words.length) return []; const span = Math.max(0.001, to - from); const timed: { n: string; t: number }[] = []; const sorted = [...cues].sort((a, b) => a.start - b.start); sorted.forEach((c, i) => { const end = c.end ?? sorted[i + 1]?.start ?? c.start + 3; if (end < from - 0.05 || c.start > to + 0.05) return; const ws = String(c.text ?? "").split(/\s+/).filter(Boolean); const step = ws.length ? Math.max(0, end - c.start) / ws.length : 0; ws.forEach((w, j) => timed.push({ n: norm(w), t: c.start + j * step })); }); const even = () => words.map((w, i) => ({ text: w, t: from + (i * span) / words.length })); if (!timed.length) return even(); const out: SpokenWord[] = []; let at = 0; let last = from; let hits = 0; for (const w of words) { const n = norm(w); let hit = -1; // Look ahead a little: a respelled or misheard word is skipped, not a derailment. for (let k = at; k < Math.min(timed.length, at + 8); k++) { if (n && timed[k].n === n) { hit = k; break; } } if (hit >= 0) { hits += 1; last = Math.max(last, timed[hit].t); at = hit + 1; } out.push({ text: w, t: last }); } // Mostly not found (another track's words, a paraphrase): spread it instead. return hits * 2 < words.length ? even() : out; } /** How many words are lit at `now` (the spoken ones). */ export function litCount(words: readonly SpokenWord[], now: number): number { let n = 0; for (const w of words) if (w.t <= now + 0.05) n += 1; return n; } export const spanClock = (s: number) => { const v = Math.max(0, s); const m = Math.floor(v / 60); const r = Math.floor(v % 60); return `${m}:${String(r).padStart(2, "0")}`; };