@@ -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 }