Model

Struct Model 

Source
pub struct Model { /* private fields */ }
Expand description

High-level wrapper for the AIC audio enhancement model.

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, ModelType};

let license_key = std::env::var("AIC_SDK_LICENSE").unwrap();
let mut model = Model::new(ModelType::QuailS48, &license_key).unwrap();

model.initialize(48000, 1, 1024, false).unwrap();

// Process audio data
let mut audio_buffer = vec![0.0f32; 1024];
model.process_interleaved(&mut audio_buffer, 1, 1024).unwrap();

Implementations§

Source§

impl Model

Source

pub fn new(model_type: ModelType, license_key: &str) -> Result<Self, AicError>

Creates a new audio enhancement model instance.

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

§Arguments
  • model_type - Selects the enhancement algorithm variant
  • license_key - Valid license key for the AIC SDK
§Returns

Returns a Result containing the new Model instance or an AicError if creation fails.

§Example
let license_key = std::env::var("AIC_SDK_LICENSE").unwrap();
let model = Model::new(ModelType::QuailS48, &license_key).unwrap();
Examples found in repository?
examples/basic_usage.rs (line 18)
6fn main() -> Result<(), Box<dyn std::error::Error>> {
7    // Display library version
8    println!("ai-coustics SDK version: {}", aic_sdk::get_version());
9
10    // Get license key from environment variable
11    let license = env::var("AIC_SDK_LICENSE").map_err(|_| {
12        eprintln!("Error: AIC_SDK_LICENSE environment variable not set");
13        eprintln!("Please set it with: export AIC_SDK_LICENSE=your_license_key");
14        std::io::Error::new(std::io::ErrorKind::NotFound, "AIC_SDK_LICENSE not set")
15    })?;
16
17    // Model creation with license key
18    let mut model = Model::new(ModelType::QuailS48, &license)?;
19    println!("Model created successfully");
20
21    // Get optimal settings
22    let optimal_sample_rate = model.optimal_sample_rate()?;
23    println!("Optimal sample rate: {} Hz", optimal_sample_rate);
24
25    let optimal_num_frames = model.optimal_num_frames(optimal_sample_rate)?;
26    println!("Optimal frame count: {}", optimal_num_frames);
27
28    // Initialize with basic audio config
29    model.initialize(optimal_sample_rate, NUM_CHANNELS, optimal_num_frames, true)?;
30    println!("Model initialized successfully");
31
32    // Get output delay
33    let delay = model.output_delay()?;
34    println!("Output delay: {} samples", delay);
35
36    // Test parameter setting and getting
37    model.set_parameter(Parameter::EnhancementLevel, 0.7)?;
38    println!("Parameter set successfully");
39
40    let enhancement_level = model.get_parameter(Parameter::EnhancementLevel)?;
41    println!("Enhancement level: {}", enhancement_level);
42
43    // Create minimal test audio - planar format (separate buffers for each channel)
44    let mut audio_buffer_left = vec![0.0f32; optimal_num_frames];
45    let mut audio_buffer_right = vec![0.0f32; optimal_num_frames];
46
47    // Create mutable references for planar processing
48    let mut audio_planar = vec![
49        audio_buffer_left.as_mut_slice(),
50        audio_buffer_right.as_mut_slice(),
51    ];
52
53    // Test planar audio processing
54    match model.process_planar(&mut audio_planar) {
55        Ok(()) => println!("Planar processing succeeded"),
56        Err(e) => println!("Planar processing failed: {}", e),
57    }
58
59    // Create interleaved test audio (all channels mixed together)
60    let mut audio_buffer_interleaved = vec![0.0f32; NUM_CHANNELS as usize * optimal_num_frames];
61
62    // Test interleaved audio processing
63    match model.process_interleaved(
64        &mut audio_buffer_interleaved,
65        NUM_CHANNELS,
66        optimal_num_frames,
67    ) {
68        Ok(()) => println!("Interleaved processing succeeded"),
69        Err(e) => println!("Interleaved processing failed: {}", e),
70    }
71
72    // Test reset functionality
73    match model.reset() {
74        Ok(()) => println!("Model reset succeeded"),
75        Err(e) => println!("Model reset failed: {}", e),
76    }
77
78    // Clean up is handled automatically by Rust's Drop trait
79    println!("All tests completed");
80
81    Ok(())
82}
Source

pub fn initialize( &mut self, sample_rate: u32, num_channels: u16, num_frames: usize, allow_variable_frames: bool, ) -> Result<(), AicError>

Configures the model for a specific audio format.

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

§Arguments
  • sample_rate - Audio sample rate in Hz (8000 - 192000)
  • num_channels - Number of audio channels (1 for mono, 2 for stereo, etc.)
  • num_frames - Number of samples per channel in each process call
  • allow_variable_frames - Allows varying frame counts per process call (up to num_frames), but increases delay.
§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 model instances.

