Voice-to-text dictation app with local (Nvidia Parakeet/Whisper) and cloud models (BYOK). Privacy-first and available cross-platform.
JavaScript aianthropiccross-platformgeminigroqlinuxmacosnvidiaopen-sourceopenaiparakeetspeech-to-texttranscribewhisperwindows
fix(audio): retry on default mic when the pinned deviceId is stale (#978)
Alcahest90c78a391135
diff --git a/src/helpers/audioManager.js b/src/helpers/audioManager.jsindex 3547758fb..80dad2c4f 100644--- a/src/helpers/audioManager.js+++ b/src/helpers/audioManager.js@@ -15,6 +15,7 @@ import {recordLocalSpeechWindow,} from "./localSpeechGate";import { reacquireIfDead } from "./micTrackHealth";+import { isStaleDeviceError } from "./staleMicDevice";import { shouldSaveDiscardedRecording } from "./discardedRecording";import {getSettings,@@ -319,7 +320,7 @@ registerProcessor("pcm-streaming-processor", PCMStreamingProcessor);return this.sttConfig?.streamingProvider || defaultProvider;}- async getAudioConstraints() {+ async getAudioConstraints(forceDefaultMic = false) {const { preferBuiltInMic: preferBuiltIn, selectedMicDeviceId: selectedDeviceId } =getSettings();@@ -334,6 +335,13 @@ registerProcessor("pcm-streaming-processor", PCMStreamingProcessor);channelCount: 2,};+ // Pinned device was unavailable (Chromium rotates IDs / device unplugged); fall back to the+ // system default for this capture without discarding the saved preference. See #900.+ if (forceDefaultMic) {+ logger.debug("Using default microphone (pinned device unavailable)", {}, "audio");+ return { audio: noProcessing };+ }+if (preferBuiltIn) {if (this.cachedMicDeviceId) {logger.debug(@@ -394,13 +402,13 @@ registerProcessor("pcm-streaming-processor", PCMStreamingProcessor);}}- async startRecording() {+ async startRecording(forceDefaultMic = false) {try {if (this.isRecording || this.isProcessing || this.mediaRecorder?.state === "recording") {return false;}- const constraints = await this.getAudioConstraints();+ const constraints = await this.getAudioConstraints(forceDefaultMic);const micStream = await reacquireIfDead(await navigator.mediaDevices.getUserMedia(constraints),() => {@@ -536,6 +544,13 @@ registerProcessor("pcm-streaming-processor", PCMStreamingProcessor);return true;} catch (error) {+ if (isStaleDeviceError(error) && !forceDefaultMic) {+ // Pinned mic is gone (Chromium rotates IDs / device unplugged). Retry once on the default mic. See #900.+ logger.warn("Pinned microphone unavailable, retrying on default mic", {}, "audio");+ this.cachedMicDeviceId = null;+ return this.startRecording(true);+ }+let errorTitle = "Recording Error";let errorDescription = `Failed to access microphone: ${error.message}`;@@ -2472,7 +2487,7 @@ registerProcessor("pcm-streaming-processor", PCMStreamingProcessor);return this.persistentAudioContext;}- async startStreamingRecording() {+ async startStreamingRecording(forceDefaultMic = false) {try {if (this.streamingStartInProgress) {return false;@@ -2487,7 +2502,7 @@ registerProcessor("pcm-streaming-processor", PCMStreamingProcessor);this.stopRequestedDuringStreamingStart = false;const t0 = performance.now();- const constraints = await this.getAudioConstraints();+ const constraints = await this.getAudioConstraints(forceDefaultMic);const tConstraints = performance.now();// 1. Get mic stream (can take 10-15s on cold macOS mic driver)@@ -2693,8 +2708,21 @@ registerProcessor("pcm-streaming-processor", PCMStreamingProcessor);}return true;} catch (error) {+ const stopRequested = this.stopRequestedDuringStreamingStart;this.streamingStartInProgress = false;this.stopRequestedDuringStreamingStart = false;++ if (isStaleDeviceError(error) && !forceDefaultMic && !stopRequested) {+ // Pinned mic is gone (Chromium rotates IDs / device unplugged). Retry once on the default mic. See #900.+ logger.warn("Pinned microphone unavailable, retrying streaming on default mic", {}, "streaming");+ this.cachedMicDeviceId = null;+ await this.cleanupStreaming();+ this.isRecording = false;+ this.recordingStartTime = null;+ this.onStateChange?.({ isRecording: false, isProcessing: false, isStreaming: false });+ return this.startStreamingRecording(true);+ }+logger.error("Failed to start streaming recording", { error: error.message }, "streaming");let errorTitle = "Streaming Error";diff --git a/src/helpers/staleMicDevice.js b/src/helpers/staleMicDevice.jsnew file mode 100644index 000000000..afb8ccdb7--- /dev/null+++ b/src/helpers/staleMicDevice.js@@ -0,0 +1,10 @@+// Detects the OverconstrainedError getUserMedia throws when a saved deviceId no+// longer matches any device. Chromium rotates mediaDevices IDs over time, so a+// pinned { deviceId: { exact } } eventually goes stale and recording fails. See #900.+export function isStaleDeviceError(error) {+ if (!error || error.name !== "OverconstrainedError") return false;+ // deviceId is the only constraint pinned with { exact }; treat an unknown/empty+ // constraint as the device too, but skip a known non-deviceId constraint.+ const constraint = error.constraint;+ return !constraint || constraint === "deviceId";+}diff --git a/test/helpers/staleMicDevice.test.js b/test/helpers/staleMicDevice.test.jsnew file mode 100644index 000000000..40885bf5d--- /dev/null+++ b/test/helpers/staleMicDevice.test.js@@ -0,0 +1,78 @@+const test = require("node:test");+const assert = require("node:assert/strict");++const load = () => import("../../src/helpers/staleMicDevice.js");++test("treats an OverconstrainedError on deviceId as a stale device", async () => {+ const { isStaleDeviceError } = await load();+ assert.equal(+ isStaleDeviceError({ name: "OverconstrainedError", constraint: "deviceId" }),+ true+ );+});++test("treats an OverconstrainedError with an empty constraint as a stale device", async () => {+ // Chromium sometimes reports OverconstrainedError without naming the constraint.+ const { isStaleDeviceError } = await load();+ assert.equal(isStaleDeviceError({ name: "OverconstrainedError", constraint: "" }), true);+});++test("treats an OverconstrainedError with no constraint property as a stale device", async () => {+ const { isStaleDeviceError } = await load();+ assert.equal(isStaleDeviceError({ name: "OverconstrainedError" }), true);+});++test("does not treat an OverconstrainedError on a non-deviceId constraint as stale", async () => {+ // Guards against wiping a valid mic pick when some other exact constraint fails.+ const { isStaleDeviceError } = await load();+ assert.equal(+ isStaleDeviceError({ name: "OverconstrainedError", constraint: "channelCount" }),+ false+ );+});++test("reads constraint from a getter without throwing", async () => {+ const { isStaleDeviceError } = await load();+ const error = {+ name: "OverconstrainedError",+ get constraint() {+ return "deviceId";+ },+ };+ assert.equal(isStaleDeviceError(error), true);+});++test("returns false for NotFoundError", async () => {+ const { isStaleDeviceError } = await load();+ assert.equal(isStaleDeviceError({ name: "NotFoundError" }), false);+});++test("returns false for NotAllowedError", async () => {+ const { isStaleDeviceError } = await load();+ assert.equal(isStaleDeviceError({ name: "NotAllowedError" }), false);+});++test("returns false for NotReadableError (mic in use)", async () => {+ const { isStaleDeviceError } = await load();+ assert.equal(isStaleDeviceError({ name: "NotReadableError" }), false);+});++test("returns false for a generic Error", async () => {+ const { isStaleDeviceError } = await load();+ assert.equal(isStaleDeviceError(new Error("boom")), false);+});++test("returns false for an object without a name", async () => {+ const { isStaleDeviceError } = await load();+ assert.equal(isStaleDeviceError({ constraint: "deviceId" }), false);+});++test("returns false for null", async () => {+ const { isStaleDeviceError } = await load();+ assert.equal(isStaleDeviceError(null), false);+});++test("returns false for undefined", async () => {+ const { isStaleDeviceError } = await load();+ assert.equal(isStaleDeviceError(undefined), false);+});