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
impl Model
Sourcepub fn new(model_type: ModelType, license_key: &str) -> Result<Self, AicError>
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 variantlicense_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?
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}Sourcepub fn initialize(
&mut self,
sample_rate: u32,
num_channels: u16,
num_frames: usize,
allow_variable_frames: bool,
) -> Result<(), AicError>
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 callallow_variable_frames- Allows varying frame counts per process call (up tonum_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?
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}Sourcepub fn reset(&mut self) -> Result<(), AicError>
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?
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}Sourcepub fn process_planar(
&mut self,
audio: &mut [&mut [f32]],
) -> Result<(), AicError>
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?
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}Sourcepub fn process_interleaved(
&mut self,
audio: &mut [f32],
num_channels: u16,
num_frames: usize,
) -> Result<(), AicError>
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 sizenum_channels*num_framesnum_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?
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}Sourcepub fn set_parameter(
&mut self,
parameter: Parameter,
value: f32,
) -> Result<(), AicError>
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 modifyvalue- 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 = enabledExamples found in repository?
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}Sourcepub fn get_parameter(&self, parameter: Parameter) -> Result<f32, AicError>
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?
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}Sourcepub fn output_delay(&self) -> Result<usize, AicError>
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?
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}Sourcepub fn optimal_sample_rate(&self) -> Result<u32, AicError>
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?
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}Sourcepub fn optimal_num_frames(&self, sample_rate: u32) -> Result<usize, AicError>
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?
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}