§Example
model.initialize(48000, 1, 1024, true).unwrap();
Examples found in repository?
examples/basic_usage.rs (line 29)
6fn main() -> Result<(), Box<dyn std::error::Error>> {
7    // Display library version
8    println!("ai-coustics SDK version: {}", aic_sdk::get_version());
9
10    // Get license key from environment variable
11    let license = env::var("AIC_SDK_LICENSE").map_err(|_| {
12        eprintln!("Error: AIC_SDK_LICENSE environment variable not set");
13        eprintln!("Please set it with: export AIC_SDK_LICENSE=your_license_key");
14        std::io::Error::new(std::io::ErrorKind::NotFound, "AIC_SDK_LICENSE not set")
15    })?;
16
17    // Model creation with license key
18    let mut model = Model::new(ModelType::QuailS48, &license)?;
19    println!("Model created successfully");
20
21    // Get optimal settings
22    let optimal_sample_rate = model.optimal_sample_rate()?;
23    println!("Optimal sample rate: {} Hz", optimal_sample_rate);
24
25    let optimal_num_frames = model.optimal_num_frames(optimal_sample_rate)?;
26    println!("Optimal frame count: {}", optimal_num_frames);
27
28    // Initialize with basic audio config
29    model.initialize(optimal_sample_rate, NUM_CHANNELS, optimal_num_frames, true)?;
30    println!("Model initialized successfully");
31
32    // Get output delay
33    let delay = model.output_delay()?;
34    println!("Output delay: {} samples", delay);
35
36    // Test parameter setting and getting
37    model.set_parameter(Parameter::EnhancementLevel, 0.7)?;
38    println!("Parameter set successfully");
39
40    let enhancement_level = model.get_parameter(Parameter::EnhancementLevel)?;
41    println!("Enhancement level: {}", enhancement_level);
42
43    // Create minimal test audio - planar format (separate buffers for each channel)
44    let mut audio_buffer_left = vec![0.0f32; optimal_num_frames];
45    let mut audio_buffer_right = vec![0.0f32; optimal_num_frames];
46
47    // Create mutable references for planar processing
48    let mut audio_planar = vec![
49        audio_buffer_left.as_mut_slice(),
50        audio_buffer_right.as_mut_slice(),
51    ];
52
53    // Test planar audio processing
54    match model.process_planar(&mut audio_planar) {
55        Ok(()) => println!("Planar processing succeeded"),
56        Err(e) => println!("Planar processing failed: {}", e),
57    }
58
59    // Create interleaved test audio (all channels mixed together)
60    let mut audio_buffer_interleaved = vec![0.0f32; NUM_CHANNELS as usize * optimal_num_frames];
61
62    // Test interleaved audio processing
63    match model.process_interleaved(
64        &mut audio_buffer_interleaved,
65        NUM_CHANNELS,
66        optimal_num_frames,
67    ) {
68        Ok(()) => println!("Interleaved processing succeeded"),
69        Err(e) => println!("Interleaved processing failed: {}", e),
70    }
71
72    // Test reset functionality
73    match model.reset() {
74        Ok(()) => println!("Model reset succeeded"),
75        Err(e) => println!("Model reset failed: {}", e),
76    }
77
78    // Clean up is handled automatically by Rust's Drop trait
79    println!("All tests completed");
80
81    Ok(())
82}
Source

pub fn reset(&mut self) -> Result<(), AicError>

Clears all internal state and buffers.

Call this when the audio stream is interrupted or when seeking to prevent artifacts from previous audio content.

The model stays initialized to the configured settings.

§Returns

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

§Thread Safety

Real-time safe. Can be called from audio processing threads.

