From 5ff0fa186247079a6a5ef438a427c4876392e53b Mon Sep 17 00:00:00 2001 From: Benjamin Ver Date: Mon, 28 Sep 2020 14:03:16 +0200 Subject: [PATCH] added buffer for results --- script/resultHandling.js | 131 ++++++++++++++++++++------------------- 1 file changed, 66 insertions(+), 65 deletions(-) diff --git a/script/resultHandling.js b/script/resultHandling.js index 4d7b15c..dfbf9ea 100644 --- a/script/resultHandling.js +++ b/script/resultHandling.js @@ -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'); @@ -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));