feat(audio,bridge,flutter): v1 audio + PTT lifecycle implementation (SDD-094..097)

Implement the SDD-094 / SDD-095 / SDD-096 / SDD-097 detailed designs
committed in dfa84ee.

Rust side
- chanora_audio::TransmitMode enum (Ptt/Continuous/VoiceActivity) with
  serde-friendly u8 repr (SDD-095).
- chanora_audio::TransmitModeSelector: lock-free Atomic-backed selector
  that is the sole writer of transmit_active (per SAD-083), applying
  hard_mute as a final clamp. VoiceActivity falls through to Continuous
  for v1 (DEC-030 placeholder).
- chanora_audio::ReleaseTailTimer: tokio-task-owning struct driving the
  selector's ptt_held input; default 200 ms tail, configurable 0–500 ms
  with AtomicU32 hot read; pending JoinHandle held in a std::sync::Mutex
  touched only on PTT edge transitions (SDD-096).
- chanora_storage: AudioMeta persisted as audio_meta.json next to
  identity.dek; get/set_transmit_mode + get/set_release_tail_ms with
  0..=500 clamp on write.
- chanora_core::ChanoraSession: voice_join(channel, password) and
  voice_leave() are the new lifecycle entry points; ensure_audio_running
  and shutdown_audio_if_idle are private helpers around the existing
  Option<AudioEngine> field. SessionEvent::VoiceState carries the
  in_channel / transmit_mode / mute / release_tail_ms tuple. Selector
  state survives reconnect; supervisor rewires it to each fresh engine
  gate.
- chanora_bridge: drop start_audio; add voice_join, voice_leave,
  set/get_transmit_mode, set/get_release_tail_ms, set_hard_mute.
  BridgeEvent::VoiceState mirrors the core event. AudioStarted/Stopped
  kept for backwards compat but Flutter ignores them in the new UI.

Flutter side
- New apps/chanora_flutter/lib/widgets/voice_bar.dart replaces the
  legacy _AudioControls widget. Renders channel pill, mode badge,
  mute toggle, level meter, PttCapabilityBadge, leave button. No
  manual Start affordance anywhere.
- New apps/chanora_flutter/lib/widgets/voice_settings.dart dialog with
  TransmitMode radio group (VoiceActivity disabled with 'Coming soon'
  trailing label per DEC-030), bind-key button, release-tail slider
  0–500 ms step 25.
- main.dart: state fields _inChannel, _transmitMode, _hardMute,
  _releaseTailMs driven by BridgeEvent_VoiceState. Channel-tap now
  calls voiceJoin instead of moveToChannel. Removed _onStartAudio,
  _audioStarted-gated branch, and the FilledButton.
- l10n: 11 new strings in app_en.arb + app_zh.arb.

Verification
- cargo check --workspace: clean.
- cargo test --workspace --lib: 72 passed / 0 failed / 1 ignored
  (chanora_audio: +12 new tests for TransmitMode/Selector/ReleaseTail;
  chanora_storage: +2 new tests for audio_meta round-trip).
- flutter analyze: 0 errors, 0 warnings; 6 infos are the Flutter 3.32
  Radio.groupValue deprecation (pre-existing API usage).
- FRB Dart/Rust bindings regenerated via flutter_rust_bridge_codegen.

Follow-up (intentionally deferred)
- PttController and per-platform PTT backends still drive AudioTransmitGate
  directly via the legacy set_ptt path; routing those key edges through
  ChanoraSession::release_tail_timer().{key_down,key_up} so the tail
  applies to native PTT input is a contained wiring change in a follow-up.
