Skip to main content

Vad

Struct Vad 

Source
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>

Source

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
§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)?;
Source

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)?;
Source

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)?;
Source

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)?;
Source

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 match block_size from initialization, or if variable_block_size was enabled, must be less than or equal to block_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)?;
Source

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();
Source

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§

Source§

impl<'a> Drop for Vad<'a>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more
Source§

impl<'a> Send for Vad<'a>

Source§

impl<'a> Sync for Vad<'a>

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more