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
/
Copy pathspeak.xml
6 lines (6 loc) · 2.93 KB
/
speak.xml
1
<blocks app="Snap! 4.0, http://snap.berkeley.edu" version="1"><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 >= 0 && voice < voices.length) {
 utterance.voice = voices[Math.floor(voice)];
 } else {
 alert("Only " + voices.length + " voices are available. 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><block-definition s="voice name %'voice'" type="reporter" category="sound"><header></header><code></code><inputs><input type="%n"></input></inputs><script><block s="doReport"><block s="evaluate"><block s="reportJSFunction"><list><l>voice</l></list><l>var voices = window.speechSynthesis.getVoices();




if (voice >= 0 && voice < voices.length) {




 return voices[Math.floor(voice)].name;




} else {




 alert("Only " + voices.length + " voices are available. You can't choose voice number " + voice);




}</l></block><list><block var="voice"/></list></block></block></script></block-definition></blocks>