-
Notifications
You must be signed in to change notification settings - Fork 291
feat: automatically equalize participant audio levels #1689
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| import { useEffect, useRef } from 'react' | ||
| import { useRemoteParticipants, useRoomContext } from '@livekit/components-react' | ||
| import { RemoteParticipant, RemoteTrackPublication, Track } from 'livekit-client' | ||
| import { useSnapshot } from 'valtio' | ||
| import { userChoicesStore } from '@/stores/userChoices' | ||
|
|
||
| const TARGET_LEVEL = 0.12 | ||
| const ADAPTATION_RATE = 0.05 | ||
| const MIN_GAIN = 0.2 | ||
| const MAX_GAIN = 3.0 | ||
| const TICK_INTERVAL_MS = 500 | ||
|
|
||
| interface ParticipantGainState { | ||
| currentGain: number | ||
| smoothedLevel: number | ||
| } | ||
|
|
||
| /** | ||
| * Adaptive per-participant audio level normalization (issue #1345). | ||
| * | ||
| * Every TICK_INTERVAL_MS, samples each remote participant's audioLevel, | ||
| * smooths it via EMA, and gradually adjusts their audio element volume | ||
| * toward TARGET_LEVEL. Gain is clamped to [MIN_GAIN, MAX_GAIN]. | ||
| * Resets all volumes to 1.0 when disabled. | ||
| */ | ||
| export const useAudioLevelEqualization = () => { | ||
| const room = useRoomContext() | ||
| const remoteParticipants = useRemoteParticipants() | ||
| const { audioLevelEqualizationEnabled } = useSnapshot(userChoicesStore) | ||
| const gainStateRef = useRef<Map<string, ParticipantGainState>>(new Map()) | ||
|
|
||
| useEffect(() => { | ||
| if (!audioLevelEqualizationEnabled) { | ||
| for (const participant of room.remoteParticipants.values()) { | ||
| const audioElement = getAudioElement(participant) | ||
| if (audioElement) audioElement.volume = 1.0 | ||
| } | ||
| gainStateRef.current.clear() | ||
| return | ||
| } | ||
|
|
||
| const tick = () => { | ||
| for (const participant of room.remoteParticipants.values()) { | ||
| const sid = participant.sid | ||
| const audioLevel = participant.audioLevel ?? 0 | ||
|
|
||
| if (!gainStateRef.current.has(sid)) { | ||
| gainStateRef.current.set(sid, { currentGain: 1.0, smoothedLevel: audioLevel }) | ||
| } | ||
|
|
||
| const state = gainStateRef.current.get(sid)! | ||
|
|
||
| // EMA to avoid reacting to transient spikes | ||
| state.smoothedLevel = | ||
| state.smoothedLevel * (1 - ADAPTATION_RATE) + audioLevel * ADAPTATION_RATE | ||
|
|
||
| // Only adjust gain when participant is speaking, not on silence/noise floor | ||
| if (state.smoothedLevel > 0.01) { | ||
| const ratio = TARGET_LEVEL / state.smoothedLevel | ||
| state.currentGain = | ||
| state.currentGain * (1 - ADAPTATION_RATE) + ratio * ADAPTATION_RATE | ||
| } | ||
|
|
||
| state.currentGain = Math.min(MAX_GAIN, Math.max(MIN_GAIN, state.currentGain)) | ||
|
|
||
| const audioElement = getAudioElement(participant) | ||
| if (audioElement) audioElement.volume = state.currentGain | ||
|
Comment on lines
+64
to
+67
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 1. Quiet speakers halt audio leveling tick clamps state.currentGain at 3.0 and writes it directly to HTMLMediaElement.volume, whose valid range ends at 1.0. Whenever a participant’s sampled level is below the 0.12 target, the calculated ratio can exceed one—for example, a level of 0.02 yields 1.25 on the first update—so the assignment throws, prevents that quiet speaker from being boosted, and aborts the interval callback before later participants are adjusted. Agent Prompt
|
||
| } | ||
|
|
||
| // Clean up state for departed participants | ||
| for (const sid of gainStateRef.current.keys()) { | ||
| if (!room.remoteParticipants.has(sid)) { | ||
| gainStateRef.current.delete(sid) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| const intervalId = setInterval(tick, TICK_INTERVAL_MS) | ||
| return () => clearInterval(intervalId) | ||
| }, [audioLevelEqualizationEnabled, room, remoteParticipants]) | ||
| } | ||
|
|
||
| function getAudioElement(participant: RemoteParticipant): HTMLAudioElement | null { | ||
| const micPub = participant.getTrackPublication( | ||
| Track.Source.Microphone | ||
| ) as RemoteTrackPublication | undefined | ||
|
|
||
| if (!micPub?.trackSid) return null | ||
|
|
||
| return document.querySelector<HTMLAudioElement>( | ||
| `audio[data-lk-sid="${micPub.trackSid}"]` | ||
| ) | ||
|
Comment on lines
+90
to
+92
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 3. Equalization never reaches remote audio getAudioElement searches for an audio[data-lk-sid] node, but remote playback is delegated to RoomAudioRenderer and no repository-owned renderer creates an audio element with that attribute. Every tick therefore receives null and skips the only volume assignment, so enabling the setting has no effect for remote participants. Agent Prompt
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,6 +17,7 @@ import { | |
| saveAudioInputDeviceId, | ||
| saveAudioOutputDeviceId, | ||
| saveNoiseReductionEnabled, | ||
| saveAudioLevelEqualizationEnabled, | ||
| userChoicesStore, | ||
| } from '@/stores/userChoices' | ||
| import { captureEvent } from '@/features/analytics/telemetry' | ||
|
|
@@ -30,7 +31,7 @@ export const AudioTab = ({ id }: AudioTabProps) => { | |
| const { t } = useTranslation('settings') | ||
| const { localParticipant } = useRoomContext() | ||
|
|
||
| const { noiseReductionEnabled, audioDeviceId, audioOutputDeviceId } = | ||
| const { noiseReductionEnabled, audioDeviceId, audioOutputDeviceId, audioLevelEqualizationEnabled } = | ||
| useSnapshot(userChoicesStore) | ||
|
|
||
| const isSpeaking = useIsSpeaking(localParticipant) | ||
|
|
@@ -136,6 +137,22 @@ export const AudioTab = ({ id }: AudioTabProps) => { | |
| <div /> | ||
| </RowWrapper> | ||
| )} | ||
| {/* Audio level equalization — issue #1345 */} | ||
| <RowWrapper heading={t('audio.audioLevelEqualization.heading')} beta> | ||
| <Switch | ||
| aria-label={t( | ||
| `audio.audioLevelEqualization.ariaLabel.${audioLevelEqualizationEnabled ? 'disable' : 'enable'}` | ||
| )} | ||
| isSelected={audioLevelEqualizationEnabled} | ||
| onChange={(v) => { | ||
| saveAudioLevelEqualizationEnabled(v) | ||
| if (v) captureEvent('audio-level-equalization-init') | ||
| }} | ||
| > | ||
| {t('audio.audioLevelEqualization.label')} | ||
| </Switch> | ||
| <div /> | ||
|
Comment on lines
+152
to
+154
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 2. Users miss the setting explanation AudioTab renders an empty <div /> beside the switch instead of the translated audio.audioLevelEqualization.description value. Whenever users evaluate this beta setting, they see its label but not the available explanation of its continuous per-participant behavior. Agent Prompt
|
||
| </RowWrapper> | ||
| </TabPanel> | ||
| ) | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
4. Pauses make the next words too loud
🐞 Bug≡ CorrectnessAgent Prompt
ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools