|
| 1 | +//! Local VAD barge-in policy and coupled-output debounce. |
| 2 | +
|
| 3 | +use super::human_floor::HumanFloor; |
| 4 | + |
| 5 | +/// Whether local audio should use VAD barge-in for this frame. |
| 6 | +/// |
| 7 | +/// This currently matches STT's `vad_flush_allowed`, but the two decisions are |
| 8 | +/// kept separate deliberately: one assigns cancellation ownership and the |
| 9 | +/// other controls utterance endpointing. |
| 10 | +pub(super) fn enabled(ptt_mode: bool, manually_open: bool, ptt_held: bool) -> bool { |
| 11 | + !ptt_mode || (manually_open && !ptt_held) |
| 12 | +} |
| 13 | + |
| 14 | +/// Consecutive 16 ms VAD-positive frames required to restore local barge-in |
| 15 | +/// on acoustically coupled output. The prior implementation shipped 20 frames |
| 16 | +/// after 5 frames caused speaker-bleed self-cancellation (`b29c8cdaa^`). |
| 17 | +const COUPLED_BARGE_IN_FRAMES: usize = 20; |
| 18 | + |
| 19 | +#[derive(Debug, Default)] |
| 20 | +pub(super) struct LocalBargeIn { |
| 21 | + acquired_floor: bool, |
| 22 | + coupled_positive_frames: usize, |
| 23 | +} |
| 24 | + |
| 25 | +impl LocalBargeIn { |
| 26 | + pub(super) fn observe( |
| 27 | + &mut self, |
| 28 | + probability: f32, |
| 29 | + confirmed_onset: bool, |
| 30 | + human_floor: &HumanFloor, |
| 31 | + output_device: Option<&str>, |
| 32 | + onset_threshold: f32, |
| 33 | + ) { |
| 34 | + if self.acquired_floor { |
| 35 | + return; |
| 36 | + } |
| 37 | + let sustained_coupled = self.track_sustained_coupled(probability, onset_threshold); |
| 38 | + if !confirmed_onset && !sustained_coupled { |
| 39 | + return; |
| 40 | + } |
| 41 | + let route_isolated = super::audio_output::output_route_is_isolated(output_device); |
| 42 | + self.acquire(human_floor, route_isolated, sustained_coupled); |
| 43 | + } |
| 44 | + |
| 45 | + pub(super) fn acquire( |
| 46 | + &mut self, |
| 47 | + human_floor: &HumanFloor, |
| 48 | + route_isolated: bool, |
| 49 | + sustained_coupled: bool, |
| 50 | + ) { |
| 51 | + self.acquired_floor = human_floor.enter_local(route_isolated, sustained_coupled); |
| 52 | + } |
| 53 | + |
| 54 | + fn track_sustained_coupled(&mut self, probability: f32, onset_threshold: f32) -> bool { |
| 55 | + if probability > onset_threshold { |
| 56 | + self.coupled_positive_frames = self.coupled_positive_frames.saturating_add(1); |
| 57 | + } else { |
| 58 | + self.coupled_positive_frames = 0; |
| 59 | + } |
| 60 | + self.coupled_positive_frames >= COUPLED_BARGE_IN_FRAMES |
| 61 | + } |
| 62 | + |
| 63 | + pub(super) fn release(&mut self, human_floor: &HumanFloor) { |
| 64 | + if self.acquired_floor { |
| 65 | + human_floor.leave_local(); |
| 66 | + } |
| 67 | + *self = Self::default(); |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +#[derive(Debug)] |
| 72 | +pub(super) struct WorkerLocalBargeIn { |
| 73 | + state: LocalBargeIn, |
| 74 | + human_floor: HumanFloor, |
| 75 | +} |
| 76 | + |
| 77 | +impl WorkerLocalBargeIn { |
| 78 | + pub(super) fn new(human_floor: HumanFloor) -> Self { |
| 79 | + Self { |
| 80 | + state: LocalBargeIn::default(), |
| 81 | + human_floor, |
| 82 | + } |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +impl std::ops::Deref for WorkerLocalBargeIn { |
| 87 | + type Target = LocalBargeIn; |
| 88 | + |
| 89 | + fn deref(&self) -> &Self::Target { |
| 90 | + &self.state |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +impl std::ops::DerefMut for WorkerLocalBargeIn { |
| 95 | + fn deref_mut(&mut self) -> &mut Self::Target { |
| 96 | + &mut self.state |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +impl Drop for WorkerLocalBargeIn { |
| 101 | + fn drop(&mut self) { |
| 102 | + self.state.release(&self.human_floor); |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +#[cfg(test)] |
| 107 | +mod tests { |
| 108 | + use super::*; |
| 109 | + |
| 110 | + #[test] |
| 111 | + fn manual_open_mic_enables_vad_barge_in_in_ptt_mode() { |
| 112 | + assert!(enabled(true, true, false)); |
| 113 | + assert!(!enabled(true, false, false)); |
| 114 | + assert!(!enabled(true, true, true)); |
| 115 | + assert!(enabled(false, false, false)); |
| 116 | + } |
| 117 | + |
| 118 | + #[test] |
| 119 | + fn manual_open_ptt_sustained_speech_acquires_coupled_floor() { |
| 120 | + assert!(enabled(true, true, false)); |
| 121 | + let human_floor = HumanFloor::new(); |
| 122 | + let mut barge_in = LocalBargeIn::default(); |
| 123 | + for _ in 0..COUPLED_BARGE_IN_FRAMES { |
| 124 | + let sustained = barge_in.track_sustained_coupled(0.9, 0.5); |
| 125 | + if sustained { |
| 126 | + barge_in.acquire(&human_floor, false, true); |
| 127 | + } |
| 128 | + } |
| 129 | + assert!(barge_in.acquired_floor); |
| 130 | + assert!(human_floor.is_blocked()); |
| 131 | + } |
| 132 | + |
| 133 | + #[test] |
| 134 | + fn coupled_barge_in_requires_twenty_consecutive_positive_frames() { |
| 135 | + let mut barge_in = LocalBargeIn::default(); |
| 136 | + for _ in 0..COUPLED_BARGE_IN_FRAMES - 1 { |
| 137 | + assert!(!barge_in.track_sustained_coupled(0.9, 0.5)); |
| 138 | + } |
| 139 | + assert!(barge_in.track_sustained_coupled(0.9, 0.5)); |
| 140 | + } |
| 141 | + |
| 142 | + #[test] |
| 143 | + fn coupled_barge_in_debounce_resets_on_a_non_speech_frame() { |
| 144 | + let mut barge_in = LocalBargeIn::default(); |
| 145 | + for _ in 0..COUPLED_BARGE_IN_FRAMES - 1 { |
| 146 | + assert!(!barge_in.track_sustained_coupled(0.9, 0.5)); |
| 147 | + } |
| 148 | + assert!(!barge_in.track_sustained_coupled(0.1, 0.5)); |
| 149 | + assert!(!barge_in.track_sustained_coupled(0.9, 0.5)); |
| 150 | + } |
| 151 | +} |
0 commit comments