forked from puppeteer/examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspeech_synth.html
30 lines (26 loc) · 894 Bytes
/
speech_synth.html
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
<button>speak</button>
<script>
async function speak(text) {
const msg = new SpeechSynthesisUtterance();
msg.text = text;
// msg.volume = 1; // 0 to 1
// msg.rate = 1; // 0.1 to 10
// msg.pitch = 1; //0 to 2
// msg.lang = this.DEST_LANG;
msg.voice = await new Promise(resolve => {
// Voice are populated, async.
speechSynthesis.onvoiceschanged = (e) => {
const voices = window.speechSynthesis.getVoices();
// for (const voice of voices) {
// console.log(voice.name, voice.localService)
// }
const name = 'Google UK English Male'; // Note: only works in Google Chrome.
resolve(voices.find(voice => voice.name === name));
};
});
msg.onend = e => console.log('SPEECH_DONE');
speechSynthesis.speak(msg);
}
const button = document.querySelector('button');
button.addEventListener('click', e => speak(TEXT2SPEECH));
</script>