This repository has been archived by the owner on Jul 21, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
speak and listen.xml
107 lines (107 loc) · 18.1 KB
/
speak and listen.xml
1
<project name="speak and listen" app="Snap! 4.0, http://snap.berkeley.edu" version="1"><notes></notes><thumbnail>data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAKAAAAB4CAYAAAB1ovlvAAAGXUlEQVR4Xu2dS0jcVxTGv5koMfgYKFp8a200IiWL1tCubKGKq64K1iQ2wU2EaBeFZiWkVLCL0kALXYUsunRhN5WiG0FQCq1GBbsRxIpvI2rE8dWoU+6FhkjU5IQDk3P7/TfKzPHMOb/v553HZiKJRCIBXiSQJAIRCpgk8nxYT4ACUoSkEjhRwLGxMRQVFSElJSWpw73Mg8/NzaGiogLnz59/mXLWvGYEnhOwv78fJSUlSRtzf2Uac7/9hKPdzTNneDM3H3lN32N3dxePHj3C5cuXkZGRkbS5+cCvRuCYgMPDw0hPT/ed3O9Xrlw51nV5eRm5ublwP93J4+4/qe6sUV5UP/3Nh/io9IJvce6t93H49x+nttv96iFWV1ef3l9VVfVqFPhXSSNwTMCFhQV/mrjr/v37uHPnDu7du4fKykrk5+djYGAAtbW16O3txeTkJOrq6rC2toZoNOrFvXjxIgYHB9HW1oa+vj6UlZX5v3G3u/udLLOzs15id8pOTExgf38f2dnZuHbtGra3t7H0XT0+/uwWojllSGyvIxLLw8HDbqS8+ykQAXDwBPu/fu1njH/5J5aWlvzveXl5vi8vWwSOCTg9Pf30RHnw4AEKCwvR3d2Nzs5OtLa2orm5GZcuXcLIyAjGx8dx/fp1zMzMIDU1FQUFBdjZ2cHo6Cja29u9eB0dHWhoaEA8Hkd5ebmXNi0tzb9mm5+fR05ODhYXF+FOLif2ysoK1n74BHW3v0U0twKHMyNAahqiOW8j+kYxnoz+gkjqBfzT/6On/PiL3+H+adzlZnUz8LJF4JiA7s3H1tYWIhF31ACxWMwLs7e3h6ysLC+NO2mcNO5EdCfWs5f7e3fqFRcX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W8WK1MgAIqA2U7GQEKKOPFamUCFFAZKNvJCFBAGS9WKxOggMpA2U5GgALKeLFamQAFVAbKdjICFFDGi9XKBCigMlC2kxGggDJerFYmQAGVgbKdjAAFlPFitTIBCqgMlO1kBCigjBerlQlQQGWgbCcjQAFlvFitTIACKgNlOxkBCijjxWplAhRQGSjbyQhQQBkvVisToIDKQNlORoACynixWpnAv8d7H8aF7BpuAAAAAElFTkSuQmCC</thumbnail><stage name="Stage" width="480" height="360" costume="0" tempo="60" threadsafe="false" lines="round" codify="false" inheritance="false" sublistIDs="false" scheduled="false" id="1"><pentrails>data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAeAAAAFoCAYAAACPNyggAAAOhUlEQVR4Xu3VwQkAAAjEMN1/abewn7jAQRC64wgQIECAAIF3gX1fNEiAAAECBAiMAHsCAgQIECAQCAhwgG6SAAECBAgIsB8gQIAAAQKBgAAH6CYJECBAgIAA+wECBAgQIBAICHCAbpIAAQIECAiwHyBAgAABAoGAAAfoJgkQIECAgAD7AQIECBAgEAgIcIBukgABAgQICLAfIECAAAECgYAAB+gmCRAgQICAAPsBAgQIECAQCAhwgG6SAAECBAgIsB8gQIAAAQKBgAAH6CYJECBAgIAA+wECBAgQIBAICHCAbpIAAQIECAiwHyBAgAABAoGAAAfoJgkQIECAgAD7AQIECBAgEAgIcIBukgABAgQICLAfIECAAAECgYAAB+gmCRAgQICAAPsBAgQIECAQCAhwgG6SAAECBAgIsB8gQIAAAQKBgAAH6CYJECBAgIAA+wECBAgQIBAICHCAbpIAAQIECAiwHyBAgAABAoGAAAfoJgkQIECAgAD7AQIECBAgEAgIcIBukgABAgQICLAfIECAAAECgYAAB+gmCRAgQICAAPsBAgQIECAQCAhwgG6SAAECBAgIsB8gQIAAAQKBgAAH6CYJECBAgIAA+wECBAgQIBAICHCAbpIAAQIECAiwHyBAgAABAoGAAAfoJgkQIECAgAD7AQIECBAgEAgIcIBukgABAgQICLAfIECAAAECgYAAB+gmCRAgQICAAPsBAgQIECAQCAhwgG6SAAECBAgIsB8gQIAAAQKBgAAH6CYJECBAgIAA+wECBAgQIBAICHCAbpIAAQIECAiwHyBAgAABAoGAAAfoJgkQIECAgAD7AQIECBAgEAgIcIBukgABAgQICLAfIECAAAECgYAAB+gmCRAgQICAAPsBAgQIECAQCAhwgG6SAAECBAgIsB8gQIAAAQKBgAAH6CYJECBAgIAA+wECBAgQIBAICHCAbpIAAQIECAiwHyBAgAABAoGAAAfoJgkQIECAgAD7AQIECBAgEAgIcIBukgABAgQICLAfIECAAAECgYAAB+gmCRAgQICAAPsBAgQIECAQCAhwgG6SAAECBAgIsB8gQIAAAQKBgAAH6CYJECBAgIAA+wECBAgQIBAICHCAbpIAAQIECAiwHyBAgAABAoGAAAfoJgkQIECAgAD7AQIECBAgEAgIcIBukgABAgQICLAfIECAAAECgYAAB+gmCRAgQICAAPsBAgQIECAQCAhwgG6SAAECBAgIsB8gQIAAAQKBgAAH6CYJECBAgIAA+wECBAgQIBAICHCAbpIAAQIECAiwHyBAgAABAoGAAAfoJgkQIECAgAD7AQIECBAgEAgIcIBukgABAgQICLAfIECAAAECgYAAB+gmCRAgQICAAPsBAgQIECAQCAhwgG6SAAECBAgIsB8gQIAAAQKBgAAH6CYJECBAgIAA+wECBAgQIBAICHCAbpIAAQIECAiwHyBAgAABAoGAAAfoJgkQIECAgAD7AQIECBAgEAgIcIBukgABAgQICLAfIECAAAECgYAAB+gmCRAgQICAAPsBAgQIECAQCAhwgG6SAAECBAgIsB8gQIAAAQKBgAAH6CYJECBAgIAA+wECBAgQIBAICHCAbpIAAQIECAiwHyBAgAABAoGAAAfoJgkQIECAgAD7AQIECBAgEAgIcIBukgABAgQICLAfIECAAAECgYAAB+gmCRAgQICAAPsBAgQIECAQCAhwgG6SAAECBAgIsB8gQIAAAQKBgAAH6CYJECBAgIAA+wECBAgQIBAICHCAbpIAAQIECAiwHyBAgAABAoGAAAfoJgkQIECAgAD7AQIECBAgEAgIcIBukgABAgQICLAfIECAAAECgY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BukgABAgQICLAfIECAAAECgYAAB+gmCRAgQICAAPsBAgQIECAQCAhwgG6SAAECBAgIsB8gQIAAAQKBgAAH6CYJECBAgIAA+wECBAgQIBAICHCAbpIAAQIECAiwHyBAgAABAoGAAAfoJgkQIECAgAD7AQIECBAgEAgIcIBukgABAgQICLAfIECAAAECgYAAB+gmCRAgQICAAPsBAgQIECAQCAhwgG6SAAECBAgIsB8gQIAAAQKBgAAH6CYJECBAgIAA+wECBAgQIBAICHCAbpIAAQIECAiwHyBAgAABAoGAAAfoJgkQIECAgAD7AQIECBAgEAgIcIBukgABAgQICLAfIECAAAECgYAAB+gmCRAgQICAAPsBAgQIECAQCAhwgG6SAAECBAgIsB8gQIAAAQKBgAAH6CYJECBAgIAA+wECBAgQIBAICHCAbpIAAQIECAiwHyBAgAABAoGAAAfoJgkQIECAgAD7AQIECBAgEAgIcIBukgABAgQICLAfIECAAAECgYAAB+gmCRAgQICAAPsBAgQIECAQCAhwgG6SAAECBAgIsB8gQIAAAQKBgAAH6CYJECBAgIAA+wECBAgQIBAICHCAbpIAAQIECAiwHyBAgAABAoGAAAfoJgkQIECAgAD7AQIECBAgEAgIcIBukgABAgQICLAfIECAAAECgYAAB+gmCRAgQICAAPsBAgQIECAQCAhwgG6SAAECBAgIsB8gQIAAAQKBgAAH6CYJECBAgIAA+wECBAgQIBAICHCAbpIAAQIECAiwHyBAgAABAoGAAAfoJgkQIECAgAD7AQIECBAgEAgIcIBukgABAgQICLAfIECAAAECgYAAB+gmCRAgQICAAPsBAgQIECAQCAhwgG6SAAECBAgIsB8gQIAAAQKBgAAH6CYJECBAgIAA+wECBAgQIBAICHCAbpIAAQIECAiwHyBAgAABAoGAAAfoJgkQIECAgAD7AQIECBAgEAgIcIBukgABAgQICLAfIECAAAECgYAAB+gmCRAgQICAAPsBAgQIECAQCAhwgG6SAAECBAgIsB8gQIAAAQKBgAAH6CYJECBAgIAA+wECBAgQIBAICHCAbpIAAQIECAiwHyBAgAABAoGAAAfoJgkQIECAgAD7AQIECBAgEAgIcIBukgABAgQICLAfIECAAAECgYAAB+gmCRAgQICAAPsBAgQIECAQCAhwgG6SAAECBAgIsB8gQIAAAQKBgAAH6CYJECBAgIAA+wECBAgQIBAICHCAbpIAAQIECAiwHyBAgAABAoGAAAfoJgkQIECAgAD7AQIECBAgEAgIcIBukgABAgQICLAfIECAAAECgYAAB+gmCRAgQICAAPsBAgQIECAQCAhwgG6SAAECBAgIsB8gQIAAAQKBgAAH6CYJECBAgIAA+wECBAgQIBAICHCAbpIAAQIECAiwHyBAgAABAoGAAAfoJgkQIECAgAD7AQIECBAgEAgIcIBukgABAgQICLAfIECAAAECgYAAB+gmCRAgQICAAPsBAgQIECAQCAhwgG6SAAECBAgIsB8gQIAAAQKBgAAH6CYJECBAgIAA+wECBAgQIBAICHCAbpIAAQIECAiwHyBAgAABAoGAAAfoJgkQIECAgAD7AQIECBAgEAgIcIBukgABAgQICLAfIECAAAECgYAAB+gmCRAgQICAAPsBAgQIECAQCAhwgG6SAAECBAgIsB8gQIAAAQKBgAAH6CYJECBAgIAA+wECBAgQIBAICHCAbpIAAQIECAiwHyBAgAABAoGAAAfoJgkQIECAgAD7AQIECBAgEAgIcIBukgABAgQICLAfIECAAAECgYAAB+gmCRAgQICAAPsBAgQIECAQCAhwgG6SAAECBAgIsB8gQIAAAQKBgAAH6CYJECBAgIAA+wECBAgQIBAICHCAbpIAAQIECAiwHyBAgAABAoGAAAfoJgkQIECAgAD7AQIECBAgEAgIcIBukgABAgQIHLFxAWmhEwHPAAAAAElFTkSuQmCC</pentrails><costumes><list id="2"></list></costumes><sounds><list id="3"></list></sounds><variables></variables><blocks></blocks><scripts></scripts><sprites><sprite name="Sprite" idx="1" x="0" y="0" heading="90" scale="1" rotation="1" draggable="true" costume="0" color="80,80,80" pen="tip" id="8"><costumes><list id="9"></list></costumes><sounds><list id="10"></list></sounds><variables></variables><blocks></blocks><scripts><script x="21" y="18"><block s="receiveGo"></block><custom-block s="speak %s then %cmdRing in language %s with pitch %n with rate %n with voice %n with volume %n"><l>Say something</l><block s="reifyScript"><script><custom-block s="listen"></custom-block></script><list></list></block><l></l><l></l><l></l><l></l><l></l></custom-block></script></scripts></sprite><watcher var="last thing spoken" style="normal" x="10" y="10" color="243,118,29"/><watcher var="listening" style="normal" x="10" y="31.000001999999995" color="243,118,29" hidden="true"/></sprites></stage><hidden></hidden><headers></headers><code></code><blocks><block-definition s="speak %'text' %'what to do when finished'" type="command" category="sound"><header></header><code></code><inputs><input type="%s"></input><input type="%cmdRing"></input></inputs><script><block s="doRun"><block s="reportJSFunction"><list><l>message</l><l>finished_callback</l></list><l>var utterance = new SpeechSynthesisUtterance(message);





