fix(audio): address CoreML VAD review feedback

This commit is contained in:
Edison Jwa
2026-06-02 19:39:11 +09:00
parent 966afd2b53
commit ddf858cc6c
7 changed files with 40 additions and 21 deletions
+3 -6
View File
@@ -100,12 +100,6 @@ pub enum VadBackend {
Disabled,
}
#[cfg(target_os = "ios")]
fn default_vad_backend() -> VadBackend {
VadBackend::SileroOnnx
}
#[cfg(not(target_os = "ios"))]
fn default_vad_backend() -> VadBackend {
VadBackend::SileroOnnx
}
@@ -114,6 +108,9 @@ impl VadBackend {
/// Stable bridge/debug string.
pub fn as_str(self) -> &'static str {
match self {
#[cfg(any(target_os = "ios", target_os = "macos"))]
Self::SileroOnnx => "apple_coreml",
#[cfg(not(any(target_os = "ios", target_os = "macos")))]
Self::SileroOnnx => "silero_vad_onnx",
Self::WebrtcVad => "webrtc_vad",
Self::EnergyDebug => "energy_debug",
-1
View File
@@ -312,7 +312,6 @@ pub struct AudioEngine {
voice_activity_selector: Option<Arc<crate::TransmitModeSelector>>,
#[cfg(any(target_os = "ios", target_os = "macos", target_os = "android"))]
mic_gain: f32,
#[cfg(any(target_os = "ios", target_os = "macos"))]
// Streams must be dropped to stop audio. Both are `!Send` because
// cpal's Stream isn't Send on some backends; we keep them in an
// Option wrapped by Mutex so stop() can move them out. On Linux
+9 -9
View File
@@ -94,6 +94,7 @@ pub struct AppleCoreMlVad {
handle: *mut c_void,
symbols: AppleSileroSymbols,
accum: Vec<f32>,
frame_scratch: Box<[f32; SILERO_COREML_FRAME_16K]>,
last_probability: f32,
}
@@ -130,6 +131,7 @@ impl AppleCoreMlVad {
handle,
symbols,
accum: Vec::with_capacity(SILERO_COREML_FRAME_16K),
frame_scratch: Box::new([0.0; SILERO_COREML_FRAME_16K]),
last_probability: 0.0,
})
}
@@ -148,14 +150,13 @@ impl AppleCoreMlVad {
}
}
fn calc_level(&mut self, audio_frame: &[f32]) -> f32 {
debug_assert_eq!(audio_frame.len(), SILERO_COREML_FRAME_16K);
fn calc_level(&mut self) -> f32 {
let mut probability = self.last_probability;
let rc = unsafe {
(self.symbols.process)(
self.handle,
audio_frame.as_ptr(),
audio_frame.len(),
self.frame_scratch.as_ptr(),
self.frame_scratch.len(),
&mut probability,
)
};
@@ -183,11 +184,10 @@ impl VoiceActivityDetector for AppleCoreMlVad {
self.accum.extend_from_slice(samples);
if self.accum.len() >= SILERO_COREML_FRAME_16K {
let audio_frame: Vec<f32> = self.accum[..SILERO_COREML_FRAME_16K].to_vec();
self.calc_level(&audio_frame);
let overflow: Vec<f32> = self.accum.drain(SILERO_COREML_FRAME_16K..).collect();
self.accum.clear();
self.accum.extend_from_slice(&overflow);
self.frame_scratch
.copy_from_slice(&self.accum[..SILERO_COREML_FRAME_16K]);
self.calc_level();
drop(self.accum.drain(..SILERO_COREML_FRAME_16K));
}
VadOutput {
+4 -4
View File
@@ -1,9 +1,9 @@
//! Voice activity detection backends and helpers.
//!
//! iOS capture feeds VoiceProcessingIO-processed microphone frames into
//! this module and uses the realtime-safe WebRTC fallback. Other
//! platforms may use a model-backed detector when available so
//! VoiceActivity mode never collapses back to Continuous transmit.
//! Apple capture feeds VoiceProcessingIO/CoreAudio-processed microphone
//! frames into this module and prefers Apple CoreML Silero VAD when the
//! Swift bridge is linked. WebRTC VAD remains the realtime-safe fallback;
//! non-Apple platforms may use ONNX-backed Silero when available.
#[cfg(any(target_os = "ios", target_os = "macos"))]
pub mod apple_coreml;