UNPKG

js-tts-wrapper

Version:

A JavaScript/TypeScript library that provides a unified API for working with multiple cloud-based Text-to-Speech (TTS) services

40 lines (39 loc) 1.55 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.estimateWordBoundaries = estimateWordBoundaries; /** * Estimate word boundaries for a given text * * This is a simple estimator that assumes a constant speaking rate. * It's not accurate but provides a reasonable approximation when * the TTS engine doesn't provide word boundary information. * * @param text Text to estimate word boundaries for * @param options Options for estimation * @returns Array of word boundary objects */ function estimateWordBoundaries(text, options = {}) { // Default options const wordsPerMinute = options.wordsPerMinute || 150; // Average speaking rate const startTime = options.startTime || 0; // Split text into words const words = text.split(/\s+/).filter((word) => word.length > 0); // Calculate time per word in milliseconds const msPerWord = (60 * 1000) / wordsPerMinute; // Generate word boundaries const wordBoundaries = []; let currentTime = startTime; for (const word of words) { // Estimate duration based on word length // Longer words take more time to pronounce const lengthFactor = Math.max(0.5, Math.min(2.0, word.length / 5)); const duration = msPerWord * lengthFactor; wordBoundaries.push({ word, start: currentTime / 1000, // Convert to seconds end: (currentTime + duration) / 1000, // Convert to seconds }); currentTime += duration; } return wordBoundaries; }