diff --git a/web/src/App.tsx b/web/src/App.tsx index 3a4a27f..c65683a 100644 --- a/web/src/App.tsx +++ b/web/src/App.tsx @@ -136,6 +136,7 @@ const INPUT_DEVICE_KEY = "webspeak3:input-device"; const PLAYBACK_VOLUME_KEY = "webspeak3:playback-volume"; const NOISE_SUPPRESSION_KEY = "webspeak3:noise-suppression"; const ECHO_CANCELLATION_KEY = "webspeak3:echo-cancellation"; +const AUTO_GAIN_CONTROL_KEY = "webspeak3:auto-gain-control"; const VAD_HANGOVER_KEY = "webspeak3:vad-hangover"; const DESIGN_THEME_KEY = "webspeak3:design-theme"; // The user's actual pick - "standard" | "nova" | "custom:". DESIGN_THEME_KEY above @@ -3397,6 +3398,8 @@ interface AudioSettings { onToggleNoiseSuppression: () => void; echoCancellationEnabled: boolean; onToggleEchoCancellation: () => void; + autoGainControlEnabled: boolean; + onToggleAutoGainControl: () => void; } function MicLevelBar({ levelRef, active }: { levelRef: React.MutableRefObject; active: boolean }) { @@ -3587,6 +3590,14 @@ function AufnahmePanel({ audio }: { audio: AudioSettings }) { /> {t("recording.noiseSuppression")} + @@ -4555,6 +4566,9 @@ function AppInner() { const [echoCancellationEnabled, setEchoCancellationEnabled] = useState(() => loadBoolPref(ECHO_CANCELLATION_KEY, true) ); + const [autoGainControlEnabled, setAutoGainControlEnabled] = useState(() => + loadBoolPref(AUTO_GAIN_CONTROL_KEY, true) + ); const [micTestOn, setMicTestOn] = useState(false); const [connectionsMenuOpen, setConnectionsMenuOpen] = useState(false); // Nova-theme-only: collapses the classic menu items behind a hamburger button. @@ -4714,6 +4728,10 @@ function AppInner() { localStorage.setItem(ECHO_CANCELLATION_KEY, echoCancellationEnabled ? "1" : "0"); }, [echoCancellationEnabled]); + useEffect(() => { + localStorage.setItem(AUTO_GAIN_CONTROL_KEY, autoGainControlEnabled ? "1" : "0"); + }, [autoGainControlEnabled]); + useEffect(() => { if (inputDeviceId) localStorage.setItem(INPUT_DEVICE_KEY, inputDeviceId); }, [inputDeviceId]); @@ -5526,6 +5544,7 @@ function AppInner() { deviceId?: string; echoCancellation?: boolean; noiseSuppression?: boolean; + autoGainControl?: boolean; }) => { const audioContext = ensureAudioContext(); try { @@ -5542,6 +5561,7 @@ function AppInner() { deviceId: overrides?.deviceId ?? (inputDeviceId || undefined), echoCancellation: overrides?.echoCancellation ?? echoCancellationEnabled, noiseSuppression: overrides?.noiseSuppression ?? noiseSuppressionEnabled, + autoGainControl: overrides?.autoGainControl ?? autoGainControlEnabled, }); await mic.start(); micCaptureRef.current = mic; @@ -5558,6 +5578,7 @@ function AppInner() { deviceId?: string; echoCancellation?: boolean; noiseSuppression?: boolean; + autoGainControl?: boolean; }) => { micCaptureRef.current?.stop(); micCaptureRef.current = null; @@ -5599,6 +5620,12 @@ function AppInner() { if (micCaptureRef.current) void restartMic({ echoCancellation: next }); }; + const handleToggleAutoGainControl = () => { + const next = !autoGainControlEnabled; + setAutoGainControlEnabled(next); + if (micCaptureRef.current) void restartMic({ autoGainControl: next }); + }; + const handleToggleMicTest = () => { const next = !micTestOn; setMicTestOn(next); @@ -7309,6 +7336,8 @@ function AppInner() { onToggleNoiseSuppression: handleToggleNoiseSuppression, echoCancellationEnabled, onToggleEchoCancellation: handleToggleEchoCancellation, + autoGainControlEnabled, + onToggleAutoGainControl: handleToggleAutoGainControl, }} /> )} diff --git a/web/src/i18n.tsx b/web/src/i18n.tsx index b438777..c47f6d9 100644 --- a/web/src/i18n.tsx +++ b/web/src/i18n.tsx @@ -291,6 +291,7 @@ const translations: Record> = { "recording.typingAttenuation": "Typing attenuation", "recording.echoCancellation": "Echo Dämpfung", "recording.noiseSuppression": "Hintergrundgeräusche entfernen", + "recording.autoGainControl": "Automatische Verstärkung", "info.address": "Adresse:", "info.version": "Version:", @@ -813,6 +814,7 @@ const translations: Record> = { "recording.typingAttenuation": "Typing attenuation", "recording.echoCancellation": "Echo cancellation", "recording.noiseSuppression": "Remove background noise", + "recording.autoGainControl": "Automatic gain control", "info.address": "Address:", "info.version": "Version:", diff --git a/web/src/voice.ts b/web/src/voice.ts index d444fc5..f451a6c 100644 --- a/web/src/voice.ts +++ b/web/src/voice.ts @@ -36,6 +36,11 @@ export interface MicCaptureOptions { deviceId?: string; echoCancellation?: boolean; noiseSuppression?: boolean; + /** Automatic gain control. Exposed as its own toggle (rather than folded into + * echoCancellation) because AGC re-amplifying the mic signal after the + * browser's own AEC runs can bring back audible echo residue in speaker+mic + * (no headset) setups - some users get a cleaner result with it off. */ + autoGainControl?: boolean; } /** @@ -61,6 +66,7 @@ export class MicCapture { private deviceId?: string; private echoCancellation: boolean; private noiseSuppression: boolean; + private autoGainControl: boolean; private context: AudioContext; threshold: number; hangoverSeconds: number; @@ -75,6 +81,7 @@ export class MicCapture { this.deviceId = options.deviceId; this.echoCancellation = options.echoCancellation ?? true; this.noiseSuppression = options.noiseSuppression ?? true; + this.autoGainControl = options.autoGainControl ?? true; } async start(): Promise { @@ -84,8 +91,15 @@ export class MicCapture { this.stream = await navigator.mediaDevices.getUserMedia({ audio: { channelCount: 1, + // Requesting the pipeline's own rate (rather than leaving it to the + // device/OS default) avoids an extra internal resampling step ahead + // of the browser's own echo canceller - standard constraint, honored + // by both Chromium and Firefox, "ideal" so it's not fatal if a device + // genuinely can't provide it. + sampleRate: { ideal: SAMPLE_RATE }, echoCancellation: this.echoCancellation, noiseSuppression: this.noiseSuppression, + autoGainControl: this.autoGainControl, ...(this.deviceId ? { deviceId: { exact: this.deviceId } } : {}), }, });