-
Notifications
You must be signed in to change notification settings - Fork 2
local VAD implementation #507
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
5404015
Add client VAD
Magmusacy e0199ee
Change api for client vad
Magmusacy d882387
Remove timestamp
Magmusacy 3ced8eb
Add useLocalVAD and simplify the logic
Magmusacy 50da0eb
Remove setLocalVadStatus
Magmusacy b411809
Change api for useLocalVAD and add/change JSDocs
Magmusacy 8cee0ad
Add dynamic scaling of dbOv to linear scale
Magmusacy 882501c
Merge remote-tracking branch 'origin/main' into FCE-3021
Magmusacy d57b832
Add cue showing that peer is speaking
Magmusacy 7a0bc5d
Fix issues outlined by copilot
Magmusacy 5c309ea
Add more docs
Magmusacy b84a6d0
Fix link issue
Magmusacy afb9791
Prevent bubbling up of the error
Magmusacy aa8e8ce
Fix trackAudio being null potentially making localVAD permantently ac…
Magmusacy 10f75b3
Remove redundant comment
Magmusacy 4401db5
Fix issues pointed out in PR
Magmusacy cc543c6
Fix stupid issues
Magmusacy 8b13b89
Fix the issues i forgot to commit
Magmusacy 039ebb5
Fix JSDocs
Magmusacy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| import { useContext, useEffect, useState } from "react"; | ||
|
|
||
| import { FishjamClientContext } from "../contexts/fishjamClient"; | ||
| import type { PeerId } from "../types/public"; | ||
| import { usePeers } from "./usePeers"; | ||
|
|
||
| // This is a dBov-to-linear conversion. -32 dBov number is taken from backend VAD threshold | ||
| // formula for dBov to linear conversion: linear = 10 ^ (dBov / 20) | ||
| // So -32 dBov = 10^(-32/20) ≈ 0.025. This is the minimum audio level considered "speech". | ||
| const THRESHOLD = 10 ** (-32 / 20); | ||
|
|
||
| // Number of consecutive "silence" ticks before we consider speech to have stopped. Helps with smoothing out brief pauses in speech. | ||
| const SILENCE_DEBOUNCE_TICKS = 2; | ||
|
|
||
| /** | ||
| * Client-side voice activity detection for the local peer. | ||
| * | ||
| * Polls the local microphone's audio level every 100ms and derives a speech/silence | ||
| * state from it. A level above ~0.025 (approximately −32 dBov, scaled to [0, 1]) | ||
| * is treated as speech. Silence is debounced over 2 consecutive ticks (~200ms) | ||
| * to prevent rapid flapping. | ||
| * | ||
| * This is purely client-side — it does not signal other peers. Remote participants | ||
| * receive the local peer's VAD status via backend `vadNotification` messages. | ||
| * | ||
| * @internal Used by `useVAD` when the local peer's id is included in `peerIds`. | ||
| * @returns A record mapping the local peer's id to its current speaking state, | ||
| * or an empty object if `options.disabled` is true, the local peer is not available, or no microphone track is found. | ||
| */ | ||
| export const useLocalVAD = (options: { disabled: boolean }): Record<PeerId, boolean> => { | ||
| const fishjamClient = useContext(FishjamClientContext); | ||
| const [isSpeaking, setIsSpeaking] = useState(false); | ||
| const { localPeer } = usePeers(); | ||
| const localPeerId = localPeer?.id; | ||
| const microphoneTrackId = localPeer?.microphoneTrack?.trackId; | ||
|
|
||
| useEffect(() => { | ||
| if (options.disabled || !localPeerId || !microphoneTrackId) return; | ||
|
|
||
| let silenceTicks = 0; | ||
| let timeoutId: ReturnType<typeof setTimeout>; | ||
|
|
||
| const poll = async () => { | ||
| const trackAudio = await fishjamClient?.current?.getLocalTrackAudioLevel(microphoneTrackId); | ||
| if (trackAudio != null && trackAudio.level > THRESHOLD) { | ||
| silenceTicks = 0; | ||
| setIsSpeaking(true); | ||
| } else { | ||
| silenceTicks += 1; | ||
| if (silenceTicks >= SILENCE_DEBOUNCE_TICKS) { | ||
| setIsSpeaking(false); | ||
| } | ||
| } | ||
|
|
||
| timeoutId = setTimeout(poll, 100); | ||
| }; | ||
|
|
||
| timeoutId = setTimeout(poll, 0); | ||
|
|
||
| return () => { | ||
| clearTimeout(timeoutId); | ||
| setIsSpeaking(false); | ||
| }; | ||
Magmusacy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }, [options.disabled, fishjamClient, localPeerId, microphoneTrackId]); | ||
|
|
||
Magmusacy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if (!localPeerId || options.disabled || !microphoneTrackId) return {}; | ||
| return { [localPeerId]: isSpeaking }; | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.