pub struct Vad<'a> { /* private fields */ }Expand description
High-level wrapper for the ai-coustics voice activity detector.
A Vad is created from a VAD model (e.g. vad-2.1-xxs-16khz). Enhancement models
cannot be used for voice activity detection; pass them to a Processor
instead.
Feed the audio to be examined to Vad::process. The audio is not modified, it only
updates the detector’s prediction, which is read through a VadContext.
§Example
use aic_sdk::{Model, ProcessorConfig, Vad};
let license_key = std::env::var("AIC_SDK_LICENSE").unwrap();
let model = Model::from_file("/path/to/vad_model.aicmodel")?;
let config = ProcessorConfig::optimal(&model);
let mut vad = Vad::new(&model, &license_key)?.with_config(&config)?;
let vad_ctx = vad.context();
let audio_block = vec![0.0f32; config.block_size];
vad.process(&audio_block)?;
if vad_ctx.is_speech_detected() {
println!("Speech detected!");
}Implementations§
Source§impl<'a> Vad<'a>
impl<'a> Vad<'a>
Sourcepub fn new(model: &Model<'a>, license_key: &str) -> Result<Self, AicError>
pub fn new(model: &Model<'a>, license_key: &str) -> Result<Self, AicError>
Creates a new voice activity detector instance.
Multiple VAD instances can be created to process different audio streams simultaneously.
The same Model may be passed to this function more than once: each call creates an
independent VAD that shares the underlying model data internally.
§Arguments
model- The loaded model instance. Must be a VAD model, otherwiseAicError::ModelTypeUnsupportedis returned.license_key- license key for the ai-coustics SDK (generate your key at developers.ai-coustics.com)
§Returns
Returns a Result containing the new Vad instance or an AicError if creation fails.
§Example
let license_key = std::env::var("AIC_SDK_LICENSE").unwrap();
let model = Model::from_file("/path/to/vad_model.aicmodel")?;
let vad = Vad::new(&model, &license_key)?;Sourcepub fn with_otel_config(
model: &Model<'a>,
license_key: &str,
otel_config: &OtelConfig,
) -> Result<Self, AicError>
pub fn with_otel_config( model: &Model<'a>, license_key: &str, otel_config: &OtelConfig, ) -> Result<Self, AicError>
Creates a new voice activity detector instance with explicit OpenTelemetry configuration.
If provided, telemetry will be sent according to the provided configuration. Otherwise it will be configured according to the runtime environment.
This overrides the SDK’s environment-based telemetry defaults (e.g.
AIC_SDK_OTEL_ENABLE) for this VAD.
§Example
let model = Model::from_file("/path/to/vad_model.aicmodel")?;
let otel = OtelConfig::enabled();
let vad = Vad::with_otel_config(&model, &license_key, &otel)?;Sourcepub fn with_config(self, config: &ProcessorConfig) -> Result<Self, AicError>
pub fn with_config(self, config: &ProcessorConfig) -> Result<Self, AicError>
Initializes the VAD with the given configuration.
This is a convenience method that calls Vad::initialize internally and returns self.
The VAD is immediately ready to process audio after calling this method, so you don’t
need to call Vad::initialize separately.
§Arguments
config- Audio processing configuration
§Returns
Returns Ok(Self) with the initialized VAD, or an AicError if initialization fails.
§Example
let license_key = std::env::var("AIC_SDK_LICENSE").unwrap();
let model = Model::from_file("/path/to/vad_model.aicmodel")?;
let config = ProcessorConfig::optimal(&model);
let mut vad = Vad::new(&model, &license_key)?.with_config(&config)?;
// VAD is ready to use - no need to call initialize()
let audio_block = vec![0.0f32; config.block_size];
vad.process(&audio_block)?;Sourcepub fn initialize(&mut self, config: &ProcessorConfig) -> Result<(), AicError>
pub fn initialize(&mut self, config: &ProcessorConfig) -> Result<(), AicError>
Configures the VAD for specific audio settings.
This function must be called before processing any audio.
For the most frequent prediction updates, use the sample rate and block size returned by
Model::optimal_sample_rate and Model::optimal_block_size.
§Arguments
config- Audio processing configuration
§Returns
Returns Ok(()) on success or an AicError if initialization fails.
§Warning
Do not call from audio processing threads as this allocates memory.
§Example
let config = ProcessorConfig::optimal(&model);
vad.initialize(&config)?;Sourcepub fn process(&mut self, audio: &[f32]) -> Result<(), AicError>
pub fn process(&mut self, audio: &[f32]) -> Result<(), AicError>
Processes mono audio and updates the VAD prediction.
This function does not modify the input audio buffer. Read the prediction through a
VadContext.
§Recommendation
When enhancement and VAD run together, pass the original input audio here, not the output
of Processor::process. Enhancement is designed to change the
signal, so running the VAD on its output means detecting speech in audio that no longer
matches what the VAD model expects, and it stacks the processor’s audio delay on top of the
VAD’s prediction delay. Because this function does not modify its input, calling it on the
same buffer before Processor::process is enough:
vad.process(&audio)?; // reads the block, does not modify it
processor.process(&mut audio)?; // enhances the block in-place§Arguments
audio- Mono audio block to examine. Must matchblock_sizefrom initialization, or ifvariable_block_sizewas enabled, must be less than or equal toblock_size.
§Returns
Returns Ok(()) on success or an AicError if processing fails.
§Real-time safety
Real-time safe. Can be called from audio processing threads.
§Example
let config = ProcessorConfig::optimal(&model);
vad.initialize(&config)?;
let audio = vec![0.0f32; config.block_size];
vad.process(&audio)?;Sourcepub fn context(&self) -> VadContext
pub fn context(&self) -> VadContext
Creates a VadContext instance.
This can be used to read the prediction and to control all parameters and other
settings of the VAD.
All handles created from a given VAD reference the same VAD instance.
§Example
let license_key = std::env::var("AIC_SDK_LICENSE").unwrap();
let model = Model::from_file("/path/to/vad_model.aicmodel")?;
let vad = Vad::new(&model, &license_key)?;
let vad_ctx = vad.context();Sourcepub fn terminate_session(&mut self) -> Result<(), AicError>
pub fn terminate_session(&mut self) -> Result<(), AicError>
Terminates the telemetry session associated with this VAD.
Once the request has been handled, the VAD is no longer allowed to process audio.
This function is meant to be used in lifecycle management events. A telemetry session is automatically stopped when a VAD is destroyed. However, in cases where this SDK is integrated with languages with automatic memory management, object deallocation could be delayed. Use this function to terminate the session explicitly.
This function blocks until the telemetry session is terminated, unless another session is still alive. In that case, this function returns early and termination happens asynchronously. This keeps lifecycle management smooth while ensuring all sessions are closed when the last VAD is terminated.
§Returns
Returns Ok(()) on success or an AicError if termination cannot be requested.
§Real-time safety
This function is not real-time safe. It may block until the session is terminated. Avoid calling it from audio threads.
§Example
let mut vad = Vad::new(&model, &license_key)?;
vad.terminate_session()?;Trait Implementations§
Auto Trait Implementations§
impl<'a> Freeze for Vad<'a>
impl<'a> RefUnwindSafe for Vad<'a>
impl<'a> Unpin for Vad<'a>
impl<'a> UnsafeUnpin for Vad<'a>
impl<'a> UnwindSafe for Vad<'a>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more