Processor

Struct Processor 

Source
pub struct Processor<'a> { /* private fields */ }
Expand description

High-level wrapper for the ai-coustics audio enhancement processor.

This struct provides a safe, Rust-friendly interface to the underlying C library. It handles memory management automatically and converts C-style error codes to Rust Result types.

§Example

use aic_sdk::{Model, ProcessorConfig, Processor};

let license_key = std::env::var("AIC_SDK_LICENSE").unwrap();
let model = Model::from_file("/path/to/model.aicmodel").unwrap();
let config = ProcessorConfig {
    num_channels: 2,
    num_frames: 1024,
    ..ProcessorConfig::optimal(&model)
};

let mut processor = Processor::new(&model, &license_key).unwrap();
processor.initialize(&config).unwrap();

let mut audio_buffer = vec![0.0f32; config.num_channels as usize * config.num_frames];
processor.process_interleaved(&mut audio_buffer).unwrap();

Implementations§

Source§

impl<'a> Processor<'a>

Source

pub fn new(model: &Model<'a>, license_key: &str) -> Result<Self, AicError>

Creates a new audio enhancement processor instance.

Multiple processors can be created to process different audio streams simultaneously or to switch between different enhancement algorithms during runtime.

§Arguments
  • model - The loaded model instance
  • license_key - license key for the ai-coustics SDK (generate your key at developers.ai-coustics.com)
§Returns

Returns a Result containing the new Processor 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/model.aicmodel").unwrap();
let processor = Processor::new(&model, &license_key).unwrap();
Source

pub fn with_config(self, config: &ProcessorConfig) -> Result<Self, AicError>

Initializes the processor with the given configuration.

This is a convenience method that calls Processor::initialize internally and returns self. The processor is immediately ready to process audio after calling this method, so you don’t need to call Processor::initialize separately.

§Arguments
  • config - Audio processing configuration
§Returns

Returns Ok(Self) with the initialized processor, or an AicError if initialization fails.

§Example
let license_key = std::env::var("AIC_SDK_LICENSE").unwrap();
let model = Model::from_file("/path/to/model.aicmodel")?;
let config = ProcessorConfig::optimal(&model).with_num_channels(2);

let mut processor = Processor::new(&model, &license_key)?.with_config(&config)?;

// Processor is ready to use - no need to call initialize()
let mut audio = vec![0.0f32; config.num_channels as usize * config.num_frames];
processor.process_interleaved(&mut audio).unwrap();
Source

pub fn processor_context(&self) -> ProcessorContext

Creates a ProcessorContext instance. This can be used to control all parameters and other settings of the processor.

§Example
let license_key = std::env::var("AIC_SDK_LICENSE").unwrap();
let model = Model::from_file("/path/to/model.aicmodel").unwrap();
let processor = Processor::new(&model, &license_key).unwrap();
let processor_context = processor.processor_context();
Source

pub fn vad_context(&self) -> VadContext

Creates a Voice Activity Detector Context instance.

§Example
let license_key = std::env::var("AIC_SDK_LICENSE").unwrap();
let model = Model::from_file("/path/to/model.aicmodel").unwrap();
let processor = Processor::new(&model, &license_key).unwrap();
let vad = processor.vad_context();
Source

pub fn initialize(&mut self, config: &ProcessorConfig) -> Result<(), AicError>

Configures the processor for specific audio settings.

This function must be called before processing any audio. For the lowest delay use the sample rate and frame size returned by Model::optimal_sample_rate and Model::optimal_num_frames.

§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.

§Note

All channels are mixed to mono for processing. To process channels independently, create separate Processor instances.

§Example
let config = ProcessorConfig::optimal(&model);
processor.initialize(&config).unwrap();
Source

pub fn process_planar<V: AsMut<[f32]>>( &mut self, audio: &mut [V], ) -> Result<(), AicError>

Processes audio with separate buffers for each channel (planar layout).

Enhances speech in the provided audio buffers in-place.

Memory Layout:

  • Separate buffer for each channel
  • Each buffer contains num_frames floats
  • Maximum of 16 channels supported
  • Example for 2 channels, 4 frames:
    audio[0] -> [ch0_f0, ch0_f1, ch0_f2, ch0_f3]
    audio[1] -> [ch1_f0, ch1_f1, ch1_f2, ch1_f3]

The function accepts any type of collection of f32 values that implements as_mut, e.g.:

  • [vec![0.0; 128]; 2]
  • [[0.0; 128]; 2]
  • [&mut ch1, &mut ch2]
§Arguments
  • audio - Array of mutable channel buffer slices to be enhanced in-place. Each channel buffer must be exactly of size num_frames, or if allow_variable_frames was enabled, less than the initialization value.
§Note

Maximum supported number of channels is 16. Exceeding this will return an error.

§Returns

Returns Ok(()) on success or an AicError if processing fails.

§Example
let config = ProcessorConfig::optimal(&model).with_num_channels(2);
processor.initialize(&config).unwrap();
let mut audio = vec![vec![0.0f32; config.num_frames]; config.num_channels as usize];
processor.process_planar(&mut audio).unwrap();
Source

pub fn process_interleaved(&mut self, audio: &mut [f32]) -> Result<(), AicError>

Processes audio with interleaved channel data.

Enhances speech in the provided audio buffer in-place.

Memory Layout:

  • Single contiguous buffer with samples alternating between channels
  • Buffer size: num_channels * num_frames floats
  • Example for 2 channels, 4 frames:
    audio -> [ch0_f0, ch1_f0, ch0_f1, ch1_f1, ch0_f2, ch1_f2, ch0_f3, ch1_f3]
§Arguments
  • audio - Interleaved audio buffer to be enhanced in-place. Must be exactly of size num_channels * num_frames, or if allow_variable_frames was enabled, less than the initialization value per channel.
§Returns

Returns Ok(()) on success or an AicError if processing fails.

§Example
let config = ProcessorConfig::optimal(&model).with_num_channels(2);
processor.initialize(&config).unwrap();
let mut audio = vec![0.0f32; config.num_channels as usize * config.num_frames];
processor.process_interleaved(&mut audio).unwrap();
Source

pub fn process_sequential(&mut self, audio: &mut [f32]) -> Result<(), AicError>

Processes audio with sequential channel data.

Enhances speech in the provided audio buffer in-place.

Memory Layout:

  • Single contiguous buffer with all samples for each channel stored sequentially
  • Buffer size: num_channels * num_frames floats
  • Example for 2 channels, 4 frames:
    audio -> [ch0_f0, ch0_f1, ch0_f2, ch0_f3, ch1_f0, ch1_f1, ch1_f2, ch1_f3]
§Arguments
  • audio - Sequential audio buffer to be enhanced in-place. Must be exactly of size num_channels * num_frames, or if allow_variable_frames was enabled, less than the initialization value per channel.
§Returns

Returns Ok(()) on success or an AicError if processing fails.

§Example
let config = ProcessorConfig::optimal(&model).with_num_channels(2);;
processor.initialize(&config).unwrap();
let mut audio = vec![0.0f32; config.num_channels as usize * config.num_frames];
processor.process_sequential(&mut audio).unwrap();

Trait Implementations§

Source§

impl<'a> Drop for Processor<'a>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl<'a> Send for Processor<'a>

Source§

impl<'a> Sync for Processor<'a>

Auto Trait Implementations§

§

impl<'a> Freeze for Processor<'a>

§

impl<'a> RefUnwindSafe for Processor<'a>

§

impl<'a> Unpin for Processor<'a>

§

impl<'a> UnwindSafe for Processor<'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, 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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.