Fix playback performance on long texts
This commit is contained in:
@@ -36,6 +36,32 @@ export function getTimingMultiplier(text: string, adaptiveTiming: boolean): numb
|
||||
return multiplier;
|
||||
}
|
||||
|
||||
export function getTimingMultiplierForWords(
|
||||
words: string[],
|
||||
startIndex: number,
|
||||
endIndex: number,
|
||||
adaptiveTiming: boolean,
|
||||
): number {
|
||||
if (!adaptiveTiming) return 1;
|
||||
if (endIndex <= startIndex) return 1;
|
||||
|
||||
let maxLen = 0;
|
||||
for (let i = startIndex; i < endIndex; i++) {
|
||||
const w = words[i];
|
||||
if (w && w.length > maxLen) maxLen = w.length;
|
||||
}
|
||||
let multiplier = 1 + Math.sqrt(maxLen) * 0.04;
|
||||
|
||||
const lastToken = words[endIndex - 1] || "";
|
||||
const last = stripTrailingClosers(lastToken);
|
||||
if (/[.!?]$/.test(last)) {
|
||||
multiplier = Math.max(multiplier, 2.5);
|
||||
} else if (/[,;:]$/.test(last)) {
|
||||
multiplier = Math.max(multiplier, 1.8);
|
||||
}
|
||||
return multiplier;
|
||||
}
|
||||
|
||||
export function getWordDelay(
|
||||
text: string,
|
||||
baseDelayMs: number,
|
||||
@@ -44,6 +70,19 @@ export function getWordDelay(
|
||||
return baseDelayMs * getTimingMultiplier(text, adaptiveTiming);
|
||||
}
|
||||
|
||||
export function getWordDelayForWords(
|
||||
words: string[],
|
||||
startIndex: number,
|
||||
endIndex: number,
|
||||
baseDelayMs: number,
|
||||
adaptiveTiming: boolean,
|
||||
): number {
|
||||
return (
|
||||
baseDelayMs *
|
||||
getTimingMultiplierForWords(words, startIndex, endIndex, adaptiveTiming)
|
||||
);
|
||||
}
|
||||
|
||||
export function formatReadingTime(minutes: number): string {
|
||||
minutes = Math.round(minutes);
|
||||
if (minutes < 1) return "< 1 min";
|
||||
|
||||
Reference in New Issue
Block a user