Files
buzz/desktop/src-tauri/src/huddle/tts_append.rs
klopez4212 8c0f42e187 Add mobile Huddles voice MVP (#6056)
## Summary
- add foreground mobile Huddles on Android and iOS with native Opus
capture/playback, mute, speaker routing, participants, lifecycle, and
minimized drawer UI
- keep mobile Huddle cards and roster state live, including ended rooms,
relay-resolved profiles, and agents
- broadcast desktop agent TTS through the existing Huddle audio protocol

## Scope
Foreground human-to-human voice MVP only. Agent setup/transcripts,
background calling, recording, and advanced device controls remain out
of scope.

## Validation
- `just mobile-check`
- `just mobile-test` — 1,500 passed
- `just desktop-check` and `just desktop-test` — 4,957 passed
- desktop typecheck, strict Clippy, and Tauri tests — 2,445 passed, 15
ignored
- mobile worktree identity contract checks
- physical Pixel/iPhone behavior reviewed during development

---------

Signed-off-by: kenny lopez <klopez4212@gmail.com>
Signed-off-by: Kenny Lopez <klopez4212@gmail.com>
Signed-off-by: Princess Donut <b238ea756dee4d98afa5883fc7f1de61eeabe65bf700e3a5a5a80db5e42e2c2b@buzz.block.builderlab.xyz>
Signed-off-by: Tom Brow <tomb@block.xyz>
Co-authored-by: Carl <3c4caeafb646d23867f1c4832e68211d77e2561946171625f75c3ce1a3f2670f@buzz.block.builderlab.xyz>
Co-authored-by: Princess Donut <b238ea756dee4d98afa5883fc7f1de61eeabe65bf700e3a5a5a80db5e42e2c2b@buzz.block.builderlab.xyz>
Co-authored-by: leader <71e9f2c44a6932b6772caaaccda1911d010463c3e2c6c40410b8329956046801@buzz.block.builderlab.xyz>
Co-authored-by: Tom Brow <tomb@block.xyz>
Co-authored-by: Mongo <9cfd347903944d5b85aa6c93d2ab67381b978a92a31914bca69998968752a1d7@buzz.block.builderlab.xyz>
2026-08-22 05:28:16 -07:00

108 lines
4.1 KiB
Rust

//! Commits synthesized audio to local playback and remote broadcast atomically.
use super::*;
pub(super) struct TtsAppendContext<'a> {
pub(super) playback: &'a PlaybackCoordinator,
#[cfg(test)]
pub(super) human_floor: &'a HumanFloor,
pub(super) cancel: &'a AtomicBool,
pub(super) voice_cancel: &'a AtomicBool,
pub(super) shutdown: &'a AtomicBool,
pub(super) tts_active: &'a AtomicBool,
pub(super) speaker_generations: &'a SpeakerGenerations,
pub(super) active_speaker: &'a ActiveSpeaker,
pub(super) activity_frames: &'a Mutex<VecDeque<TtsSpeakerActivityFrame>>,
pub(super) broadcasters: &'a TtsBroadcasters,
pub(super) channels: NonZero<u16>,
pub(super) rate: NonZero<u32>,
}
pub(super) fn append_worker_audio(
context: &TtsAppendContext<'_>,
prepared: PreparedModelAudio,
route_id: u64,
speaker_pubkey: Option<&str>,
speaker_generation: u64,
floor_epoch: u64,
publish_broadcast: impl FnOnce(),
) -> bool {
// Keep the shared floor in this context so the regression can mutation-check
// that authorization never moves back inside the coordinator callback.
#[cfg(test)]
let _ = context.human_floor;
let sample_count = prepared.sample_count;
let chunk_index = prepared.chunk_index;
let activity = speaker_pubkey.map(|pubkey| {
build_tts_speaker_activity_frames(&prepared.buffer, pubkey, SAMPLE_RATE as usize)
});
let floor_authorization = context.playback.append_if_human_floor_permits(
rodio::buffer::SamplesBuffer::new(context.channels, context.rate, prepared.buffer),
floor_epoch,
|player_empty| {
if context.cancel.load(Ordering::Acquire)
|| context.voice_cancel.load(Ordering::Acquire)
|| context.shutdown.load(Ordering::Acquire)
{
context.broadcasters.cancel_all();
let reason = if context.shutdown.load(Ordering::Acquire) {
"shutdown"
} else if context.cancel.load(Ordering::Acquire) {
"barge_in"
} else {
"voice_switch"
};
eprintln!(
"buzz-desktop: tts stage=synthesis status=cancelled reason={reason} route_id={route_id}"
);
return false;
}
if speaker_pubkey.is_some_and(|pubkey| {
current_speaker_generation(context.speaker_generations, pubkey)
!= speaker_generation
}) {
eprintln!(
"buzz-desktop: tts stage=synthesis status=cancelled reason=speaker_removed route_id={route_id}"
);
return false;
}
if let Some(pubkey) = speaker_pubkey {
let mut active = context
.active_speaker
.lock()
.unwrap_or_else(|error| error.into_inner());
if player_empty {
active.take();
}
if active
.as_deref()
.is_some_and(|current| !current.eq_ignore_ascii_case(pubkey))
{
return false;
}
active.get_or_insert_with(|| pubkey.to_ascii_lowercase());
context
.activity_frames
.lock()
.unwrap_or_else(|error| error.into_inner())
.extend(activity.unwrap_or_default());
}
true
},
// Commit local and published activity under the same playback lock.
// A concurrent floor onset/cancel therefore cannot invalidate the
// player and then let this remote packet escape afterward.
|| {
publish_broadcast();
context.tts_active.store(true, Ordering::Release);
},
);
if floor_authorization != HumanFloorAuthorization::Permitted {
return false;
}
eprintln!(
"buzz-desktop: tts stage=player status=append_accepted route_id={route_id} chunk_index={chunk_index} sample_count={sample_count}"
);
true
}