Skip to content

Instantly share code, notes, and snippets.

@csf30816
Created July 21, 2016 21:39
Show Gist options
  • Save csf30816/555e77bc93dbd14c4d0889e0959ab673 to your computer and use it in GitHub Desktop.
Save csf30816/555e77bc93dbd14c4d0889e0959ab673 to your computer and use it in GitHub Desktop.
javascript: 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);
})();
new(function() {
var ext = this;
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: [
['w', 'speak %s', 'speak_text', 'Hello!'],
],
};
ScratchExtensions.register('Text to Speech', descriptor, ext);
})();
new(function() {
var ext = this;
ext._shutdown = function() {};
ext._getStatus = function() {
return {
status: 2,
msg: 'Ready'
};
};
ext.trueBlock = function() {
return true;
};
var descriptor = {
blocks: [
['b', 'true', 'trueBlock'],
]
};
ScratchExtensions.register('Cool helpful things extension', descriptor, ext);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment