From bf4507ae9d74d03f61bd5c460dc9b519975504df Mon Sep 17 00:00:00 2001 From: Moepchi Date: Sat, 3 Oct 2026 01:42:13 +0000 Subject: [PATCH] feat: share a tab's or window's audio into the voice channel The new toolbar button asks the browser for a tab or window with audio and mixes it into what the mic sends. It joins after AEC3, per 20 ms frame, so the canceller only ever sees the mic, and while sharing the voice activation stays open so music pauses don't cut out. Hidden where getDisplayMedia is missing (mobile); a restarted mic picks the shared audio up again. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Hu2cuPLqpuKVfd9wWFFakU --- web/src/App.tsx | 47 ++++++++++++++++++++++++++++++++++++++ web/src/locales/de.json | 3 +++ web/src/locales/en.json | 3 +++ web/src/locales/fa.json | 3 +++ web/src/locales/zh-CN.json | 3 +++ web/src/voice.ts | 40 ++++++++++++++++++++++++++++---- 6 files changed, 95 insertions(+), 4 deletions(-) diff --git a/web/src/App.tsx b/web/src/App.tsx index d6ca071..c4af7c8 100644 --- a/web/src/App.tsx +++ b/web/src/App.tsx @@ -6721,6 +6721,9 @@ function AppInner() { const audioContextRef = useRef(null); const audioPlayerRef = useRef(null); const micCaptureRef = useRef(null); + // A shared tab's or window's audio, mixed into the mic (MicCapture.setExtraAudio). + const tabAudioRef = useRef(null); + const [tabAudioOn, setTabAudioOn] = useState(false); const recordProcessorRef = useRef(null); const recordSilenceRef = useRef(null); const recordChunksRef = useRef<{ left: Float32Array[]; right: Float32Array[] }>({ left: [], right: [] }); @@ -8504,6 +8507,7 @@ function AppInner() { } micCaptureRef.current?.stop(); micCaptureRef.current = mic; + mic.setExtraAudio(tabAudioRef.current); setMicOn(true); refreshOutputDevices(); refreshInputDevices(); @@ -8607,6 +8611,38 @@ function AppInner() { } }; + const stopTabAudio = () => { + tabAudioRef.current?.getTracks().forEach((track) => track.stop()); + tabAudioRef.current = null; + micCaptureRef.current?.setExtraAudio(null); + setTabAudioOn(false); + }; + + const handleToggleTabAudio = async () => { + if (tabAudioRef.current) { + stopTabAudio(); + return; + } + let stream: MediaStream; + try { + // Browsers only offer audio together with a video capture. + stream = await navigator.mediaDevices.getDisplayMedia({ video: true, audio: true }); + } catch { + return; // picker cancelled + } + stream.getVideoTracks().forEach((track) => track.stop()); + const track = stream.getAudioTracks()[0]; + if (!track) { + appendLog({ text: t("tabAudio.noAudio"), kind: "error" }); + return; + } + // Also the browser's own "stop sharing" bar. + track.addEventListener("ended", stopTabAudio); + tabAudioRef.current = stream; + micCaptureRef.current?.setExtraAudio(stream); + setTabAudioOn(true); + }; + const handleToggleMicTest = () => { const next = !micTestOn; setMicTestOn(next); @@ -10290,6 +10326,17 @@ function AppInner() { > {publishState === "live" ? "🛑" : "🖥️"} + {typeof navigator.mediaDevices?.getDisplayMedia === "function" && ( + + )}