add Voice control interface #32

Merged
merged 5 commits into from Jul 8, 2016
Jump to file or symbol
Failed to load files and symbols.
+162 −26
Diff settings

Always

Just for now

Viewing a subset of changes. View all

use regex to catch command

  • Loading branch information...
Jeff Zhang Jeff Zhang
Jeff Zhang authored and Jeff Zhang committed Jul 7, 2016
commit f80fe4a905c0c2c52966d72a4faf3df1d1f37f8a
View
@@ -64,7 +64,6 @@
<script src="user_scripts/XAudioJS/resampler.js"></script>
<script src="user_scripts/XAudioJS/XAudioServer.js"></script>
<script src="user_scripts/Interface.js"></script>
- <script src="user_scripts/annyang.min.js"></script>
<script src="user_scripts/VoiceController.js"></script>
<link rel="stylesheet" href="user_css/main.css">
</head>
@@ -2,7 +2,7 @@ var mapButtontoIndex = {"a":0, "b":1, "select":2, "start":3, "right":4, "left":
function pressButton(button, time) {
- if(button in mapButtontoIndex ){
+ if( button in mapButtontoIndex ){
index = mapButtontoIndex[button]
IodineGUI.Iodine.keyDown(index);
@@ -1,32 +1,171 @@
-if(annyang){
-
- pressKeyFunctionConstructor = function(key){
- key = key.toLowerCase()
- var func = function(){
- pressButton(key,20);
- };
- return func;
+// if(annyang){
+
+// pressKeyFunctionConstructor = function(key){
+// key = key.toLowerCase()
+// var func = function(){
+// pressButton(key,20);
+// };
+// return func;
+// }
+
+// handleButton = function(key){
+// //lower case the key to ignore capitals
+// key = key.toLowerCase();
+// pressButton(key, 20);
+// }
+
+
+
+// var commands = {};
+
+// for (var key in mapButtontoIndex){
+// commands[key] = pressKeyFunctionConstructor(key);
+// }
+// commands["press (the) button *key"] = handleButton;
+// commands["press (the) key *key"] = handleButton;
+
+
+
+// // Add our commands to annyang
+// annyang.addCommands(commands);
+// annyang.start();
+// }
+
+var mapButtontoIndex = {"a":0, "b":1, "select":2, "start":3, "right":4, "left": 5, "up":6 ,"down":7, "r":8, "l":9};
+var SpeechRecognition = SpeechRecognition ||
+ webkitSpeechRecognition ||
+ mozSpeechRecognition ||
+ msSpeechRecognition ||
+ oSpeechRecognition;
+
+if (SpeechRecognition){
+ recognition = new SpeechRecognition();
+ recognition.maxAlternatives = 30;
+ recognition.continuous = true;
+ recognition.lang = 'en-US';
+
+ recognition.onstart = function() {
+ console.log("start");
+ };
+
+ recognition.onerror = function(event) {
+ console.log(event);
+ };
+
+ recognition.onend = function() {
+ console.log("end");
+ recognition.start();
+ };
+
+
+ var controlRegex = new RegExp(/(?:press|push)(?:\sthe)?(?:\sbutton|\skey)?\s(\w+)/, 'i');
+ handleButton = function(result) {
+ var match = controlRegex.exec(result);
+ if (match != null){
+ var key = match[1].toLowerCase();
+ if (key in mapButtontoIndex){
+ pressButton(key, 20);
+ return true;
+ }
+ }
+ return false;
}
- handleButton = function(key){
- //lower case the key to ignore capitals
- key = key.toLowerCase();
- pressButton(key, 20);
+ // var fullScreenRegex = new RegExp(/fullscreen|full-screen/, 'i');
+ // handleFullscreen = function(result) {
+ // console.log("fullscreen handler");
+ // if (fullScreenRegex.test(result)){
+ // toggleFullScreen();
+ // return true;
+ // }
+ // return false;
+ // }
+
+ var settingsRegexOne = new RegExp(/(increase|up|decrease|down)(?:\sthe)?\s(volume|speed)(\sby\s(\w+))?/, 'i');
+ var settingsRegexTwo = new RegExp(/(?:set|move|get)(?:\sthe)?\s(volume|speed)(?:\sto)?\s(\w+)/, 'i');
+ handleSettings = function(result) {
+ var matchOne = settingsRegexOne.exec(result);
+ var matchTwo = settingsRegexTwo.exec(result);
+ if(matchOne != null){
+ var action = matchOne[1].toLowerCase();
+ var obj = matchOne[2].toLowerCase();
+ var num = matchOne[4];
+ if( isNaN(num) ){
+ num = 0.1;
+ }
+ else{
+ num = parseInt(num) / 100;
+ }
+ if (action == "increase" || action == "up"){
+ num = num;
+ }
+ else{
+ num = -num;
+ }
+ if (obj == "volume"){
+ stepVolume(num);
+ }
+ else{
+ IodineGUI.Iodine.incrementSpeed(num);
+ }
+ return true;
+ }
+
+ if(matchTwo != null){
+ var obj = matchTwo[1].toLowerCase();
+ var num = matchTwo[2];
+ if( isNaN(num) ){
+ num = 0.1;
+ }
+ else{
+ num = parseInt(num) / 100;
+ }
+ if (obj == "volume"){
+ stepVolume(-1);
+ stepVolume(num);
+ }
+ else{
+ IodineGUI.Iodine.setSpeed(num);
+ }
+ return true;
+ }
+ return false;
}
- var mapButtontoIndex = {"a":0, "b":1, "select":2, "start":3, "right":4, "left": 5, "up":6 ,"down":7, "r":8, "l":9};
+ var RegexFunc = [handleButton, handleSettings];
- var commands = {};
+ handleResults = function(results) {
+ console.log(results);
- for (var key in mapButtontoIndex){
- commands[key] = pressKeyFunctionConstructor(key);
+ var handled = false;
+ for (var k = 0; k < results.length; k++){
+ var result = results[k];
+ for (var funcIndex = 0; funcIndex < RegexFunc.length; funcIndex++){
+ if (RegexFunc[funcIndex](result) ) {
+ handled = true;
+ break;
+ }
+ }
+ if (handled){
+ break;
+ }
+ }
}
- commands["press (the) button *key"] = handleButton;
- commands["press (the) key *key"] = handleButton;
+
+ recognition.onresult = function(event) {
+ console.log('result');
+ var SpeechRecognitionResult = event.results[event.resultIndex];
+ var results = [];
+ for (var k = 0; k<SpeechRecognitionResult.length; k++) {
+ results[k] = SpeechRecognitionResult[k].transcript;
+ }
+ handleResults(results);
+ };
+
+ recognition.start();
+
+}
+
- // Add our commands to annyang
- annyang.addCommands(commands);
- annyang.start();
-}
@@ -259,8 +259,6 @@
results[k] = SpeechRecognitionResult[k].transcript;
}
console.log(results);
- results[0] = "press key a";
- console.log(results);
parseResults(results);
};