use crate::error::*;
use aic_sdk_sys::{AicVadParameter::*, *};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum VadParameter {
SpeechHoldDuration,
Sensitivity,
MinimumSpeechDuration,
}
impl From<VadParameter> for AicVadParameter::Type {
fn from(parameter: VadParameter) -> Self {
match parameter {
VadParameter::SpeechHoldDuration => AIC_VAD_PARAMETER_SPEECH_HOLD_DURATION,
VadParameter::Sensitivity => AIC_VAD_PARAMETER_SENSITIVITY,
VadParameter::MinimumSpeechDuration => AIC_VAD_PARAMETER_MINIMUM_SPEECH_DURATION,
}
}
}
pub struct VadContext {
inner: *mut AicVadContext,
}
impl VadContext {
pub(crate) fn new(vad_ptr: *mut AicVadContext) -> Self {
Self { inner: vad_ptr }
}
fn as_const_ptr(&self) -> *const AicVadContext {
self.inner as *const AicVadContext
}
pub fn is_speech_detected(&self) -> bool {
let mut value: bool = false;
let error_code =
unsafe { aic_vad_context_is_speech_detected(self.as_const_ptr(), &mut value) };
assert!(handle_error(error_code).is_ok());
value
}
pub fn set_parameter(&self, parameter: VadParameter, value: f32) -> Result<(), AicError> {
let error_code =
unsafe { aic_vad_context_set_parameter(self.as_const_ptr(), parameter.into(), value) };
handle_error(error_code)
}
pub fn parameter(&self, parameter: VadParameter) -> Result<f32, AicError> {
let mut value: f32 = 0.0;
let error_code = unsafe {
aic_vad_context_get_parameter(self.as_const_ptr(), parameter.into(), &mut value)
};
handle_error(error_code)?;
Ok(value)
}
}
impl Drop for VadContext {
fn drop(&mut self) {
if !self.inner.is_null() {
unsafe { aic_vad_context_destroy(self.inner) };
}
}
}
unsafe impl Send for VadContext {}
unsafe impl Sync for VadContext {}