Word timings
const result = await tts.generate('Highlight these words during playback.');
console.table(result.wordTimings);
Each timing contains wordIndex, word, startTime, and endTime.
Browser highlighting
await tts.play(result, {
onPlaybackStart: () => {
const startedAt = Date.now();
const timer = setInterval(() => {
const seconds = (Date.now() - startedAt) / 1000;
const active = result.wordTimings.find(
word => seconds >= word.startTime && seconds < word.endTime,
);
renderActiveWord(active?.wordIndex ?? null);
}, 50);
},
});
Start from the player's callback, not generation completion, so loading time does not shift highlights.
Limits
- Timings are model-predicted and are not forced alignment.
- Generate a sentence or short paragraph for best mapping.
- Indexes are local to one result.
- Duration data can be unavailable for some outputs.
- Use
stream()and switch timing arrays for long documents.
The repository includes a complete Vite React word-timings example.