-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtts.js
69 lines (57 loc) · 1.9 KB
/
tts.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
class TTS {
#_speechSynthesis = window.speechSynthesis || null;
#_speechSynthesisUtterance = new SpeechSynthesisUtterance();
#_voiceId = -1;
constructor() {
this.#_speechSynthesisUtterance.volume = localStorage.getItem('tts-volume') || 1;
this.#_speechSynthesisUtterance.pitch = localStorage.getItem('tts-pitch') || 1;
this.#_speechSynthesisUtterance.rate = localStorage.getItem('tts-rate') || 1;
}
loadVoicesAsync() {
return new Promise((resolve, reject) => {
if (this.#_speechSynthesis) {
let timerId, attempts = 0;
timerId = setInterval(() => {
if (++attempts > 20) {
reject();
clearInterval(timerId);
} else {
if (this.#_speechSynthesis.getVoices().length !== 0) {
resolve();
clearInterval(timerId);
}
}
}, 100);
} else {
reject('SpeechSynthesis is not supported!');
}
});
}
getVoiceId() {
return this.#_voiceId;
}
setVoiceId(val) {
this.#_voiceId = val;
}
getSpeechSynthesis() {
return this.#_speechSynthesis;
}
getSpeechSynthesisUtterance() {
return this.#_speechSynthesisUtterance;
}
setVolume(val) {
localStorage.setItem('tts-volume', this.#_speechSynthesisUtterance.volume = val);
}
setPitch(val) {
localStorage.setItem('tts-pitch', this.#_speechSynthesisUtterance.pitch = val);
}
setRate(val) {
localStorage.setItem('tts-rate', this.#_speechSynthesisUtterance.rate = val);
}
setVoice(val) {
this.#_speechSynthesisUtterance.voice = val;
}
setLang(val) {
this.#_speechSynthesisUtterance.lang = val;
}
}