§Example
model.reset().unwrap();
Examples found in repository?
examples/basic_usage.rs (line 73)
6fn main() -> Result<(), Box<dyn std::error::Error>> {
7    // Display library version
8    println!("ai-coustics SDK version: {}", aic_sdk::get_version());
9
10    // Get license key from environment variable
11    let license = env::var("AIC_SDK_LICENSE").map_err(|_| {
12        eprintln!("Error: AIC_SDK_LICENSE environment variable not set");
13        eprintln!("Please set it with: export AIC_SDK_LICENSE=your_license_key");
14        std::io::Error::new(std::io::ErrorKind::NotFound, "AIC_SDK_LICENSE not set")
15    })?;
16
17    // Model creation with license key
18    let mut model = Model::new(ModelType::QuailS48, &license)?;
19    println!("Model created successfully");
20
21    // Get optimal settings
22    let optimal_sample_rate = model.optimal_sample_rate()?;
23    println!("Optimal sample rate: {} Hz", optimal_sample_rate);
24
25    let optimal_num_frames = model.optimal_num_frames(optimal_sample_rate)?;
26    println!("Optimal frame count: {}", optimal_num_frames);
27
28    // Initialize with basic audio config
29    model.initialize(optimal_sample_rate, NUM_CHANNELS, optimal_num_frames, true)?;
30    println!("Model initialized successfully");
31
32    // Get output delay
33    let delay = model.output_delay()?;
34    println!("Output delay: {} samples", delay);
35
36    // Test parameter setting and getting
37    model.set_parameter(Parameter::EnhancementLevel, 0.7)?;
38    println!("Parameter set successfully");
39
40    let enhancement_level = model.get_parameter(Parameter::EnhancementLevel)?;
41    println!("Enhancement level: {}", enhancement_level);
42
43    // Create minimal test audio - planar format (separate buffers for each channel)
44    let mut audio_buffer_left = vec![0.0f32; optimal_num_frames];
45    let mut audio_buffer_right = vec![0.0f32; optimal_num_frames];
46
47    // Create mutable references for planar processing
48    let mut audio_planar = vec![
49        audio_buffer_left.as_mut_slice(),
50        audio_buffer_right.as_mut_slice(),
51    ];
52
53    // Test planar audio processing
54    match model.process_planar(&mut audio_planar) {
55        Ok(()) => println!("Planar processing succeeded"),
56        Err(e) => println!("Planar processing failed: {}", e),
57    }
58
59    // Create interleaved test audio (all channels mixed together)
60    let mut audio_buffer_interleaved = vec![0.0f32; NUM_CHANNELS as usize * optimal_num_frames];
61
62    // Test interleaved audio processing
63    match model.process_interleaved(
64        &mut audio_buffer_interleaved,
65        NUM_CHANNELS,
66        optimal_num_frames,
67    ) {
68        Ok(()) => println!("Interleaved processing succeeded"),
69        Err(e) => println!("Interleaved processing failed: {}", e),
70    }
71
72    // Test reset functionality
73    match model.reset() {
74        Ok(()) => println!("Model reset succeeded"),
75        Err(e) => println!("Model reset failed: {}", e),
76    }
77
78    // Clean up is handled automatically by Rust's Drop trait
79    println!("All tests completed");
80
81    Ok(())
82}
Source

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

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

Enhances speech in the provided audio buffers in-place.

The planar function allows a maximum of 16 channels.

§Arguments
  • audio - Array of channel buffer pointers to be enhanced in-place
§Returns

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

§Example
let mut audio = vec![vec![0.0f32; 480]; 2]; // 2 channels, 480 frames each
let mut audio_refs: Vec<&mut [f32]> = audio.iter_mut().map(|ch| ch.as_mut_slice()).collect();
model.initialize(48000, 2, 480, false).unwrap();
model.process_planar(&mut audio_refs).unwrap();
Examples found in repository?
examples/basic_usage.rs (line 54)
6fn main() -> Result<(), Box<dyn std::error::Error>> {
7    // Display library version
8    println!("ai-coustics SDK version: {}", aic_sdk::get_version());
9
10    // Get license key from environment variable
11    let license = env::var("AIC_SDK_LICENSE").map_err(|_| {
12        eprintln!("Error: AIC_SDK_LICENSE environment variable not set");
13        eprintln!("Please set it with: export AIC_SDK_LICENSE=your_license_key");
14        std::io::Error::new(std::io::ErrorKind::NotFound, "AIC_SDK_LICENSE not set")
15    })?;
16
17    // Model creation with license key
18    let mut model = Model::new(ModelType::QuailS48, &license)?;
19    println!("Model created successfully");
20
21    // Get optimal settings
22    let optimal_sample_rate = model.optimal_sample_rate()?;
23    println!("Optimal sample rate: {} Hz", optimal_sample_rate);
24
25    let optimal_num_frames = model.optimal_num_frames(optimal_sample_rate)?;
26    println!("Optimal frame count: {}", optimal_num_frames);
27
28    // Initialize with basic audio config
29    model.initialize(optimal_sample_rate, NUM_CHANNELS, optimal_num_frames, true)?;
30    println!("Model initialized successfully");
31
32    // Get output delay
33    let delay = model.output_delay()?;
34    println!("Output delay: {} samples", delay);
35
36    // Test parameter setting and getting
37    model.set_parameter(Parameter::EnhancementLevel, 0.7)?;
38    println!("Parameter set successfully");
39
40    let enhancement_level = model.get_parameter(Parameter::EnhancementLevel)?;
41    println!("Enhancement level: {}", enhancement_level);
42
43    // Create minimal test audio - planar format (separate buffers for each channel)
44    let mut audio_buffer_left = vec![0.0f32; optimal_num_frames];
45    let mut audio_buffer_right = vec![0.0f32; optimal_num_frames];
46
47    // Create mutable references for planar processing
48    let mut audio_planar = vec![
49        audio_buffer_left.as_mut_slice(),
50        audio_buffer_right.as_mut_slice(),
51    ];
52
53    // Test planar audio processing
54    match model.process_planar(&mut audio_planar) {
55        Ok(()) => println!("Planar processing succeeded"),
56        Err(e) => println!("Planar processing failed: {}", e),
57    }
58
59    // Create interleaved test audio (all channels mixed together)
60    let mut audio_buffer_interleaved = vec![0.0f32; NUM_CHANNELS as usize * optimal_num_frames];
61
62    // Test interleaved audio processing
63    match model.process_interleaved(
64        &mut audio_buffer_interleaved,
65        NUM_CHANNELS,
66        optimal_num_frames,
67    ) {
68        Ok(()) => println!("Interleaved processing succeeded"),
69        Err(e) => println!("Interleaved processing failed: {}", e),
70    }
71
72    // Test reset functionality
73    match model.reset() {
74        Ok(()) => println!("Model reset succeeded"),
75        Err(e) => println!("Model reset failed: {}", e),
76    }
77
78    // Clean up is handled automatically by Rust's Drop trait
79    println!("All tests completed");
80
81    Ok(())
82}
Source

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

