mirror of
https://github.com/Moepchi/webspeak3.git
synced 2026-12-01 00:42:49 +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" : ""}`}
|
||||
|
||||
@@ -90,6 +90,9 @@
|
||||
"toolbar.micMute": "Mikrofon stummschalten",
|
||||
"toolbar.micUnmute": "Mikrofon aktivieren (unmute)",
|
||||
"toolbar.vadSensitivity": "Empfindlichkeit der Sprachaktivierung",
|
||||
"tabAudio.start": "Ton eines Tabs oder Fensters in den Sprachkanal geben (mit Kopfhörer kein Echo)",
|
||||
"tabAudio.stop": "Ton-Freigabe beenden",
|
||||
"tabAudio.noAudio": "Es wurde kein Ton freigegeben. Im Auswahldialog einen Tab oder ein Fenster wählen und den Ton einschalten.",
|
||||
"toolbar.muteSound": "Ton stummschalten (Ausgabe stumm)",
|
||||
"toolbar.unmuteSound": "Ton aktivieren (Ausgabe an)",
|
||||
"toolbar.chooseOutputDevice": "Wiedergabegerät wählen",
|
||||
|
||||
@@ -90,6 +90,9 @@
|
||||
"toolbar.micMute": "Mute microphone",
|
||||
"toolbar.micUnmute": "Unmute microphone",
|
||||
"toolbar.vadSensitivity": "Voice activation sensitivity",
|
||||
"tabAudio.start": "Share tab or window audio into the voice channel (headphones avoid an echo)",
|
||||
"tabAudio.stop": "Stop sharing audio",
|
||||
"tabAudio.noAudio": "No audio was shared. In the picker, choose a tab or window and turn on its audio.",
|
||||
"toolbar.muteSound": "Mute sound (deafen)",
|
||||
"toolbar.unmuteSound": "Unmute sound (undeafen)",
|
||||
"toolbar.chooseOutputDevice": "Choose output device",
|
||||
|
||||
@@ -90,6 +90,9 @@
|
||||
"toolbar.micMute": "بیصدا کردن میکروفون",
|
||||
"toolbar.micUnmute": "فعال کردن صدای میکروفون",
|
||||
"toolbar.vadSensitivity": "حساسیت فعالسازی صوتی",
|
||||
"tabAudio.start": "صدای یک زبانه یا پنجره را در کانال صوتی پخش کن (با هدفون پژواک ایجاد نمیشود)",
|
||||
"tabAudio.stop": "توقف اشتراک صدا",
|
||||
"tabAudio.noAudio": "صدایی به اشتراک گذاشته نشد. در پنجره انتخاب، یک زبانه یا پنجره را انتخاب و صدای آن را روشن کنید.",
|
||||
"toolbar.muteSound": "بیصدا کردن صدا (کر کردن)",
|
||||
"toolbar.unmuteSound": "فعال کردن صدا",
|
||||
"toolbar.chooseOutputDevice": "انتخاب دستگاه خروجی",
|
||||
|
||||
@@ -43,6 +43,9 @@
|
||||
"toolbar.micMute": "麦克风静音",
|
||||
"toolbar.micUnmute": "取消麦克风静音",
|
||||
"toolbar.vadSensitivity": "语音激活灵敏度",
|
||||
"tabAudio.start": "将标签页或窗口的声音共享到语音频道(戴耳机可避免回声)",
|
||||
"tabAudio.stop": "停止共享声音",
|
||||
"tabAudio.noAudio": "没有共享声音。请在选择窗口中选择标签页或窗口,并开启其声音。",
|
||||
"toolbar.muteSound": "关闭声音(拒听)",
|
||||
"toolbar.unmuteSound": "开启声音(取消拒听)",
|
||||
"toolbar.chooseOutputDevice": "选择输出设备",
|
||||
|
||||
+36
-4
@@ -60,6 +60,9 @@ export interface MicCaptureOptions {
|
||||
export class MicCapture {
|
||||
private stream: MediaStream | null = null;
|
||||
private source: MediaStreamAudioSourceNode | null = null;
|
||||
private merger: ChannelMergerNode | null = null;
|
||||
private extra: MediaStreamAudioSourceNode | null = null;
|
||||
private pendingExtra: number[] = [];
|
||||
private processor: ScriptProcessorNode | null = null;
|
||||
private silence: GainNode | null = null;
|
||||
private monitorTarget: AudioNode | null = null;
|
||||
@@ -117,7 +120,9 @@ export class MicCapture {
|
||||
// before the AudioContext would otherwise be allowed to leave "suspended".
|
||||
if (this.context.state === "suspended") await this.context.resume();
|
||||
this.source = this.context.createMediaStreamSource(this.stream);
|
||||
this.processor = this.context.createScriptProcessor(2048, 1, 1);
|
||||
// Channel 0 is the mic, channel 1 the optional extra source (setExtraAudio).
|
||||
this.merger = this.context.createChannelMerger(2);
|
||||
this.processor = this.context.createScriptProcessor(2048, 2, 1);
|
||||
this.processor.onaudioprocess = (event) => {
|
||||
const raw = event.inputBuffer.getChannelData(0);
|
||||
let input: Float32Array = raw;
|
||||
@@ -141,7 +146,8 @@ export class MicCapture {
|
||||
|
||||
const now = this.context.currentTime;
|
||||
if (rms >= this.threshold) this.activeUntil = now + this.hangoverSeconds;
|
||||
const shouldBeActive = now < this.activeUntil;
|
||||
// Shared audio (music, a video) has its own pauses; keep sending throughout.
|
||||
const shouldBeActive = this.extra !== null || now < this.activeUntil;
|
||||
if (shouldBeActive !== this.active) {
|
||||
this.active = shouldBeActive;
|
||||
this.onActivity?.(this.active);
|
||||
@@ -151,11 +157,19 @@ export class MicCapture {
|
||||
if (!this.active) return;
|
||||
|
||||
for (let i = 0; i < input.length; i++) this.pending.push(input[i]);
|
||||
// Mixed in per frame rather than per tick: AEC3 may hand back a block of
|
||||
// a different size than it got, and the extra source never went through
|
||||
// it (it isn't an echo of anything).
|
||||
if (this.extra) {
|
||||
const extra = event.inputBuffer.getChannelData(1);
|
||||
for (let i = 0; i < extra.length; i++) this.pendingExtra.push(extra[i]);
|
||||
}
|
||||
while (this.pending.length >= FRAME_SAMPLES) {
|
||||
const frame = this.pending.splice(0, FRAME_SAMPLES);
|
||||
const extra = this.pendingExtra.splice(0, FRAME_SAMPLES);
|
||||
const int16 = new Int16Array(FRAME_SAMPLES);
|
||||
for (let i = 0; i < FRAME_SAMPLES; i++) {
|
||||
const clamped = Math.max(-1, Math.min(1, frame[i]));
|
||||
const clamped = Math.max(-1, Math.min(1, frame[i] + (extra[i] ?? 0)));
|
||||
int16[i] = clamped < 0 ? clamped * 32768 : clamped * 32767;
|
||||
}
|
||||
this.onFrame(int16ToBase64(int16));
|
||||
@@ -165,11 +179,24 @@ export class MicCapture {
|
||||
// it through a muted gain node so we don't hear our own mic echoed back.
|
||||
this.silence = this.context.createGain();
|
||||
this.silence.gain.value = 0;
|
||||
this.source.connect(this.processor);
|
||||
this.source.connect(this.merger, 0, 0);
|
||||
this.merger.connect(this.processor);
|
||||
this.processor.connect(this.silence);
|
||||
this.silence.connect(this.context.destination);
|
||||
}
|
||||
|
||||
/** Mixes a second stream (e.g. a shared tab's audio) into what is sent, or
|
||||
* removes it with null. The caller keeps ownership of the stream. */
|
||||
setExtraAudio(stream: MediaStream | null): void {
|
||||
this.extra?.disconnect();
|
||||
this.extra = null;
|
||||
this.pendingExtra = [];
|
||||
if (stream && this.merger) {
|
||||
this.extra = this.context.createMediaStreamSource(stream);
|
||||
this.extra.connect(this.merger, 0, 1);
|
||||
}
|
||||
}
|
||||
|
||||
/** The raw mic input node, e.g. to additionally route it into a recorder. */
|
||||
getSourceNode(): AudioNode | null {
|
||||
return this.source;
|
||||
@@ -198,6 +225,11 @@ export class MicCapture {
|
||||
this.processor?.disconnect();
|
||||
this.silence?.disconnect();
|
||||
this.source?.disconnect();
|
||||
this.extra?.disconnect();
|
||||
this.merger?.disconnect();
|
||||
this.extra = null;
|
||||
this.merger = null;
|
||||
this.pendingExtra = [];
|
||||
this.stream?.getTracks().forEach((t) => t.stop());
|
||||
this.processor = null;
|
||||
this.silence = null;
|
||||
|
||||
Reference in new issue
Block a user