Skip to content

Commit

Permalink
added buffer for results
Browse files Browse the repository at this point in the history
  • Loading branch information
wasnoplus committed Sep 28, 2020
1 parent a33cdf2 commit 5ff0fa1
Showing 1 changed file with 66 additions and 65 deletions.
131 changes: 66 additions & 65 deletions script/resultHandling.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
const callbacks = [];
const allWords = [];
var eventWordMap = {};
var resultBuffer = true;

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

addToCommands(next, 'navigate-forward');
Expand All @@ -15,85 +16,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);
}
};
// 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);
// }
// };
// }


function resetResultBuffer() {
resultBuffer = true;
}


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);
}
});
}
resultBuffer = false;
setTimeout(resetResultBuffer, 1000)
}


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);
// const handle = debounce(function(result) {
// handleResult(result, sendCommand);
// }, 250, true);

thisRec = new VoskJS.Recognizer(langModel);
thisRec.onresult = (result) => {
console.log({hasStarted})

if (!hasStarted) {
console.log('start');
hasStarted = true;
if (resultBuffer && (result != resultBuffer)) {
resultBuffer = result;
handleResult(result, sendCommand);
}
handle(result);

// handle(result);

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

0 comments on commit 5ff0fa1

Please sign in to comment.