Processes audio with interleaved channel data.

Enhances speech in the provided audio buffer in-place.

§Arguments
  • audio - Interleaved audio buffer to be enhanced in-place. Must be exactly of size num_channels * num_frames
  • num_channels - Number of channels (must match initialization)
  • num_frames - Number of frames (must match initialization)
§Returns

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

§Example
let mut audio = vec![0.0f32; 2 * 480]; // 2 channels, 480 frames
model.initialize(48000, 2, 480, false).unwrap();
model.process_interleaved(&mut audio, 2, 480).unwrap();
Examples found in repository?
examples/basic_usage.rs (lines 63-67)
6fn main() -> Result<(), Box<dyn std::error::Error>> {
7    // Display library version
8    println!("ai-coustics SDK version: {}", aic_sdk::get_version());
9
10    // Get license key from environment variable
11    let license = env::var("AIC_SDK_LICENSE").map_err(|_| {
12        eprintln!("Error: AIC_SDK_LICENSE environment variable not set");
13        eprintln!("Please set it with: export AIC_SDK_LICENSE=your_license_key");
14        std::io::Error::new(std::io::ErrorKind::NotFound, "AIC_SDK_LICENSE not set")
15    })?;
16
17    // Model creation with license key
18    let mut model = Model::new(ModelType::QuailS48, &license)?;
19    println!("Model created successfully");
20
21    // Get optimal settings
22    let optimal_sample_rate = model.optimal_sample_rate()?;
23    println!("Optimal sample rate: {} Hz", optimal_sample_rate);
24
25    let optimal_num_frames = model.optimal_num_frames(optimal_sample_rate)?;
26    println!("Optimal frame count: {}", optimal_num_frames);
27
28    // Initialize with basic audio config
29    model.initialize(optimal_sample_rate, NUM_CHANNELS, optimal_num_frames, true)?;
30    println!("Model initialized successfully");
31
32    // Get output delay
33    let delay = model.output_delay()?;
34    println!("Output delay: {} samples", delay);
35
36    // Test parameter setting and getting
37    model.set_parameter(Parameter::EnhancementLevel, 0.7)?;
38    println!("Parameter set successfully");
39
40    let enhancement_level = model.get_parameter(Parameter::EnhancementLevel)?;
41    println!("Enhancement level: {}", enhancement_level);
42
43    // Create minimal test audio - planar format (separate buffers for each channel)
44    let mut audio_buffer_left = vec![0.0f32; optimal_num_frames];
45    let mut audio_buffer_right = vec![0.0f32; optimal_num_frames];
46
47    // Create mutable references for planar processing
48    let mut audio_planar = vec![
49        audio_buffer_left.as_mut_slice(),
50        audio_buffer_right.as_mut_slice(),
51    ];
52
53    // Test planar audio processing
54    match model.process_planar(&mut audio_planar) {
55        Ok(()) => println!("Planar processing succeeded"),
56        Err(e) => println!("Planar processing failed: {}", e),
57    }
58
59    // Create interleaved test audio (all channels mixed together)
60    let mut audio_buffer_interleaved = vec![0.0f32; NUM_CHANNELS as usize * optimal_num_frames];
61
62    // Test interleaved audio processing
63    match model.process_interleaved(
64        &mut audio_buffer_interleaved,
65        NUM_CHANNELS,
66        optimal_num_frames,
67    ) {
68        Ok(()) => println!("Interleaved processing succeeded"),
69        Err(e) => println!("Interleaved processing failed: {}", e),
70    }
71
72    // Test reset functionality
73    match model.reset() {
74        Ok(()) => println!("Model reset succeeded"),
75        Err(e) => println!("Model reset failed: {}", e),
76    }
77
78    // Clean up is handled automatically by Rust's Drop trait
79    println!("All tests completed");
80
81    Ok(())
82}
Source

pub fn set_parameter( &mut self, parameter: Parameter, value: f32, ) -> Result<(), AicError>

Modifies a model parameter.

All parameters can be changed during audio processing. This function can be called from any thread.

§Arguments
  • parameter - Parameter to modify
  • value - New parameter value. See parameter documentation for ranges
§Returns

Returns Ok(()) on success or an AicError if the parameter cannot be set.