- Real audio-level RMS in BridgeAudioStats (current meter is binary).
- VoiceActivity backend (DEC-030).
This commit is contained in:
EdisonJwa
2026-05-15 23:05:37 +08:00
parent dfa84ee7bb
commit ba444d94bd
25 changed files with 2519 additions and 178 deletions
+139 -11
View File
@@ -232,24 +232,128 @@ pub async fn is_connected() -> bool {
// ---------- Audio commands (Beta) ----------
/// Start the audio engine on the active connection. Requires a
/// connection; idempotent (will replace any previous engine).
pub async fn start_audio() -> Result<(), BridgeError> {
// `start_audio` was removed per SDD-094 — voice activation now
// flows through `voice_join` / `voice_leave`, which transparently
// drive `AudioEngine::ensure_running` / `shutdown_if_idle`.
/// Set the push-to-talk state.
///
/// Superseded in v1 by [`set_transmit_mode`] + the binding capture
/// dialog. Retained so legacy callers and integration tests keep
/// working; the new VoiceBar UI no longer invokes this.
pub async fn set_ptt(active: bool) -> Result<(), BridgeError> {
runtime()
.spawn(async {
session()
.start_audio(chanora_core::AudioEngineConfig::default())
.await
})
.spawn(async move { session().set_ptt(active).await })
.await
.map_err(|e| BridgeError::Unmapped(format!("join: {e}")))??;
Ok(())
}
/// Set the push-to-talk state.
pub async fn set_ptt(active: bool) -> Result<(), BridgeError> {
// ---------- v1 voice lifecycle (SDD-094/095/096) ----------
/// Voice transmit mode mirror (SDD-095). Schema-controlled enum;
/// the wire encoding matches [`chanora_core::TransmitMode::as_u8`].
#[derive(Debug, Clone, Copy)]
pub enum BridgeTransmitMode {
/// Push-to-talk (default).
Ptt,
/// Continuous transmission while in channel and not muted.
Continuous,
/// Voice-activity detection — reserved per DEC-030; v1 behaves
/// as `Continuous`.
VoiceActivity,
}
impl From<BridgeTransmitMode> for chanora_core::TransmitMode {
fn from(m: BridgeTransmitMode) -> Self {
match m {
BridgeTransmitMode::Ptt => Self::Ptt,
BridgeTransmitMode::Continuous => Self::Continuous,
BridgeTransmitMode::VoiceActivity => Self::VoiceActivity,
}
}
}
impl From<chanora_core::TransmitMode> for BridgeTransmitMode {
fn from(m: chanora_core::TransmitMode) -> Self {
match m {
chanora_core::TransmitMode::Ptt => Self::Ptt,
chanora_core::TransmitMode::Continuous => Self::Continuous,
chanora_core::TransmitMode::VoiceActivity => Self::VoiceActivity,
}
}
}
fn transmit_mode_from_u8(v: u8) -> BridgeTransmitMode {
chanora_core::TransmitMode::from_u8(v)
.unwrap_or_default()
.into()
}
/// Join a voice channel (SDD-094). Moves the user to `channel_id`,
/// brings up the audio engine if needed, and emits
/// `BridgeEvent::VoiceState`. `password` may be empty.
pub async fn voice_join(channel_id: u64, password: String) -> Result<(), BridgeError> {
let pw = if password.is_empty() { None } else { Some(password) };
runtime()
.spawn(async move { session().set_ptt(active).await })
.spawn(async move { session().voice_join(channel_id, pw).await })
.await
.map_err(|e| BridgeError::Unmapped(format!("join: {e}")))??;
Ok(())
}
/// Leave the current voice channel (SDD-094). Tears down the audio
/// engine and emits `BridgeEvent::VoiceState`.
pub async fn voice_leave() -> Result<(), BridgeError> {
runtime()
.spawn(async { session().voice_leave().await })
.await
.map_err(|e| BridgeError::Unmapped(format!("join: {e}")))??;
Ok(())
}
/// Set the active transmit mode (SDD-095).
pub async fn set_transmit_mode(mode: BridgeTransmitMode) -> Result<(), BridgeError> {
runtime()
.spawn(async move { session().set_transmit_mode(mode.into()).await })
.await
.map_err(|e| BridgeError::Unmapped(format!("join: {e}")))??;
Ok(())
}
/// Read the active transmit mode.
pub async fn get_transmit_mode() -> BridgeTransmitMode {
runtime()
.spawn(async { session().transmit_mode() })
.await
.unwrap_or(chanora_core::TransmitMode::Ptt)
.into()
}
/// Update the release-tail in milliseconds (SDD-096). Values are
/// clamped to `0..=500` on the Rust side; passing anything larger
/// silently saturates.
pub async fn set_release_tail_ms(ms: u32) -> Result<(), BridgeError> {
runtime()
.spawn(async move { session().set_release_tail_ms(ms).await })
.await
.map_err(|e| BridgeError::Unmapped(format!("join: {e}")))??;
Ok(())
}
/// Read the current release-tail in milliseconds.
pub async fn get_release_tail_ms() -> u32 {
runtime()
.spawn(async { session().release_tail_ms() })
.await
.unwrap_or(200)
}
/// Engage or release the hard-mute clamp (SDD-094). When `true`
/// the audio engine transmits nothing regardless of mode.
pub async fn set_hard_mute(muted: bool) -> Result<(), BridgeError> {
runtime()
.spawn(async move { session().set_hard_mute(muted).await })
.await
.map_err(|e| BridgeError::Unmapped(format!("join: {e}")))??;
Ok(())
@@ -579,6 +683,19 @@ pub enum BridgeEvent {
/// `"mouse-side-button"`); empty when no binding is active.
bound_input_class: String,
},
/// Voice subsystem state snapshot (SDD-094). The Flutter
/// VoiceBar listens to this stream.
VoiceState {
/// True when the session is currently joined to a voice
/// channel and the audio engine is running.
in_channel: bool,
/// Active transmit mode.
transmit_mode: BridgeTransmitMode,
/// True when the hard-mute clamp is engaged.
mute: bool,
/// Current release-tail in milliseconds (0..=500).
release_tail_ms: u32,
},
}
impl From<chanora_core::SessionEvent> for BridgeEvent {
@@ -612,6 +729,17 @@ impl From<chanora_core::SessionEvent> for BridgeEvent {
backend_id,
bound_input_class,
},
chanora_core::SessionEvent::VoiceState {
in_channel,
transmit_mode,
mute,
release_tail_ms,
} => BridgeEvent::VoiceState {
in_channel,
transmit_mode: transmit_mode_from_u8(transmit_mode),
mute,
release_tail_ms,
},
}
}
}