Skip to content

Commit 82d23b8

Browse files
npub1qyvc0c5kl4gqv2fd97fsk46tu378sqgy35vc83rvgfwne90sel7s0ed67dtlongwell-block
andcommitted
fix(huddle): release tts_active on item receipt when playback has drained
The persistent-player loop only released tts_active in the recv-timeout arm. If item N's audio drained and item N+1 arrived before the 100 ms timeout fired, the entire preprocessing + synthesis pass for N+1 ran with tts_active stuck true and nothing playing — STT discarded human speech as "echo" during a window where the agent was silent, and the remote-interrupt frame counter (gated on tts_active) was distorted. Run the same drained-player check on item receipt, before synthesis. Pipelining is unaffected: while audio is still draining, player.empty() is false and the gate holds across items. Found in PR #997 review (Max). Co-authored-by: Tyler Longwell <[email protected]> Signed-off-by: Tyler Longwell <[email protected]>
1 parent 9b8db1d commit 82d23b8

2 files changed

Lines changed: 45 additions & 6 deletions

File tree

‎desktop/src-tauri/src/huddle/tts.rs‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,8 @@ fn tts_worker(
346346
// most RECV_TIMEOUT and never waits for playback to drain before taking
347347
// the next item — synthesis of item N+1 overlaps playback of item N.
348348
// `tts_active` lifecycle: set on the first append while idle, cleared
349-
// only when the channel is quiet AND the player has fully drained.
349+
// whenever the player has fully drained — either in the idle timeout
350+
// arm or on item receipt before synthesis begins.
350351
let silence_buf_len = (INTER_SENTENCE_SILENCE * SAMPLE_RATE as f32) as usize;
351352
// `first_append` = "no audio queued since the player last went idle".
352353
// Flipped by `build_sentence_append_buffer` on the first real append; the
@@ -390,6 +391,19 @@ fn tts_worker(
390391
continue;
391392
}
392393

394+
// If playback already drained while we were waiting for this item,
395+
// the agent is silent — release the mic gate BEFORE preprocessing/
396+
// synthesis. Without this, an item arriving inside the recv timeout
397+
// window would run the whole synthesis pass with `tts_active` stuck
398+
// true and nothing playing, making STT discard human speech as
399+
// "echo" during a silent window. (Pipelining is unaffected: when
400+
// audio is still draining, `player.empty()` is false and the flag
401+
// stays set across items.)
402+
if player.empty() && !first_append {
403+
tts_active.store(false, Ordering::Release);
404+
first_append = true;
405+
}
406+
393407
// Preprocess text.
394408
let text = preprocess_for_tts(&raw_text);
395409
if text.is_empty() {

‎desktop/src-tauri/src/huddle/tts_tests.rs‎

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,12 @@ fn simulate_cancel_consumption(
111111
false
112112
}
113113

114-
/// Simulate the idle branch of the TTS worker's main loop (the
115-
/// recv-timeout arm): with nothing queued, `tts_active` is released and
116-
/// the lead-in re-armed only when the player has fully drained AND audio
117-
/// was actually queued since the last idle period. Must match the
118-
/// production logic in `tts_worker`.
114+
/// Simulate the drained-player check in the TTS worker's main loop. It
115+
/// runs in TWO places — the recv-timeout (idle) arm, and on item receipt
116+
/// before synthesis begins: `tts_active` is released and the lead-in
117+
/// re-armed only when the player has fully drained AND audio was actually
118+
/// queued since the last idle period. Must match the production logic in
119+
/// `tts_worker`.
119120
fn simulate_idle_check(player_empty: bool, first_append: &mut bool, tts_active: &AtomicBool) {
120121
if player_empty && !*first_append {
121122
tts_active.store(false, Ordering::Release);
@@ -439,6 +440,30 @@ fn idle_check_noop_when_nothing_was_queued() {
439440
assert!(first_append, "lead-in should remain armed");
440441
}
441442

443+
/// On-receipt check (PR #997 review blocker): item N's audio drains, then
444+
/// item N+1 arrives BEFORE the recv timeout fires. The same drained-player
445+
/// check must run on receipt so `tts_active` is released before the
446+
/// synthesis pass — otherwise STT discards human speech as "echo" for the
447+
/// whole synthesis window while the agent is actually silent.
448+
#[test]
449+
fn on_receipt_check_releases_mic_gate_before_synthesis() {
450+
let tts_active = AtomicBool::new(true);
451+
let mut first_append = false; // item N appended audio, now fully drained
452+
453+
// Item N+1 received with the player already empty: release + re-arm.
454+
simulate_idle_check(true, &mut first_append, &tts_active);
455+
456+
assert!(
457+
!tts_active.load(Ordering::Acquire),
458+
"tts_active must be released before synthesizing the next item \
459+
when playback has already drained",
460+
);
461+
assert!(
462+
first_append,
463+
"lead-in must re-arm so the next utterance gets a fresh cushion"
464+
);
465+
}
466+
442467
/// Full cycle: remote speech → cancel → TTS consumption → new TTS → cancel again.
443468
/// Validates the cancel mechanism is reusable across TTS sessions.
444469
/// The false→true transition on tts_active auto-clears counters — no

0 commit comments

Comments
 (0)