From acb62df85962bbad65de1864e8aab38b574066c8 Mon Sep 17 00:00:00 2001 From: Leo dev Date: Tue, 6 Jan 2026 18:37:51 +0100 Subject: [PATCH] Improving the structure --- src/components/app/SidebarChannels.tsx | 14 +------ src/components/channel/ChannelView.tsx | 2 +- src/components/channel/VoiceBox.tsx | 51 ++++++++++++++++++++++++-- src/lib/audio.ts | 10 +---- 4 files changed, 53 insertions(+), 24 deletions(-) diff --git a/src/components/app/SidebarChannels.tsx b/src/components/app/SidebarChannels.tsx index 28bafba..4c18047 100644 --- a/src/components/app/SidebarChannels.tsx +++ b/src/components/app/SidebarChannels.tsx @@ -1,10 +1,9 @@ "use client"; import { ScrollArea } from "../ui/scroll-area"; -import { Server, Channel } from "@/types/types"; +import { Channel } from "@/types/types"; import { Card } from "../ui/card"; -import { HashIcon, PhoneOff, Volume2Icon } from "lucide-react"; +import { HashIcon, Volume2Icon } from "lucide-react"; import App from "@/types/app"; -import { Button } from "../ui/button"; export function ChannelIcon({ kind }: { kind: "text" | "voice" }) { switch (kind) { @@ -50,15 +49,6 @@ export default function SidebarChannels({ app }: { app: App }) { ))} - - ); } diff --git a/src/components/channel/ChannelView.tsx b/src/components/channel/ChannelView.tsx index c5d0a21..775b3db 100644 --- a/src/components/channel/ChannelView.tsx +++ b/src/components/channel/ChannelView.tsx @@ -54,7 +54,7 @@ export default function ChannelView({ }} sendVoice={(data) => { while (!serverRef.current || serverRef.current?.CONNECTING) {} - sendVoice(serverRef.current, 0, data); + sendVoice(serverRef.current, data); }} indicators={indicators.filter( (indicator) => diff --git a/src/components/channel/VoiceBox.tsx b/src/components/channel/VoiceBox.tsx index 263437d..407272c 100644 --- a/src/components/channel/VoiceBox.tsx +++ b/src/components/channel/VoiceBox.tsx @@ -5,27 +5,72 @@ import { ClientMessage, IndicatorContext } from "@/types/protocol"; import { Message } from "@/types/types"; import { useEffect, useState } from "react"; import { startMic } from "@/lib/audio"; +import { Button } from "../ui/button"; +import { + ChevronLeftIcon, + PhoneCall, + PhoneOff, + Volume2Icon, +} from "lucide-react"; export default function VoiceBox({ sendVoice, + app, + channelName, }: { app: App; sendRequest: (req: ClientMessage) => void; channelName?: string; - channelIcon?: string; messages: Message[]; indicators: IndicatorContext[]; sendVoice: (data: Int16Array) => void; }) { + const [voiceConn, setVoiceConn] = useState(false); + useEffect(() => { + if (!voiceConn) return; + const stop = startMic(sendVoice); return () => { stop.then((s) => s()); }; - }, []); + }, [voiceConn]); return ( -
+
+ {channelName && ( +
+ app.setSidebarOpen(true)} + className="cursor-pointer flex gap-2" + > + + + {channelName} + +
+ )} + {voiceConn ? ( +
+
+ +
+
+ ) : ( + + )} +
); } diff --git a/src/lib/audio.ts b/src/lib/audio.ts index d71e39e..0823394 100644 --- a/src/lib/audio.ts +++ b/src/lib/audio.ts @@ -62,14 +62,8 @@ function float32ToPCM16(float32: Float32Array): Int16Array { return pcm16; } -export function sendVoice(ws: WebSocket, voiceId: number, pcm: Int16Array) { - const buffer = new ArrayBuffer(2 + pcm.byteLength); - const view = new DataView(buffer); - - view.setUint16(0, voiceId, true); // little endian - new Uint8Array(buffer, 2).set(new Uint8Array(pcm.buffer)); - - ws.send(buffer); +export function sendVoice(ws: WebSocket, pcm: Int16Array) { + ws.send(new Uint8Array(pcm.buffer, pcm.byteOffset, pcm.byteLength)); } let ctx: AudioContext | null = null;