if (typeof finished_callback === 'object') {





 // callback provided





 utterance.onend = function (event) {





 invoke(finished_callback, new List([message]));





 };





}





if (window.speech_recognition) {





 window.speech_recognition.abort();





}





window.speechSynthesis.speak(utterance);





</l></block><list><block var="text"/><block var="what to do when finished"/></list></block></script></block-definition><block-definition s="listen and then %'do with recognised words' %'do if nothing recognised'" type="command" category="other"><header></header><code></code><inputs><input type="%cmdRing"></input><input type="%cmdRing"></input></inputs><script><block s="doRun"><block s="reportJSFunction"><list><l>spoken_callback</l><l>error_callback</l></list><l>var restart = function () {





 if (window.speechSynthesis.speaking) { // don't listen while speaking





 // there remains the possibility of speech happening after recognition starts





 setTimeout(restart, 500); // try again in half a second





 return;





 }





 try {





 window.speech_recognition.start();





 console.log("recognition started");





 } catch (error) {





 if (error.name === 'InvalidStateError') {





 // delay needed at least in Chrome 52





 setTimeout(restart, 2000);





 } else {





 console.log(error);





 }





 }





};





var handle_result = function (callback, event) {





 var spoken = event.results[0][0].transcript;





 console.log("Confidence is " + event.results[0][0].confidence + " for " + spoken, spoken_callback);





 window.speech_recognition.stop();





 invoke(callback, new List([spoken]));





};





