Add /voiceover script list and take detail pages, teleprompter reader, voice capture/recorder components, submission server actions with a 9 MB audio cap, and a Contribute > Voiceover entry in the user menu. Ultraworked with [Sisyphus](https://github.com/code-yeongju/oh-my-openagent) Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
120 lines
3.6 KiB
TypeScript
120 lines
3.6 KiB
TypeScript
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<PacingWord>;
|
|
}
|
|
|
|
export interface ScrollDirective {
|
|
readonly atMs: number;
|
|
readonly multiplier: number;
|
|
}
|
|
|
|
export interface PacingTimeline {
|
|
readonly segmentPacing: ReadonlyArray<SegmentPacing | undefined>;
|
|
readonly scrollDirectives: ReadonlyArray<ScrollDirective>;
|
|
readonly totalMs: number;
|
|
}
|
|
|
|
const WORD_RATE_MULTIPLIERS: Record<string, number> = { slow: 1.5, fast: 0.7, normal: 1 };
|
|
const SCROLL_RATE_MULTIPLIERS: Record<string, number> = { 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<VoiceoverSegment>,
|
|
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<SegmentPacing | undefined> = [];
|
|
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 };
|
|
}
|