Skip to content

Commit

Permalink
1. Hop
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonVanneste committed Sep 28, 2020
1 parent 4dd2f78 commit a33cdf2
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 54 deletions.
2 changes: 1 addition & 1 deletion script/redEn.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const langModel = "model-en.tar.gz";

const next = ["next"];
const next = ["next", 'mixed'];
const prev = ["back", "bag", "beck", "previous", "preview"];
127 changes: 74 additions & 53 deletions script/resultHandling.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
const callbacks = [];
const allWords = []
var eventWordMap;

function addToCommands (words, command) {
words.forEach(function(w) {
eventWordMap[w] = command;
allWords.push(w);
})
const allWords = [];
var eventWordMap = {};

function addToCommands(words, command) {
words.forEach(function(w) {
eventWordMap[w] = command;
allWords.push(w);
});
}

addToCommands(next, 'navigate-forward');
Expand All @@ -15,64 +15,85 @@ addToCommands(prev, 'navigate-backward');

window.iridium = {};
window.iridium.onExternalNavigate = (callback) => {
callbacks.push(callback);
callbacks.push(callback);

return () => {
const callbackIndex = callbacks.indexOf(callback);
callbacks.splice(callbackIndex, 1);
};
return () => {
const callbackIndex = callbacks.indexOf(callback);
callbacks.splice(callbackIndex, 1);
};
};


// source: https://davidwalsh.name/javascript-debounce-function
function debounce(func, wait, immediate) {
var timeout;
return function() {
var context = this, args = arguments;
var later = function() {
timeout = null;
if (!immediate) func.apply(context, args);
};
var callNow = immediate && !timeout;
clearTimeout(timeout);
timeout = setTimeout(later, wait);
if (callNow) func.apply(context, args);
};
};
var timeout;

return function() {
var context = this, args = arguments;
var later = function() {
timeout = null;
if (!immediate) {
func.apply(context, args);
}
};
var callNow = immediate && !timeout;
clearTimeout(timeout);
timeout = setTimeout(later, wait);
if (callNow) {
func.apply(context, args);
}
};
}


function handleResult(res, handleIt, reset=null) {
if (typeof reset == "function") {
reset();
}
var wordHandled = false;
if (typeof res.result != undefined) {
// words = res.result;
res.result.forEach((x) => {
if (allWords.includes(x.word)) {
wordHandled = true;
handleIt(x.word, x.conf);
}
});
}
if (typeof reset === 'function') {
reset();
}
var wordHandled = false;
if (typeof res.result !== "undefined") {
// words = res.result;
res.result.forEach((x) => {
if (allWords.includes(x.word)) {
wordHandled = true;
handleIt(x.word, x.conf);
}
});
}
}


function sendCommand(w, c) {
if (c >= .5) {
const action = eventWordMap[w];
console.log(`${w} detected with confidence ${c}: action ${action}`);
for (const callback of callbacks) {
callback(action);
}
}
// if (c >= .5) {
const action = eventWordMap[w];
console.log(`${w} detected with confidence ${c}: action ${action}`);
for (const callback of callbacks) {
callback(action);
}
// }
}

let hasStarted = false;

const handle = debounce(function(result) {
handleResult(result, sendCommand);
console.log({result});
console.log('stop');
hasStarted = false;
}, 250, true);

thisRec = new VoskJS.Recognizer(langModel);
thisRec.onresult = result => {
debounce(function() { handleResult(result, sendCommand) }, 200);
// if (result.result) {
// handleResult(result, sendCommand);
// }
}
thisRec.getActive().then(active => thisRec.setActive(!active));
thisRec.onresult = (result) => {
console.log({hasStarted})

if (!hasStarted) {
console.log('start');
hasStarted = true;
}
handle(result);

// if (result.result) {
// handleResult(result, sendCommand);
// }
};
thisRec.getActive().then((active) => thisRec.setActive(!active));

0 comments on commit a33cdf2

Please sign in to comment.