Add auto gain control toggle for echo cancellation tuning (#2)

Exposes autoGainControl as its own Options -> Aufnahme toggle (was
implicitly left to the browser default) and requests the pipeline's own
48kHz sample rate explicitly, so the mic doesn't need an extra internal
resample before the browser's own AEC runs. Both are standard
getUserMedia constraints, honored the same way by Chromium and Firefox
(unlike vendor-prefixed googEchoCancellation-style flags, which only
affect Chromium).

Helps speaker+mic (no headset) setups where AGC re-amplifying the mic
signal after AEC can bring back audible echo residue - see #2.
This commit is contained in:
Moepchi committed 2026-08-29 04:29:04 +02:00
1 parent 24f070ce67
commit f490ca5776
3 files changed
+45

No files matched your search

+29
View File
@@ -136,6 +136,7 @@ const INPUT_DEVICE_KEY = "webspeak3:input-device";
const PLAYBACK_VOLUME_KEY = "webspeak3:playback-volume"; const PLAYBACK_VOLUME_KEY = "webspeak3:playback-volume";
const NOISE_SUPPRESSION_KEY = "webspeak3:noise-suppression"; const NOISE_SUPPRESSION_KEY = "webspeak3:noise-suppression";
const ECHO_CANCELLATION_KEY = "webspeak3:echo-cancellation"; const ECHO_CANCELLATION_KEY = "webspeak3:echo-cancellation";
const AUTO_GAIN_CONTROL_KEY = "webspeak3:auto-gain-control";
const VAD_HANGOVER_KEY = "webspeak3:vad-hangover"; const VAD_HANGOVER_KEY = "webspeak3:vad-hangover";
const DESIGN_THEME_KEY = "webspeak3:design-theme"; const DESIGN_THEME_KEY = "webspeak3:design-theme";
// The user's actual pick - "standard" | "nova" | "custom:<id>". DESIGN_THEME_KEY above // The user's actual pick - "standard" | "nova" | "custom:<id>". DESIGN_THEME_KEY above
@@ -3397,6 +3398,8 @@ interface AudioSettings {
onToggleNoiseSuppression: () => void; onToggleNoiseSuppression: () => void;
echoCancellationEnabled: boolean; echoCancellationEnabled: boolean;
onToggleEchoCancellation: () => void; onToggleEchoCancellation: () => void;
autoGainControlEnabled: boolean;
onToggleAutoGainControl: () => void;
} }
function MicLevelBar({ levelRef, active }: { levelRef: React.MutableRefObject<number>; active: boolean }) { function MicLevelBar({ levelRef, active }: { levelRef: React.MutableRefObject<number>; active: boolean }) {
@@ -3587,6 +3590,14 @@ function AufnahmePanel({ audio }: { audio: AudioSettings }) {
/> />
{t("recording.noiseSuppression")} {t("recording.noiseSuppression")}
</label> </label>
<label className="ts-options-checkbox">
<input
type="checkbox"
checked={audio.autoGainControlEnabled}
onChange={audio.onToggleAutoGainControl}
/>
{t("recording.autoGainControl")}
</label>
</div> </div>
</fieldset> </fieldset>
</div> </div>
@@ -4555,6 +4566,9 @@ function AppInner() {
const [echoCancellationEnabled, setEchoCancellationEnabled] = useState(() => const [echoCancellationEnabled, setEchoCancellationEnabled] = useState(() =>
loadBoolPref(ECHO_CANCELLATION_KEY, true) loadBoolPref(ECHO_CANCELLATION_KEY, true)
); );
const [autoGainControlEnabled, setAutoGainControlEnabled] = useState(() =>
loadBoolPref(AUTO_GAIN_CONTROL_KEY, true)
);
const [micTestOn, setMicTestOn] = useState(false); const [micTestOn, setMicTestOn] = useState(false);
const [connectionsMenuOpen, setConnectionsMenuOpen] = useState(false); const [connectionsMenuOpen, setConnectionsMenuOpen] = useState(false);
// Nova-theme-only: collapses the classic menu items behind a hamburger button. // 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"); localStorage.setItem(ECHO_CANCELLATION_KEY, echoCancellationEnabled ? "1" : "0");
}, [echoCancellationEnabled]); }, [echoCancellationEnabled]);
useEffect(() => {
localStorage.setItem(AUTO_GAIN_CONTROL_KEY, autoGainControlEnabled ? "1" : "0");
}, [autoGainControlEnabled]);
useEffect(() => { useEffect(() => {
if (inputDeviceId) localStorage.setItem(INPUT_DEVICE_KEY, inputDeviceId); if (inputDeviceId) localStorage.setItem(INPUT_DEVICE_KEY, inputDeviceId);
}, [inputDeviceId]); }, [inputDeviceId]);
@@ -5526,6 +5544,7 @@ function AppInner() {
deviceId?: string; deviceId?: string;
echoCancellation?: boolean; echoCancellation?: boolean;
noiseSuppression?: boolean; noiseSuppression?: boolean;
autoGainControl?: boolean;
}) => { }) => {
const audioContext = ensureAudioContext(); const audioContext = ensureAudioContext();
try { try {
@@ -5542,6 +5561,7 @@ function AppInner() {
deviceId: overrides?.deviceId ?? (inputDeviceId || undefined), deviceId: overrides?.deviceId ?? (inputDeviceId || undefined),
echoCancellation: overrides?.echoCancellation ?? echoCancellationEnabled, echoCancellation: overrides?.echoCancellation ?? echoCancellationEnabled,
noiseSuppression: overrides?.noiseSuppression ?? noiseSuppressionEnabled, noiseSuppression: overrides?.noiseSuppression ?? noiseSuppressionEnabled,
autoGainControl: overrides?.autoGainControl ?? autoGainControlEnabled,
}); });
await mic.start(); await mic.start();
micCaptureRef.current = mic; micCaptureRef.current = mic;
@@ -5558,6 +5578,7 @@ function AppInner() {
deviceId?: string; deviceId?: string;
echoCancellation?: boolean; echoCancellation?: boolean;
noiseSuppression?: boolean; noiseSuppression?: boolean;
autoGainControl?: boolean;
}) => { }) => {
micCaptureRef.current?.stop(); micCaptureRef.current?.stop();
micCaptureRef.current = null; micCaptureRef.current = null;
@@ -5599,6 +5620,12 @@ function AppInner() {
if (micCaptureRef.current) void restartMic({ echoCancellation: next }); if (micCaptureRef.current) void restartMic({ echoCancellation: next });
}; };
const handleToggleAutoGainControl = () => {
const next = !autoGainControlEnabled;
setAutoGainControlEnabled(next);
if (micCaptureRef.current) void restartMic({ autoGainControl: next });
};
const handleToggleMicTest = () => { const handleToggleMicTest = () => {
const next = !micTestOn; const next = !micTestOn;
setMicTestOn(next); setMicTestOn(next);
@@ -7309,6 +7336,8 @@ function AppInner() {
onToggleNoiseSuppression: handleToggleNoiseSuppression, onToggleNoiseSuppression: handleToggleNoiseSuppression,
echoCancellationEnabled, echoCancellationEnabled,
onToggleEchoCancellation: handleToggleEchoCancellation, onToggleEchoCancellation: handleToggleEchoCancellation,
autoGainControlEnabled,
onToggleAutoGainControl: handleToggleAutoGainControl,
}} }}
/> />
)} )}
+2
View File
@@ -291,6 +291,7 @@ const translations: Record<Lang, Record<string, string>> = {
"recording.typingAttenuation": "Typing attenuation", "recording.typingAttenuation": "Typing attenuation",
"recording.echoCancellation": "Echo Dämpfung", "recording.echoCancellation": "Echo Dämpfung",
"recording.noiseSuppression": "Hintergrundgeräusche entfernen", "recording.noiseSuppression": "Hintergrundgeräusche entfernen",
"recording.autoGainControl": "Automatische Verstärkung",
"info.address": "Adresse:", "info.address": "Adresse:",
"info.version": "Version:", "info.version": "Version:",
@@ -813,6 +814,7 @@ const translations: Record<Lang, Record<string, string>> = {
"recording.typingAttenuation": "Typing attenuation", "recording.typingAttenuation": "Typing attenuation",
"recording.echoCancellation": "Echo cancellation", "recording.echoCancellation": "Echo cancellation",
"recording.noiseSuppression": "Remove background noise", "recording.noiseSuppression": "Remove background noise",
"recording.autoGainControl": "Automatic gain control",
"info.address": "Address:", "info.address": "Address:",
"info.version": "Version:", "info.version": "Version:",
+14
View File
@@ -36,6 +36,11 @@ export interface MicCaptureOptions {
deviceId?: string; deviceId?: string;
echoCancellation?: boolean; echoCancellation?: boolean;
noiseSuppression?: 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 deviceId?: string;
private echoCancellation: boolean; private echoCancellation: boolean;
private noiseSuppression: boolean; private noiseSuppression: boolean;
private autoGainControl: boolean;
private context: AudioContext; private context: AudioContext;
threshold: number; threshold: number;
hangoverSeconds: number; hangoverSeconds: number;
@@ -75,6 +81,7 @@ export class MicCapture {
this.deviceId = options.deviceId; this.deviceId = options.deviceId;
this.echoCancellation = options.echoCancellation ?? true; this.echoCancellation = options.echoCancellation ?? true;
this.noiseSuppression = options.noiseSuppression ?? true; this.noiseSuppression = options.noiseSuppression ?? true;
this.autoGainControl = options.autoGainControl ?? true;
} }
async start(): Promise<void> { async start(): Promise<void> {
@@ -84,8 +91,15 @@ export class MicCapture {
this.stream = await navigator.mediaDevices.getUserMedia({ this.stream = await navigator.mediaDevices.getUserMedia({
audio: { audio: {
channelCount: 1, 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, echoCancellation: this.echoCancellation,
noiseSuppression: this.noiseSuppression, noiseSuppression: this.noiseSuppression,
autoGainControl: this.autoGainControl,
...(this.deviceId ? { deviceId: { exact: this.deviceId } } : {}), ...(this.deviceId ? { deviceId: { exact: this.deviceId } } : {}),
}, },
}); });