diff --git a/web/src/components/Icon.vue b/web/src/components/Icon.vue index bf779d1..9451817 100644 --- a/web/src/components/Icon.vue +++ b/web/src/components/Icon.vue @@ -31,7 +31,12 @@ - + + + + + + diff --git a/web/src/composables/useVoiceWebSocket.ts b/web/src/composables/useVoiceWebSocket.ts index a2f8253..88f736f 100644 --- a/web/src/composables/useVoiceWebSocket.ts +++ b/web/src/composables/useVoiceWebSocket.ts @@ -109,6 +109,15 @@ export function useVoiceWebSocket() { let micSource: MediaStreamAudioSourceNode | null = null; let micGain: GainNode | null = null; let silentGain: GainNode | null = null; + const accompanimentActive = ref(false); + const accompanimentSupported = ref(typeof navigator !== "undefined" && Boolean(navigator.mediaDevices?.getDisplayMedia)); + const accompanimentErrorCode = ref<"" | "unsupported" | "needsWebRtc" | "noAudio" | "permission">(""); + let accompanimentStream: MediaStream | null = null; + let webrtcMixDestination: MediaStreamAudioDestinationNode | null = null; + let webrtcMixMicSource: MediaStreamAudioSourceNode | null = null; + let webrtcMixMicGain: GainNode | null = null; + let webrtcMixAccompanimentSource: MediaStreamAudioSourceNode | null = null; + let webrtcMixAccompanimentGain: GainNode | null = null; let webrtcMicMonitorSource: MediaStreamAudioSourceNode | null = null; let webrtcMicMonitorAnalyser: AnalyserNode | null = null; let webrtcMicMonitorGain: GainNode | null = null; @@ -514,19 +523,140 @@ export function useVoiceWebSocket() { return false; } + function stopWebRtcMix(): void { + webrtcMixAccompanimentSource?.disconnect(); + webrtcMixAccompanimentGain?.disconnect(); + webrtcMixMicSource?.disconnect(); + webrtcMixMicGain?.disconnect(); + webrtcMixDestination?.disconnect(); + webrtcMixAccompanimentSource = null; + webrtcMixAccompanimentGain = null; + webrtcMixMicSource = null; + webrtcMixMicGain = null; + webrtcMixDestination = null; + } + + function releaseAccompanimentStream(): void { + accompanimentStream?.getTracks().forEach((track) => track.stop()); + accompanimentStream = null; + accompanimentActive.value = false; + } + + function createWebRtcMixStream(): MediaStream { + if (!micStream) throw new Error("没有可用的麦克风音轨"); + const ctx = getAudioCtx(); + stopWebRtcMix(); + const destination = ctx.createMediaStreamDestination(); + destination.channelCount = 1; + destination.channelCountMode = "explicit"; + const microphoneSource = ctx.createMediaStreamSource(micStream); + const microphoneGain = ctx.createGain(); + microphoneGain.gain.value = microphoneMuted.value ? 0 : inputVolume.value; + microphoneSource.connect(microphoneGain); + microphoneGain.connect(destination); + + webrtcMixDestination = destination; + webrtcMixMicSource = microphoneSource; + webrtcMixMicGain = microphoneGain; + + const accompanimentTrack = accompanimentStream?.getAudioTracks()[0]; + if (accompanimentTrack) { + const accompanimentSource = ctx.createMediaStreamSource(accompanimentStream!); + const accompanimentGain = ctx.createGain(); + accompanimentGain.gain.value = 0.78; + accompanimentSource.connect(accompanimentGain); + accompanimentGain.connect(destination); + webrtcMixAccompanimentSource = accompanimentSource; + webrtcMixAccompanimentGain = accompanimentGain; + } + return destination.stream; + } + + async function replaceWebRtcAudioTrack(): Promise { + if (!webrtcPeer || !micStream) return; + const sender = webrtcPeer.getSenders().find((candidate) => candidate.track?.kind === "audio"); + if (!sender) throw new Error("WebRTC 音频轨道尚未就绪"); + const mixedStream = createWebRtcMixStream(); + const mixedTrack = mixedStream.getAudioTracks()[0]; + if (!mixedTrack) throw new Error("混合音频轨道创建失败"); + await sender.replaceTrack(mixedTrack); + } + + async function startAccompaniment(): Promise { + accompanimentErrorCode.value = ""; + if (!accompanimentSupported.value) { + accompanimentErrorCode.value = "unsupported"; + throw new Error("伴奏共享不可用"); + } + if (!webrtcActive.value || !webrtcPeer || !micStream) { + accompanimentErrorCode.value = "needsWebRtc"; + throw new Error("伴奏功能需要启用 WebRTC"); + } + + const audioConstraints = {} as MediaTrackConstraints & { restrictOwnAudio?: boolean }; + const supportedConstraints = navigator.mediaDevices.getSupportedConstraints?.() as Record | undefined; + if (supportedConstraints?.restrictOwnAudio) audioConstraints.restrictOwnAudio = true; + const options = { + video: { displaySurface: "browser" }, + audio: audioConstraints, + selfBrowserSurface: "exclude", + systemAudio: "include", + windowAudio: "window", + } as unknown as DisplayMediaStreamOptions; + + let nextStream: MediaStream; + try { + nextStream = await navigator.mediaDevices.getDisplayMedia(options); + } catch (error) { + if (error instanceof DOMException && error.name === "AbortError") return; + accompanimentErrorCode.value = "permission"; + throw error; + } + const audioTrack = nextStream.getAudioTracks()[0]; + nextStream.getVideoTracks().forEach((track) => track.stop()); + if (!audioTrack) { + nextStream.getTracks().forEach((track) => track.stop()); + accompanimentErrorCode.value = "noAudio"; + throw new Error("所选来源没有可共享音频"); + } + + accompanimentStream?.getTracks().forEach((track) => track.stop()); + accompanimentStream = nextStream; + accompanimentActive.value = true; + audioTrack.addEventListener("ended", () => { void stopAccompaniment(); }, { once: true }); + try { + await replaceWebRtcAudioTrack(); + } catch (error) { + releaseAccompanimentStream(); + stopWebRtcMix(); + accompanimentErrorCode.value = "permission"; + throw error; + } + } + + async function stopAccompaniment(): Promise { + accompanimentErrorCode.value = ""; + releaseAccompanimentStream(); + if (webrtcActive.value && webrtcPeer && micStream) await replaceWebRtcAudioTrack(); + else stopWebRtcMix(); + } + async function startWebRtcTransport(sequence: number, socket: WebSocket): Promise { if (typeof RTCPeerConnection === "undefined") throw new Error("当前浏览器不支持 WebRTC"); await ensureMicrophone(); if (sequence !== connectionSequence || socket.readyState !== WebSocket.OPEN || !micStream) return; - const track = micStream.getAudioTracks()[0]; - if (!track) throw new Error("没有可用的麦克风音轨"); + const microphoneTrack = micStream.getAudioTracks()[0]; + if (!microphoneTrack) throw new Error("没有可用的麦克风音轨"); stopCaptureGraph(); const peer = new RTCPeerConnection({ iceServers: [] }); webrtcPeer = peer; webrtcFallbackStarted = false; - track.enabled = !microphoneMuted.value; - peer.addTrack(track, micStream); + microphoneTrack.enabled = !microphoneMuted.value; + const mixedStream = createWebRtcMixStream(); + const mixedTrack = mixedStream.getAudioTracks()[0]; + if (!mixedTrack) throw new Error("混合音频轨道创建失败"); + peer.addTrack(mixedTrack, mixedStream); peer.ontrack = (event) => { const stream = event.streams[0] ?? new MediaStream([event.track]); // Use the browser's native WebRTC media output. Routing the remote @@ -556,7 +686,7 @@ export function useVoiceWebSocket() { } void syncWebRtcPlayback(); }; - startWebRtcMicMonitor(getAudioCtx(), micStream, track); + startWebRtcMicMonitor(getAudioCtx(), micStream, microphoneTrack); peer.onconnectionstatechange = () => { if (peer.connectionState === "failed") void fallbackFromWebRtc(sequence, socket); }; @@ -632,6 +762,8 @@ export function useVoiceWebSocket() { webrtcPeer = null; webrtcActive.value = false; webrtcNegotiationPromise = null; + releaseAccompanimentStream(); + stopWebRtcMix(); stopWebRtcMicMonitor(); stopWebRtcPlayback(); if (peer) void peer.close(); @@ -716,6 +848,8 @@ export function useVoiceWebSocket() { function stopMicrophone(closeContext = true): void { stopCaptureGraph(); + releaseAccompanimentStream(); + stopWebRtcMix(); micStream?.getTracks().forEach((track) => track.stop()); micStream = null; if (closeContext) { @@ -1341,6 +1475,7 @@ export function useVoiceWebSocket() { voxRelease = 0; accumLen = 0; if (webrtcActive.value) micStream?.getAudioTracks().forEach((track) => { track.enabled = !muted; }); + if (webrtcMixMicGain) webrtcMixMicGain.gain.value = muted ? 0 : inputVolume.value; if (webrtcActive.value) sendCmd("setMicrophoneMuted", { muted }); if (muted && state.tsClientId) clearSpeaking(state.tsClientId); void saveAudioPreferences(); @@ -1394,6 +1529,7 @@ export function useVoiceWebSocket() { function setInputVolume(volume: number): void { inputVolume.value = Math.max(0, Math.min(1, volume)); if (micGain) micGain.gain.value = inputVolume.value; + if (webrtcMixMicGain) webrtcMixMicGain.gain.value = microphoneMuted.value ? 0 : inputVolume.value; void saveAudioPreferences(); } @@ -1443,6 +1579,9 @@ export function useVoiceWebSocket() { whisperTargetIds, whisperActive, volumes, + accompanimentActive, + accompanimentSupported, + accompanimentErrorCode, setVolume, setInputVolume, setOutputVolume, @@ -1467,6 +1606,8 @@ export function useVoiceWebSocket() { setWhisperTargets, setWhisperActive, setMicrophoneMuted, + startAccompaniment, + stopAccompaniment, checkSupport, clearError, }; diff --git a/web/src/views/WebClient.vue b/web/src/views/WebClient.vue index 7ad472b..b1c9411 100644 --- a/web/src/views/WebClient.vue +++ b/web/src/views/WebClient.vue @@ -95,8 +95,8 @@ - - + + {{ t('langSwitch') }} {{ t('exit') }} @@ -198,7 +198,14 @@ {{ t('noMatchingMembers') }} - {{ t('volumeTip') }} + + {{ t('desktopAudioControls') }}{{ accompanimentActive ? t('accompanimentActive') : t('volumeTip') }} + + + + + + @@ -307,6 +314,10 @@ const { setWhisperTargets, setWhisperActive, setMicrophoneMuted, + accompanimentActive, + accompanimentErrorCode, + startAccompaniment, + stopAccompaniment, checkSupport, clearError, } = useVoiceWebSocket(); @@ -425,6 +436,16 @@ const translations: Record> = { serverOptions: "更多服务器选项", online: "在线", audioSettings: "音频设置", + desktopAudioControls: "音频控制", + startAccompaniment: "共享伴奏", + stopAccompaniment: "停止伴奏", + accompanimentStarted: "伴奏共享已开始", + accompanimentStopped: "伴奏共享已停止", + accompanimentActive: "伴奏共享中", + accompanimentNeedsWebRtc: "伴奏功能需要启用 WebRTC", + accompanimentNoAudio: "所选来源没有可共享音频,请重新选择并勾选共享音频", + accompanimentPermissionDenied: "无法获取伴奏音频,请允许屏幕共享并勾选共享音频", + accompanimentUnsupported: "当前浏览器不支持伴奏共享", serverPassword: "服务器密码", optionalPassword: "没有密码可留空", switchChannel: "切换频道", @@ -645,6 +666,16 @@ const translations: Record> = { serverOptions: "More server options", online: "Online", audioSettings: "Audio settings", + desktopAudioControls: "Audio controls", + startAccompaniment: "Share accompaniment", + stopAccompaniment: "Stop accompaniment", + accompanimentStarted: "Accompaniment sharing started", + accompanimentStopped: "Accompaniment sharing stopped", + accompanimentActive: "Accompaniment sharing", + accompanimentNeedsWebRtc: "Accompaniment requires WebRTC", + accompanimentNoAudio: "The selected source has no shareable audio. Select it again and enable audio sharing", + accompanimentPermissionDenied: "Could not access accompaniment audio. Allow screen sharing and enable audio sharing", + accompanimentUnsupported: "This browser does not support accompaniment sharing", serverPassword: "Server password", optionalPassword: "Leave blank if not required", switchChannel: "Switch channel", @@ -1053,6 +1084,7 @@ onMounted(() => { viewportChangeHandler = () => { isMobileViewport.value = viewportMediaQuery?.matches ?? false; if (!isMobileViewport.value) memberMenu.value = null; + else if (accompanimentActive.value) void stopAccompaniment(); }; viewportChangeHandler(); viewportMediaQuery.addEventListener?.("change", viewportChangeHandler); @@ -1361,6 +1393,27 @@ function toggleMicrophone(): void { showToast(microphoneMuted.value ? t("microphoneMuted") : t("microphoneActive")); } +async function toggleAccompaniment(): Promise { + if (accompanimentActive.value) { + await stopAccompaniment(); + showToast(t("accompanimentStopped")); + return; + } + try { + await startAccompaniment(); + showToast(t("accompanimentStarted")); + } catch { + const messageKey = accompanimentErrorCode.value === "needsWebRtc" + ? "accompanimentNeedsWebRtc" + : accompanimentErrorCode.value === "noAudio" + ? "accompanimentNoAudio" + : accompanimentErrorCode.value === "unsupported" + ? "accompanimentUnsupported" + : "accompanimentPermissionDenied"; + showToast(t(messageKey)); + } +} + function onWhisperPttDown(event: PointerEvent): void { if (!whisperTargetIds.size) return; const target = event.currentTarget as HTMLElement | null; @@ -1636,8 +1689,9 @@ function stopWhisperTalk(): void { .app-shell { grid-template-rows: minmax(0, 1fr) 220px; } } -/* Keep connected controls in the header; the old bottom dock consumed the - chat viewport and made the settings action appear detached from its icon. */ +/* Desktop audio controls live in the member rail so the workspace header + stays focused on navigation. Mobile keeps its existing controls below the + voice cards and in the More panel. */ .header-tools { align-items: center; } .header-action, .round-icon { line-height: 0; } .guide-button { display: inline-flex; align-items: center; gap: 6px; min-height: 30px; padding: 0 10px; color: #006a64; background: #edf7f4; border: 1px solid #d7ebe6; border-radius: 8px; font-size: 12px; font-weight: 700; text-decoration: none; cursor: pointer; } @@ -1645,6 +1699,15 @@ function stopWhisperTalk(): void { .workspace-actions { align-items: center; flex-wrap: nowrap; } .workspace-actions .header-action { flex: 0 0 34px; padding: 0; line-height: 0; } .workspace-actions .header-action .ui-icon { margin: 0; } +.desktop-audio-dock { display: flex; align-items: center; gap: 9px; flex: 0 0 auto; min-width: 0; margin-top: 12px; padding: 9px; color: var(--text-muted); background: color-mix(in srgb, var(--accent) 8%, var(--surface-1)); border: 1px solid var(--border); border-radius: 12px; box-shadow: 0 8px 20px color-mix(in srgb, var(--text-primary) 10%, transparent); } +.desktop-audio-dock-copy { display: flex; min-width: 0; flex: 1; flex-direction: column; gap: 3px; } +.desktop-audio-dock-copy strong { overflow: hidden; color: var(--text-primary); font-size: 12px; text-overflow: ellipsis; white-space: nowrap; } +.desktop-audio-dock-copy span { overflow: hidden; font-size: 10px; line-height: 1.35; text-overflow: ellipsis; white-space: nowrap; } +.desktop-audio-dock-actions { display: flex; align-items: center; gap: 4px; flex: 0 0 auto; } +.dock-audio-button { display: grid; place-items: center; width: 34px; height: 34px; padding: 0; color: var(--text-muted); background: transparent; border: 1px solid transparent; border-radius: 9px; cursor: pointer; transition: color .16s, background .16s, border-color .16s, transform .16s; } +.dock-audio-button:hover, .dock-audio-button:focus-visible { color: var(--accent); background: color-mix(in srgb, var(--accent) 14%, var(--surface-1)); border-color: color-mix(in srgb, var(--accent) 32%, var(--border)); transform: translateY(-1px); } +.dock-audio-button.microphone-header-toggle.muted { color: var(--danger); background: color-mix(in srgb, var(--danger) 12%, var(--surface-1)); } +.dock-audio-button.accompaniment-toggle.active { color: var(--accent); background: color-mix(in srgb, var(--accent) 18%, var(--surface-1)); border-color: color-mix(in srgb, var(--accent) 40%, var(--border)); box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 12%, transparent); } .settings-mode-switch { width: fit-content; margin: 0 0 7px; } .settings-hint { margin: -1px 0 19px; color: #8b9994; font-size: 11px; } @@ -1869,7 +1932,7 @@ function stopWhisperTalk(): void { .member-presence { width: 10px; height: 10px; } .member-copy strong { font-size: 14px; } .member-copy span { margin-top: 3px; font-size: 12px; } - .member-flags, .member-volume, .member-panel-tip { display: none; } + .desktop-audio-dock, .member-flags, .member-volume, .member-panel-tip { display: none; } .member-action-button { display: grid; place-items: center; width: 38px; height: 38px; flex: 0 0 38px; color: var(--text-muted); background: transparent; border-radius: 10px; cursor: pointer; } .member-action-button:hover, .member-action-button:active { color: var(--accent); background: color-mix(in srgb, var(--accent) 14%, var(--surface-1)); }