From 7b52c0baa4882fe401844a04aae4aa91b37f7057 Mon Sep 17 00:00:00 2001 From: Moepchi Date: Thu, 17 Sep 2026 18:41:31 +0000 Subject: [PATCH] Route mic self-test monitoring through the (possibly AEC3-cancelled) signal setMonitoring() connected the raw pre-AEC3 mic source straight to playback, so the self-test howled regardless of AEC3 - it was bypassing the whole cancellation path entirely, not failing to cancel. Mirror the processed signal into the ScriptProcessorNode's own output and monitor that instead. Co-Authored-By: Claude Sonnet 5 Claude-Session: https://claude.ai/code/session_01Hu2cuPLqpuKVfd9wWFFakU --- web/src/voice.ts | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/web/src/voice.ts b/web/src/voice.ts index f3a589a..461ef75 100644 --- a/web/src/voice.ts +++ b/web/src/voice.ts @@ -128,6 +128,11 @@ export class MicCapture { this.aec3.process(this.aec3Out, captureIn); input = this.aec3Out[0]; } + // Mirrors the (possibly AEC3-cancelled) signal to the output buffer, so + // setMonitoring() below can tap what's actually being sent rather than + // the raw pre-cancellation mic feed - a self-test through the raw feed + // always howls, whether or not AEC3 is on. + event.outputBuffer.getChannelData(0).set(input); let sumSquares = 0; for (let i = 0; i < input.length; i++) sumSquares += input[i] * input[i]; @@ -170,21 +175,25 @@ export class MicCapture { return this.source; } - /** Routes the raw mic signal to `destination` too, so you can hear yourself (a mic test), until disabled again. */ + /** Routes the (possibly AEC3-cancelled) mic signal to `destination` too, so + * you can hear yourself - a mic test - until disabled again. Taps the + * processor's output rather than the raw source, so the test actually + * reflects AEC3 when it's on, instead of always howling via the + * uncancelled raw feed. */ setMonitoring(enabled: boolean, destination: AudioNode): void { - if (!this.source) return; + if (!this.processor) return; if (enabled && this.monitorTarget !== destination) { - if (this.monitorTarget) this.source.disconnect(this.monitorTarget); - this.source.connect(destination); + if (this.monitorTarget) this.processor.disconnect(this.monitorTarget); + this.processor.connect(destination); this.monitorTarget = destination; } else if (!enabled && this.monitorTarget) { - this.source.disconnect(this.monitorTarget); + this.processor.disconnect(this.monitorTarget); this.monitorTarget = null; } } stop(): void { - if (this.source && this.monitorTarget) this.source.disconnect(this.monitorTarget); + if (this.processor && this.monitorTarget) this.processor.disconnect(this.monitorTarget); this.monitorTarget = null; this.processor?.disconnect(); this.silence?.disconnect();