forked from sayamindu/scratch-extensions
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtext_to_speech_extension.js
51 lines (40 loc) · 1.36 KB
/
text_to_speech_extension.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
/* Extension using the JavaScript Speech API for text to speech */
/* Sayamindu Dasgupta <[email protected]>, April 2014 */
new (function() {
var ext = this;
/*function _get_voices() {
var ret = [];
var voices = speechSynthesis.getVoices();
for(var i = 0; i < voices.length; i++ ) {
ret.push(voices[i].name);
console.log(voices.toString());
}
return ret;
}
ext.set_voice = function() {
};*/
ext.speak_text = function (text, callback) {
var u = new SpeechSynthesisUtterance(text.toString());
u.onend = function(event) {
if (typeof callback=="function") callback();
};
speechSynthesis.speak(u);
};
ext._shutdown = function() {};
ext._getStatus = function() {
if (window.SpeechSynthesisUtterance === undefined) {
return {status: 1, msg: 'Your browser does not support text to speech. Try using Google Chrome or Safari.'};
}
return {status: 2, msg: 'Ready'};
};
var descriptor = {
blocks: [
//['', 'set voice to %m.voices', 'set_voice', ''],
['w', 'speak %s', 'speak_text', 'Hello!'],
],
/*menus: {
voices: _get_voices(),
},*/
};
ScratchExtensions.register('Text to Speech', descriptor, ext);
})();