Repository navigation
fix(desktop): the microphone noise gate runs on the audio thread, in direct calls too - #380
Merged
Merged
Conversation
…ined sound The microphone's noise gate and its "speaking" decision run in an AudioWorkletProcessor of the app's own (noiseGateWorklet.ts), a thin shell around a pure class (noiseGate.ts), placed where the gain gate was: after the noise suppression, or after the raw microphone when suppression is off or has failed. The noise-suppression worklet is unchanged. The rule, on the incoming samples: - The level is the RMS of the last 10 ms (one pitch period of a 100 Hz voice) of the mix of the input's channels, compared with the user's threshold. - A sound opens the gate once its level has stayed over the threshold for 35 ms (25 ms of sound plus the window's memory). A burst that stops within 25 ms does not open it, however loud; a click or a key press whose sound decays slowly enough to keep the level over the threshold that long is a longer sound, and does. - The gate closes 300 ms after the last sample at which a sound met that rule, with a 30 ms ramp. A sound that does not meet the rule passes while the gate is open but does not restart the 300 ms. - The audio leaves through a 40 ms look-ahead delay (the 35 ms decision plus the 5 ms opening ramp), so a sound well over the threshold leaves the gate at full gain from its first sample. This is the latency the gate adds. The processor reports each change of its decision by message; the page does with it what the page timer's tick does (the speaking emit and the speaking callback), and the state it sets is what a later resend sends. The threshold, the pause and a stop reach the processor by message, and the state set before it loads is its start state. In a space call a pause closes the gate; in a direct call it only stops the reports, as the timer's pause leaves a direct call's gain open. The pipeline is built synchronously as before and the gated track stays the same object: the module loads afterwards, only for the pipeline that asked, and takes over from the page timer, which decides meanwhile. A space call's gain starts closed, so nothing passes ungated during the load; a direct call's starts open, as in the fallback (a direct call does not send this track: only its speaking state comes from here). Where the module cannot load, the node cannot be made or the processor fails, the page timer decides from then on, with one warning by message; a direct call keeps its gain open there. In a space call, releasing the push-to-talk key while the audio-thread gate decides pauses the microphone producer 65 ms later (the look-ahead plus a margin), so the audio still in the delay line is sent; the raw microphone track is disabled at once, as before. A key pressed again, a new producer and the end of the session cancel the pending pause; a mute, a deafen, a server mute of a member not muted already, a pipeline rebuild and a change of voice mode make it immediate. While the page timer decides the pause is immediate, as before. getSpeakingDecider() answers which of the two decides. A development build exposes it as a global, and the secure-voice end-to-end spec waits for the audio-thread gate after a join.
A direct call's audio senders take the gated stream, as a space call's microphone producer does: a new peer adds the gated track, a pipeline rebuild swaps each peer's audio sender to the new gated track with replaceTrack, and a peer with no audio sender yet (a call joined listen-only) has the gated track added. The suppressed stream, then the raw microphone, are used only while no gated stream exists; a start of speaking detection that fails halfway leaves none. The audio-thread noise gate therefore gates a direct call, and the direct call's speaking indicator follows its decision. While the page timer decides (the gate's module loading, or the fallback) a direct call's gain stays open on the same gated stream, so the call is heard as before. The ring a person sees on their own avatar in a direct call follows the decision the other person is told (isSpeechDetected) instead of a level meter of its own: it lights when the detector hears speech, at the user's threshold, and stays off while muted, with the push-to-talk key held too, as it does for the other person. getAudioLevel, which only that ring read, is removed. Mute, deafen and push-to-talk still disable the raw microphone track, from which the gated stream is made. Nothing else is paused at a direct call's push-to-talk release, so the delay line drains to the peers.
…ise gate Below 100 Hz a voice's pitch period is longer than the gate's 10 ms level window, which then misses each period's pulse for the period minus the window: the level of a steady voiced sound dips under the threshold inside every period, and a deep voice only slightly over the threshold never stayed over it for the opening time. A dip of the level under the threshold no longer than 5 ms (the gap a pitch period leaves in the window down to about 67 Hz) now pauses the count of the time the sound has been over the threshold instead of ending the sound. The dip adds nothing to the count, so short bursts with short gaps between them must still be over the threshold for the whole opening time, and only a sample over the threshold opens the gate or restarts its hold. While the gate is open, a sound that has met the opening rule goes on through a dip of up to 20 ms: a voice that drops to a creak at the end of a phrase (pulses 17 to 25 ms apart) no longer closes the gate 300 ms into the creak, which the gate on the page timer passed whole. The opening rule is unchanged. That bridge has a cost, measured after speech. A sound that starts within 20 ms of the level falling under the threshold continues the sound before it: a key press 15 to 25 ms after a word closes the gate 30 to 50 ms later. So does the ripple of a noise just under the threshold: at 0.98 times the threshold the gate closes a median of 22 ms later for white noise and 40 ms for a low-passed one, about 100 and 340 ms later in the worst tenth of 100 runs; at 0.9 times, nothing for white noise and a median of 15 ms for the low-passed one. Both bridges are by time, not by a second, lower level: only a sample over the threshold counts as sound.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
What was wrong
What the code does now
noiseGateWorklet.ts): a thin shell around a pure class (noiseGate.ts), placed after the noise suppression, or after the raw microphone when suppression is off or has failed. The noise suppression's worklet is unchanged.GATE_HOLD_BRIDGE_MSset toGATE_DIP_BRIDGE_MSthe gate behaves as without this bridge.replaceTrack.Key files
apps/desktop/src/services/noiseGate.ts(the rule and its constants, with the reason for each)apps/desktop/src/services/noiseGateWorklet.tsapps/desktop/src/services/audioAnalyser.ts(the load, the hand-over, the fallback)apps/desktop/src/stores/voiceStore.ts(which track a direct call sends; the pause at a push-to-talk release)apps/desktop/src/hooks/usePushToTalk.ts,apps/desktop/src/hooks/useLocalSpeaking.tstests/e2e/secure-voice.spec.ts(waits for the audio-thread gate after a join, through a development-only global)Migrations
None. The server is unchanged.
Tests
noiseGate.test.ts: bursts, sustained sound and the look-ahead, low-pitched voices, a creak while open, the hold, bursts after speech, threshold, pause, reports, sample rates, block sizes, channels.noiseGateWorklet.test.ts: the processor shell.audioThreadGate.test.ts: the load and the hand-over, a load that never settles in both kinds of call, the fallback, a late load after a rebuild, a start that fails halfway, the track's identity, messages, the mode, what decides.voicePushToTalkRelease.test.tsx: the delayed pause and every way it is cancelled or made immediate, driven through the real hook.voiceMicPipeline.test.ts: a direct call's senders take the gated track.useLocalSpeaking.test.tsx: the ring follows the detector.pnpm typecheck,pnpm lint, the desktop and server unit suites and the desktop build pass on the branch. The build emits the worklet as a file of the app's own origin, beside the noise suppression's.To check by hand
[SpeakingDetection] The audio-thread noise gate(that line is the fallback). In the installed desktop app above all.