§Example
model.set_parameter(Parameter::EnhancementLevel, 0.8).unwrap();
model.set_parameter(Parameter::NoiseGateEnable, 1.0).unwrap(); // 1.0 = enabled
Examples found in repository?
examples/basic_usage.rs (line 37)
6fn main() -> Result<(), Box<dyn std::error::Error>> {
7    // Display library version
8    println!("ai-coustics SDK version: {}", aic_sdk::get_version());
9
10    // Get license key from environment variable
11    let license = env::var("AIC_SDK_LICENSE").map_err(|_| {
12        eprintln!("Error: AIC_SDK_LICENSE environment variable not set");
13        eprintln!("Please set it with: export AIC_SDK_LICENSE=your_license_key");
14        std::io::Error::new(std::io::ErrorKind::NotFound, "AIC_SDK_LICENSE not set")
15    })?;
16
17    // Model creation with license key
18    let mut model = Model::new(ModelType::QuailS48, &license)?;
19    println!("Model created successfully");
20
21    // Get optimal settings
22    let optimal_sample_rate = model.optimal_sample_rate()?;
23    println!("Optimal sample rate: {} Hz", optimal_sample_rate);
24
25    let optimal_num_frames = model.optimal_num_frames(optimal_sample_rate)?;
26    println!("Optimal frame count: {}", optimal_num_frames);
27
28    // Initialize with basic audio config
29    model.initialize(optimal_sample_rate, NUM_CHANNELS, optimal_num_frames, true)?;
30    println!("Model initialized successfully");
31
32    // Get output delay
33    let delay = model.output_delay()?;
34    println!("Output delay: {} samples", delay);
35
36    // Test parameter setting and getting
37    model.set_parameter(Parameter::EnhancementLevel, 0.7)?;
38    println!("Parameter set successfully");
39
40    let enhancement_level = model.get_parameter(Parameter::EnhancementLevel)?;
41    println!("Enhancement level: {}", enhancement_level);
42
43    // Create minimal test audio - planar format (separate buffers for each channel)
44    let mut audio_buffer_left = vec![0.0f32; optimal_num_frames];
45    let mut audio_buffer_right = vec![0.0f32; optimal_num_frames];
46
47    // Create mutable references for planar processing
48    let mut audio_planar = vec![
49        audio_buffer_left.as_mut_slice(),
50        audio_buffer_right.as_mut_slice(),
51    ];
52
53    // Test planar audio processing
54    match model.process_planar(&mut audio_planar) {
55        Ok(()) => println!("Planar processing succeeded"),
56        Err(e) => println!("Planar processing failed: {}", e),
57    }
58
59    // Create interleaved test audio (all channels mixed together)
60    let mut audio_buffer_interleaved = vec![0.0f32; NUM_CHANNELS as usize * optimal_num_frames];
61
62    // Test interleaved audio processing
63    match model.process_interleaved(
64        &mut audio_buffer_interleaved,
65        NUM_CHANNELS,
66        optimal_num_frames,
67    ) {
68        Ok(()) => println!("Interleaved processing succeeded"),
69        Err(e) => println!("Interleaved processing failed: {}", e),
70    }
71
72    // Test reset functionality
73    match model.reset() {
74        Ok(()) => println!("Model reset succeeded"),
75        Err(e) => println!("Model reset failed: {}", e),
76    }
77
78    // Clean up is handled automatically by Rust's Drop trait
79    println!("All tests completed");
80
81    Ok(())
82}
Source

pub fn get_parameter(&self, parameter: Parameter) -> Result<f32, AicError>

Retrieves the current value of a parameter.

This function can be called from any thread.

§Arguments
  • parameter - Parameter to query
§Returns

Returns Ok(value) containing the current parameter value, or an AicError if the query fails.

