|
|
|
|
@@ -2,10 +2,11 @@ import {removeEmojis} from './string.ts';
|
|
|
|
|
|
|
|
|
|
export class TTS {
|
|
|
|
|
private static readonly QUALITY_PATTERNS = ['Google', 'Microsoft', 'Samantha', 'Premium', 'Natural', 'Neural'];
|
|
|
|
|
private static _errorHandlerInstalled = false;
|
|
|
|
|
|
|
|
|
|
private _currentUtterance: SpeechSynthesisUtterance | null = null;
|
|
|
|
|
private _voicesLoaded: Promise<void>;
|
|
|
|
|
private _isStopping: boolean = false;
|
|
|
|
|
private _stoppedUtterances = new WeakSet<SpeechSynthesisUtterance>();
|
|
|
|
|
|
|
|
|
|
private _rate: number = 1.0;
|
|
|
|
|
get rate(): number { return this._rate; }
|
|
|
|
|
@@ -35,8 +36,8 @@ export class TTS {
|
|
|
|
|
if(this._currentUtterance && value) this._currentUtterance.voice = value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Create a TTS instance with optional configuration */
|
|
|
|
|
constructor(config?: {rate?: number; pitch?: number; volume?: number; voice?: SpeechSynthesisVoice | null}) {
|
|
|
|
|
TTS.installErrorHandler();
|
|
|
|
|
this._voicesLoaded = this.initializeVoices();
|
|
|
|
|
if(config) {
|
|
|
|
|
if(config.rate !== undefined) this._rate = config.rate;
|
|
|
|
|
@@ -46,7 +47,14 @@ export class TTS {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Initializes voice loading and sets default voice if needed */
|
|
|
|
|
private static installErrorHandler(): void {
|
|
|
|
|
if(this._errorHandlerInstalled) return;
|
|
|
|
|
window.addEventListener('unhandledrejection', (event) => {
|
|
|
|
|
if(event.reason?.error === 'interrupted' && event.reason instanceof SpeechSynthesisErrorEvent) event.preventDefault();
|
|
|
|
|
});
|
|
|
|
|
this._errorHandlerInstalled = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private initializeVoices(): Promise<void> {
|
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
|
const voices = window.speechSynthesis.getVoices();
|
|
|
|
|
@@ -64,11 +72,6 @@ export class TTS {
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Selects the best available TTS voice, prioritizing high-quality options
|
|
|
|
|
* @param lang Speaking language
|
|
|
|
|
* @returns Highest quality voice
|
|
|
|
|
*/
|
|
|
|
|
private static bestVoice(lang = 'en'): SpeechSynthesisVoice | undefined {
|
|
|
|
|
const voices = window.speechSynthesis.getVoices();
|
|
|
|
|
for (const pattern of this.QUALITY_PATTERNS) {
|
|
|
|
|
@@ -78,14 +81,12 @@ export class TTS {
|
|
|
|
|
return voices.find(v => v.lang.startsWith(lang));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Cleans text for TTS by removing emojis, markdown and code block */
|
|
|
|
|
private static cleanText(text: string): string {
|
|
|
|
|
return removeEmojis(text)
|
|
|
|
|
.replace(/```[\s\S]*?```/g, ' code block ')
|
|
|
|
|
.replace(/[#*_~`]/g, '');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Creates a speech utterance with current options */
|
|
|
|
|
private createUtterance(text: string): SpeechSynthesisUtterance {
|
|
|
|
|
const cleanedText = TTS.cleanText(text);
|
|
|
|
|
const utterance = new SpeechSynthesisUtterance(cleanedText);
|
|
|
|
|
@@ -97,45 +98,31 @@ export class TTS {
|
|
|
|
|
return utterance;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Speaks text and returns a Promise which resolves once complete */
|
|
|
|
|
async speak(text: string): Promise<void> {
|
|
|
|
|
if(!text.trim()) return Promise.resolve();
|
|
|
|
|
await this._voicesLoaded;
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
this._currentUtterance = this.createUtterance(text);
|
|
|
|
|
this._currentUtterance.onend = () => {
|
|
|
|
|
const utterance = this._currentUtterance;
|
|
|
|
|
utterance.onend = () => {
|
|
|
|
|
this._currentUtterance = null;
|
|
|
|
|
resolve();
|
|
|
|
|
};
|
|
|
|
|
this._currentUtterance.onerror = (error) => {
|
|
|
|
|
utterance.onerror = (error) => {
|
|
|
|
|
this._currentUtterance = null;
|
|
|
|
|
if(this._isStopping && error.error === 'interrupted') resolve();
|
|
|
|
|
if(this._stoppedUtterances.has(utterance) && error.error === 'interrupted') resolve();
|
|
|
|
|
else reject(error);
|
|
|
|
|
};
|
|
|
|
|
window.speechSynthesis.speak(this._currentUtterance);
|
|
|
|
|
window.speechSynthesis.speak(utterance);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Stops all TTS */
|
|
|
|
|
stop(): void {
|
|
|
|
|
this._isStopping = true;
|
|
|
|
|
if(this._currentUtterance) this._stoppedUtterances.add(this._currentUtterance);
|
|
|
|
|
window.speechSynthesis.cancel();
|
|
|
|
|
this._currentUtterance = null;
|
|
|
|
|
setTimeout(() => { this._isStopping = false; }, 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Initialize a stream that chunks text into sentences and speak them.
|
|
|
|
|
*
|
|
|
|
|
* @example
|
|
|
|
|
* const stream = tts.speakStream();
|
|
|
|
|
* stream.next("Hello ");
|
|
|
|
|
* stream.next("World. How");
|
|
|
|
|
* stream.next(" are you?");
|
|
|
|
|
* await stream.done();
|
|
|
|
|
*
|
|
|
|
|
* @returns Object with next function for passing chunk of streamed text and done for completing the stream
|
|
|
|
|
*/
|
|
|
|
|
speakStream(): {next: (text: string) => void, done: () => Promise<void>} {
|
|
|
|
|
let buffer = '';
|
|
|
|
|
let streamPromise: Promise<void> = Promise.resolve();
|
|
|
|
|
|