diff --git a/CHANGELOG.md b/CHANGELOG.md index b3731fb..e692042 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,10 @@ # Changelog -## 0.1.3 — unreleased +## 0.1.4 — unreleased + +Version 0.1.3 was tagged but never released: a race in the transmit-mode handshake was found before +publication (an older answer could re-enable forwarding while a newer mode change was still pending). It is +fixed here. - Added voice activation as a third transmission mode next to push to talk and the open microphone (Settings → Audio → Voice transmission, sensitivity High / Normal / Low). The core decides locally with @@ -14,8 +18,10 @@ stored settings keep their mode, and new profiles still start with push to talk. - The transmission mode moved from Settings → Keyboard to Settings → Audio; the push-to-talk key stays in Keyboard. The microphone test explains that it only shows the input level and never transmits. -- The renderer forwards microphone audio only after the core has confirmed the selected mode; a failed - mode change is shown and nothing is sent until it is applied. +- The renderer forwards microphone audio only after the core has confirmed the selected mode; only the + answer to the latest request counts (older answers, also from a restarted core, are ignored), and a failed + mode change is shown and nothing is sent until it is applied. Renderer diagnostics now count frames + forwarded to the core, not "sent" frames; whether voice is sent is the core's transmitting state. - Fixed: a preference write failure no longer changes the in-memory value the app keeps using. - ARM64 CI runs the core unit tests (voice detector, resampler, identity, codecs) under qemu-aarch64 (emulated, not native hardware). diff --git a/FEATURE_COMPATIBILITY.md b/FEATURE_COMPATIBILITY.md index a724524..5254436 100644 --- a/FEATURE_COMPATIBILITY.md +++ b/FEATURE_COMPATIBILITY.md @@ -14,7 +14,7 @@ UI, evidence path given; **implemented** = code + unit/loopback tests, no live p | Server password on connect | implemented, not exercised | the test server has no password | | Channel tree, join channel, password channels (inline prompt) | proven (join), implemented (password) | probe joined `cs`; no password test performed | | Voice TX/RX (Opus via core), talking indicators, mute, deafen RX+TX block/restore, PTT, open mic | proven between two TeamsPeach clients with synthetic capture | `docs/evidence/runtime/2026-10-24-voice-chat/`; the path after capture is real. Live microphone voice with the official TS6 client and Asahi hardware operation are not proven | -| Voice activation (local voice detector in the core, sensitivity High/Normal/Low, 100 ms preroll, 400 ms hangover) | proven between two TeamsPeach clients with a real speech recording as file-fed capture; detector unit tests on real speech and synthetic noise | `docs/evidence/runtime/2026-10-24-vad-dev/`, `docs/evidence/vad-0.1.3.md`: speech opens and silence closes the gate, mute/deafen/push-to-talk-not-held/disconnected send nothing, setting persists. A live microphone, an official TS6 receiver and native ARM64 hardware are not proven (ARM64 unit tests run under qemu) | +| Voice activation (local voice detector in the core, sensitivity High/Normal/Low, 100 ms preroll, 400 ms hangover) | proven between two TeamsPeach clients with a real speech recording as file-fed capture; detector unit tests on real speech and synthetic noise | `docs/evidence/runtime/2026-10-24-vad-dev/`, `docs/evidence/vad-0.1.4.md`: speech opens and silence closes the gate, mute/deafen/push-to-talk-not-held/disconnected send nothing, setting persists. A live microphone, an official TS6 receiver and native ARM64 hardware are not proven (ARM64 unit tests run under qemu) | | Private chat | proven | voice-chat run `15/16-*.png` | | Channel chat | proven error path | server denies guests: TS 2568, permission 224, shown in UI | | Server chat | implemented | not exercised live | diff --git a/app/renderer/js/app.js b/app/renderer/js/app.js index b5c8b8b..348819f 100644 --- a/app/renderer/js/app.js +++ b/app/renderer/js/app.js @@ -472,7 +472,7 @@ Object.defineProperty(window, 'teamspeachDiagnostics', { clients: S.snap ? S.snap.clients.map((c) => ({ id: c.id, nickname: c.nickname, channel: c.channel_id, type: c.client_type, inputMuted: c.input_muted })) : [], talking: Array.from(S.talking), audio: { ...S.audio }, - voice: { ...voice.voiceStats, sending: voice.isSending(), capturing: voice.isCapturing(), coreMode: voice.confirmedTransmitMode() }, + voice: { ...voice.voiceStats, forwarding: voice.isForwarding(), capturing: voice.isCapturing(), coreMode: voice.confirmedTransmitMode() }, streams: Array.from(watch.directory.streams.values()).map((s) => ({ id: s.id, clientId: s.clientId, name: s.name })), watch: { ...watch.watchStats }, videoCodecs: settings.videoCodecs(), diff --git a/app/renderer/js/voice.js b/app/renderer/js/voice.js index de054d5..b4ceaa5 100644 --- a/app/renderer/js/voice.js +++ b/app/renderer/js/voice.js @@ -9,11 +9,12 @@ import { toast } from './toast.js'; import { errorText, keyLabel } from './text.js'; import { t } from './i18n.js'; -export const voiceStats = { framesSent: 0, bursts: 0, framesPlayed: 0, level: 0 }; +// framesForwarded counts PCM frames handed to the core; whether voice was sent is S.audio.transmitting. +export const voiceStats = { framesForwarded: 0, forwardRuns: 0, framesPlayed: 0, level: 0 }; let engine = null; -let sending = false; -let burst = null; +let forwarding = false; +let forwardRun = null; let micTest = false; let meterRaf = 0; let assigningPtt = null; @@ -84,7 +85,11 @@ function channelCanTransmit() { // "direct": the core sends every forwarded frame (push to talk, open microphone). "vad": the core's voice // detector decides, so frames are forwarded continuously and only speech is sent. let coreTx = null; -let coreTxPending = null; +// Every request and every core restart bumps the epoch; only the answer to the latest request may change +// coreTx, so an older answer (even for the same settings, or from a previous core) never re-enables +// forwarding while a newer change is still on its way. +let coreTxEpoch = 0; +let coreTxPendingKey = null; let coreTxFailed = false; function wantedCoreTx() { @@ -93,13 +98,13 @@ function wantedCoreTx() { const txKey = (mode) => (mode ? `${mode.mode}:${mode.sensitivity ?? ''}` : ''); -/** Stops forwarding at once and ends an open burst (the core ends its talk spurt on audio_end). */ +/** Stops forwarding at once (audio_end: the core ends any talk spurt and, in voice activation, forgets the audio). */ function stopForwarding() { - if (!sending) return; + if (!forwarding) return; tp.endAudio(); - sending = false; - log('debug', `voice: burst end frames=${burst.frames} peak=${burst.peakMax > 0 ? (20 * Math.log10(burst.peakMax)).toFixed(1) : '-inf'} dBFS`); - burst = null; + forwarding = false; + log('debug', `voice: forwarding end frames=${forwardRun.frames} peak=${forwardRun.peakMax > 0 ? (20 * Math.log10(forwardRun.peakMax)).toFixed(1) : '-inf'} dBFS`); + forwardRun = null; hooks.onTransmitChanged(); } @@ -112,33 +117,39 @@ export async function applyTransmitMode() { if (!S.prefs) return; const want = wantedCoreTx(); const key = txKey(want); - if (txKey(coreTx) === key && !coreTxFailed) return; + if (coreTxPendingKey === key) return; // the latest request already asks for exactly this + if (coreTxPendingKey === null && txKey(coreTx) === key && !coreTxFailed) return; + const epoch = ++coreTxEpoch; coreTx = null; + coreTxFailed = false; stopForwarding(); - coreTxPending = key; + coreTxPendingKey = key; try { const result = await call('set_transmit', want); - if (coreTxPending !== key) return; // a newer change is on its way + if (epoch !== coreTxEpoch) return; // superseded, or the core restarted coreTx = { mode: result.mode, sensitivity: result.sensitivity }; - coreTxFailed = false; log('info', `voice: transmit mode ${result.mode}${result.sensitivity ? ` (${result.sensitivity})` : ''}`); } catch (err) { - if (coreTxPending !== key) return; + if (epoch !== coreTxEpoch) return; coreTxFailed = true; log('error', `voice: set_transmit failed: ${err.code} ${err.message}`); toast(() => t('error.transmit_mode', { detail: errorText(err) }), 'error'); } finally { - if (coreTxPending === key) coreTxPending = null; - hooks.onTransmitChanged(); + if (epoch === coreTxEpoch) { + coreTxPendingKey = null; + hooks.onTransmitChanged(); + } } } -/** The core stopped or restarted: its transmit mode is unknown until applied again. */ +/** The core stopped or restarted: its transmit mode is unknown, and answers from before are void. */ export function forgetCoreTransmitMode() { + coreTxEpoch += 1; coreTx = null; - coreTxPending = null; - sending = false; - burst = null; + coreTxPendingKey = null; + coreTxFailed = false; + forwarding = false; + forwardRun = null; } /** Whether captured frames go to the core right now (not whether voice is sent: see S.audio.transmitting). */ @@ -155,16 +166,16 @@ function onFrame(pcm, rms, peak) { if (micTest && !meterRaf) meterRaf = requestAnimationFrame(drawMeter); if (shouldForward()) { tp.sendAudioFrame(new Uint8Array(pcm)); - voiceStats.framesSent += 1; - if (!sending) { - sending = true; - voiceStats.bursts += 1; - burst = { frames: 0, peakMax: 0, mode: S.prefs.transmitMode }; - log('debug', `voice: burst start mode=${burst.mode} focused=${document.hasFocus()} capture=${engine.capturing}`); + voiceStats.framesForwarded += 1; + if (!forwarding) { + forwarding = true; + voiceStats.forwardRuns += 1; + forwardRun = { frames: 0, peakMax: 0, mode: S.prefs.transmitMode }; + log('debug', `voice: forwarding start mode=${forwardRun.mode} focused=${document.hasFocus()} capture=${engine.capturing}`); hooks.onTransmitChanged(); } - burst.frames += 1; - if (peak > burst.peakMax) burst.peakMax = peak; + forwardRun.frames += 1; + if (peak > forwardRun.peakMax) forwardRun.peakMax = peak; } else { stopForwarding(); } @@ -362,8 +373,9 @@ export function confirmedTransmitMode() { return coreTx ? { ...coreTx } : null; } -export function isSending() { - return sending; +/** Whether captured PCM is currently being handed to the core (not whether voice is sent). */ +export function isForwarding() { + return forwarding; } export function channelCodecProblem() { diff --git a/core/Cargo.lock b/core/Cargo.lock index 73822bd..19ff026 100644 --- a/core/Cargo.lock +++ b/core/Cargo.lock @@ -1875,7 +1875,7 @@ checksum = "7b2093cf4c8eb1e67749a6762251bc9cd836b6fc171623bd0a9d324d37af2417" [[package]] name = "teamspeach-core" -version = "0.1.3" +version = "0.1.4" dependencies = [ "audiopus", "base64 0.22.1", diff --git a/core/Cargo.toml b/core/Cargo.toml index e4aed3a..42107f1 100644 --- a/core/Cargo.toml +++ b/core/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "teamspeach-core" -version = "0.1.3" +version = "0.1.4" edition = "2021" rust-version = "1.89" description = "TeamsPeach native TeamSpeak protocol core (stdio JSON bridge)" diff --git a/docs/evidence/runtime/2026-10-24-vad-dev/80-audio-settings-voice-activation.png b/docs/evidence/runtime/2026-10-24-vad-dev/80-audio-settings-voice-activation.png index 6331759..5a97300 100644 Binary files a/docs/evidence/runtime/2026-10-24-vad-dev/80-audio-settings-voice-activation.png and b/docs/evidence/runtime/2026-10-24-vad-dev/80-audio-settings-voice-activation.png differ diff --git a/docs/evidence/runtime/2026-10-24-vad-dev/result.json b/docs/evidence/runtime/2026-10-24-vad-dev/result.json index d462973..7c84453 100644 --- a/docs/evidence/runtime/2026-10-24-vad-dev/result.json +++ b/docs/evidence/runtime/2026-10-24-vad-dev/result.json @@ -1,16 +1,21 @@ { - "startedAt": "2026-10-24T06:25:21.850Z", + "startedAt": "2026-10-24T06:44:19.203Z", "server": "10.10.10.160:9987", "steps": [ + { + "name": "build", + "at": "2026-10-24T06:44:19.206Z", + "exe": "dev tree" + }, { "name": "input", - "at": "2026-10-24T06:25:21.852Z", + "at": "2026-10-24T06:44:19.206Z", "file": "silence 2 s + speech-librivox-48k.wav (4 s) + silence 3 s, looped", "seconds": 9 }, { "name": "A voice activation confirmed by the core", - "at": "2026-10-24T06:25:24.624Z", + "at": "2026-10-24T06:44:21.705Z", "coreMode": { "mode": "vad", "sensitivity": "normal" @@ -18,29 +23,29 @@ }, { "name": "both connected, channel empty otherwise", - "at": "2026-10-24T06:25:27.832Z", + "at": "2026-10-24T06:44:24.984Z", "channel": "Welcome to the club buddy" }, { "name": "speech and silence", - "at": "2026-10-24T06:25:46.339Z", + "at": "2026-10-24T06:44:43.544Z", "spurts": 6, "onMs": [ - 738, - 1979, - 622, - 744, - 1975, - 556 + 748, + 1922, + 683, + 745, + 1919, + 623 ], "offMs": [ - 1305, + 1300, 0, - 556, - 4732, - 0, - 561, - 3927 + 563, + 4710, + 62, + 557, + 3920 ], "onFraction": 0.36, "bHeardA": true, @@ -51,34 +56,34 @@ }, { "name": "muted: nothing sent", - "at": "2026-10-24T06:25:55.427Z", + "at": "2026-10-24T06:44:52.666Z", "transmitted": false, "bReceivedFrames": 0 }, { "name": "deafened: nothing sent", - "at": "2026-10-24T06:26:04.472Z", + "at": "2026-10-24T06:45:01.745Z", "transmitted": false, "bReceivedFrames": 0 }, { "name": "push to talk not held: nothing sent", - "at": "2026-10-24T06:26:13.579Z", + "at": "2026-10-24T06:45:10.854Z", "transmitted": false, "bReceivedFrames": 0 }, { "name": "voice activation again: speech sent", - "at": "2026-10-24T06:26:14.191Z" + "at": "2026-10-24T06:45:11.333Z" }, { "name": "disconnected", - "at": "2026-10-24T06:26:17.261Z", + "at": "2026-10-24T06:45:14.413Z", "transmitted": false }, { "name": "restart: voice activation persisted and confirmed", - "at": "2026-10-24T06:26:18.635Z", + "at": "2026-10-24T06:45:15.633Z", "transmitMode": "vad", "vadSensitivity": "normal", "coreMode": { diff --git a/docs/evidence/vad-0.1.3.md b/docs/evidence/vad-0.1.4.md similarity index 92% rename from docs/evidence/vad-0.1.3.md rename to docs/evidence/vad-0.1.4.md index 8042be6..2161ff7 100644 --- a/docs/evidence/vad-0.1.3.md +++ b/docs/evidence/vad-0.1.4.md @@ -1,4 +1,4 @@ -# 0.1.3 voice activation: design, measurements, limits +# 0.1.4 voice activation: design, measurements, limits ## What it is A third transmission mode. The renderer forwards 20 ms frames (48 kHz mono) to the core continuously while @@ -30,7 +30,10 @@ frame is sent). There is one decision owner per mode: the renderer for `direct`, `set_transmit` is answered only after the core discarded frames queued under the previous mode (control messages use a separate channel and are not affected) and ended any open burst. The renderer forwards nothing until the answer matches the preferences; a failed change shows an error and sends nothing (never open -microphone). Mute, deafen and disconnect stop forwarding immediately. The own "talking" indicator is the core's +microphone). Only the answer to the latest request counts: every request and every core restart starts a new +epoch, so an older answer (also for the same settings, or from a previous core) cannot re-enable forwarding +while a newer change is pending (tests: out-of-order vad → direct → vad answers; answers from a restarted core). +Mute, deafen and disconnect stop forwarding immediately. The own "talking" indicator is the core's `audio_state.transmitting`, true only while voice packets are sent. Tests: `test/voice-transmit.test.mjs`. ## Measurements (deterministic unit tests, `cargo test`, x64 Windows) diff --git a/docs/i18n.md b/docs/i18n.md index d033dce..ed2a5f1 100644 --- a/docs/i18n.md +++ b/docs/i18n.md @@ -61,7 +61,7 @@ The isolated smoke does not establish live-server call or active-stream continui The targeted command above passed **14/14 tests**. The real Electron smoke passed with no renderer page errors on Electron 44.9.3 / Windows x64: fresh-profile English, all seven categories and both actual-device context menus in both languages, live notification translation, Russian persistence after restart, and switching back to English. Capture remained off, the UID/nickname and unrelated preferences stayed unchanged, and the connection-form draft survived. No horizontal document overflow was detected at 880×560. The generated result and matching English/Russian screenshots are under `docs/evidence/runtime/i18n-2026-10-24/`; the temporary profile was deleted. Screenshots show disposable smoke identity data and real OS-provided device labels, not an owner's identity or server data. -### Voice activation settings, 0.1.3 development UI +### Voice activation settings, 0.1.4 development UI The single transmission-mode selector is in Settings → Audio: push to talk, voice activation, or an explicitly continuous open microphone. Keyboard retains the push-to-talk key binding. The `transmit.*` messages distinguish local speech detection from the input-level meter, recording, and speech recognition/transcription. They describe the approximate 0.4-second transmission tail and explain that noise suppression cannot guarantee rejection of television audio, other voices, or noise. `error.transmit_mode` retains the same `{detail}` placeholder in both catalogs. diff --git a/package-lock.json b/package-lock.json index 8b26063..27cfc89 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "teamspeach", - "version": "0.1.3", + "version": "0.1.4", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "teamspeach", - "version": "0.1.3", + "version": "0.1.4", "license": "UNLICENSED", "dependencies": { "electron-updater": "6.8.11", diff --git a/package.json b/package.json index 6a8aec6..719a9fb 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "teamspeach", "productName": "TeamsPeach", - "version": "0.1.3", + "version": "0.1.4", "description": "Desktop TeamSpeak 6 client: voice, chat and TS6 screenshare viewing", "main": "app/main/main.js", "private": true, diff --git a/scripts/smoke-devices-menu.mjs b/scripts/smoke-devices-menu.mjs index 9d5b006..571cfef 100644 --- a/scripts/smoke-devices-menu.mjs +++ b/scripts/smoke-devices-menu.mjs @@ -32,7 +32,7 @@ async function launch(label) { const state = (page) => page.evaluate(() => ({ audio: window.teamspeachDiagnostics.snapshot().audio, capturing: window.teamspeachDiagnostics.snapshot().voice.capturing, - framesSent: window.teamspeachDiagnostics.snapshot().voice.framesSent, + framesForwarded: window.teamspeachDiagnostics.snapshot().voice.framesForwarded, menu: Boolean(document.querySelector('.device-menu')), items: Array.from(document.querySelectorAll('.device-menu [role="menuitemradio"]')).map((i) => ({ label: i.textContent, checked: i.getAttribute('aria-checked') })), focused: document.activeElement?.id || document.activeElement?.className || '', @@ -52,7 +52,7 @@ try { await page.click('#btn-mute', { button: 'right' }); await page.waitForSelector('.device-menu [role="menuitemradio"]'); let s = await state(page); - step('mic right-click', { menu: s.menu, inputMuted: s.audio.input_muted, capturing: s.capturing, framesSent: s.framesSent, items: s.items, insideViewport: s.inside, focused: s.focused }); + step('mic right-click', { menu: s.menu, inputMuted: s.audio.input_muted, capturing: s.capturing, framesForwarded: s.framesForwarded, items: s.items, insideViewport: s.inside, focused: s.focused }); if (s.audio.input_muted) throw new Error('right-click toggled mute'); await page.screenshot({ path: path.join(out, '62-mic-device-menu.png') }); const pick = s.items[Math.min(2, s.items.length - 1)].label; diff --git a/scripts/smoke-devices.mjs b/scripts/smoke-devices.mjs index 3973975..486d362 100644 --- a/scripts/smoke-devices.mjs +++ b/scripts/smoke-devices.mjs @@ -38,7 +38,7 @@ const readSelects = (page) => page.evaluate(() => { inputValueIsDevice: Boolean(document.querySelector('#input-device').value), outputValueIsDevice: Boolean(document.querySelector('#output-device').value), status: document.querySelector('#device-status').textContent, - framesSent: window.teamspeachDiagnostics.snapshot().voice.framesSent, + framesForwarded: window.teamspeachDiagnostics.snapshot().voice.framesForwarded, capturing: window.teamspeachDiagnostics.snapshot().voice.capturing, conn: window.teamspeachDiagnostics.snapshot().conn, toasts: Array.from(document.querySelectorAll('.toast[data-kind="error"]')).map((t) => t.textContent), @@ -58,7 +58,7 @@ try { await page.click('#settings-nav .nav-item[data-panel="audio"]'); await page.waitForFunction(() => document.querySelector('#device-status').textContent.length > 0, null, { timeout: 10000 }); let s = await readSelects(page); - step('listed without capture', { inputs: s.inputs.length - 1, outputs: s.outputs.length - 1, inputLabels: s.inputs.map((o) => o.label), outputLabels: s.outputs.map((o) => o.label), status: s.status, conn: s.conn, framesSent: s.framesSent, capturing: s.capturing }); + step('listed without capture', { inputs: s.inputs.length - 1, outputs: s.outputs.length - 1, inputLabels: s.inputs.map((o) => o.label), outputLabels: s.outputs.map((o) => o.label), status: s.status, conn: s.conn, framesForwarded: s.framesForwarded, capturing: s.capturing }); await page.screenshot({ path: path.join(out, '60-audio-devices-listed.png') }); if (s.inputs.length < 2 || s.outputs.length < 2) throw new Error('no physical devices listed'); @@ -69,7 +69,7 @@ try { await page.selectOption('#output-device', { label: outputChoice }); await page.waitForTimeout(1000); s = await readSelects(page); - step('selected', { inputChoice, outputChoice, toasts: s.toasts, framesSent: s.framesSent, capturing: s.capturing }); + step('selected', { inputChoice, outputChoice, toasts: s.toasts, framesForwarded: s.framesForwarded, capturing: s.capturing }); await app.close(); ({ app, page } = await launch('run2')); diff --git a/scripts/smoke-voice.mjs b/scripts/smoke-voice.mjs index 1f53e90..c965ed9 100644 --- a/scripts/smoke-voice.mjs +++ b/scripts/smoke-voice.mjs @@ -105,7 +105,7 @@ try { let sa = await diag(a.page); let sb = await diag(b.page); const playback = await b.page.evaluate(() => window.teamspeachDiagnostics.playback()); - step('open mic received', { aFramesSent: sa.voice.framesSent, aTransmitting: sa.audio.transmitting, bTalking: sb.talking, bFramesPlayed: sb.voice.framesPlayed, bPlayback: playback }); + step('open mic received', { aFramesForwarded: sa.voice.framesForwarded, aTransmitting: sa.audio.transmitting, bTalking: sb.talking, bFramesPlayed: sb.voice.framesPlayed, bPlayback: playback }); if (sb.voice.framesPlayed < 50) throw new Error('B received too few audio frames'); await b.page.screenshot({ path: path.join(out, '10-b-sees-a-talking.png') }); await a.page.screenshot({ path: path.join(out, '11-a-open-mic.png') }); diff --git a/scripts/update-e2e.mjs b/scripts/update-e2e.mjs index 33ed979..1325334 100644 --- a/scripts/update-e2e.mjs +++ b/scripts/update-e2e.mjs @@ -5,7 +5,7 @@ // 4. the relaunched process reports the new version; its file matches the release feed's sha512; // 5. start the new AppImage again: version, UID, nickname, bookmark and device pref are unchanged. // -// xvfb-run -a node scripts/update-e2e.mjs --from --to-tag v0.1.3 --out update-e2e-out \ +// xvfb-run -a node scripts/update-e2e.mjs --from --to-tag v0.1.4 --out update-e2e-out \ // [--new-prefs '{"transmitMode":"vad","vadSensitivity":"high"}'] // // The old app stores non-default values it supports itself (transmitMode 'open', devices, …). --new-prefs are diff --git a/test/voice-transmit.test.mjs b/test/voice-transmit.test.mjs index a3885f5..f596ea2 100644 --- a/test/voice-transmit.test.mjs +++ b/test/voice-transmit.test.mjs @@ -53,7 +53,7 @@ test('push to talk forwards only while held, under the direct mode', () => { voice.setPtt(false); const ends = env.ended; assert.equal(forwarded(() => env.frame()), 0); - assert.equal(env.ended, ends + 1, 'releasing ends the burst'); + assert.equal(env.ended, ends + 1, 'releasing ends forwarding'); }); test('switching to voice activation forwards nothing until "vad" is acknowledged', async () => { @@ -91,7 +91,7 @@ test('mute, deafen and disconnect stop forwarding at once', () => { const ends = env.ended; set(); assert.equal(forwarded(() => env.frame()), 0, `${name}: nothing forwarded`); - assert.equal(env.ended, ends + 1, `${name}: burst ended`); + assert.equal(env.ended, ends + 1, `${name}: forwarding ended`); unset(); } }); @@ -115,3 +115,76 @@ test('a core restart forgets the confirmed mode', async () => { voice.forgetCoreTransmitMode(); assert.equal(forwarded(() => env.frame()), 0); }); + +/** Answers each set_transmit only when the test resolves it, in any order. */ +function manualCore() { + const pending = []; + env.respond = (cmd, args) => { + if (cmd !== 'set_transmit') return Promise.resolve({ ok: true, result: {} }); + return new Promise((resolve) => { + pending.push({ + args, + ok: () => resolve({ ok: true, result: args.mode === 'vad' ? { mode: 'vad', sensitivity: args.sensitivity } : { mode: 'direct' } }), + fail: () => resolve({ ok: false, error: { code: 'internal', message: 'old core' } }), + }); + }); + }; + return pending; +} +const settle = () => new Promise((r) => setTimeout(r, 0)); + +test('out-of-order answers (vad → direct → vad): only the latest one enables forwarding', async () => { + const pending = manualCore(); + S.prefs.transmitMode = 'vad'; + S.prefs.vadSensitivity = 'normal'; + const a1 = voice.applyTransmitMode(); + S.prefs.transmitMode = 'open'; + const b = voice.applyTransmitMode(); + S.prefs.transmitMode = 'vad'; + const a2 = voice.applyTransmitMode(); + assert.deepEqual(pending.map((p) => p.args.mode), ['vad', 'direct', 'vad']); + // The core answers in order; the first "vad" answer matches the current settings but is not the latest. + pending[0].ok(); + await settle(); + assert.equal(forwarded(() => env.frame()), 0, 'stale vad answer while direct is still being applied'); + pending[1].ok(); + await settle(); + assert.equal(forwarded(() => env.frame()), 0, 'intermediate direct answer'); + pending[2].ok(); + await Promise.all([a1, b, a2]); + assert.equal(forwarded(() => env.frame()), 1, 'latest answer'); + assert.deepEqual(voice.confirmedTransmitMode(), { mode: 'vad', sensitivity: 'normal' }); +}); + +test('an identical change already on its way is not requested twice', async () => { + const pending = manualCore(); + S.prefs.vadSensitivity = 'high'; + const first = voice.applyTransmitMode(); + const second = voice.applyTransmitMode(); + assert.equal(pending.length, 1); + pending[0].ok(); + await Promise.all([first, second]); + assert.equal(forwarded(() => env.frame()), 1); +}); + +test('answers from a core that has since restarted are ignored', async () => { + let pending = manualCore(); + S.prefs.transmitMode = 'open'; + const old = voice.applyTransmitMode(); + voice.forgetCoreTransmitMode(); // core restarted before answering + pending[0].ok(); + await old; + assert.equal(forwarded(() => env.frame()), 0, 'old core success must not enable forwarding'); + pending = manualCore(); + const failing = voice.applyTransmitMode(); + voice.forgetCoreTransmitMode(); + pending[0].fail(); + await failing; + // The new core is asked afresh and its answer applies. + pending = manualCore(); + const fresh = voice.applyTransmitMode(); + assert.equal(pending.length, 1); + pending[0].ok(); + await fresh; + assert.equal(forwarded(() => env.frame()), 1); +});