§Example
let enhancement_level = model.get_parameter(Parameter::EnhancementLevel).unwrap();
println!("Current enhancement level: {}", enhancement_level);
Examples found in repository?
examples/basic_usage.rs (line 40)
6fn main() -> Result<(), Box<dyn std::error::Error>> {
7    // Display library version
8    println!("ai-coustics SDK version: {}", aic_sdk::get_version());
9
10    // Get license key from environment variable
11    let license = env::var("AIC_SDK_LICENSE").map_err(|_| {
12        eprintln!("Error: AIC_SDK_LICENSE environment variable not set");
13        eprintln!("Please set it with: export AIC_SDK_LICENSE=your_license_key");
14        std::io::Error::new(std::io::ErrorKind::NotFound, "AIC_SDK_LICENSE not set")
15    })?;
16
17    // Model creation with license key
18    let mut model = Model::new(ModelType::QuailS48, &license)?;
19    println!("Model created successfully");
20
21    // Get optimal settings
22    let optimal_sample_rate = model.optimal_sample_rate()?;
23    println!("Optimal sample rate: {} Hz", optimal_sample_rate);
24
25    let optimal_num_frames = model.optimal_num_frames(optimal_sample_rate)?;
26    println!("Optimal frame count: {}", optimal_num_frames);
27
28    // Initialize with basic audio config
29    model.initialize(optimal_sample_rate, NUM_CHANNELS, optimal_num_frames, true)?;
30    println!("Model initialized successfully");
31
32    // Get output delay
33    let delay = model.output_delay()?;
34    println!("Output delay: {} samples", delay);
35
36    // Test parameter setting and getting
37    model.set_parameter(Parameter::EnhancementLevel, 0.7)?;
38    println!("Parameter set successfully");
39
40    let enhancement_level = model.get_parameter(Parameter::EnhancementLevel)?;
41    println!("Enhancement level: {}", enhancement_level);
42
43    // Create minimal test audio - planar format (separate buffers for each channel)
44    let mut audio_buffer_left = vec![0.0f32; optimal_num_frames];
45    let mut audio_buffer_right = vec![0.0f32; optimal_num_frames];
46
47    // Create mutable references for planar processing
48    let mut audio_planar = vec![
49        audio_buffer_left.as_mut_slice(),
50        audio_buffer_right.as_mut_slice(),
51    ];
52
53    // Test planar audio processing
54    match model.process_planar(&mut audio_planar) {
55        Ok(()) => println!("Planar processing succeeded"),
56        Err(e) => println!("Planar processing failed: {}", e),
57    }
58
59    // Create interleaved test audio (all channels mixed together)
60    let mut audio_buffer_interleaved = vec![0.0f32; NUM_CHANNELS as usize * optimal_num_frames];
61
62    // Test interleaved audio processing
63    match model.process_interleaved(
64        &mut audio_buffer_interleaved,
65        NUM_CHANNELS,
66        optimal_num_frames,
67    ) {
68        Ok(()) => println!("Interleaved processing succeeded"),
69        Err(e) => println!("Interleaved processing failed: {}", e),
70    }
71
72    // Test reset functionality
73    match model.reset() {
74        Ok(()) => println!("Model reset succeeded"),
75        Err(e) => println!("Model reset failed: {}", e),
76    }
77
78    // Clean up is handled automatically by Rust's Drop trait
79    println!("All tests completed");
80
81    Ok(())
82}
Source

pub fn output_delay(&self) -> Result<usize, AicError>

Returns the total output delay in samples for the current audio configuration.

This function provides the complete end-to-end latency introduced by the model, which includes both algorithmic processing delay and any buffering overhead. Use this value to synchronize enhanced audio with other streams or to implement delay compensation in your application.

Delay behavior:

  • Before initialization: Returns the base processing delay using the model’s optimal frame size at its native sample rate
  • After initialization: Returns the actual delay for your specific configuration, including any additional buffering introduced by non-optimal frame sizes

Important: The delay value is always expressed in samples at the sample rate you configured during initialize. To convert to time units: delay_ms = (delay_samples * 1000) / sample_rate

Note: Using frame sizes different from the optimal value returned by optimal_num_frames will increase the delay beyond the model’s base latency.

§Returns

Returns Ok(delay_samples) or an AicError if the query fails.

§Example
let delay = model.output_delay().unwrap();
println!("Output delay: {} samples", delay);
Examples found in repository?
examples/basic_usage.rs (line 33)
6fn main() -> Result<(), Box<dyn std::error::Error>> {
7    // Display library version
8    println!("ai-coustics SDK version: {}", aic_sdk::get_version());
9
10    // Get license key from environment variable
11    let license = env::var("AIC_SDK_LICENSE").map_err(|_| {
12        eprintln!("Error: AIC_SDK_LICENSE environment variable not set");
13        eprintln!("Please set it with: export AIC_SDK_LICENSE=your_license_key");
14        std::io::Error::new(std::io::ErrorKind::NotFound, "AIC_SDK_LICENSE not set")
15    })?;
16
17    // Model creation with license key
18    let mut model = Model::new(ModelType::QuailS48, &license)?;
19    println!("Model created successfully");
20
21    // Get optimal settings
22    let optimal_sample_rate = model.optimal_sample_rate()?;
23    println!("Optimal sample rate: {} Hz", optimal_sample_rate);
24
25    let optimal_num_frames = model.optimal_num_frames(optimal_sample_rate)?;
26    println!("Optimal frame count: {}", optimal_num_frames);
27
28    // Initialize with basic audio config
29    model.initialize(optimal_sample_rate, NUM_CHANNELS, optimal_num_frames, true)?;
30    println!("Model initialized successfully");
31
32    // Get output delay
33    let delay = model.output_delay()?;
34    println!("Output delay: {} samples", delay);
35
36    // Test parameter setting and getting
37    model.set_parameter(Parameter::EnhancementLevel, 0.7)?;
38    println!("Parameter set successfully");
39
40    let enhancement_level = model.get_parameter(Parameter::EnhancementLevel)?;
41    println!("Enhancement level: {}", enhancement_level);
42
43    // Create minimal test audio - planar format (separate buffers for each channel)
44    let mut audio_buffer_left = vec![0.0f32; optimal_num_frames];
45    let mut audio_buffer_right = vec![0.0f32; optimal_num_frames];
46
47    // Create mutable references for planar processing
48    let mut audio_planar = vec![
49        audio_buffer_left.as_mut_slice(),
50        audio_buffer_right.as_mut_slice(),
51    ];
52
53    // Test planar audio processing
54    match model.process_planar(&mut audio_planar) {
55        Ok(()) => println!("Planar processing succeeded"),
56        Err(e) => println!("Planar processing failed: {}", e),
57    }
58
59    // Create interleaved test audio (all channels mixed together)
60    let mut audio_buffer_interleaved = vec![0.0f32; NUM_CHANNELS as usize * optimal_num_frames];
61
62    // Test interleaved audio processing
63    match model.process_interleaved(
64        &mut audio_buffer_interleaved,
65        NUM_CHANNELS,
66        optimal_num_frames,
67    ) {
68        Ok(()) => println!("Interleaved processing succeeded"),
69        Err(e) => println!("Interleaved processing failed: {}", e),
70    }
71
72    // Test reset functionality
73    match model.reset() {
74        Ok(()) => println!("Model reset succeeded"),
75        Err(e) => println!("Model reset failed: {}", e),
76    }
77
78    // Clean up is handled automatically by Rust's Drop trait
79    println!("All tests completed");
80
81    Ok(())
82}
Source

