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>
impl<'a> Processor<'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 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 instancelicense_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();Sourcepub fn with_config(self, config: &ProcessorConfig) -> Result<Self, AicError>
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();Sourcepub fn processor_context(&self) -> ProcessorContext
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();Sourcepub fn vad_context(&self) -> VadContext
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();Sourcepub fn initialize(&mut self, config: &ProcessorConfig) -> Result<(), AicError>
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();Sourcepub fn process_planar<V: AsMut<[f32]>>(
&mut self,
audio: &mut [V],
) -> Result<(), AicError>
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_framesfloats - 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 sizenum_frames, or ifallow_variable_frameswas 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();Sourcepub fn process_interleaved(&mut self, audio: &mut [f32]) -> Result<(), AicError>
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_framesfloats - 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 sizenum_channels*num_frames, or ifallow_variable_frameswas 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();Sourcepub fn process_sequential(&mut self, audio: &mut [f32]) -> Result<(), AicError>
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_framesfloats - 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 sizenum_channels*num_frames, or ifallow_variable_frameswas 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();