forked from scratchfoundation/scratchx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
speech_to_text_extension.js
39 lines (31 loc) · 1.2 KB
/
speech_to_text_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
/* Extension using the JavaScript Speech API for speech to text */
/* Sayamindu Dasgupta <[email protected]>, April 2014 */
new (function() {
var ext = this;
var recognized_speech = '';
ext.recognize_speech = function (callback) {
var recognition = new webkitSpeechRecognition();
recognition.onresult = function(event) {
if (event.results.length > 0) {
recognized_speech = event.results[0][0].transcript;
if (typeof callback=="function") callback();
}
};
recognition.start();
};
ext.recognized_speech = function () {return recognized_speech;};
ext._shutdown = function() {};
ext._getStatus = function() {
if (window.webkitSpeechRecognition === undefined) {
return {status: 1, msg: 'Your browser does not support speech recognition. Try using Google Chrome.'};
}
return {status: 2, msg: 'Ready'};
};
var descriptor = {
blocks: [
['w', 'wait and recognize speech', 'recognize_speech'],
['r', 'recognized speech', 'recognized_speech']
],
};
ScratchExtensions.register('Speech To Text', descriptor, ext);
})();