pub fn optimal_sample_rate(&self) -> Result<u32, AicError>

Retrieves the native sample rate of the selected model.

Each model is optimized for a specific sample rate, which determines the frequency range of the enhanced audio output. While you can process audio at any sample rate, understanding the model’s native rate helps predict the enhancement quality.

How sample rate affects enhancement:

  • Models trained at lower sample rates (e.g., 8 kHz) can only enhance frequencies up to their Nyquist limit (4 kHz for 8 kHz models)
  • When processing higher sample rate input (e.g., 48 kHz) with a lower-rate model, only the lower frequency components will be enhanced

Enhancement blending: When enhancement strength is set below 1.0, the enhanced signal is blended with the original, maintaining the full frequency spectrum of your input while adding the model’s noise reduction capabilities to the lower frequencies.

Sample rate and optimal frames relationship: When using different sample rates than the model’s native rate, the optimal number of frames (returned by optimal_num_frames) will change. The model’s output delay remains constant regardless of sample rate as long as you use the optimal frame count for that rate.

Recommendation: For maximum enhancement quality across the full frequency spectrum, match your input sample rate to the model’s native rate when possible.

§Returns

Returns Ok(sample_rate) or an AicError if the query fails.

§Example
let optimal_rate = model.optimal_sample_rate().unwrap();
println!("Optimal sample rate: {} Hz", optimal_rate);
Examples found in repository?
examples/basic_usage.rs (line 22)
6fn main() -> Result<(), Box<dyn std::error::Error>> {
7    // Display library version
8    println!("ai-coustics SDK version: {}", aic_sdk::get_version());
9
10    // Get license key from environment variable
11    let license = env::var("AIC_SDK_LICENSE").map_err(|_| {
12        eprintln!("Error: AIC_SDK_LICENSE environment variable not set");
13        eprintln!("Please set it with: export AIC_SDK_LICENSE=your_license_key");
14        std::io::Error::new(std::io::ErrorKind::NotFound, "AIC_SDK_LICENSE not set")
15    })?;
16
17    // Model creation with license key
18    let mut model = Model::new(ModelType::QuailS48, &license)?;
19    println!("Model created successfully");
20
21    // Get optimal settings
22    let optimal_sample_rate = model.optimal_sample_rate()?;
23    println!("Optimal sample rate: {} Hz", optimal_sample_rate);
24
25    let optimal_num_frames = model.optimal_num_frames(optimal_sample_rate)?;
26    println!("Optimal frame count: {}", optimal_num_frames);
27
28    // Initialize with basic audio config
29    model.initialize(optimal_sample_rate, NUM_CHANNELS, optimal_num_frames, true)?;
30    println!("Model initialized successfully");
31
32    // Get output delay
33    let delay = model.output_delay()?;
34    println!("Output delay: {} samples", delay);
35
36    // Test parameter setting and getting
37    model.set_parameter(Parameter::EnhancementLevel, 0.7)?;
38    println!("Parameter set successfully");
39
40    let enhancement_level = model.get_parameter(Parameter::EnhancementLevel)?;
41    println!("Enhancement level: {}", enhancement_level);
42
43    // Create minimal test audio - planar format (separate buffers for each channel)
44    let mut audio_buffer_left = vec![0.0f32; optimal_num_frames];
45    let mut audio_buffer_right = vec![0.0f32; optimal_num_frames];
46
47    // Create mutable references for planar processing
48    let mut audio_planar = vec![
49        audio_buffer_left.as_mut_slice(),
50        audio_buffer_right.as_mut_slice(),
51    ];
52
53    // Test planar audio processing
54    match model.process_planar(&mut audio_planar) {
55        Ok(()) => println!("Planar processing succeeded"),
56        Err(e) => println!("Planar processing failed: {}", e),
57    }
58
59    // Create interleaved test audio (all channels mixed together)
60    let mut audio_buffer_interleaved = vec![0.0f32; NUM_CHANNELS as usize * optimal_num_frames];
61
62    // Test interleaved audio processing
63    match model.process_interleaved(
64        &mut audio_buffer_interleaved,
65        NUM_CHANNELS,
66        optimal_num_frames,
67    ) {
68        Ok(()) => println!("Interleaved processing succeeded"),
69        Err(e) => println!("Interleaved processing failed: {}", e),
70    }
71
72    // Test reset functionality
73    match model.reset() {
74        Ok(()) => println!("Model reset succeeded"),
75        Err(e) => println!("Model reset failed: {}", e),
76    }
77
78    // Clean up is handled automatically by Rust's Drop trait
79    println!("All tests completed");
80
81    Ok(())
82}
Source

