From 335d93a8b88cbea6294a78c9be63504f781bd4d5 Mon Sep 17 00:00:00 2001 From: illusia <64828020+EchoSixHIYA@users.noreply.github.com> Date: Sun, 6 Sep 2026 09:54:01 +0800 Subject: [PATCH] fix: preserve accompaniment levels and channel password retry --- package-lock.json | 4 +- package.json | 2 +- src/server/webrtc-audio.ts | 16 ++-- web/src/composables/useVoiceWebSocket.ts | 44 ++++++++--- web/src/views/WebClient.vue | 96 +++++++++++++++++++++++- 5 files changed, 140 insertions(+), 22 deletions(-) diff --git a/package-lock.json b/package-lock.json index 2457092..c50704f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,7 +10,7 @@ "license": "AGPL-3.0-only", "dependencies": { "@discordjs/opus": "^0.10.0", - "@echosixhiya/teamspeak-client": "git+https://github.com/EchoSixHIYA/teamspeak-js.git#4e9edf7", + "@echosixhiya/teamspeak-client": "git+https://github.com/EchoSixHIYA/teamspeak-js.git#8d91c30", "express": "^5.2.1", "pino": "^10.3.1", "werift": "0.24.4", @@ -63,7 +63,7 @@ }, "node_modules/@echosixhiya/teamspeak-client": { "version": "0.2.4", - "resolved": "git+https://github.com/EchoSixHIYA/teamspeak-js.git#4e9edf7198418d8a0a0eff97e05aa2b3369c2fe0", + "resolved": "git+https://github.com/EchoSixHIYA/teamspeak-js.git#8d91c300438b45c2f5e96638daabbc68277f378b", "license": "MIT", "dependencies": { "@noble/curves": "^2.0.1" diff --git a/package.json b/package.json index be38a8e..9a1ae2c 100644 --- a/package.json +++ b/package.json @@ -19,7 +19,7 @@ }, "dependencies": { "@discordjs/opus": "^0.10.0", - "@echosixhiya/teamspeak-client": "git+https://github.com/EchoSixHIYA/teamspeak-js.git#4e9edf7", + "@echosixhiya/teamspeak-client": "git+https://github.com/EchoSixHIYA/teamspeak-js.git#8d91c30", "express": "^5.2.1", "pino": "^10.3.1", "werift": "0.24.4", diff --git a/src/server/webrtc-audio.ts b/src/server/webrtc-audio.ts index 8f8b311..06f18c1 100644 --- a/src/server/webrtc-audio.ts +++ b/src/server/webrtc-audio.ts @@ -184,17 +184,21 @@ export class WebRtcAudioSession { if (!this.opusPayloadTypes.has(rtp.header.payloadType)) return; const payload = Buffer.from(rtp.payload); // The track is a browser-side mix of microphone and accompaniment. - // Decode it once to reject malformed RTP. A normal microphone still - // uses the low-level comfort-noise gate, while accompaniment must not - // be treated as speech: quiet music frames are valid audio and are - // forwarded with TeamSpeak's music codec marker. + // Accompaniment is already a complete Opus stream. Forward it as-is: + // do not decode it for RMS analysis or pass it through any speech + // gate, because either step can discard valid quiet music frames. + if (this.accompanimentActive) { + this.onVoiceFrame(payload, 5); + return; + } + // A normal microphone still uses the low-level comfort-noise gate. const rms = this.getIngressRms(payload); if (rms === null) return; - if (!this.accompanimentActive && rms < MIN_FORWARD_RMS) { + if (rms < MIN_FORWARD_RMS) { this.stats.webrtcIngressQuietFrames++; return; } - this.onVoiceFrame(payload, this.accompanimentActive ? 5 : 4); + this.onVoiceFrame(payload, 4); }); void audio.sender.replaceTrack(this.outgoingTrack).catch((error: unknown) => { this.logger.warn({ err: error instanceof Error ? error.message : String(error) }, "Could not attach WebRTC output track"); diff --git a/web/src/composables/useVoiceWebSocket.ts b/web/src/composables/useVoiceWebSocket.ts index 1ef4331..9f35683 100644 --- a/web/src/composables/useVoiceWebSocket.ts +++ b/web/src/composables/useVoiceWebSocket.ts @@ -12,6 +12,7 @@ export interface VoiceState { tsClientId: number; error: string; errorCode: string; + channelSwitchedChannelId: string; } export interface ChannelMember { @@ -78,7 +79,7 @@ export interface ServerEvent { export function useVoiceWebSocket() { const ws = ref(null); - const state = reactive({ connected: false, connecting: false, reconnecting: false, reconnectAttempt: 0, reconnectFailed: false, tsClientId: 0, error: "", errorCode: "" }); + const state = reactive({ connected: false, connecting: false, reconnecting: false, reconnectAttempt: 0, reconnectFailed: false, tsClientId: 0, error: "", errorCode: "", channelSwitchedChannelId: "" }); const members = reactive([]); const channels = reactive([]); const chatMessages = reactive([]); @@ -117,7 +118,6 @@ export function useVoiceWebSocket() { 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; @@ -486,12 +486,10 @@ export function useVoiceWebSocket() { function stopWebRtcMix(): void { webrtcMixAccompanimentSource?.disconnect(); - webrtcMixAccompanimentGain?.disconnect(); webrtcMixMicSource?.disconnect(); webrtcMixMicGain?.disconnect(); webrtcMixDestination?.disconnect(); webrtcMixAccompanimentSource = null; - webrtcMixAccompanimentGain = null; webrtcMixMicSource = null; webrtcMixMicGain = null; webrtcMixDestination = null; @@ -523,12 +521,11 @@ export function useVoiceWebSocket() { 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); + // Keep the captured application audio at its source level. Do not add + // a fixed attenuation/gain node: it makes music sound quieter than the + // source and encourages later "compensation" steps to pump its volume. + accompanimentSource.connect(destination); webrtcMixAccompanimentSource = accompanimentSource; - webrtcMixAccompanimentGain = accompanimentGain; } return destination.stream; } @@ -554,7 +551,16 @@ export function useVoiceWebSocket() { throw new Error("伴奏功能需要启用 WebRTC"); } - const audioConstraints = {} as MediaTrackConstraints & { restrictOwnAudio?: boolean }; + const captureProcessingConstraints: MediaTrackConstraints = { + // Display/application audio must not pass through browser voice + // processing. Those processors are designed for speech and can change + // music level from frame to frame (AGC), suppress quiet passages, or + // cancel sustained tones. + autoGainControl: false, + echoCancellation: false, + noiseSuppression: false, + }; + const audioConstraints = { ...captureProcessingConstraints } as MediaTrackConstraints & { restrictOwnAudio?: boolean }; const supportedConstraints = navigator.mediaDevices.getSupportedConstraints?.() as Record | undefined; if (supportedConstraints?.restrictOwnAudio) audioConstraints.restrictOwnAudio = true; const options = { @@ -581,6 +587,18 @@ export function useVoiceWebSocket() { throw new Error("所选来源没有可共享音频"); } + try { + // Do not pass the Chromium-only restrictOwnAudio hint to + // applyConstraints: rejecting an unknown key could otherwise cause the + // browser to discard all three standard processing-off constraints. + await audioTrack.applyConstraints(captureProcessingConstraints); + } catch { + // Some browsers expose display audio but reject one or more optional + // processing constraints. The capture can still proceed without + // introducing a WebSpeak-side gain stage. + } + if ("contentHint" in audioTrack) audioTrack.contentHint = "music"; + accompanimentStream?.getTracks().forEach((track) => track.stop()); accompanimentStream = nextStream; accompanimentActive.value = true; @@ -1158,6 +1176,7 @@ export function useVoiceWebSocket() { state.reconnectFailed = false; state.tsClientId = 0; state.errorCode = ""; + state.channelSwitchedChannelId = ""; if (!keepRememberedIdentity) identityMaterial.value = ""; members.length = 0; channels.length = 0; @@ -1185,6 +1204,7 @@ export function useVoiceWebSocket() { state.reconnectFailed = false; state.error = ""; state.errorCode = ""; + state.channelSwitchedChannelId = ""; state.tsClientId = Number(msg.tsClientId) || 0; applyWhisperState(msg.whisperTargetIds, msg.whisperActive); if (Array.isArray(msg.members)) { @@ -1271,6 +1291,9 @@ export function useVoiceWebSocket() { }); break; case "channelSwitched": + state.channelSwitchedChannelId = typeof msg.channelId === "string" || typeof msg.channelId === "number" ? String(msg.channelId) : ""; + state.error = ""; + state.errorCode = ""; break; case "disconnected": state.connected = false; @@ -1349,6 +1372,7 @@ export function useVoiceWebSocket() { function switchChannel(channelId: string, password = ""): void { state.error = ""; state.errorCode = ""; + state.channelSwitchedChannelId = ""; sendCmd("switchChannel", { channelId, ...(password ? { password } : {}) }); } diff --git a/web/src/views/WebClient.vue b/web/src/views/WebClient.vue index f09a298..7bc85dc 100644 --- a/web/src/views/WebClient.vue +++ b/web/src/views/WebClient.vue @@ -247,6 +247,23 @@ + + +