forked from quisquous/cactbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
browser_tts_engine.js
108 lines (96 loc) · 2.83 KB
/
browser_tts_engine.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
const TTSEngineType = {
SpeechSynthesis: 0,
GoogleTTS: 1,
};
class TTSItem {
play() {}
}
class SpeechTTSItem extends TTSItem {
constructor(text, lang, voice) {
super();
this.text = text;
this.item = new SpeechSynthesisUtterance(text);
this.item.lang = lang;
this.item.voice = voice;
}
play() {
window.speechSynthesis.speak(this.item);
}
}
class GoogleTTSItem extends TTSItem {
constructor(text, lang) {
super();
this.lang = lang;
this.text = text;
const iframe = document.createElement('iframe');
// remove sandbox so we can modify contents/call play on audio element later
iframe.removeAttribute('sandbox');
iframe.style.display = 'none';
document.body.appendChild(iframe);
const encText = encodeURIComponent(text);
iframe.contentDocument.body.innerHTML = '<audio src="https://translate.google.com/translate_tts?ie=UTF-8&client=tw-ob&tl=' + lang + '&q=' + encText + '" id="TTS">';
this.item = iframe.contentDocument.body.firstElementChild;
}
play() {
this.item.play();
}
}
export default class BrowserTTSEngine {
constructor(lang) {
this.googleTTSLang = lang === 'cn' ? 'zh' : lang;
// TODO: should there be options for different voices here so that
// everybody isn't forced into Microsoft Anna?
const cactbotLangToSpeechLang = {
en: 'en-US',
de: 'de-DE',
fr: 'fr-FR',
ja: 'ja-JP',
// TODO: maybe need to provide an option of zh-CN, zh-HK, zh-TW?
cn: 'zh-CN',
ko: 'ko-KR',
};
// figure out what TTS engine type we need
if (window.speechSynthesis !== undefined) {
window.speechSynthesis.onvoiceschanged = () => {
const speechLang = cactbotLangToSpeechLang[lang];
const voice = window.speechSynthesis.getVoices().find((voice) => voice.lang === speechLang);
if (voice) {
this.speechLang = speechLang;
this.speechVoice = voice;
window.speechSynthesis.onvoiceschanged = null;
this.engineType = TTSEngineType.SpeechSynthesis;
}
};
}
this.engineType = TTSEngineType.GoogleTTS;
this.ttsItems = {};
}
play(text) {
try {
if (this.ttsItems[text] !== undefined)
this.ttsItems[text].play();
else
this.playTTS(text);
} catch (e) {
console.log('Exception performing TTS', e);
}
}
playTTS(text) {
switch (this.engineType) {
case TTSEngineType.SpeechSynthesis:
this.playSpeechTTS(text);
break;
case TTSEngineType.GoogleTTS:
this.playGoogleTTS(text);
break;
}
}
playSpeechTTS(text) {
this.ttsItems[text] = new SpeechTTSItem(text, this.speechLang, this.speechVoice);
this.ttsItems[text].play();
}
playGoogleTTS(text) {
this.ttsItems[text] = new GoogleTTSItem(text, this.googleTTSLang);
this.ttsItems[text].play();
}
}