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): fall back to the default mic when the preferred device stays muted (#1204)
Alcahest Malus Dienheimd5b95d642942
diff --git a/src/helpers/audioManager.js b/src/helpers/audioManager.jsindex 72ab4dad9..1d9ebb580 100644--- a/src/helpers/audioManager.js+++ b/src/helpers/audioManager.js@@ -292,6 +292,7 @@ class AudioManager {this.cachedMicDeviceId = null;this.validatedSelectedMicDeviceId = null;this.micDriverWarmedUp = false;+ this.rejectedMicDeviceId = null;};navigator.mediaDevices?.addEventListener?.("devicechange", this._onDeviceChange);this.cachedTranscriptionEndpoint = null;@@ -312,6 +313,7 @@ class AudioManager {this.streamingTextDebounce = null;this.cachedMicDeviceId = null;this.validatedSelectedMicDeviceId = null;+ this.rejectedMicDeviceId = null;this.persistentAudioContext = null;this.workletModuleLoaded = false;this.workletBlobUrl = null;@@ -479,6 +481,16 @@ registerProcessor("pcm-streaming-processor", PCMStreamingProcessor);if (preferBuiltIn) {if (this.cachedMicDeviceId) {+ // The device was already proven silent this session; don't pin it again.+ if (this.cachedMicDeviceId === this.rejectedMicDeviceId) {+ logger.debug(+ "Skipping cached microphone (delivered no audio)",+ { deviceId: this.cachedMicDeviceId },+ "audio"+ );+ return { audio: noProcessing };+ }+logger.debug("Using cached microphone device ID",{ deviceId: this.cachedMicDeviceId },@@ -493,6 +505,16 @@ registerProcessor("pcm-streaming-processor", PCMStreamingProcessor);const builtInMic = audioInputs.find((d) => isBuiltInMicrophone(d.label));if (builtInMic) {+ // Leave it uncached so a later devicechange can re-resolve it cleanly.+ if (builtInMic.deviceId === this.rejectedMicDeviceId) {+ logger.debug(+ "Skipping built-in microphone (delivered no audio)",+ { deviceId: builtInMic.deviceId, label: builtInMic.label },+ "audio"+ );+ return { audio: noProcessing };+ }+this.cachedMicDeviceId = builtInMic.deviceId;logger.debug("Using built-in microphone (cached for next time)",@@ -540,6 +562,15 @@ registerProcessor("pcm-streaming-processor", PCMStreamingProcessor);}}+ if (resolvedDeviceId === this.rejectedMicDeviceId) {+ logger.debug(+ "Skipping selected microphone (delivered no audio)",+ { deviceId: resolvedDeviceId },+ "audio"+ );+ return { audio: noProcessing };+ }+logger.debug("Using selected microphone", { deviceId: resolvedDeviceId }, "audio");return { audio: { deviceId: { exact: resolvedDeviceId }, ...noProcessing } };}@@ -583,6 +614,44 @@ registerProcessor("pcm-streaming-processor", PCMStreamingProcessor);}}+ // Recovers a dead/muted capture: retries the same device, then hops to the OS default,+ // remembering a silent pinned device for the session. Throws MicUnusableError when no+ // input delivers audio. See #1152.+ async acquireHealthyMicStream(rawStream, constraints) {+ const pinnedMicDeviceId = constraints.audio?.deviceId?.exact ?? null;+ let fallbackMicUnusable = false;+ // Keep verifying after a rejection too, otherwise a muted default records silence unnoticed.+ const verifyMic = pinnedMicDeviceId !== null || this.rejectedMicDeviceId !== null;+ const stream = await reacquireIfDead(+ rawStream,+ () => {+ this.cachedMicDeviceId = null;+ return this.getAudioConstraints();+ },+ logger,+ verifyMic+ ? {+ getConstraints: () => this.getAudioConstraints(true),+ onDeviceRejected: () => {+ if (pinnedMicDeviceId) this.rejectedMicDeviceId = pinnedMicDeviceId;+ },+ onFallbackUnusable: () => {+ fallbackMicUnusable = true;+ },+ }+ : null+ );++ if (fallbackMicUnusable) {+ stream.getTracks().forEach((track) => track.stop());+ const micError = new Error("No microphone is delivering audio");+ micError.name = "MicUnusableError";+ throw micError;+ }++ return stream;+ }+async startRecording(forceDefaultMic = false) {try {if (this.isRecording || this.isProcessing || this.mediaRecorder?.state === "recording") {@@ -590,14 +659,11 @@ registerProcessor("pcm-streaming-processor", PCMStreamingProcessor);}const constraints = await this.getAudioConstraints(forceDefaultMic);- const micStream = await reacquireIfDead(+ const micStream = await this.acquireHealthyMicStream(await navigator.mediaDevices.getUserMedia(constraints),- () => {- this.cachedMicDeviceId = null;- return this.getAudioConstraints();- },- logger+ constraints);+const audioTrack = micStream.getAudioTracks()[0];if (audioTrack) {@@ -774,6 +840,10 @@ registerProcessor("pcm-streaming-processor", PCMStreamingProcessor);errorTitle = "Microphone In Use";errorDescription ="The microphone is being used by another application. Please close other apps and try again.";+ } else if (error.name === "MicUnusableError") {+ errorTitle = "Microphone Muted";+ errorDescription =+ "Your microphones stayed muted and produced no audio. Please check your sound input settings and try again.";}this.onError?.({@@ -2958,14 +3028,8 @@ registerProcessor("pcm-streaming-processor", PCMStreamingProcessor);const rawStream = await navigator.mediaDevices.getUserMedia(constraints);const tMedia = performance.now();- const stream = await reacquireIfDead(- rawStream,- () => {- this.cachedMicDeviceId = null;- return this.getAudioConstraints();- },- logger- );+ const stream = await this.acquireHealthyMicStream(rawStream, constraints);+const audioTrack = stream.getAudioTracks()[0];if (audioTrack) {@@ -3194,6 +3258,10 @@ registerProcessor("pcm-streaming-processor", PCMStreamingProcessor);} else if (error.code === "NETWORK_ERROR") {errorTitle = "streaming.errors.cloudUnreachable.title";errorDescription = error.messageKey || "streaming.errors.cloudUnreachable.generic";+ } else if (error.name === "MicUnusableError") {+ errorTitle = "Microphone Muted";+ errorDescription =+ "Your microphones stayed muted and produced no audio. Please check your sound input settings and try again.";}this.onError?.({diff --git a/src/helpers/micTrackHealth.js b/src/helpers/micTrackHealth.jsindex 401cddaef..3cc05dddb 100644--- a/src/helpers/micTrackHealth.js+++ b/src/helpers/micTrackHealth.js@@ -57,23 +57,61 @@ export const waitForTrackReady = (track, timeoutMs) =>// Re-acquires the mic once if the first track never delivers audio (dead/muted after idle).// getFreshConstraints must drop any pinned device id so the retry re-resolves the device.// Returns a live stream — the original when it was healthy or the retry failed.-export const reacquireIfDead = async (stream, getFreshConstraints, logger) => {+// Optional fallback = { getConstraints, onDeviceRejected, onFallbackUnusable } adds a second+// hop to the system default when the retry reopens the same device and it is still silent.+export const reacquireIfDead = async (stream, getFreshConstraints, logger, fallback = null) => {const track = stream.getAudioTracks()[0];if (!track || (await waitForTrackReady(track, TRACK_READY_TIMEOUT_MS))) {return stream;}+ // The stream we still own and must stop if a later hop replaces it.+ let current = stream;+ let retryStream = null;try {- const retryStream = await navigator.mediaDevices.getUserMedia(await getFreshConstraints());+ retryStream = await navigator.mediaDevices.getUserMedia(await getFreshConstraints());stream.getTracks().forEach((t) => t.stop());logger.info("Re-acquired microphone after dead/muted track", {}, "audio");- return retryStream;+ current = retryStream;} catch (error) {logger.warn(- "Microphone re-acquire failed, using original stream",+ fallback+ ? "Microphone re-acquire failed, trying the default mic"+ : "Microphone re-acquire failed, using original stream",{ error: error.message },"audio");- return stream;+ // A retry that cannot even open the device counts as proof it is unusable.+ if (!fallback) return stream;+ }++ if (!fallback) return retryStream;++ // The retry usually resolves back to the same device; a healthy track means it just woke up.+ if (retryStream) {+ const retryTrack = retryStream.getAudioTracks()[0];+ if (retryTrack && (await waitForTrackReady(retryTrack, TRACK_READY_TIMEOUT_MS))) {+ return retryStream;+ }+ }++ fallback.onDeviceRejected();++ let fallbackStream;+ try {+ fallbackStream = await navigator.mediaDevices.getUserMedia(await fallback.getConstraints());+ } catch (error) {+ logger.warn("Microphone fallback failed, no usable input", { error: error.message }, "audio");+ fallback.onFallbackUnusable();+ return current;+ }++ current.getTracks().forEach((t) => t.stop());+ logger.info("Fell back to the default microphone after a silent device", {}, "audio");++ const fallbackTrack = fallbackStream.getAudioTracks()[0];+ if (!fallbackTrack || !(await waitForTrackReady(fallbackTrack, TRACK_READY_TIMEOUT_MS))) {+ fallback.onFallbackUnusable();}+ return fallbackStream;};diff --git a/test/helpers/micTrackHealth.test.js b/test/helpers/micTrackHealth.test.jsindex 17d665efb..30edb7086 100644--- a/test/helpers/micTrackHealth.test.js+++ b/test/helpers/micTrackHealth.test.js@@ -186,3 +186,185 @@ test("reacquireIfDead falls back to the original stream when the retry fails", arestore();}});++// Track that records whether (and how often) reacquireIfDead stopped it.+function trackWithStop(options) {+ const track = new FakeTrack(options);+ track._stopCount = 0;+ track.stop = () => {+ track._stopCount += 1;+ track._stopped = true;+ };+ return track;+}++// Fallback options plus call counters, mirroring what audioManager passes in.+function spyFallback(getConstraints = () => ({})) {+ const calls = { getConstraints: 0, onDeviceRejected: 0, onFallbackUnusable: 0 };+ return {+ calls,+ options: {+ getConstraints: () => {+ calls.getConstraints += 1;+ return getConstraints();+ },+ onDeviceRejected: () => {+ calls.onDeviceRejected += 1;+ },+ onFallbackUnusable: () => {+ calls.onFallbackUnusable += 1;+ },+ },+ };+}++test("reacquireIfDead keeps the re-acquired stream when the retry track woke up", async () => {+ const { reacquireIfDead } = await import("../../src/helpers/micTrackHealth.js");+ const stream = new FakeStream(trackWithStop({ readyState: "ended" }));+ const retry = new FakeStream(trackWithStop({ muted: false }));+ const fallback = spyFallback();+ const restore = stubGetUserMedia(async () => retry);+ try {+ const result = await reacquireIfDead(stream, () => ({}), noopLogger, fallback.options);+ assert.equal(result, retry);+ assert.equal(fallback.calls.getConstraints, 0);+ assert.equal(fallback.calls.onDeviceRejected, 0);+ assert.equal(fallback.calls.onFallbackUnusable, 0);+ assert.equal(retry.track._stopped, undefined);+ } finally {+ restore();+ }+});++test("reacquireIfDead falls back to the default mic when the retry track is still dead", async () => {+ const { reacquireIfDead } = await import("../../src/helpers/micTrackHealth.js");+ const stream = new FakeStream(trackWithStop({ readyState: "ended" }));+ const retry = new FakeStream(trackWithStop({ readyState: "ended" }));+ const fallbackStream = new FakeStream(trackWithStop({ muted: false }));+ const fallback = spyFallback();+ let calls = 0;+ const restore = stubGetUserMedia(async () => {+ calls += 1;+ return calls === 1 ? retry : fallbackStream;+ });+ try {+ const result = await reacquireIfDead(stream, () => ({}), noopLogger, fallback.options);+ assert.equal(result, fallbackStream);+ assert.equal(fallback.calls.onDeviceRejected, 1);+ assert.equal(fallback.calls.getConstraints, 1);+ assert.equal(fallback.calls.onFallbackUnusable, 0);+ assert.equal(retry.track._stopped, true);+ } finally {+ restore();+ }+});++test("reacquireIfDead reports an unusable mic when the fallback track is dead too", async () => {+ const { reacquireIfDead } = await import("../../src/helpers/micTrackHealth.js");+ const stream = new FakeStream(trackWithStop({ readyState: "ended" }));+ const retry = new FakeStream(trackWithStop({ readyState: "ended" }));+ const fallbackStream = new FakeStream(trackWithStop({ readyState: "ended" }));+ const fallback = spyFallback();+ let calls = 0;+ const restore = stubGetUserMedia(async () => {+ calls += 1;+ return calls === 1 ? retry : fallbackStream;+ });+ try {+ const result = await reacquireIfDead(stream, () => ({}), noopLogger, fallback.options);+ assert.equal(result, fallbackStream);+ assert.equal(fallback.calls.onDeviceRejected, 1);+ assert.equal(fallback.calls.onFallbackUnusable, 1);+ } finally {+ restore();+ }+});++test("reacquireIfDead reports an unusable mic when the fallback request fails", async () => {+ const { reacquireIfDead } = await import("../../src/helpers/micTrackHealth.js");+ const stream = new FakeStream(trackWithStop({ readyState: "ended" }));+ const retry = new FakeStream(trackWithStop({ readyState: "ended" }));+ const fallback = spyFallback();+ let calls = 0;+ const restore = stubGetUserMedia(async () => {+ calls += 1;+ if (calls === 1) return retry;+ throw new Error("device busy");+ });+ try {+ const result = await reacquireIfDead(stream, () => ({}), noopLogger, fallback.options);+ assert.equal(result, retry);+ assert.equal(fallback.calls.onDeviceRejected, 1);+ assert.equal(fallback.calls.onFallbackUnusable, 1);+ assert.equal(retry.track._stopped, undefined);+ } finally {+ restore();+ }+});++test("reacquireIfDead falls back to the default mic when the retry request fails", async () => {+ const { reacquireIfDead } = await import("../../src/helpers/micTrackHealth.js");+ const stream = new FakeStream(trackWithStop({ readyState: "ended" }));+ const fallbackStream = new FakeStream(trackWithStop({ muted: false }));+ const fallback = spyFallback();+ let calls = 0;+ const restore = stubGetUserMedia(async () => {+ calls += 1;+ if (calls === 1) throw new Error("device busy");+ return fallbackStream;+ });+ try {+ const result = await reacquireIfDead(stream, () => ({}), noopLogger, fallback.options);+ assert.equal(result, fallbackStream);+ assert.equal(fallback.calls.onDeviceRejected, 1);+ assert.equal(fallback.calls.onFallbackUnusable, 0);+ assert.equal(stream.track._stopCount, 1);+ } finally {+ restore();+ }+});++test("reacquireIfDead reports an unusable mic when the retry and the fallback both fail", async () => {+ const { reacquireIfDead } = await import("../../src/helpers/micTrackHealth.js");+ const stream = new FakeStream(trackWithStop({ readyState: "ended" }));+ const fallback = spyFallback();+ const restore = stubGetUserMedia(async () => {+ throw new Error("device busy");+ });+ try {+ const result = await reacquireIfDead(stream, () => ({}), noopLogger, fallback.options);+ assert.equal(result, stream);+ assert.equal(fallback.calls.onDeviceRejected, 1);+ assert.equal(fallback.calls.onFallbackUnusable, 1);+ assert.equal(stream.track._stopCount, 0);+ } finally {+ restore();+ }+});++test("reacquireIfDead falls back when the re-acquired track stays muted", async (t) => {+ const { reacquireIfDead } = await import("../../src/helpers/micTrackHealth.js");+ t.mock.timers.enable({ apis: ["setTimeout"] });+ const stream = new FakeStream(trackWithStop({ readyState: "ended" }));+ const retry = new FakeStream(trackWithStop({ muted: true }));+ const fallbackStream = new FakeStream(trackWithStop({ muted: false }));+ const fallback = spyFallback();+ let calls = 0;+ const restore = stubGetUserMedia(async () => {+ calls += 1;+ return calls === 1 ? retry : fallbackStream;+ });+ try {+ const pending = reacquireIfDead(stream, () => ({}), noopLogger, fallback.options);+ // Let the retry's readiness timer register before firing it.+ await new Promise((resolve) => setImmediate(resolve));+ t.mock.timers.tick(600);+ assert.equal(await pending, fallbackStream);+ assert.equal(fallback.calls.onDeviceRejected, 1);+ assert.equal(fallback.calls.onFallbackUnusable, 0);+ assert.equal(retry.track._stopped, true);+ assert.equal(retry.track._listenerCount, 0);+ } finally {+ restore();+ }+});