Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions apps/desktop/src/__tests__/components/DMVoicePanelRenders.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -192,16 +192,23 @@ describe('DMCallPanel (the conversation header)', () => {
expect(renders).toBe(2);
});

it('push-to-talk held while muted: the ring shows the detector\'s answer, "not speaking" under a mute', () => {
it('push-to-talk held while muted: the ring is not hidden by the mute flag, it follows the detector', () => {
useVoiceStore.setState({ dmCallConversationId: 'conv-1', dmCallUsers: [me, peer], selfMute: true, pttActive: true });
audio.paused = true; // what the mute did to the detector
// (That the key press ends the pause a mute put on the detector is held
// by voicePushToTalkRelease.test.tsx; here the detector is a stand-in)
mount(<DMCallPanel />);
playFrames(5, () => 0.5);
// Heard (push-to-talk overrides the mute) with no ring, here as on the other side
expect(container.querySelector('[data-testid="avatar-Me"]')!.getAttribute('data-speaking')).toBe('true');
// Silence under the held key: no ring
playFrames(5, () => 0);
expect(container.querySelector('[data-testid="avatar-Me"]')!.getAttribute('data-speaking')).toBe('false');
// The ring is not hidden by the mute: it follows the detector once that answers again
audio.paused = false;
});

it('a detector still paused shows no ring, whatever the key and the level say', () => {
useVoiceStore.setState({ dmCallConversationId: 'conv-1', dmCallUsers: [me, peer], selfMute: true, pttActive: true });
audio.paused = true;
mount(<DMCallPanel />);
playFrames(5, () => 0.5);
expect(container.querySelector('[data-testid="avatar-Me"]')!.getAttribute('data-speaking')).toBe('true');
expect(container.querySelector('[data-testid="avatar-Me"]')!.getAttribute('data-speaking')).toBe('false');
});
});
64 changes: 64 additions & 0 deletions apps/desktop/src/__tests__/services/audioAnalyser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ import {
getGatedStream,
getSuppressedStream,
getSpeakingDetectionMode,
getSpeakingDecider,
applyNoiseSuppression,
startSpeakingDetection,
stopSpeakingDetection,
Expand Down Expand Up @@ -542,6 +543,69 @@ describe('audioAnalyser', () => {
});
});

describe('a pause and its end, while the page timer decides', () => {
const level = (value: number) => {
mockAnalyserNode.getFloatTimeDomainData.mockImplementation((data: Float32Array) => { data.fill(value); });
};
const sent = () => vi.mocked(getSocket()!.emit).mock.calls;
/** The targets the tick ramped the gain to, in order (1 is open). */
const ramps = () => mockGainNode.gain.linearRampToValueAtTime.mock.calls.map((call) => call[0]);

beforeEach(() => {
vi.useFakeTimers();
level(0);
});

afterEach(() => {
mockAnalyserNode.getFloatTimeDomainData.mockReset();
});

it('a voice channel: paused, the tick does nothing while the microphone is loud; the pause over, it opens the gain and says "speaking" again', () => {
startSpeakingDetection(micStream(), 'server');
expect(getSpeakingDecider()).toBe('page-timer');
level(0.5);
vi.advanceTimersByTime(20);
expect(sent()).toEqual([['voice:speaking', true]]);

setSpeakingDetectionPaused(true);
expect(sent().at(-1)).toEqual(['voice:speaking', false]);
expect(mockGainNode.gain.setTargetAtTime.mock.calls.at(-1)?.[0]).toBe(0);
const sentPaused = sent().length;
const rampsPaused = ramps().length;
vi.advanceTimersByTime(500); // still loud: a held key under the mute
expect(sent()).toHaveLength(sentPaused);
expect(ramps()).toHaveLength(rampsPaused);
expect(isSpeechDetected()).toBe(false);

setSpeakingDetectionPaused(false); // what the key press does
vi.advanceTimersByTime(20);
expect(ramps().length).toBeGreaterThan(rampsPaused);
expect(ramps().at(-1)).toBe(1);
expect(sent().at(-1)).toEqual(['voice:speaking', true]);
expect(isSpeechDetected()).toBe(true);
});

it('a direct call: the pause holds back the state only; the pause over, "speaking" goes out again', () => {
startSpeakingDetection(micStream(), 'dm');
expect(getSpeakingDecider()).toBe('page-timer');
level(0.5);
vi.advanceTimersByTime(20);
expect(sent()).toEqual([['dm:voice:speaking', true]]);

setSpeakingDetectionPaused(true);
expect(sent().at(-1)).toEqual(['dm:voice:speaking', false]);
// A direct call's gain is never closed by a pause
expect(mockGainNode.gain.setTargetAtTime).not.toHaveBeenCalled();
const sentPaused = sent().length;
vi.advanceTimersByTime(500);
expect(sent()).toHaveLength(sentPaused);

setSpeakingDetectionPaused(false);
vi.advanceTimersByTime(20);
expect(sent().at(-1)).toEqual(['dm:voice:speaking', true]);
});
});

