Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| e32e5d6f71 |
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@ztimson/utils",
|
"name": "@ztimson/utils",
|
||||||
"version": "0.28.10",
|
"version": "0.28.11",
|
||||||
"description": "Utility library",
|
"description": "Utility library",
|
||||||
"author": "Zak Timson",
|
"author": "Zak Timson",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
|
|||||||
14
src/tts.ts
14
src/tts.ts
@@ -5,7 +5,7 @@ export class TTS {
|
|||||||
|
|
||||||
private _currentUtterance: SpeechSynthesisUtterance | null = null;
|
private _currentUtterance: SpeechSynthesisUtterance | null = null;
|
||||||
private _voicesLoaded: Promise<void>;
|
private _voicesLoaded: Promise<void>;
|
||||||
private _isStopping: boolean = false;
|
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; }
|
||||||
@@ -103,25 +103,25 @@ export class TTS {
|
|||||||
await this._voicesLoaded;
|
await this._voicesLoaded;
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
this._currentUtterance = this.createUtterance(text);
|
this._currentUtterance = this.createUtterance(text);
|
||||||
this._currentUtterance.onend = () => {
|
const utterance = this._currentUtterance;
|
||||||
|
utterance.onend = () => {
|
||||||
this._currentUtterance = null;
|
this._currentUtterance = null;
|
||||||
resolve();
|
resolve();
|
||||||
};
|
};
|
||||||
this._currentUtterance.onerror = (error) => {
|
utterance.onerror = (error) => {
|
||||||
this._currentUtterance = null;
|
this._currentUtterance = null;
|
||||||
if(this._isStopping && error.error === 'interrupted') resolve();
|
if(this._stoppedUtterances.has(utterance) && error.error === 'interrupted') resolve();
|
||||||
else reject(error);
|
else reject(error);
|
||||||
};
|
};
|
||||||
window.speechSynthesis.speak(this._currentUtterance);
|
window.speechSynthesis.speak(utterance);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Stops all TTS */
|
/** Stops all TTS */
|
||||||
stop(): void {
|
stop(): void {
|
||||||
this._isStopping = true;
|
if(this._currentUtterance) this._stoppedUtterances.add(this._currentUtterance);
|
||||||
window.speechSynthesis.cancel();
|
window.speechSynthesis.cancel();
|
||||||
this._currentUtterance = null;
|
this._currentUtterance = null;
|
||||||
setTimeout(() => { this._isStopping = false; }, 0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user