mirror of
https://github.com/Moepchi/webspeak3.git
synced 2026-12-03 01:42:50 +08:00
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 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Hu2cuPLqpuKVfd9wWFFakU
This commit is contained in:
1 parent
12e482b223
commit
bf4507ae9d
6 files changed
+95
-4
No files matched your search
@@ -6721,6 +6721,9 @@ function AppInner() {
|
||||
const audioContextRef = useRef<AudioContext | null>(null);
|
||||
const audioPlayerRef = useRef<AudioPlayer | null>(null);
|
||||
const micCaptureRef = useRef<MicCapture | null>(null);
|
||||
// A shared tab's or window's audio, mixed into the mic (MicCapture.setExtraAudio).
|
||||
const tabAudioRef = useRef<MediaStream | null>(null);
|
||||
const [tabAudioOn, setTabAudioOn] = useState(false);
|
||||
const recordProcessorRef = useRef<ScriptProcessorNode | null>(null);
|
||||
const recordSilenceRef = useRef<GainNode | null>(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" ? "🛑" : "🖥️"}
|
||||
</button>
|
||||
{typeof navigator.mediaDevices?.getDisplayMedia === "function" && (
|
||||
<button
|
||||
className={`ts-icon-button${tabAudioOn ? " ts-mic-on" : ""}`}
|
||||
onClick={handleToggleTabAudio}
|
||||
disabled={!micOn}
|
||||
aria-pressed={tabAudioOn}
|
||||
title={tabAudioOn ? t("tabAudio.stop") : t("tabAudio.start")}
|
||||
>
|
||||
🎵
|
||||
</button>
|
||||
)}
|
||||
<span className="ts-toolbar-sep" />
|
||||
<button
|
||||
className={`ts-icon-button${outputMuted ? " ts-muted-on" : ""}`}
|
||||
|
||||
Reference in new issue
Block a user