describe('stopNoiseSuppression', () => {
it('should handle being called when no pipeline is active', () => {
expect(() => stopNoiseSuppression()).not.toThrow();
Expand Down
2 changes: 1 addition & 1 deletion apps/desktop/src/__tests__/services/noiseGate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ describe('the noise gate', () => {
gate.setPaused(true);
expect(reports.map((r) => r.speaking)).toEqual([true, false]);

// Push-to-talk held while muted: heard, gated by the rule, not reported
// The audio goes on being gated by the rule, and nothing is reported
const held = run(steady(samples(200, RATE), 0.5), 128);
expect(held.every((v) => v === 0.5)).toBe(true);
expect(reports).toHaveLength(2);
Expand Down
40 changes: 39 additions & 1 deletion apps/desktop/src/__tests__/stores/voiceMicTrackLife.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ describe('the microphone producer never stops the pipeline\'s track', () => {
expect(raw.enabled).toBe(false);
});

it('server-muted at a transport restart (the raw track stays enabled): the next producer is paused, its track disabled', async () => {
it('server-muted at a transport restart: the raw track closes with the flag, the next producer is paused, its track disabled', async () => {
const [raw, gated] = [fakeTrack('raw'), fakeTrack('gated')];
inChannel(raw, gated);
await useVoiceStore.getState().handleTransportCreated(transportCreated());
Expand All @@ -360,6 +360,9 @@ describe('the microphone producer never stops the pipeline\'s track', () => {
expect(raw.enabled).toBe(true);

(sendTransports[0] as unknown as { handler: { setConnectionState: (state: string) => void } }).handler.setConnectionState('failed');
// The restart closes the raw track under the mute flag, as a socket
// reconnect does (the person's own unmute opens it again)
expect(raw.enabled).toBe(false);
await useVoiceStore.getState().handleTransportCreated(transportCreated());

const [second] = micProducers();
Expand Down Expand Up @@ -567,6 +570,41 @@ describe('the speaking state once the microphone producer is made', () => {
}
});

it('push-to-talk held under the mute flag at a transport restart: the raw track closes, as at a socket reconnect', async () => {
useSettingsStore.setState({ voiceMode: 'push_to_talk' });
try {
const raw = fakeTrack('raw');
inChannel(raw, fakeTrack('gated'));
await useVoiceStore.getState().handleTransportCreated(transportCreated());
// The key opened the microphone under the flag
useVoiceStore.setState({ pttActive: true, selfMute: true });
raw.enabled = true;
restartTransport(0);
// The new producer starts paused under the flag: a microphone left open
// would be detected, and shown as speaking, with nobody hearing it
expect(raw.enabled).toBe(false);
expect(restartJoinSelfMute()).toBe(true);
} finally {
useSettingsStore.setState({ voiceMode: 'voice_activity' });
}
});

it('push-to-talk held with no mute flag at a transport restart: the raw track stays open', async () => {
useSettingsStore.setState({ voiceMode: 'push_to_talk' });
try {
const raw = fakeTrack('raw');
inChannel(raw, fakeTrack('gated'));
await useVoiceStore.getState().handleTransportCreated(transportCreated());
useVoiceStore.setState({ pttActive: true });
raw.enabled = true;
restartTransport(0);
expect(raw.enabled).toBe(true);
expect(restartJoinSelfMute()).toBe(false);
} finally {
useSettingsStore.setState({ voiceMode: 'voice_activity' });
}
});

it('outside push-to-talk a transport restart joins with the manual mute flag', async () => {
inChannel(fakeTrack('raw'), fakeTrack('gated'));
await useVoiceStore.getState().handleTransportCreated(transportCreated());
Expand Down
142 changes: 142 additions & 0 deletions apps/desktop/src/__tests__/stores/voicePushToTalkRelease.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import { createRoot, type Root } from 'react-dom/client';
* said up to the release is sent) and is immediate under the page timer. One
* place owns the pending pause; what changes the producer cancels it or makes
* it immediate, and a late fire pauses nothing it was not scheduled for.
*
* And the key pressed under the mute flag: it opens the microphone, so it
* ends the pause the mute put on speaking detection (the second block).
*/

vi.mock('mediasoup-client', () => ({ Device: vi.fn() }));
Expand Down Expand Up @@ -305,3 +308,142 @@ describe('a push-to-talk release in a voice channel', () => {
expect(socket.emit).toHaveBeenCalledWith('dm:voice:mute', true);
});
});

describe('the push-to-talk key pressed under the mute flag', () => {
/** What speaking detection was last told: true is paused. */
const lastPause = () => aa.setSpeakingDetectionPaused.mock.calls.at(-1)?.[0];

it('a voice channel: the key opens the microphone and ends the pause the mute put on speaking detection', () => {
act(() => useVoiceStore.getState().toggleMute());
expect(useVoiceStore.getState().selfMute).toBe(true);
expect(lastPause()).toBe(true);

press();
expect(raw.track.enabled).toBe(true);
expect(producer.resume).toHaveBeenCalled();
expect(socket.emit).toHaveBeenLastCalledWith('voice:mute', false);
// A pause left in place keeps the gate closed: silence under a button that shows an open microphone
expect(lastPause()).toBe(false);
// The flag is the person\'s own: the key does not clear it
expect(useVoiceStore.getState().selfMute).toBe(true);
});

it('the release is the ordinary one: it tells speaking detection nothing more', () => {
act(() => useVoiceStore.getState().toggleMute());
press();
const told = aa.setSpeakingDetectionPaused.mock.calls.length;
const paused = producer.pause.mock.calls.length;

release();
expect(raw.track.enabled).toBe(false);
// Not at once: the look-ahead drains first, as without the flag
expect(producer.pause).toHaveBeenCalledTimes(paused);
vi.advanceTimersByTime(PTT_RELEASE_PAUSE_DELAY_MS);
expect(producer.pause).toHaveBeenCalledTimes(paused + 1);
vi.advanceTimersByTime(1_000);
expect(socket.emit).toHaveBeenLastCalledWith('voice:mute', true);
expect(aa.setSpeakingDetectionPaused).toHaveBeenCalledTimes(told);
});

it('a mute switched on while the key is held closes the microphone and pauses detection at once', () => {
press();
expect(lastPause()).toBe(false);
act(() => useVoiceStore.getState().toggleMute());
expect(raw.track.enabled).toBe(false);
expect(lastPause()).toBe(true);
// With the key still down, nothing reopens it by itself
vi.advanceTimersByTime(1_000);
expect(raw.track.enabled).toBe(false);
expect(lastPause()).toBe(true);
release();
press();
expect(raw.track.enabled).toBe(true);
expect(lastPause()).toBe(false);
});

it('a deafen switched on while the key is held under the mute flag closes the microphone and pauses detection too', () => {
act(() => useVoiceStore.getState().toggleMute());
press();
expect(raw.track.enabled).toBe(true);
expect(lastPause()).toBe(false);
const paused = producer.pause.mock.calls.length;

act(() => useVoiceStore.getState().toggleDeaf());
expect(useVoiceStore.getState().selfDeaf).toBe(true);
expect(useVoiceStore.getState().selfMute).toBe(true);
expect(raw.track.enabled).toBe(false);
expect(producer.pause).toHaveBeenCalledTimes(paused + 1);
expect(lastPause()).toBe(true);
});

it('a deafen with the key up under the mute flag has nothing to close and pauses nothing again', () => {
act(() => useVoiceStore.getState().toggleMute());
const told = aa.setSpeakingDetectionPaused.mock.calls.length;
act(() => useVoiceStore.getState().toggleDeaf());
expect(useVoiceStore.getState().selfDeaf).toBe(true);
expect(aa.setSpeakingDetectionPaused).toHaveBeenCalledTimes(told);
});

it('a deafen switched on while the key is held, with no mute flag, does the same as a mute', () => {
press();
act(() => useVoiceStore.getState().toggleDeaf());
expect(useVoiceStore.getState().selfMute).toBe(true);
expect(raw.track.enabled).toBe(false);
expect(lastPause()).toBe(true);
release();
press();
expect(lastPause()).toBe(false);
});

it('a direct call: a deafen switched on while the key is held under the mute flag closes the microphone there too', () => {
useVoiceStore.setState({ activeChannelId: null, dmCallConversationId: 'conv-1', msProducers: new Map() });
act(() => useVoiceStore.getState().toggleMute());
press();
expect(raw.track.enabled).toBe(true);
expect(lastPause()).toBe(false);

act(() => useVoiceStore.getState().toggleDeaf());
expect(raw.track.enabled).toBe(false);
expect(lastPause()).toBe(true);
expect(socket.emit).toHaveBeenLastCalledWith('dm:voice:deaf', true);
});

it('a direct call: the key ends the pause there too', () => {
useVoiceStore.setState({ activeChannelId: null, dmCallConversationId: 'conv-1', msProducers: new Map() });
act(() => useVoiceStore.getState().toggleMute());
expect(socket.emit).toHaveBeenLastCalledWith('dm:voice:mute', true);
expect(lastPause()).toBe(true);

press();
expect(raw.track.enabled).toBe(true);
expect(socket.emit).toHaveBeenLastCalledWith('dm:voice:mute', false);
expect(lastPause()).toBe(false);
});

it('without the flag the key press tells detection the same, and nothing was paused', () => {
press();
expect(aa.setSpeakingDetectionPaused.mock.calls).toEqual([[false]]);
});

it('with no call the key touches nothing', () => {
useVoiceStore.setState({ activeChannelId: null, dmCallConversationId: null });
press();
expect(raw.track.enabled).toBe(false);
expect(aa.setSpeakingDetectionPaused).not.toHaveBeenCalled();
expect(socket.emit).not.toHaveBeenCalled();
});

it('the call ending with the key held clears the hold and stops detection; the key coming up then sends nothing', () => {
act(() => useVoiceStore.getState().toggleMute());
press();
expect(useVoiceStore.getState().pttActive).toBe(true);
act(() => useVoiceStore.getState().leaveChannel());
expect(useVoiceStore.getState().pttActive).toBe(false);
expect(aa.stopSpeakingDetection).toHaveBeenCalled();
// The key comes up with no call: nothing is sent or scheduled for it
socket.emit.mockClear();
release();
vi.advanceTimersByTime(1_000);
expect(socket.emit).not.toHaveBeenCalled();
});
});
6 changes: 4 additions & 2 deletions apps/desktop/src/hooks/useLocalSpeaking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ import { isSpeechDetected } from '../services/audioAnalyser';
* page timer), so the ring lights when the detector hears speech, at the
* user's threshold: under the audio-thread gate that is what the gate lets
* through; while the page timer decides a direct call sends everything, and
* the ring still lights only over the threshold. False while muted (the
* detector is paused then), with the push-to-talk key held too. This
* the ring still lights only over the threshold. False while muted: the
* detector is paused, or hears the disabled microphone. The push-to-talk key
* pressed under a mute opens the microphone and ends the pause, so the ring
* can light while the key is held, as it does for the other person. This
* reads it once an animation frame but sets state only when the answer
* FLIPS, so its component re-renders when the user starts or stops speaking,
* not on every frame. While `enabled` is false (no DM call, or a ring that
Expand Down
9 changes: 9 additions & 0 deletions apps/desktop/src/hooks/usePushToTalk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { useEffect, useRef } from 'react';
import { useSettingsStore } from '../stores/settingsStore';
import { useVoiceStore, isMicProducer, pauseMicProducersOnPushToTalkRelease, cancelPendingMicPause } from '../stores/voiceStore';
import { getSocket } from '../services/socket';
import { setSpeakingDetectionPaused } from '../services/audioAnalyser';

function isTextInput(el: Element | null): boolean {
if (!el) return false;
Expand Down Expand Up @@ -73,6 +74,14 @@ export function usePushToTalk() {
// Enable raw mic tracks
localStream.getAudioTracks().forEach((track) => { track.enabled = true; });

// The microphone is open, so speaking detection answers again. A mute or
// a deafen paused it, and a pause left in place while the key is held
// keeps a voice channel's gate closed (silence is sent under a button
// that shows an open microphone) and shows no ring in either kind of
// call. The release needs no counterpart: with the raw track disabled
// the detector hears silence, as after any push-to-talk release.
setSpeakingDetectionPaused(false);

// Resume the SFU mic producer (server voice only — screen audio unaffected)
if (activeChannelId) {
const { msProducers } = useVoiceStore.getState();
Expand Down
8 changes: 6 additions & 2 deletions apps/desktop/src/services/audioAnalyser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -351,10 +351,14 @@ export function onSpeakingChange(cb: ((speaking: boolean) => void) | null) {
* getFloatTimeDomainData, no RMS calculation, no socket emissions. The
* audio-thread gate is told too and goes on processing every block (it
* still delays the audio): in a space call it closes, in a direct call it
* goes on gating the audio (push-to-talk overrides the mute there); either
* goes on gating the audio; either
* way the page takes no report from it until the pause ends, as the timer
* makes none (the one "not speaking" the gate posts at a pause is what the
* page has already said).
* page has already said). The pause ends at an unmute, and when the
* push-to-talk key is pressed under the mute flag (the key opens the
* microphone: usePushToTalk). A stop resets it, so every rebuild of the
* pipeline starts unpaused: the disabled microphone track is then what keeps
* a muted person silent.
*/
export function setSpeakingDetectionPaused(paused: boolean) {
detectionPaused = paused;
Expand Down
4 changes: 2 additions & 2 deletions apps/desktop/src/services/noiseGate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,8 @@ export interface NoiseGateOptions {
open?: boolean;
/**
* Whether a pause closes the gate (a space call: the page's gain gate
* closed too) or only stops the reports (a direct call: push-to-talk held
* while muted is still heard, gated by the rule). Default true.
* closed too) or only stops the reports (a direct call: the audio goes on
* being gated by the rule). Default true.
*/
pauseClosesGate?: boolean;
/** Told each change of the decision, once, never while paused. */
Expand Down
Loading
Loading