scratch-extension-docs/speech_to_text_extension.js

39 lines
1.2 KiB
JavaScript
Raw Normal View History

2014-05-17 00:07:40 -04:00
/* Extension using the JavaScript Speech API for speech to text */
/* Sayamindu Dasgupta <sayamindu@media.mit.edu>, 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);
})();