Skip to content

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

feat(audio): add microphone input selection with built-in mic preference to prevent Bluetooth audio interruptions

Gabriel Steinb36c73ffbf45
diff --git a/src/components/SettingsPage.tsx b/src/components/SettingsPage.tsx
index 04966ab83..1fe64b8b9 100644
--- a/src/components/SettingsPage.tsx
+++ b/src/components/SettingsPage.tsx
@@ -4,6 +4,7 @@ import { Input } from "./ui/input";
import { RefreshCw, Download, Command, Mic, Shield } from "lucide-react";
import MarkdownRenderer from "./ui/MarkdownRenderer";
import MicPermissionWarning from "./ui/MicPermissionWarning";
+import MicrophoneSettings from "./ui/MicrophoneSettings";
import TranscriptionModelPicker from "./TranscriptionModelPicker";
import { ConfirmDialog, AlertDialog } from "./ui/dialog";
import { useSettings } from "../hooks/useSettings";
@@ -63,6 +64,10 @@ export default function SettingsPage({ activeSection = "general" }: SettingsPage
geminiApiKey,
groqApiKey,
dictationKey,
+ preferBuiltInMic,
+ selectedMicDeviceId,
+ setPreferBuiltInMic,
+ setSelectedMicDeviceId,
setUseLocalWhisper,
setWhisperModel,
setAllowOpenAIFallback,
@@ -626,6 +631,22 @@ export default function SettingsPage({ activeSection = "general" }: SettingsPage
</div>
</div>
+ <div className="border-t pt-8">
+ <div>
+ <h3 className="text-lg font-semibold text-gray-900 mb-2">Microphone Input</h3>
+ <p className="text-sm text-gray-600 mb-6">
+ Choose which microphone to use for dictation. Enable "Prefer Built-in" to prevent
+ audio interruptions when using Bluetooth headphones.
+ </p>
+ </div>
+ <MicrophoneSettings
+ preferBuiltInMic={preferBuiltInMic}
+ selectedMicDeviceId={selectedMicDeviceId}
+ onPreferBuiltInChange={setPreferBuiltInMic}
+ onDeviceSelect={setSelectedMicDeviceId}
+ />
+ </div>
+
<div className="border-t pt-8">
<div>
<h3 className="text-lg font-semibold text-gray-900 mb-2">About OpenWhispr</h3>
diff --git a/src/components/ui/MicrophoneSettings.tsx b/src/components/ui/MicrophoneSettings.tsx
new file mode 100644
index 000000000..a0b2fc458
--- /dev/null
+++ b/src/components/ui/MicrophoneSettings.tsx
@@ -0,0 +1,172 @@
+import React, { useState, useEffect, useCallback, useRef } from "react";
+import { Toggle } from "./toggle";
+import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "./select";
+import { Button } from "./button";
+import { RefreshCw, Mic } from "lucide-react";
+import { isBuiltInMicrophone } from "../../utils/audioDeviceUtils";
+
+interface AudioDevice {
+ deviceId: string;
+ label: string;
+ isBuiltIn: boolean;
+}
+
+interface MicrophoneSettingsProps {
+ preferBuiltInMic: boolean;
+ selectedMicDeviceId: string;
+ onPreferBuiltInChange: (value: boolean) => void;
+ onDeviceSelect: (deviceId: string) => void;
+}
+
+export const MicrophoneSettings: React.FC<MicrophoneSettingsProps> = ({
+ preferBuiltInMic,
+ selectedMicDeviceId,
+ onPreferBuiltInChange,
+ onDeviceSelect,
+}) => {
+ const [devices, setDevices] = useState<AudioDevice[]>([]);
+ const [isLoading, setIsLoading] = useState(false);
+ const [error, setError] = useState<string | null>(null);
+
+ // Use refs to access current values without triggering re-renders
+ const preferBuiltInRef = useRef(preferBuiltInMic);
+ const selectedDeviceRef = useRef(selectedMicDeviceId);
+ const onDeviceSelectRef = useRef(onDeviceSelect);
+
+ // Keep refs in sync
+ useEffect(() => {
+ preferBuiltInRef.current = preferBuiltInMic;
+ selectedDeviceRef.current = selectedMicDeviceId;
+ onDeviceSelectRef.current = onDeviceSelect;
+ }, [preferBuiltInMic, selectedMicDeviceId, onDeviceSelect]);
+
+ const loadDevices = useCallback(async () => {
+ setIsLoading(true);
+ setError(null);
+
+ try {
+ // Request permission first to get device labels
+ const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
+ stream.getTracks().forEach((track) => track.stop());
+
+ const allDevices = await navigator.mediaDevices.enumerateDevices();
+ const audioInputs = allDevices
+ .filter((d) => d.kind === "audioinput")
+ .map((d) => ({
+ deviceId: d.deviceId,
+ label: d.label || `Microphone ${d.deviceId.slice(0, 8)}`,
+ isBuiltIn: isBuiltInMicrophone(d.label),
+ }));
+
+ setDevices(audioInputs);
+
+ // If no device is selected and not preferring built-in, select the first device
+ if (!preferBuiltInRef.current && !selectedDeviceRef.current && audioInputs.length > 0) {
+ onDeviceSelectRef.current(audioInputs[0].deviceId);
+ }
+ } catch {
+ setError("Unable to access microphone. Please check permissions.");
+ } finally {
+ setIsLoading(false);
+ }
+ }, []);
+
+ useEffect(() => {
+ loadDevices();
+
+ const handleDeviceChange = () => loadDevices();
+ navigator.mediaDevices.addEventListener("devicechange", handleDeviceChange);
+
+ return () => {
+ navigator.mediaDevices.removeEventListener("devicechange", handleDeviceChange);
+ };
+ }, [loadDevices]);
+
+ const builtInDevice = devices.find((d) => d.isBuiltIn);
+ const selectedDevice = devices.find((d) => d.deviceId === selectedMicDeviceId);
+
+ return (
+ <div className="space-y-4">
+ <div className="flex items-center justify-between p-4 bg-neutral-50 rounded-lg">
+ <div className="flex-1">
+ <p className="text-sm font-medium text-neutral-800">Prefer Built-in Microphone</p>
+ <p className="text-xs text-neutral-600 mt-1">
+ Always use your device's built-in mic to prevent audio interruptions when using
+ Bluetooth headphones
+ </p>
+ </div>
+ <Toggle checked={preferBuiltInMic} onChange={onPreferBuiltInChange} />
+ </div>
+
+ {preferBuiltInMic && builtInDevice && (
+ <div className="p-3 bg-green-50 border border-green-200 rounded-lg">
+ <div className="flex items-center gap-2">
+ <Mic className="w-4 h-4 text-green-600" />
+ <span className="text-sm text-green-800">
+ Using: <span className="font-medium">{builtInDevice.label}</span>
+ </span>
+ </div>
+ </div>
+ )}
+
+ {preferBuiltInMic && !builtInDevice && devices.length > 0 && (
+ <div className="p-3 bg-amber-50 border border-amber-200 rounded-lg">
+ <p className="text-sm text-amber-800">
+ No built-in microphone detected. Using system default.
+ </p>
+ </div>
+ )}
+
+ {!preferBuiltInMic && (
+ <div className="space-y-3">
+ <div className="flex items-center gap-2">
+ <label className="text-sm font-medium text-neutral-700">Input Device</label>
+ <Button
+ variant="ghost"
+ size="sm"
+ onClick={loadDevices}
+ disabled={isLoading}
+ className="h-7 w-7 p-0"
+ >
+ <RefreshCw className={`w-3.5 h-3.5 ${isLoading ? "animate-spin" : ""}`} />
+ </Button>
+ </div>
+
+ {error ? (
+ <p className="text-sm text-red-600">{error}</p>
+ ) : (
+ <Select
+ value={selectedMicDeviceId || "default"}
+ onValueChange={(value) => onDeviceSelect(value === "default" ? "" : value)}
+ >
+ <SelectTrigger className="w-full">
+ <SelectValue placeholder="Select a microphone">
+ {selectedMicDeviceId
+ ? selectedDevice?.label || "Unknown Device"
+ : "System Default"}
+ </SelectValue>
+ </SelectTrigger>
+ <SelectContent>
+ <SelectItem value="default">System Default</SelectItem>
+ {devices.map((device) => (
+ <SelectItem key={device.deviceId} value={device.deviceId}>
+ {device.label}
+ {device.isBuiltIn && (
+ <span className="ml-2 text-xs text-neutral-500">(Built-in)</span>
+ )}
+ </SelectItem>
+ ))}
+ </SelectContent>
+ </Select>
+ )}
+
+ <p className="text-xs text-neutral-500">
+ Select a specific microphone or use the system default setting.
+ </p>
+ </div>
+ )}
+ </div>
+ );
+};
+
+export default MicrophoneSettings;
diff --git a/src/helpers/audioManager.js b/src/helpers/audioManager.js
index 58b96de50..439b94521 100644
--- a/src/helpers/audioManager.js
+++ b/src/helpers/audioManager.js
@@ -1,6 +1,7 @@
import ReasoningService from "../services/ReasoningService";
import { API_ENDPOINTS, buildApiUrl, normalizeBaseUrl } from "../config/constants";
import logger from "../utils/logger";
+import { isBuiltInMicrophone } from "../utils/audioDeviceUtils";
const SHORT_CLIP_DURATION_SECONDS = 2.5;
const REASONING_CACHE_TTL = 30000; // 30 seconds
@@ -41,13 +42,52 @@ class AudioManager {
this.onTranscriptionComplete = onTranscriptionComplete;
}
+ async getAudioConstraints() {
+ const preferBuiltIn = localStorage.getItem("preferBuiltInMic") !== "false";
+ const selectedDeviceId = localStorage.getItem("selectedMicDeviceId") || "";
+
+ if (preferBuiltIn) {
+ try {
+ const devices = await navigator.mediaDevices.enumerateDevices();
+ const audioInputs = devices.filter((d) => d.kind === "audioinput");
+ const builtInMic = audioInputs.find((d) => isBuiltInMicrophone(d.label));
+
+ if (builtInMic) {
+ logger.debug(
+ "Using built-in microphone",
+ { deviceId: builtInMic.deviceId, label: builtInMic.label },
+ "audio"
+ );
+ return { audio: { deviceId: { exact: builtInMic.deviceId } } };
+ }
+ } catch (error) {
+ logger.debug(
+ "Failed to enumerate devices for built-in mic detection",
+ { error: error.message },
+ "audio"
+ );
+ }
+ }
+
+ // Use selected device if specified and not preferring built-in
+ if (!preferBuiltIn && selectedDeviceId) {
+ logger.debug("Using selected microphone", { deviceId: selectedDeviceId }, "audio");
+ return { audio: { deviceId: { exact: selectedDeviceId } } };
+ }
+
+ // Fall back to default device
+ logger.debug("Using default microphone", {}, "audio");
+ return { audio: true };
+ }
+
async startRecording() {
try {
if (this.isRecording || this.isProcessing || this.mediaRecorder?.state === "recording") {
return false;
}
- const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
+ const constraints = await this.getAudioConstraints();
+ const stream = await navigator.mediaDevices.getUserMedia(constraints);
this.mediaRecorder = new MediaRecorder(stream);
this.audioChunks = [];
diff --git a/src/hooks/useSettings.ts b/src/hooks/useSettings.ts
index 415bb4c15..2d56669e8 100644
--- a/src/hooks/useSettings.ts
+++ b/src/hooks/useSettings.ts
@@ -26,6 +26,11 @@ export interface HotkeySettings {
dictationKey: string;
}
+export interface MicrophoneSettings {
+ preferBuiltInMic: boolean;
+ selectedMicDeviceId: string;
+}
+
export interface ApiKeySettings {
openaiApiKey: string;
anthropicApiKey: string;
@@ -146,6 +151,17 @@ export function useSettings() {
deserialize: String,
});
+ // Microphone settings
+ const [preferBuiltInMic, setPreferBuiltInMic] = useLocalStorage("preferBuiltInMic", true, {
+ serialize: String,
+ deserialize: (value) => value !== "false",
+ });
+
+ const [selectedMicDeviceId, setSelectedMicDeviceId] = useLocalStorage("selectedMicDeviceId", "", {
+ serialize: String,
+ deserialize: String,
+ });
+
// Computed values
const reasoningProvider = getModelProvider(reasoningModel);
@@ -245,6 +261,10 @@ export function useSettings() {
setGeminiApiKey,
setGroqApiKey,
setDictationKey,
+ preferBuiltInMic,
+ selectedMicDeviceId,
+ setPreferBuiltInMic,
+ setSelectedMicDeviceId,
updateTranscriptionSettings,
updateReasoningSettings,
updateApiKeys,
diff --git a/src/utils/audioDeviceUtils.ts b/src/utils/audioDeviceUtils.ts
new file mode 100644
index 000000000..4dc5133b9
--- /dev/null
+++ b/src/utils/audioDeviceUtils.ts
@@ -0,0 +1,38 @@
+/**
+ * Utility functions for audio device detection and management.
+ * Shared between renderer components and audio manager.
+ */
+
+/**
+ * Determines if a microphone device is a built-in device based on its label.
+ * Works across macOS, Windows, and Linux platforms.
+ */
+export function isBuiltInMicrophone(label: string): boolean {
+ const lowerLabel = label.toLowerCase();
+
+ // Direct built-in indicators
+ if (
+ lowerLabel.includes("built-in") ||
+ lowerLabel.includes("internal") ||
+ lowerLabel.includes("macbook") ||
+ lowerLabel.includes("integrated")
+ ) {
+ return true;
+ }
+
+ // Generic "microphone" without external device indicators
+ if (lowerLabel.includes("microphone")) {
+ const externalIndicators = [
+ "bluetooth",
+ "airpods",
+ "wireless",
+ "usb",
+ "external",
+ "headset",
+ "webcam",
+ ];
+ return !externalIndicators.some((indicator) => lowerLabel.includes(indicator));
+ }
+
+ return false;
+}