From a33cdf2ffede2e2330ce938a4300097c1a0e58c6 Mon Sep 17 00:00:00 2001 From: Simon Vanneste Date: Mon, 28 Sep 2020 13:42:13 +0200 Subject: [PATCH] 1. Hop --- script/redEn.js | 2 +- script/resultHandling.js | 127 +++++++++++++++++++++++---------------- 2 files changed, 75 insertions(+), 54 deletions(-) diff --git a/script/redEn.js b/script/redEn.js index 6cc882c..22d363a 100644 --- a/script/redEn.js +++ b/script/redEn.js @@ -1,4 +1,4 @@ const langModel = "model-en.tar.gz"; -const next = ["next"]; +const next = ["next", 'mixed']; const prev = ["back", "bag", "beck", "previous", "preview"]; \ No newline at end of file diff --git a/script/resultHandling.js b/script/resultHandling.js index 005e9a8..4d7b15c 100644 --- a/script/resultHandling.js +++ b/script/resultHandling.js @@ -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'); @@ -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)); \ No newline at end of file +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));