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

+14
View File
@@ -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<void> {
@@ -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 } } : {}),
},
});