From 12038f70ad74f64a1c784b6b7d8b91439a46f55c Mon Sep 17 00:00:00 2001 From: Ian Bicking Date: Thu, 19 Sep 2019 14:40:15 -0500 Subject: [PATCH] Fix #162, avoid error message when you don't speak This adds onProcessing and onNoVoice callbacks. This also restores displaying a progress indicator when the voice is recorded and being sent to the server. --- extension/popup/popup.js | 7 +++++++ extension/popup/vad.js | 12 +++++++++++- extension/popup/voice.js | 15 +++++++++++++++ extension/popup/voiceShim.js | 11 ++++++++++- extension/recorder/recorder.js | 24 ++++++++++++++++++++++++ 5 files changed, 67 insertions(+), 2 deletions(-) diff --git a/extension/popup/popup.js b/extension/popup/popup.js index ea3ce1e7e..c5050a422 100644 --- a/extension/popup/popup.js +++ b/extension/popup/popup.js @@ -116,6 +116,13 @@ this.popup = (function() { ui.setState("error"); clearInterval(intervalId); }; + recorder.onProcessing = () => { + ui.setState("processing"); + }; + recorder.onNoVoice = () => { + log.debug("Closing popup because of no voice input"); + window.close(); + }; recorder.startRecording(); } diff --git a/extension/popup/vad.js b/extension/popup/vad.js index 2c25935cd..c5e151c1b 100644 --- a/extension/popup/vad.js +++ b/extension/popup/vad.js @@ -12,6 +12,14 @@ this.vad = (function() { exports.stm_vad; exports.stm_vad_ready = util.makeNakedPromise(); + exports.onProcessing = () => { + // Can be overridden + }; + + exports.onNoVoice = () => { + // Can be overridden + }; + exports.SpeakToMeVad = class SpeakToMeVad { constructor() { this.webrtc_main = Module.cwrap("main"); @@ -157,10 +165,12 @@ this.vad = (function() { this.stopGum(); // FIXME: maybe we need to signal the UI here? if (why === "GoCloud finishedvoice") { + exports.onProcessing(); if (typeof ui !== "undefined") { - // FIXME: needs updating for shim ui.setState("processing"); // TODO: send a message through voice.js to popup.js to ui.js to set the processing state } + } else if (why === "Raise novoice") { + exports.onNoVoice(); } } }; diff --git a/extension/popup/voice.js b/extension/popup/voice.js index 163d9d7e4..5bb75aa74 100644 --- a/extension/popup/voice.js +++ b/extension/popup/voice.js @@ -54,6 +54,13 @@ this.voice = (function() { this.sourceNode.disconnect(this.analyzerNode); this.analyzerNode.disconnect(this.outputNode); }; + // FIXME: this is a bad pattern, but all I got for now... + vad.onProcessing = () => { + this.onProcessing(); + }; + vad.onNoVoice = () => { + this.onNoVoice(); + }; // connect stream to our recorder this.sourceNode.connect(this.scriptprocessor); // MediaRecorder initialization @@ -94,6 +101,14 @@ this.voice = (function() { // Can be overridden } + onProcessing() { + // Can be overridden + } + + onNoVoice() { + // Can be overridden + } + /** Returns 0.0-1.0, based on our estimation of volume */ getVolumeLevel() { const MIN_DB_LEVEL = -85; // The dB level that is 0 in the levels display diff --git a/extension/popup/voiceShim.js b/extension/popup/voiceShim.js index 7782b88c3..47d6df74b 100644 --- a/extension/popup/voiceShim.js +++ b/extension/popup/voiceShim.js @@ -60,6 +60,13 @@ this.voiceShim = (function() { // override } + onProcessing(exception) { + // override + } + onNoVoice(exception) { + // override + } + getVolumeLevel() { browser.runtime .sendMessage({ @@ -85,7 +92,9 @@ this.voiceShim = (function() { const args = message.args || []; if ( activeRecorder._cancelled && - ["onEnd", "onError"].includes(message.method) + ["onEnd", "onError", "onProcessing", "onNoVoice"].includes( + message.method + ) ) { return null; } diff --git a/extension/recorder/recorder.js b/extension/recorder/recorder.js index 5b7414a28..82dd898c0 100644 --- a/extension/recorder/recorder.js +++ b/extension/recorder/recorder.js @@ -103,6 +103,30 @@ this.recorder = (function() { } } + onProcessing() { + if (this._destroyed) { + log.error("onProcessing called after ShimRecorder destroyed"); + } else { + browser.runtime.sendMessage({ + type: "onVoiceShimForward", + method: "onProcessing", + args: [], + }); + } + } + + onNoVoice() { + if (this._destroyed) { + log.error("onNoVoice called after ShimRecorder destroyed"); + } else { + browser.runtime.sendMessage({ + type: "onVoiceShimForward", + method: "onNoVoice", + args: [], + }); + } + } + destroy() { this._destroyed = true; }