-
Notifications
You must be signed in to change notification settings - Fork 8
/
speechManager.js
63 lines (48 loc) · 1.28 KB
/
speechManager.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
let speechSynthesis = window.speechSynthesis;
let speechWaitList = [];
let speaking = false;
async function handleSpeech()
{
while(true)
{
let line = new SpeechSynthesisUtterance();
if(!speaking && speechWaitList.length)
{
// console.log(speechSynthesis.getVoices());
line.text = speechWaitList[0];
line.voice = speechSynthesis.getVoices()[1]; //Microsoft Zira Desktop - English (United States)
line.rate = 1.8;
// line.pitch = 1;
line.onend = (e) =>
{
// console.log('Finished in ' + event.elapsedTime + ' seconds.');
speechWaitList.shift();
speaking = false;
};
speaking = true;
speechSynthesis.speak(line);
}
else
{
await delay(500);
}
}
};
handleSpeech();
function delay(t, val)
{
return new Promise(
function(resolve)
{
setTimeout( function(){resolve(val);}, t);
}
);
}
module.exports = {
addSpeech(speech)
{
if(speechWaitList.indexOf(speech) == -1) //push only if the speech is not alraeady waiting
speechWaitList.push(speech);
// console.log(speech);
},
}