var handle_error = function (callback, event) {





 if (event.error === 'aborted') {





 console.log("aborted so restarting speech recognition in half a second");





 setTimeout(restart, 500);





 return;





 }





 if (event.error === 'no-speech') {





 window.speech_recognition.onend = null;





 window.speech_recognition.onresult = null;





 window.speech_recognition.stop();





 }





 console.log("Recognition error: " + event.error);





 if (typeof callback === 'object') {





 invoke(callback, new List([event.error]));





 }





};





if (!window.speech_recognition) {





 window.speech_recognition = (typeof SpeechRecognition === 'undefined') ? 





 new webkitSpeechRecognition() :





 new SpeechRecognition();





}





window.speech_recognition.onresult = function (event) {





 handle_result(spoken_callback, event);





};





window.speech_recognition.onerror = function (event) {





 handle_error(error_callback, event);





};





window.speech_recognition.onend = function (event) {





 console.log("recognition ended");





 restart(); 





};





restart();





</l></block><list><block var="do with recognised words"/><block var="do if nothing recognised"/></list></block></script></block-definition><block-definition s="listen" type="command" category="other"><header></header><code></code><inputs></inputs><script><custom-block s="Turn off unloading warnings"></custom-block><block s="doSetVar"><l>listening</l><block s="reportBoolean"><l><bool>true</bool></l></block></block><custom-block s="listen and then %cmdRing %cmdRing"><block s="reifyScript"><script><block s="doSetVar"><l>last thing spoken</l><block var="spoken"/></block><block s="doBroadcast"><l>heard something</l></block><custom-block s="listen"></custom-block></script><list><l>spoken</l></list></block><block s="reifyScript"><script><block s="doSetVar"><l>speech recognition error</l><block var="error"/></block><block s="doBroadcast"><l>speech recognition error</l></block><block s="doIf"><block var="listening"/><script><block s="doIfElse"><block s="reportEquals"><block var="speech recognition error"/><l>no-speech</l></block><script><custom-block s="speak %s %cmdRing"><l>I didn't hear anything for a while. Bye.</l><block s="reifyScript"><script></script><list></list></block></custom-block><block s="doSetVar"><l>listening</l><block s="reportBoolean"><l><bool>false</bool></l></block></block></script><script><custom-block s="speak %s %cmdRing"><block s="reportJoinWords"><list><l>There was an error. </l><block var="speech recognition error"/></list></block><block s="reifyScript"><script></script><list></list></block></custom-block><block s="bubble"><block s="reportJoinWords"><list><l>There was an error. </l><block var="speech recognition error"/></list></block></block><custom-block s="speak %s %cmdRing"><l>Try again.</l><block s="reifyScript"><script><custom-block s="listen"></custom-block></script><list></list></block></custom-block></script></block></script></block></script><list><l>error</l></list></block></custom-block></script></block-definition><block-definition s="Turn off unloading warnings" type="command" category="other"><header></header><code></code><inputs></inputs><script><block s="doRun"><block s="reportJSFunction"><list></list><l>window.onbeforeunload = null;</l></block><list></list></block></script></block-definition><block-definition s="speak %'text' then %'what to do when finished' in language %'language code' with pitch %'pitch' with rate %'rate' with voice %'voice' with volume %'volume'" type="command" category="sound"><header></header><code></code><inputs><input type="%s"></input><input type="%cmdRing"></input><input type="%s"></input><input type="%n"></input><input type="%n"></input><input type="%n"></input><input type="%n"></input></inputs><script><block s="doRun"><block s="reportJSFunction"><list><l>message</l><l>finished_callback</l><l>lang</l><l>pitch</l><l>rate</l><l>voice</l><l>volume</l></list><l>// see https://developer.mozilla.org/en-US/docs/Web/API/SpeechSynthesisUtterance
var utterance = new SpeechSynthesisUtterance(message);
if (lang) {
 utterance.lang = lang;
}
if (pitch > 0) {
 utterance.pitch = pitch;
}
if (rate > 0) {
 if (rate < .1) {
 // A very slow rate breaks Chrome's speech synthesiser
 rate = .1;
 }
 if (rate > 2) {
 rate = 2; // high rate also breaks Chrome's speech synthesis
 }
 utterance.rate = rate;
}
if (voice) {
 voices = window.speechSynthesis.getVoices();
 if (voice < voices.length) {
 utterance.voice = voices[voice];
 } else {
 alert("Only " + voices.length + " voices are available so you can't choose voice number " + voice);
 }
}
if (volume > 0) {
 utterance.volume = volume;
}
if (typeof finished_callback === 'object') {
 // callback provided
 utterance.onend = function (event) {
 invoke(finished_callback, new List([message]));
 };
}
if (window.speech_recognition) {
 window.speech_recognition.abort();
}
window.speechSynthesis.speak(utterance);
</l></block><list><block var="text"/><block var="what to do when finished"/><block var="language code"/><block var="pitch"/><block var="rate"/><block var="voice"/><block var="volume"/></list></block></script></block-definition></blocks><variables><variable name="last thing spoken"><l>okay</l></variable><variable name="listening"><bool>true</bool></variable></variables></project>