From 5858631e6d001f4e5ed776ecf1fd1af06a907f59 Mon Sep 17 00:00:00 2001 From: Moepchi Date: Sun, 2 Aug 2026 14:41:54 +0200 Subject: [PATCH] Add whisper (targeted voice to channels/clients) Connector: a new "whisper ;" stdin command sets persistent whisper targets; outgoing audio then goes out as AudioData::C2SWhisper instead of the normal C2S broadcast until "unwhisper" clears it. Gateway relays a setWhisperTargets message the same way. Frontend: a toolbar toggle lists channels as checkboxes, and the client context menu gets an "add/remove as whisper target" entry; selecting any target switches all subsequent outgoing voice to whisper mode. Verified against the real test server: setting a channel target and sending a frame produced no connector error, and clearing targets resumed normal voice cleanly. --- connector/src/main.rs | 38 +++++++++-- gateway/src/index.ts | 4 ++ gateway/src/ts3/connection.ts | 10 +++ web/src/App.tsx | 125 ++++++++++++++++++++++++++++++++++ web/src/i18n.tsx | 14 +++- 5 files changed, 184 insertions(+), 7 deletions(-) diff --git a/connector/src/main.rs b/connector/src/main.rs index 4d8c251..2a3a6c1 100644 --- a/connector/src/main.rs +++ b/connector/src/main.rs @@ -445,6 +445,11 @@ async fn run(args: Args) -> Result<()> { // `send_with_result()` for text messages gets us a `MessageResult` event we // can surface as a visible error instead of the message just vanishing. let mut pending_messages: HashMap = HashMap::new(); + // When non-empty, outgoing voice is whispered to just these channels/clients + // instead of being sent to the current channel - set via the "whisper " + // stdin command, cleared via "unwhisper". + let mut whisper_channels: Vec = Vec::new(); + let mut whisper_clients: Vec = Vec::new(); enum LoopOutcome { StdinLine(std::io::Result>), @@ -582,6 +587,17 @@ async fn run(args: Args) -> Result<()> { if let Err(e) = part.send(&mut con) { emit(&Event::Error { message: e.to_string() }); } + } else if let Some(rest) = l.strip_prefix("whisper ") { + // Format: "whisper ;" + // (either side may be empty, e.g. "whisper 5;" or "whisper ;12,13"). + let mut parts = rest.splitn(2, ';'); + let channels_part = parts.next().unwrap_or(""); + let clients_part = parts.next().unwrap_or(""); + whisper_channels = channels_part.split(',').filter_map(|s| s.trim().parse().ok()).collect(); + whisper_clients = clients_part.split(',').filter_map(|s| s.trim().parse().ok()).collect(); + } else if l == "unwhisper" { + whisper_channels.clear(); + whisper_clients.clear(); } else if let Some(b64) = l.strip_prefix("audio ") { match base64::engine::general_purpose::STANDARD.decode(b64.trim()) { Ok(bytes) => { @@ -593,11 +609,23 @@ async fn run(args: Args) -> Result<()> { let mut opus_output = [0u8; 1275]; match opus_encoder.encode_float(&samples, &mut opus_output) { Ok(len) => { - let packet = OutAudio::new(&AudioData::C2S { - id: 0, - codec: CodecType::OpusVoice, - data: &opus_output[..len], - }); + let has_whisper_targets = + !whisper_channels.is_empty() || !whisper_clients.is_empty(); + let packet = if has_whisper_targets { + OutAudio::new(&AudioData::C2SWhisper { + id: 0, + codec: CodecType::OpusVoice, + channels: whisper_channels.clone(), + clients: whisper_clients.clone(), + data: &opus_output[..len], + }) + } else { + OutAudio::new(&AudioData::C2S { + id: 0, + codec: CodecType::OpusVoice, + data: &opus_output[..len], + }) + }; if let Err(e) = con.send_audio(packet) { emit(&Event::Error { message: e.to_string() }); } diff --git a/gateway/src/index.ts b/gateway/src/index.ts index 5b03824..4e97f72 100644 --- a/gateway/src/index.ts +++ b/gateway/src/index.ts @@ -114,6 +114,10 @@ wss.on("connection", (socket: WebSocket) => { await connection?.setOutputMuted(msg.muted); break; } + case "setWhisperTargets": { + await connection?.setWhisperTargets(msg.channelIds ?? [], msg.clientIds ?? []); + break; + } case "disconnect": { await connection?.disconnect(msg.message ?? ""); break; diff --git a/gateway/src/ts3/connection.ts b/gateway/src/ts3/connection.ts index 164d58c..fbf2771 100644 --- a/gateway/src/ts3/connection.ts +++ b/gateway/src/ts3/connection.ts @@ -268,6 +268,16 @@ export class Ts3Connection { this.child?.stdin.write(`muteoutput ${muted ? "1" : "0"}\n`); } + /** Empty channelIds/clientIds clears whisper mode, returning outgoing + * voice to the normal current-channel broadcast. */ + async setWhisperTargets(channelIds: number[], clientIds: number[]): Promise { + if (channelIds.length === 0 && clientIds.length === 0) { + this.child?.stdin.write("unwhisper\n"); + } else { + this.child?.stdin.write(`whisper ${channelIds.join(",")};${clientIds.join(",")}\n`); + } + } + async disconnect(message = ""): Promise { if (this.child && !this.child.killed) { const sanitized = message.replace(/[\r\n]+/g, " ").trim(); diff --git a/web/src/App.tsx b/web/src/App.tsx index a884b0c..5da3be7 100644 --- a/web/src/App.tsx +++ b/web/src/App.tsx @@ -1477,6 +1477,10 @@ function AppInner() { isSelf: boolean; } | null>(null); const clientContextMenuRef = useRef(null); + const [whisperChannelIds, setWhisperChannelIds] = useState>(new Set()); + const [whisperClientIds, setWhisperClientIds] = useState>(new Set()); + const [whisperMenuOpen, setWhisperMenuOpen] = useState(false); + const whisperMenuRef = useRef(null); const [activeTab, setActiveTab] = useState("channel"); const [theme, setTheme] = useState<"light" | "dark">(() => window.matchMedia?.("(prefers-color-scheme: dark)").matches ? "dark" : "light" @@ -2033,6 +2037,40 @@ function AppInner() { }; }, [clientContextMenu]); + useEffect(() => { + if (!connected) { + setWhisperChannelIds(new Set()); + setWhisperClientIds(new Set()); + } + }, [connected]); + + useEffect(() => { + if (!connected) return; + socketRef.current?.send( + JSON.stringify({ + type: "setWhisperTargets", + channelIds: [...whisperChannelIds], + clientIds: [...whisperClientIds], + }) + ); + }, [whisperChannelIds, whisperClientIds, connected]); + + useEffect(() => { + if (!whisperMenuOpen) return; + const onPointerDown = (e: MouseEvent) => { + if (!whisperMenuRef.current?.contains(e.target as Node)) setWhisperMenuOpen(false); + }; + const onKeyDown = (e: KeyboardEvent) => { + if (e.key === "Escape") setWhisperMenuOpen(false); + }; + window.addEventListener("mousedown", onPointerDown); + window.addEventListener("keydown", onKeyDown); + return () => { + window.removeEventListener("mousedown", onPointerDown); + window.removeEventListener("keydown", onKeyDown); + }; + }, [whisperMenuOpen]); + useEffect(() => { if (!extrasMenuOpen) return; const onPointerDown = (e: MouseEvent) => { @@ -2215,6 +2253,29 @@ function AppInner() { setPokeMessage(""); }; + const toggleWhisperChannel = (channelId: number) => { + setWhisperChannelIds((prev) => { + const next = new Set(prev); + if (next.has(channelId)) next.delete(channelId); + else next.add(channelId); + return next; + }); + }; + + const toggleWhisperClient = (clientId: number) => { + setWhisperClientIds((prev) => { + const next = new Set(prev); + if (next.has(clientId)) next.delete(clientId); + else next.add(clientId); + return next; + }); + }; + + const clearWhisperTargets = () => { + setWhisperChannelIds(new Set()); + setWhisperClientIds(new Set()); + }; + const handleClientContextMenu = ( e: React.MouseEvent, clientId: number, @@ -2500,6 +2561,54 @@ function AppInner() { )} +
+ + {whisperMenuOpen && ( +
+
{t("whisper.channels")}
+ {channels.map((ch) => ( + + ))} + {whisperClientIds.size > 0 && ( + <> +
+
{t("whisper.clients")}
+ {[...whisperClientIds].map((id) => ( + + ))} + + )} + {(whisperChannelIds.size > 0 || whisperClientIds.size > 0) && ( + <> +
+ + + )} +
+ )} +
+ + {!clientContextMenu.isSelf && ( + + )}