Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 18261dc5da | |||
| e32e5d6f71 | |||
| 41bc5e7eb5 |
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@ztimson/utils",
|
"name": "@ztimson/utils",
|
||||||
"version": "0.28.9",
|
"version": "0.28.12",
|
||||||
"description": "Utility library",
|
"description": "Utility library",
|
||||||
"author": "Zak Timson",
|
"author": "Zak Timson",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
|
|||||||
79
src/tts.ts
79
src/tts.ts
@@ -2,8 +2,11 @@ import {removeEmojis} from './string.ts';
|
|||||||
|
|
||||||
export class TTS {
|
export class TTS {
|
||||||
private static readonly QUALITY_PATTERNS = ['Google', 'Microsoft', 'Samantha', 'Premium', 'Natural', 'Neural'];
|
private static readonly QUALITY_PATTERNS = ['Google', 'Microsoft', 'Samantha', 'Premium', 'Natural', 'Neural'];
|
||||||
|
private static _errorHandlerInstalled = false;
|
||||||
|
|
||||||
private _currentUtterance: SpeechSynthesisUtterance | null = null;
|
private _currentUtterance: SpeechSynthesisUtterance | null = null;
|
||||||
|
private _voicesLoaded: Promise<void>;
|
||||||
|
private _stoppedUtterances = new WeakSet<SpeechSynthesisUtterance>();
|
||||||
|
|
||||||
private _rate: number = 1.0;
|
private _rate: number = 1.0;
|
||||||
get rate(): number { return this._rate; }
|
get rate(): number { return this._rate; }
|
||||||
@@ -33,8 +36,9 @@ export class TTS {
|
|||||||
if(this._currentUtterance && value) this._currentUtterance.voice = value;
|
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}) {
|
constructor(config?: {rate?: number; pitch?: number; volume?: number; voice?: SpeechSynthesisVoice | null}) {
|
||||||
|
TTS.installErrorHandler();
|
||||||
|
this._voicesLoaded = this.initializeVoices();
|
||||||
if(config) {
|
if(config) {
|
||||||
if(config.rate !== undefined) this._rate = config.rate;
|
if(config.rate !== undefined) this._rate = config.rate;
|
||||||
if(config.pitch !== undefined) this._pitch = config.pitch;
|
if(config.pitch !== undefined) this._pitch = config.pitch;
|
||||||
@@ -43,11 +47,31 @@ export class TTS {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
private static installErrorHandler(): void {
|
||||||
* Selects the best available TTS voice, prioritizing high-quality options
|
if(this._errorHandlerInstalled) return;
|
||||||
* @param lang Speaking language
|
window.addEventListener('unhandledrejection', (event) => {
|
||||||
* @returns Highest quality voice
|
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();
|
||||||
|
if(voices.length > 0) {
|
||||||
|
if(!this._voice) this._voice = TTS.bestVoice();
|
||||||
|
resolve();
|
||||||
|
} else {
|
||||||
|
const handler = () => {
|
||||||
|
window.speechSynthesis.removeEventListener('voiceschanged', handler);
|
||||||
|
if(!this._voice) this._voice = TTS.bestVoice();
|
||||||
|
resolve();
|
||||||
|
};
|
||||||
|
window.speechSynthesis.addEventListener('voiceschanged', handler);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
private static bestVoice(lang = 'en'): SpeechSynthesisVoice | undefined {
|
private static bestVoice(lang = 'en'): SpeechSynthesisVoice | undefined {
|
||||||
const voices = window.speechSynthesis.getVoices();
|
const voices = window.speechSynthesis.getVoices();
|
||||||
for (const pattern of this.QUALITY_PATTERNS) {
|
for (const pattern of this.QUALITY_PATTERNS) {
|
||||||
@@ -57,14 +81,12 @@ export class TTS {
|
|||||||
return voices.find(v => v.lang.startsWith(lang));
|
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 {
|
private static cleanText(text: string): string {
|
||||||
return removeEmojis(text)
|
return removeEmojis(text)
|
||||||
.replace(/```[\s\S]*?```/g, ' code block ')
|
.replace(/```[\s\S]*?```/g, ' code block ')
|
||||||
.replace(/[#*_~`]/g, '');
|
.replace(/[#*_~`]/g, '');
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Creates a speech utterance with current options */
|
|
||||||
private createUtterance(text: string): SpeechSynthesisUtterance {
|
private createUtterance(text: string): SpeechSynthesisUtterance {
|
||||||
const cleanedText = TTS.cleanText(text);
|
const cleanedText = TTS.cleanText(text);
|
||||||
const utterance = new SpeechSynthesisUtterance(cleanedText);
|
const utterance = new SpeechSynthesisUtterance(cleanedText);
|
||||||
@@ -76,63 +98,50 @@ export class TTS {
|
|||||||
return utterance;
|
return utterance;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Speaks text and returns a Promise which resolves once complete */
|
async speak(text: string): Promise<void> {
|
||||||
speak(text: string): Promise<void> {
|
|
||||||
if(!text.trim()) return Promise.resolve();
|
if(!text.trim()) return Promise.resolve();
|
||||||
|
await this._voicesLoaded;
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
this._currentUtterance = this.createUtterance(text);
|
this._currentUtterance = this.createUtterance(text);
|
||||||
|
const utterance = this._currentUtterance;
|
||||||
this._currentUtterance.onend = () => {
|
utterance.onend = () => {
|
||||||
this._currentUtterance = null;
|
this._currentUtterance = null;
|
||||||
resolve();
|
resolve();
|
||||||
};
|
};
|
||||||
|
utterance.onerror = (error) => {
|
||||||
this._currentUtterance.onerror = (error) => {
|
|
||||||
this._currentUtterance = null;
|
this._currentUtterance = null;
|
||||||
reject(error);
|
if(this._stoppedUtterances.has(utterance) && error.error === 'interrupted') resolve();
|
||||||
|
else reject(error);
|
||||||
};
|
};
|
||||||
|
window.speechSynthesis.speak(utterance);
|
||||||
window.speechSynthesis.speak(this._currentUtterance);
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Stops all TTS */
|
|
||||||
stop(): void {
|
stop(): void {
|
||||||
|
if(this._currentUtterance) this._stoppedUtterances.add(this._currentUtterance);
|
||||||
window.speechSynthesis.cancel();
|
window.speechSynthesis.cancel();
|
||||||
this._currentUtterance = null;
|
this._currentUtterance = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
speakStream(): {next: (text: string) => void, done: () => Promise<void>} {
|
||||||
* 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: () => void} {
|
|
||||||
let buffer = '';
|
let buffer = '';
|
||||||
|
let streamPromise: Promise<void> = Promise.resolve();
|
||||||
const sentenceRegex = /[^.!?\n]+[.!?\n]+/g;
|
const sentenceRegex = /[^.!?\n]+[.!?\n]+/g;
|
||||||
|
|
||||||
return {
|
return {
|
||||||
next: (text: string): void => {
|
next: (text: string): void => {
|
||||||
buffer += text;
|
buffer += text;
|
||||||
const sentences = buffer.match(sentenceRegex);
|
const sentences = buffer.match(sentenceRegex);
|
||||||
if(sentences) {
|
if(sentences) {
|
||||||
sentences.forEach(sentence => this.speak(sentence.trim()));
|
sentences.forEach(sentence => streamPromise = this.speak(sentence.trim()));
|
||||||
buffer = buffer.replace(sentenceRegex, '');
|
buffer = buffer.replace(sentenceRegex, '');
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
done: async (): Promise<void> => {
|
done: async (): Promise<void> => {
|
||||||
if(buffer.trim()) {
|
if(buffer.trim()) {
|
||||||
await this.speak(buffer.trim());
|
streamPromise = this.speak(buffer.trim());
|
||||||
buffer = '';
|
buffer = '';
|
||||||
}
|
}
|
||||||
|
await streamPromise;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user