pub fn optimal_num_frames(&self, sample_rate: u32) -> Result<usize, AicError>

Retrieves the optimal number of frames for the selected model at a given sample rate.

Using the optimal number of frames minimizes latency by avoiding internal buffering.

When you use a different frame count than the optimal value, the model will introduce additional buffering latency on top of its base processing delay.

The optimal frame count varies based on the sample rate. Each model operates on a fixed time window duration, so the required number of frames changes with sample rate. For example, a model designed for 10 ms processing windows requires 480 frames at 48 kHz, but only 160 frames at 16 kHz to capture the same duration of audio.

Call this function with your intended sample rate before calling aic_model_initialize to determine the best frame count for minimal latency.

§Arguments
  • sample_rate - The sample rate in Hz for which to calculate the optimal frame count.
§Returns

Returns Ok(num_frames) or an AicError if the query fails.

§Example
let optimal_frames = model.optimal_num_frames(sample_rate).unwrap();
println!("Optimal frame count: {}", optimal_frames);
Examples found in repository?
examples/basic_usage.rs (line 25)
6fn main() -> Result<(), Box<dyn std::error::Error>> {
7    // Display library version
8    println!("ai-coustics SDK version: {}", aic_sdk::get_version());
9
10    // Get license key from environment variable
11    let license = env::var("AIC_SDK_LICENSE").map_err(|_| {
12        eprintln!("Error: AIC_SDK_LICENSE environment variable not set");
13        eprintln!("Please set it with: export AIC_SDK_LICENSE=your_license_key");
14        std::io::Error::new(std::io::ErrorKind::NotFound, "AIC_SDK_LICENSE not set")
15    })?;
16
17    // Model creation with license key
18    let mut model = Model::new(ModelType::QuailS48, &license)?;
19    println!("Model created successfully");
20
21    // Get optimal settings
22    let optimal_sample_rate = model.optimal_sample_rate()?;
23    println!("Optimal sample rate: {} Hz", optimal_sample_rate);
24
25    let optimal_num_frames = model.optimal_num_frames(optimal_sample_rate)?;
26    println!("Optimal frame count: {}", optimal_num_frames);
27
28    // Initialize with basic audio config
29    model.initialize(optimal_sample_rate, NUM_CHANNELS, optimal_num_frames, true)?;
30    println!("Model initialized successfully");
31
32    // Get output delay
33    let delay = model.output_delay()?;
34    println!("Output delay: {} samples", delay);
35
36    // Test parameter setting and getting
37    model.set_parameter(Parameter::EnhancementLevel, 0.7)?;
38    println!("Parameter set successfully");
39
40    let enhancement_level = model.get_parameter(Parameter::EnhancementLevel)?;
41    println!("Enhancement level: {}", enhancement_level);
42
43    // Create minimal test audio - planar format (separate buffers for each channel)
44    let mut audio_buffer_left = vec![0.0f32; optimal_num_frames];
45    let mut audio_buffer_right = vec![0.0f32; optimal_num_frames];
46
47    // Create mutable references for planar processing
48    let mut audio_planar = vec![
49        audio_buffer_left.as_mut_slice(),
50        audio_buffer_right.as_mut_slice(),
51    ];
52
53    // Test planar audio processing
54    match model.process_planar(&mut audio_planar) {
55        Ok(()) => println!("Planar processing succeeded"),
56        Err(e) => println!("Planar processing failed: {}", e),
57    }
58
59    // Create interleaved test audio (all channels mixed together)
60    let mut audio_buffer_interleaved = vec![0.0f32; NUM_CHANNELS as usize * optimal_num_frames];
61
62    // Test interleaved audio processing
63    match model.process_interleaved(
64        &mut audio_buffer_interleaved,
65        NUM_CHANNELS,
66        optimal_num_frames,
67    ) {
68        Ok(()) => println!("Interleaved processing succeeded"),
69        Err(e) => println!("Interleaved processing failed: {}", e),
70    }
71
72    // Test reset functionality
73    match model.reset() {
74        Ok(()) => println!("Model reset succeeded"),
75        Err(e) => println!("Model reset failed: {}", e),
76    }
77
78    // Clean up is handled automatically by Rust's Drop trait
79    println!("All tests completed");
80
81    Ok(())
82}

Trait Implementations§

Source§

impl Drop for Model

Source§

fn drop(&mut self)

Releases all resources associated with a model instance.

After calling this function, the model handle becomes invalid. This function is safe to call with NULL.

Source§

impl Send for Model

Source§

impl Sync for Model

Auto Trait Implementations§

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.