Skip to content

fix(desktop): the microphone noise gate runs on the audio thread, in direct calls too - #380

Merged
Aizen93 merged 4 commits into
developfrom
fix/voice-gate-on-the-audio-thread
Nov 28, 2026
Merged

Aizen93 merged 4 commits into
developfrom
fix/voice-gate-on-the-audio-thread

Conversation

@Aizen93

@Aizen93 Aizen93 commented Nov 28, 2026

Copy link
Copy Markdown
Owner

What was wrong

  • A direct call sent the noise-suppressed microphone with no gate, so clicks and key presses were heard whenever they happened.
  • In a space call the gate opened for any sound over the threshold, after the sound had begun, and every such sound restarted its 300 ms hold: typing after a sentence kept it open.
  • The gate and the "speaking" state ran on a 20 ms timer of the page. A browser slows the timers of a hidden or minimised tab to about one a second, so the gate and the "speaking" notice starved and the person was not heard.

What the code does now

  • The microphone's noise gate and its "speaking" decision run on the audio thread, in an AudioWorkletProcessor of the app's own (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.
  • The rule:
    • The level is the RMS over 10 ms of the mix of the input's channels, against the user's threshold.
    • The gate opens for a sound whose level has stayed over the threshold for 35 ms (25 ms of sound plus the window's memory). A dip of up to 5 ms pauses that count, so a low-pitched voice is not cut into pieces.
    • It closes 300 ms after the last sound that met that rule, with a 30 ms ramp. A shorter sound passes while the gate is open and, the bridge of the next point apart, does not restart the hold.
    • While the gate is open, a sound that met the rule goes on through a dip of up to 20 ms, so a voice that ends a phrase in a creak is not cut 300 ms into it (the gate on the page timer passed such a creak whole). The cost, measured after speech: a key press 15 to 25 ms after a word closes the gate 30 to 50 ms later, and a noise just under the threshold keeps it open longer (at 0.98 times the threshold a median of 22 ms for white noise and 40 ms for a low-passed one, about 100 and 340 ms 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). With GATE_HOLD_BRIDGE_MS set to GATE_DIP_BRIDGE_MS the gate behaves as without this bridge.
    • What it rejects is a SHORT sound, not every key press: a press whose sound rings long enough to keep the level over the threshold for 35 ms still opens the gate, and a louder press stays over a low threshold longer.
    • The audio path is delayed by 40 ms, so the start of a word well over the threshold is kept whole (near the threshold it can be faded or partly lost).
  • The processor reports "speaking" changes to the page by message, which a hidden page still receives. The page does with each what the timer did.
  • The pipeline is still built synchronously. The gated track is the same object before and after the gate attaches, and the page timer decides until the module has loaded: meanwhile a space call is gated by the timer and a direct call's gain is open, as before this change.
  • Where the module cannot load or the processor fails, the page timer gate decides, with one warning. A direct call's gain then stays open, as before.
  • A direct call's audio senders take the gated track, swapped with replaceTrack.
  • Push-to-talk in a space call: on release the microphone track is disabled at once, as before, and while the audio-thread gate decides the producer is paused 65 ms later, so the end of the last word, still in the 40 ms delay line, is sent. A key pressed again, a new producer or the end of the session cancels the pending pause; mute, deafen, a server mute of a member not muted already, a pipeline rebuild and a change of voice mode pause at once.
  • The ring a person sees on their own avatar in a direct call follows the decision the other person is told, not a level meter of its own. It stays off while muted, with the push-to-talk key held too, as it does for the other person.
  • A start of speaking detection that fails halfway leaves no gated stream, so the call sends the suppressed or the raw microphone.

Key files

  • apps/desktop/src/services/noiseGate.ts (the rule and its constants, with the reason for each)
  • apps/desktop/src/services/noiseGateWorklet.ts
  • apps/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.ts
  • tests/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

  1. After joining a call, the console shows no line starting with [SpeakingDetection] The audio-thread noise gate (that line is the fallback). In the installed desktop app above all.
  2. Space call and direct call: the first word after a silence is whole; a single click or key in silence is not heard; typing right after a sentence: short key sounds are no longer heard once the hold has passed (a press that rings can still open the gate).
  3. A hidden or minimised browser tab, in both kinds of call: the other person hears you and sees your ring.
  4. Push-to-talk in a space call: the last word before the release is heard to its end, and nothing after it. Mute and deafen cut at once, as before, in both kinds of call; a server mute does in a space call.
  5. A direct call is heard from its first word.
  6. A voice that ends a phrase in a creak is heard to its end.
  7. With a fan or room noise close to the threshold and noise suppression off: the noise is not heard noticeably longer after each phrase than before.
  8. Noise suppression off: the same checks.

…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.
@Aizen93
Aizen93 merged commit 73ba25d into develop Nov 28, 2026
7 checks passed
@Aizen93
Aizen93 deleted the fix/voice-gate-on-the-audio-thread branch November 28, 2026 17:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant