import type { VoiceoverSegment } from "./emotionTags"; export const DEFAULT_WORDS_PER_MINUTE = 140; export const MIN_WORDS_PER_MINUTE = 60; export const MAX_WORDS_PER_MINUTE = 300; export interface PacingWord { readonly text: string; readonly whitespaceAfter: string; readonly startMs: number; readonly endMs: number; } export interface SegmentPacing { readonly leadingWhitespace: string; readonly words: ReadonlyArray; } export interface ScrollDirective { readonly atMs: number; readonly multiplier: number; } export interface PacingTimeline { readonly segmentPacing: ReadonlyArray; readonly scrollDirectives: ReadonlyArray; readonly totalMs: number; } const WORD_RATE_MULTIPLIERS: Record = { slow: 1.5, fast: 0.7, normal: 1 }; const SCROLL_RATE_MULTIPLIERS: Record = { slow: 0.6, fast: 1.5, normal: 1 }; const DIRECTIVE_PAUSE_MS = 1000; const WORD_SPLIT_PATTERN = /(\s+)/; const SENTENCE_PAUSE_PATTERN = /[.!?…]["')\]]?$/; const CLAUSE_PAUSE_PATTERN = /[,;:—–-]["')\]]?$/; function wordCost(word: string, baseMs: number): number { const spokenLength = word.replace(/[^\p{L}\p{N}]/gu, "").length || 1; const lengthFactor = spokenLength <= 3 ? 0.7 : spokenLength <= 6 ? 1 : 1 + (spokenLength - 6) * 0.1; return baseMs * lengthFactor; } function pauseAfter(word: string, whitespaceAfter: string, baseMs: number): number { if (SENTENCE_PAUSE_PATTERN.test(word)) return baseMs * 0.9; if (CLAUSE_PAUSE_PATTERN.test(word)) return baseMs * 0.4; if (whitespaceAfter.includes("\n\n")) return baseMs * 0.6; return 0; } export function buildPacingTimeline( segments: ReadonlyArray, wordsPerMinute: number, ): PacingTimeline { const clampedWpm = Math.min(MAX_WORDS_PER_MINUTE, Math.max(MIN_WORDS_PER_MINUTE, wordsPerMinute)); const baseMs = 60000 / clampedWpm; const segmentPacing: Array = []; const scrollDirectives: ScrollDirective[] = []; let cursorMs = 0; let wordRate = 1; for (const segment of segments) { if (segment.directive) { if (segment.directive === "pause") { cursorMs += DIRECTIVE_PAUSE_MS; } else if (segment.directive in WORD_RATE_MULTIPLIERS) { wordRate = WORD_RATE_MULTIPLIERS[segment.directive] ?? 1; scrollDirectives.push({ atMs: cursorMs, multiplier: SCROLL_RATE_MULTIPLIERS[segment.directive] ?? 1, }); } segmentPacing.push(undefined); continue; } if (segment.sfx || segment.text.length === 0) { segmentPacing.push(undefined); continue; } const pieces = segment.text.split(WORD_SPLIT_PATTERN); const words: Array<{ text: string; whitespaceAfter: string; startMs: number; endMs: number; }> = []; let leadingWhitespace = ""; for (const piece of pieces) { if (piece === undefined || piece.length === 0) continue; if (/^\s+$/.test(piece)) { const lastWord = words[words.length - 1]; if (lastWord) { lastWord.whitespaceAfter += piece; } else { leadingWhitespace += piece; } continue; } words.push({ text: piece, whitespaceAfter: "", startMs: 0, endMs: 0 }); } for (const word of words) { word.startMs = cursorMs; const spokenMs = (wordCost(word.text, baseMs) + pauseAfter(word.text, word.whitespaceAfter, baseMs)) * wordRate; word.endMs = cursorMs + spokenMs; cursorMs = word.endMs; } segmentPacing.push({ leadingWhitespace, words }); } return { segmentPacing, scrollDirectives, totalMs: cursorMs }; }