xonecas.io
8058
Ir

irvc / commit b05994d

feat: noise gate + speaking status

You can now see whos speaking, and tweak the silence treshold in
settings

xonecas2026-10-12 b05994db54210be12b36ec7b8aa5a07b24322ec4 parent b5a36d8

5 files changed

web/src/components/SettingsPanel.tsx+57−1
@@ -1,9 +1,18 @@
1-import { useEffect, useState } from "react";
1+import { useEffect, useRef, useState } from "react";
22 import { HeadphoneOff, Headphones } from "lucide-react";
33 import { Room } from "livekit-client";
44 import { Button } from "@/components/ui/button";
55 import { cn } from "cn";
66 import type { AudioPresetName, Settings } from "../lib/settings";
7+import { useMicPreview, type Voice } from "../lib/voice";
8+
9+const minDb = -60;
10+const maxDb = -10;
11+
12+// dbPct maps a dBFS value onto 0..100 across the slider's range.
13+function dbPct(db: number) {
14+ return Math.max(0, Math.min(1, (db - minDb) / (maxDb - minDb))) * 100;
15+}
716
817 const presets: { value: AudioPresetName; label: string }[] = [
918 { value: "telephone", label: "telephone · 12 kbps" },
@@ -39,12 +48,18 @@ function useDevices(kind: MediaDeviceKind) {
3948 export default function SettingsPanel({
4049 settings,
4150 update,
51+ voice,
4252 }: {
4353 settings: Settings;
4454 update: (patch: Partial<Settings>) => void;
55+ voice: Voice;
4556 }) {
4657 const mics = useDevices("audioinput");
4758 const speakers = useDevices("audiooutput");
59+ // Outside a call there is no published track to meter, so open the mic for a
60+ // preview. In a call, reuse the live level to avoid capturing twice.
61+ const preview = useMicPreview(settings, voice.status !== "live");
62+ const meter = voice.status === "live" ? voice.micLevel : preview;
4863
4964 return (
5065 <aside className="w-64 shrink-0 overflow-y-auto border-l border-sepia/15">
@@ -62,6 +77,22 @@ export default function SettingsPanel({
6277 </Button>
6378 </Group>
6479
80+ <Group label="Voice activation">
81+ <Level meter={meter} threshold={settings.gateThreshold} />
82+ <input
83+ type="range"
84+ min={minDb}
85+ max={maxDb}
86+ step={1}
87+ value={settings.gateThreshold}
88+ onChange={(e) => update({ gateThreshold: Number(e.target.value) })}
89+ className="w-full accent-gold"
90+ />
91+ <div className="px-1 text-xs text-sepia/50">
92+ transmits above {settings.gateThreshold} dB
93+ </div>
94+ </Group>
95+
6596 <Group label="Devices">
6697 <Field label="microphone">
6798 <Select
@@ -120,6 +151,31 @@ export default function SettingsPanel({
120151 );
121152 }
122153
154+// Level draws the live mic level and marks where the gate threshold sits, so
155+// the slider can be placed above the room's background noise.
156+function Level({ meter, threshold }: { meter: () => number; threshold: number }) {
157+ const ref = useRef<HTMLDivElement>(null);
158+ useEffect(() => {
159+ let raf = 0;
160+ const tick = () => {
161+ if (ref.current) ref.current.style.width = `${dbPct(meter())}%`;
162+ raf = requestAnimationFrame(tick);
163+ };
164+ raf = requestAnimationFrame(tick);
165+ return () => cancelAnimationFrame(raf);
166+ }, [meter]);
167+
168+ return (
169+ <div className="relative mx-1 h-1.5 overflow-hidden rounded bg-muted">
170+ <div ref={ref} className="h-full bg-gold/70" />
171+ <div
172+ className="absolute inset-y-0 w-0.5 bg-sepia/60"
173+ style={{ left: `${dbPct(threshold)}%` }}
174+ />
175+ </div>
176+ );
177+}
178+
123179 function Group({ label, children }: { label: string; children: React.ReactNode }) {
124180 return (
125181 <div className="space-y-1.5">
web/src/components/VoicePeers.tsx+13−3
@@ -1,5 +1,6 @@
11 import { useEffect, useState } from "react";
22 import { MicOff, Volume2, VolumeX } from "lucide-react";
3+import { cn } from "cn";
34 import { Button } from "@/components/ui/button";
45 import type { Voice } from "../lib/voice";
56
@@ -14,7 +15,7 @@ function load(): Record<string, number> {
1415 }
1516
1617 export default function VoicePeers({ voice, username }: { voice: Voice; username: string }) {
17- const { peers, muted: selfMuted, setPeerVolume } = voice;
18+ const { peers, muted: selfMuted, setPeerVolume, speaking } = voice;
1819 const [volumes, setVolumes] = useState<Record<string, number>>(load);
1920 const [muted, setMuted] = useState<Set<string>>(new Set());
2021 const [open, setOpen] = useState<string | null>(null);
@@ -45,7 +46,7 @@ export default function VoicePeers({ voice, username }: { voice: Voice; username
4546 return (
4647 <>
4748 <div className="flex items-center gap-1.5 text-xs text-sepia/70">
48- <span className="h-1.5 w-1.5 rounded-full bg-gold" />
49+ <Dot on={speaking.includes(username)} />
4950 {username} <span className="text-sepia/40">(you)</span>
5051 {selfMuted && <MicOff size={10} className="text-sepia/50" />}
5152 </div>
@@ -55,7 +56,7 @@ export default function VoicePeers({ voice, username }: { voice: Voice; username
5556 onClick={() => setOpen((o) => (o === p ? null : p))}
5657 className="flex w-full items-center gap-1.5 text-left text-xs text-sepia/70 hover:text-sepia"
5758 >
58- <span className="h-1.5 w-1.5 rounded-full bg-gold" />
59+ <Dot on={speaking.includes(p)} />
5960 {p}
6061 {muted.has(p) && <VolumeX size={10} className="text-sepia/50" />}
6162 </button>
@@ -86,3 +87,12 @@ export default function VoicePeers({ voice, username }: { voice: Voice; username
8687 </>
8788 );
8889 }
90+
91+// The dot stays dim until the participant is speaking, then lights up.
92+function Dot({ on }: { on: boolean }) {
93+ return (
94+ <span
95+ className={cn("h-1.5 w-1.5 rounded-full transition-colors", on ? "bg-gold" : "bg-sepia/25")}
96+ />
97+ );
98+}
web/src/lib/settings.ts+2−0
@@ -17,6 +17,7 @@ export interface Settings {
1717 micDeviceId: string;
1818 speakerDeviceId: string;
1919 deafened: boolean;
20+ gateThreshold: number;
2021 }
2122
2223 export const defaultSettings: Settings = {
@@ -28,6 +29,7 @@ export const defaultSettings: Settings = {
2829 micDeviceId: "",
2930 speakerDeviceId: "",
3031 deafened: false,
32+ gateThreshold: -60,
3133 };
3234
3335 const key = "irvc.settings";
web/src/lib/voice.ts+154−5
@@ -1,6 +1,11 @@
11 import { useCallback, useEffect, useRef, useState } from "react";
22 import { AudioPresets, Room, RoomEvent, Track } from "livekit-client";
3-import type { AudioCaptureOptions } from "livekit-client";
3+import type {
4+ AudioCaptureOptions,
5+ AudioProcessorOptions,
6+ Participant,
7+ TrackProcessor,
8+} from "livekit-client";
49 import { api } from "./api";
510 import type { Settings } from "./settings";
611 import type { Channel } from "./types";
@@ -11,6 +16,123 @@ interface VoiceToken {
1116 room: string;
1217 }
1318
19+// dbfs computes the RMS level, in dBFS, of an analyser's latest time-domain
20+// samples. Near-silence clamps toward -inf via the epsilon.
21+function dbfs(analyser: AnalyserNode, buf: Float32Array<ArrayBuffer>): number {
22+ analyser.getFloatTimeDomainData(buf);
23+ let sum = 0;
24+ for (const v of buf) sum += v * v;
25+ return 20 * Math.log10(Math.sqrt(sum / buf.length) + 1e-8);
26+}
27+
28+// A hard gate that keeps the microphone silent while its level is below the
29+// user's threshold, so room noise and keyboard clatter are not transmitted.
30+// The threshold lives in a ref, so the slider takes effect without republishing
31+// the track. `level` reports dBFS for the settings meter.
32+interface MicGate {
33+ processor: TrackProcessor<Track.Kind.Audio, AudioProcessorOptions>;
34+ threshold: { current: number };
35+ level: () => number;
36+}
37+
38+function micGate(): MicGate {
39+ const threshold = { current: -60 };
40+ const state = { level: -100 };
41+ let timer = 0;
42+
43+ const processor: TrackProcessor<Track.Kind.Audio, AudioProcessorOptions> = {
44+ name: "irvc-mic-gate",
45+ async init({ track, audioContext }) {
46+ clearInterval(timer);
47+ const source = audioContext.createMediaStreamSource(new MediaStream([track]));
48+ const analyser = audioContext.createAnalyser();
49+ analyser.fftSize = 2048;
50+ const gain = audioContext.createGain();
51+ const dest = audioContext.createMediaStreamDestination();
52+ source.connect(analyser);
53+ source.connect(gain);
54+ gain.connect(dest);
55+ processor.processedTrack = dest.stream.getAudioTracks()[0];
56+
57+ const buf = new Float32Array(analyser.fftSize);
58+ let hold = 0;
59+ timer = window.setInterval(() => {
60+ const db = dbfs(analyser, buf);
61+ state.level = db;
62+ const now = performance.now();
63+ if (db >= threshold.current) hold = now + 250;
64+ gain.gain.value = now < hold ? 1 : 0;
65+ }, 20);
66+ },
67+ async restart(opts) {
68+ await processor.destroy();
69+ await processor.init(opts);
70+ },
71+ async destroy() {
72+ clearInterval(timer);
73+ timer = 0;
74+ state.level = -100;
75+ },
76+ };
77+
78+ return { processor, threshold, level: () => state.level };
79+}
80+
81+// useMicPreview opens the microphone on its own so the settings meter works
82+// before joining a call. It returns a dBFS level getter. Nothing is published
83+// or played; the stream exists only for metering.
84+export function useMicPreview(settings: Settings, enabled: boolean): () => number {
85+ const level = useRef(-100);
86+ const settingsRef = useRef(settings);
87+ settingsRef.current = settings;
88+
89+ useEffect(() => {
90+ if (!enabled) return;
91+ let ctx: AudioContext | null = null;
92+ let stream: MediaStream | null = null;
93+ let raf = 0;
94+ let cancelled = false;
95+ (async () => {
96+ try {
97+ ctx = new AudioContext();
98+ const mic = await navigator.mediaDevices.getUserMedia({
99+ audio: capture(settingsRef.current),
100+ });
101+ if (cancelled) {
102+ mic.getTracks().forEach((t) => t.stop());
103+ ctx.close();
104+ return;
105+ }
106+ stream = mic;
107+ ctx.resume().catch(() => {});
108+ const analyser = ctx.createAnalyser();
109+ analyser.fftSize = 2048;
110+ ctx.createMediaStreamSource(mic).connect(analyser);
111+ const buf = new Float32Array(analyser.fftSize);
112+ const tick = () => {
113+ level.current = dbfs(analyser, buf);
114+ raf = requestAnimationFrame(tick);
115+ };
116+ tick();
117+ } catch {
118+ level.current = -100;
119+ }
120+ })();
121+ return () => {
122+ cancelled = true;
123+ cancelAnimationFrame(raf);
124+ stream?.getTracks().forEach((t) => t.stop());
125+ ctx?.close();
126+ level.current = -100;
127+ };
128+ // Restart on the device the user actually selected; other processing
129+ // toggles shift the level only slightly and should not re-prompt.
130+ // eslint-disable-next-line react-hooks/exhaustive-deps
131+ }, [enabled, settings.micDeviceId]);
132+
133+ return useCallback(() => level.current, []);
134+}
135+
14136 // capture maps our settings to LiveKit/browser microphone constraints.
15137 function capture(s: Settings): AudioCaptureOptions {
16138 return {
@@ -46,11 +168,13 @@ export interface Voice {
46168 status: VoiceStatus;
47169 error: string;
48170 peers: string[];
171+ speaking: string[];
49172 muted: boolean;
50173 join: (c: Channel) => void;
51174 leave: () => void;
52175 toggleMute: () => void;
53176 setPeerVolume: (name: string, volume: number) => void;
177+ micLevel: () => number;
54178 }
55179
56180 // useVoice owns the single LiveKit connection for the session so it survives
@@ -62,6 +186,7 @@ export function useVoice(settings: Settings): Voice {
62186 const [status, setStatus] = useState<VoiceStatus>("idle");
63187 const [error, setError] = useState("");
64188 const [peers, setPeers] = useState<string[]>([]);
189+ const [speaking, setSpeaking] = useState<string[]>([]);
65190 const [muted, setMuted] = useState(false);
66191 const roomRef = useRef<Room | null>(null);
67192 const audioRef = useRef<HTMLAudioElement[]>([]);
@@ -69,9 +194,15 @@ export function useVoice(settings: Settings): Voice {
69194 const settingsRef = useRef(settings);
70195 const mutedRef = useRef(false);
71196 const appliedRef = useRef("");
197+ const gateRef = useRef<MicGate | null>(null);
72198 settingsRef.current = settings;
73199 mutedRef.current = muted;
74200
201+ if (!gateRef.current) gateRef.current = micGate();
202+ const gate = gateRef.current;
203+ gate.threshold.current = settings.gateThreshold;
204+ const micLevel = useCallback(() => gate.level(), [gate]);
205+
75206 const leave = useCallback(() => {
76207 genRef.current++;
77208 const room = roomRef.current;
@@ -84,6 +215,7 @@ export function useVoice(settings: Settings): Voice {
84215 setStatus("idle");
85216 setError("");
86217 setPeers([]);
218+ setSpeaking([]);
87219 setMuted(false);
88220 }, []);
89221
@@ -106,6 +238,9 @@ export function useVoice(settings: Settings): Voice {
106238 setPeers([...room.remoteParticipants.values()].map((p) => p.name || p.identity));
107239 room.on(RoomEvent.ParticipantConnected, sync);
108240 room.on(RoomEvent.ParticipantDisconnected, sync);
241+ room.on(RoomEvent.ActiveSpeakersChanged, (speakers: Participant[]) =>
242+ setSpeaking(speakers.map((p) => p.name || p.identity)),
243+ );
109244 room.on(RoomEvent.TrackSubscribed, (track) => {
110245 if (track.kind !== Track.Kind.Audio) return;
111246 const el = track.attach() as HTMLAudioElement;
@@ -124,7 +259,8 @@ export function useVoice(settings: Settings): Voice {
124259 room.disconnect();
125260 return;
126261 }
127- await room.localParticipant.setMicrophoneEnabled(true, capture(s), publish(s));
262+ const mic = await room.localParticipant.setMicrophoneEnabled(true, capture(s), publish(s));
263+ if (mic?.audioTrack) await mic.audioTrack.setProcessor(gate.processor);
128264 if (s.speakerDeviceId) {
129265 await room.switchActiveDevice("audiooutput", s.speakerDeviceId).catch(() => {});
130266 }
@@ -137,7 +273,7 @@ export function useVoice(settings: Settings): Voice {
137273 setError((err as Error).message);
138274 }
139275 },
140- [leave],
276+ [leave, gate],
141277 );
142278
143279 // Apply mic device / processing / quality changes to a live call. LiveKit
@@ -156,9 +292,10 @@ export function useVoice(settings: Settings): Voice {
156292 capture(settings),
157293 publish(settings),
158294 );
295+ if (p?.audioTrack) await p.audioTrack.setProcessor(gate.processor);
159296 if (mutedRef.current) await p?.mute();
160297 })().catch((err) => setError((err as Error).message));
161- }, [settings, status]);
298+ }, [settings, status, gate]);
162299
163300 // Speaker changes route through setSinkId on browsers that support it.
164301 useEffect(() => {
@@ -197,5 +334,17 @@ export function useVoice(settings: Settings): Voice {
197334 [],
198335 );
199336
200- return { channel, status, error, peers, muted, join, leave, toggleMute, setPeerVolume };
337+ return {
338+ channel,
339+ status,
340+ error,
341+ peers,
342+ speaking,
343+ muted,
344+ join,
345+ leave,
346+ toggleMute,
347+ setPeerVolume,
348+ micLevel,
349+ };
201350 }
web/src/views/Chat.tsx+1−1
@@ -118,7 +118,7 @@ export default function Chat({ user, onLogout }: { user: User; onLogout: () => v
118118 </header>
119119 <ChatPane channel={selected} messages={messages} />
120120 </main>
121- {panel === "settings" && <SettingsPanel settings={settings} update={update} />}
121+ {panel === "settings" && <SettingsPanel settings={settings} update={update} voice={voice} />}
122122 {panel === "users" && <UserList users={users} online={online} />}
123123 </div>
124124 );