-
Notifications
You must be signed in to change notification settings - Fork 0
/
Text 2 Speech.js
40 lines (37 loc) · 916 Bytes
/
Text 2 Speech.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
(function(Scratch) {
'use strict';
class Tools {
getInfo () {
return {
id: 'wpstudio01tts',
name: 'System Text To Speech',
blocks: [
{
opcode: 'speak',
blockType: Scratch.BlockType.COMMAND,
text: 'speak [TEXT]',
arguments: {
TEXT: {
type: Scratch.ArgumentType.STRING,
defaultValue: 'Hello'
}
}
}
]
};
}
speak (args) {
return new Promise((resolve, reject) => {
const utterance = new SpeechSynthesisUtterance(args.TEXT);
utterance.onend = () => {
resolve();
};
utterance.onerror = () => {
reject(new Error('Utterance error'));
};
speechSynthesis.speak(utterance);
});
}
}
Scratch.extensions.register(new Tools());
})(Scratch);