Usage Examples for the Revoize SDK in Rust
Example #1: Processing a Single Audio File
This example walks through the basics: load a WAV file from disk, run it through the SDK in fixed-size chunks, and write the enhanced audio to a new file. It’s a minimal setup so you can see the full flow (get params → init → process in a loop).
WARNING
The input and output file paths and model name are hardcoded in this example. You may need to modify the input file path to match the location of your audio file. The input WAV file must be at the sample rate required by the chosen model (see model_params.input_sample_rate). Audio is processed in chunks whose size is given by model_params.input_chunk_size_samples (this varies by model; e.g. Capella uses 480, other models may use different sizes).
Here's a general sequence diagram for this example:
Import Statements
First, we need to import the necessary modules and libraries to use the Revoize SDK and process audio files. We'll use the error crate to handle errors, the path crate to work with file paths, and the hound crate to read and write WAV files.
use std::error::Error;
use std::path::Path;
use revoize_sdk::config::models;
use revoize_sdk::{init, process};
use hound::{WavReader, WavWriter, WavSpec, SampleFormat};The most important lines from the Revoize SDK usage perspective are:
use revoize_sdk::config::models;
use revoize_sdk::{init, process}; We use models::get_params(name) to obtain model parameters (chunk sizes and sample rates), then init(¶ms) and process(chunk).
Main Function Signature
This example is simple, so we only need a main function to run the code.
fn main() -> Result<(), Box<dyn Error>> {
...This function does not take any arguments and returns a Result type that can either be Ok(()) if the operation was successful or an error if something went wrong. In a real-life application, you would probably want to pass a list of input arguments like the path to the input file, the path to the output file, etc., but this is out of scope for this example.
Get model parameters and define paths
We obtain parameters for the model by name (e.g. "Capella"). The same name can be chosen from models::list_names() in a real app. Model parameters define the required input/output chunk sizes and sample rates.
// Input WAV file path
let input_wav = "input.wav";
// Output WAV file path
let output_wav = "output.wav";
// Get model parameters by name (use models::list_names() to see available names)
let model_params = models::get_params("Capella").ok_or("Model not available")?;
let chunk_size = model_params.input_chunk_size_samples;Initialize the Revoize SDK
Before we can start processing audio, we initialize the SDK with the model parameters.
init(&model_params)?;The input WAV file should be at model_params.input_sample_rate (e.g. 48000 Hz for Capella). Output will have model_params.output_chunk_size_samples per chunk at model_params.output_sample_rate.
Load the Input WAV File
Next, we need to load the input WAV file from disk. We use the WavReader from the hound crate to read the WAV file.
let mut reader = WavReader::open(&input_wav)?;
let spec = reader.spec();The spec variable contains the specifications of the input WAV file (channels, sample rate, format). The file's sample rate should match model_params.input_sample_rate for the chosen model.
Convert Audio Samples to f32
The audio samples are read from the WAV file as integers (either 16-bit or 32-bit) or as floats (32-bit). We need to convert them to f32 format to make them compatible with the Revoize SDK's process function.
let audio_samples: Vec<f32> = match (spec.sample_format, spec.bits_per_sample) {
(SampleFormat::Int, 16) => {
let max_val = i16::MAX as f32;
reader.samples::<i16>().map(|s| s.unwrap() as f32 / max_val).collect()
}
(SampleFormat::Int, 32) => {
let max_val = i32::MAX as f32;
reader.samples::<i32>().map(|s| s.unwrap() as f32 / max_val).collect()
}
(SampleFormat::Float, 32) => {
reader.samples::<f32>().map(|s| s.unwrap()).collect()
}
_ => return Err("Unsupported WAV format".into())
};This code snippet reads the audio samples from the WAV file and converts them to f32 format. It handles different sample formats (integer or float) and different bit depths (16-bit or 32-bit).
Process the Audio in Chunks
Now that we have the audio samples in f32 format, we can process them in chunks using the Revoize SDK. We iterate over the audio samples in chunks of chunk_size and process each chunk using the process function.
let mut processed_audio = Vec::new();
let num_chunks = audio_samples.len() / chunk_size;
for i in 0..num_chunks {
let start = i * chunk_size;
let end = start + chunk_size;
let chunk = &audio_samples[start..end];
// Process each chunk using Revoize SDK
let output_chunk = process(chunk)?;
processed_audio.extend(output_chunk);
}The process function takes an input audio chunk and returns the processed audio chunk.
let mut processed_audio = Vec::new();
let num_chunks = audio_samples.len() / chunk_size;
for i in 0..num_chunks {
let start = i * chunk_size;
let end = start + chunk_size;
let chunk = &audio_samples[start..end];
// Process each chunk using Revoize SDK
let output_chunk = process(chunk)?;
processed_audio.extend(output_chunk);
}We store the processed audio chunks in a vector called processed_audio.
let mut processed_audio = Vec::new();
let num_chunks = audio_samples.len() / chunk_size;
for i in 0..num_chunks {
let start = i * chunk_size;
let end = start + chunk_size;
let chunk = &audio_samples[start..end];
// Process each chunk using Revoize SDK
let output_chunk = process(chunk)?;
processed_audio.extend(output_chunk);
}This may not be the most efficient way to process audio since we will be keeping all the processed audio in memory until we go through the entire WAV file. In real-life scenarios, you would probably want to save the processed audio chunks to the output file as soon as they are processed.
Save the Processed Audio to a New WAV File
Finally, we save the processed audio to a new WAV file. We create a WavWriter using the model's output sample rate (model_params.output_sample_rate) and write the processed audio samples.
let out_spec = WavSpec {
channels: spec.channels,
sample_rate: model_params.output_sample_rate as u32,
bits_per_sample: 32,
sample_format: SampleFormat::Float,
};
let mut writer = WavWriter::create(&output_wav, out_spec)?;
for sample in processed_audio {
writer.write_sample(sample)?;
}
writer.finalize()?;Full Code Example
Below is the complete minimal source code example that demonstrates how to process a single audio file using the Revoize SDK.
use std::error::Error;
use std::path::Path;
use revoize_sdk::config::models;
use revoize_sdk::{init, process};
use hound::{WavReader, WavWriter, WavSpec, SampleFormat};
fn main() -> Result<(), Box<dyn Error>> {
// -------------------------------------------------
// 1. Get model parameters and initialize
// -------------------------------------------------
let input_wav = "input.wav";
let output_wav = "output.wav";
let model_params = models::get_params("Capella").ok_or("Model not available")?;
let chunk_size = model_params.input_chunk_size_samples;
init(&model_params)?;
// -------------------------------------------------
// 2. Load the input WAV file
// -------------------------------------------------
let mut reader = WavReader::open(&input_wav)?;
let spec = reader.spec();
// Convert samples to f32 (input file should be at model_params.input_sample_rate)
let audio_samples: Vec<f32> = match (spec.sample_format, spec.bits_per_sample) {
(SampleFormat::Int, 16) => {
let max_val = i16::MAX as f32;
reader.samples::<i16>().map(|s| s.unwrap() as f32 / max_val).collect()
}
(SampleFormat::Int, 32) => {
let max_val = i32::MAX as f32;
reader.samples::<i32>().map(|s| s.unwrap() as f32 / max_val).collect()
}
(SampleFormat::Float, 32) => {
reader.samples::<f32>().map(|s| s.unwrap()).collect()
}
_ => return Err("Unsupported WAV format".into())
};
// -------------------------------------------------
// 3. Process the audio in chunks
// -------------------------------------------------
let mut processed_audio = Vec::new();
let num_chunks = audio_samples.len() / chunk_size;
for i in 0..num_chunks {
let start = i * chunk_size;
let end = start + chunk_size;
let chunk = &audio_samples[start..end];
// **Process each chunk using Revoize SDK**
let output_chunk = process(chunk)?;
processed_audio.extend(output_chunk);
}
// -------------------------------------------------
// 4. Save the processed audio to a new WAV file
// -------------------------------------------------
let out_spec = WavSpec {
channels: spec.channels,
sample_rate: model_params.output_sample_rate as u32,
bits_per_sample: 32,
sample_format: SampleFormat::Float,
};
let mut writer = WavWriter::create(&output_wav, out_spec)?;
for sample in processed_audio {
writer.write_sample(sample)?;
}
writer.finalize()?;
Ok(())
}Example #2: Real-time Speech Enhancement
Coming Soon.