Skip to content

Voice-to-text dictation app with local (Nvidia Parakeet/Whisper) and cloud models (BYOK). Privacy-first and available cross-platform.

fix(audio): fail speech gate open when its AudioContext never runs (#1125)

Gabriel Steinc6b3d5b19c0f
diff --git a/src/helpers/audioManager.js b/src/helpers/audioManager.js
index c37200c08..294d474a0 100644
--- a/src/helpers/audioManager.js
+++ b/src/helpers/audioManager.js
@@ -484,6 +484,10 @@ registerProcessor("pcm-streaming-processor", PCMStreamingProcessor);
try {
this._silenceCtx = new AudioContext();
+ if (this._silenceCtx.state === "suspended") {
+ // Not awaited — resume() can hang when the output device is wedged.
+ this._silenceCtx.resume().catch(() => {});
+ }
this._silenceAnalyser = this._silenceCtx.createAnalyser();
this._silenceAnalyser.fftSize = 2048;
const sourceNode = this._silenceCtx.createMediaStreamSource(micStream);
@@ -491,6 +495,8 @@ registerProcessor("pcm-streaming-processor", PCMStreamingProcessor);
this._localSpeechGateState = createLocalSpeechGateState();
const dataArray = new Uint8Array(this._silenceAnalyser.fftSize);
this._silenceInterval = setInterval(() => {
+ // A stalled context reads flat silence; recording no windows fails the gate open.
+ if (this._silenceCtx?.state !== "running") return;
this._silenceAnalyser.getByteTimeDomainData(dataArray);
let sum = 0;
let peak = 0;
@@ -522,13 +528,7 @@ registerProcessor("pcm-streaming-processor", PCMStreamingProcessor);
};
this.mediaRecorder.onstop = async () => {
- if (this._silenceInterval) {
- clearInterval(this._silenceInterval);
- this._silenceInterval = null;
- }
- this._silenceCtx?.close().catch(() => {});
- this._silenceCtx = null;
- this._silenceAnalyser = null;
+ this.teardownSpeechGate();
this.cleanupPreview({ showCleanup: this.shouldShowPreviewCleanupState() });
@@ -658,9 +658,22 @@ registerProcessor("pcm-streaming-processor", PCMStreamingProcessor);
return false;
}
+ teardownSpeechGate() {
+ if (this._silenceInterval) {
+ clearInterval(this._silenceInterval);
+ this._silenceInterval = null;
+ }
+ this._silenceCtx?.close().catch(() => {});
+ this._silenceCtx = null;
+ this._silenceAnalyser = null;
+ }
+
cancelRecording() {
if (this.mediaRecorder && this.mediaRecorder.state === "recording") {
this.mediaRecorder.onstop = () => {
+ this.teardownSpeechGate();
+ this._localSpeechGateState = null;
+
const durationSeconds = this.recordingStartTime
? (Date.now() - this.recordingStartTime) / 1000
: null;
diff --git a/test/helpers/localSpeechGate.test.js b/test/helpers/localSpeechGate.test.js
index e523e80ee..6abc92039 100644
--- a/test/helpers/localSpeechGate.test.js
+++ b/test/helpers/localSpeechGate.test.js
@@ -1,6 +1,17 @@
const test = require("node:test");
const assert = require("node:assert/strict");
+test("fails open when no windows were recorded", async () => {
+ const { createLocalSpeechGateState, getLocalSpeechGateDecision } =
+ await import("../../src/helpers/localSpeechGate.js");
+
+ assert.deepEqual(getLocalSpeechGateDecision(createLocalSpeechGateState()), {
+ skip: false,
+ reason: "unavailable",
+ });
+ assert.deepEqual(getLocalSpeechGateDecision(null), { skip: false, reason: "unavailable" });
+});
+
test("treats near silence as skippable", async () => {
const { createLocalSpeechGateState, recordLocalSpeechWindow, getLocalSpeechGateDecision } =
await import("../../src/helpers/localSpeechGate.js");