Usage Examples for the Revoize SDK in C++
Example #1: Processing a Single Audio File
This example walks through the C++ flow: get model params with get_model_params("Capella"), initialize the SDK, read a WAV file, process in chunks, and write the result. Kept minimal so you can adapt it to your own I/O and error handling.
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 params->input_sample_rate). Audio is processed in chunks whose size is params->input_chunk_size_samples (this varies by model; e.g. Capella uses 480).
Here's a general sequence diagram for this example:
Include Statements
First, we need to include the necessary header files to use the Revoize SDK and process audio files. We'll use sndfile.h for reading and writing WAV files.
#include "revoize_sdk.hpp"
#include <vector>
#include <string>
#include <sndfile.h>The most important line from the Revoize SDK usage perspective is:
#include "revoize_sdk.hpp"
#include <vector>
#include <string>
#include <sndfile.h>We use revoize_sdk::get_model_params(name) to get model parameters (chunk sizes and sample rates), then revoize_sdk::init(params) and revoize_sdk::process(input).
Helper Functions
Before we start processing audio, we need some helper functions to read and write WAV files.
std::vector<float> readWavFile(const std::string& filename) {
SF_INFO sf_info;
SNDFILE* file = sf_open(filename.c_str(), SFM_READ, &sf_info);
if (!file) {
throw std::runtime_error(std::string("Could not open file: ") + filename +
" Error: " + sf_strerror(NULL));
}
std::vector<float> samples(sf_info.frames);
sf_count_t frames_read = sf_readf_float(file, samples.data(), sf_info.frames);
if (frames_read != sf_info.frames) {
sf_close(file);
throw std::runtime_error("Failed to read audio data");
}
sf_close(file);
return samples;
}
void writeWavFile(const std::string& filename, const std::vector<float>& samples, unsigned int sample_rate) {
SF_INFO sf_info;
sf_info.samplerate = sample_rate;
sf_info.channels = 1;
sf_info.format = SF_FORMAT_WAV | SF_FORMAT_FLOAT;
SNDFILE* file = sf_open(filename.c_str(), SFM_WRITE, &sf_info);
if (!file) {
throw std::runtime_error(std::string("Could not create file: ") + filename +
" Error: " + sf_strerror(NULL));
}
sf_count_t frames_written = sf_writef_float(file, samples.data(), samples.size());
if (frames_written != samples.size()) {
sf_close(file);
throw std::runtime_error("Failed to write audio data");
}
sf_close(file);
}Define Some Hardcoded Values
We get model parameters by name with revoize_sdk::get_model_params("Capella"). You can use revoize_sdk::list_names() to see available names. The parameters define input/output chunk sizes and sample rates.
const std::string input_file = "input.wav";
const std::string output_file = "output.wav";
const RevoizeModelParams* params = revoize_sdk::get_model_params("Capella");
size_t chunk_size = params->input_chunk_size_samples;Initialize the Revoize SDK
We initialize the SDK with the pointer from get_model_params.
revoize_sdk::init(params);The input WAV file should be at params->input_sample_rate. Output will have params->output_chunk_size_samples per chunk at params->output_sample_rate.
Load the Input WAV File
Next, we need to load the input WAV file from disk. We use the readWavFile helper function to read the WAV file.
// Read input WAV file
std::vector<float> input_samples = readWavFile(input_file);The readWavFile function returns a vector of audio samples. The samples are stored as float values to make them compatible with the Revoize SDK's process function.
Process the Audio in Chunks
We process the audio in chunks of params->input_chunk_size_samples. Each chunk produces params->output_chunk_size_samples output samples.
// Process the audio in chunks
std::vector<float> processed_audio;
processed_audio.reserve(input_samples.size());
for (size_t i = 0; i < input_samples.size(); i += chunk_size) {
if (i + chunk_size > input_samples.size()) {
break;
}
std::vector<float> chunk(input_samples.begin() + i,
input_samples.begin() + i + chunk_size);
std::vector<float> output_chunk = revoize_sdk::process(chunk);
// Append to output
processed_audio.insert(processed_audio.end(),
output_chunk.begin(),
output_chunk.end());
}The process function takes an input audio chunk, processes it, and returns the enhanced samples. We store all processed chunks in a vector called processed_audio.
Save the Processed Audio to a New WAV File
Finally, we save the processed audio to a new WAV file using the writeWavFile helper function.
// Write output WAV file (use model's output sample rate)
writeWavFile(output_file, processed_audio, params->output_sample_rate);Full Code Example
Below is the complete minimal source code example that demonstrates how to process a single audio file using the Revoize SDK.
#include "revoize_sdk.hpp"
#include <vector>
#include <string>
#include <sndfile.h>
std::vector<float> readWavFile(const std::string& filename) {
SF_INFO sf_info;
SNDFILE* file = sf_open(filename.c_str(), SFM_READ, &sf_info);
if (!file) {
throw std::runtime_error(std::string("Could not open file: ") + filename +
" Error: " + sf_strerror(NULL));
}
std::vector<float> samples(sf_info.frames);
sf_count_t frames_read = sf_readf_float(file, samples.data(), sf_info.frames);
if (frames_read != sf_info.frames) {
sf_close(file);
throw std::runtime_error("Failed to read audio data");
}
sf_close(file);
return samples;
}
void writeWavFile(const std::string& filename, const std::vector<float>& samples, unsigned int sample_rate) {
SF_INFO sf_info;
sf_info.samplerate = sample_rate;
sf_info.channels = 1;
sf_info.format = SF_FORMAT_WAV | SF_FORMAT_FLOAT;
SNDFILE* file = sf_open(filename.c_str(), SFM_WRITE, &sf_info);
if (!file) {
throw std::runtime_error(std::string("Could not create file: ") + filename +
" Error: " + sf_strerror(NULL));
}
sf_count_t frames_written = sf_writef_float(file, samples.data(), samples.size());
if (frames_written != samples.size()) {
sf_close(file);
throw std::runtime_error("Failed to write audio data");
}
sf_close(file);
}
int main() {
try {
// -------------------------------------------------
// 1. Hardcoded parameters and initialization
// -------------------------------------------------
const std::string input_file = "input.wav";
const std::string output_file = "output.wav";
const RevoizeModelParams* params = revoize_sdk::get_model_params("Capella");
size_t chunk_size = params->input_chunk_size_samples;
revoize_sdk::init(params);
// -------------------------------------------------
// 2. Load the input WAV file
// -------------------------------------------------
std::vector<float> input_samples = readWavFile(input_file);
// -------------------------------------------------
// 3. Process the audio in chunks
// -------------------------------------------------
std::vector<float> processed_audio;
processed_audio.reserve(input_samples.size());
for (size_t i = 0; i < input_samples.size(); i += chunk_size) {
if (i + chunk_size > input_samples.size()) {
break;
}
// Process this chunk
std::vector<float> chunk(input_samples.begin() + i,
input_samples.begin() + i + chunk_size);
std::vector<float> output_chunk = revoize_sdk::process(chunk);
// Append to output
processed_audio.insert(processed_audio.end(),
output_chunk.begin(),
output_chunk.end());
}
// -------------------------------------------------
// 4. Save the processed audio to a new WAV file
// -------------------------------------------------
writeWavFile(output_file, processed_audio, params->output_sample_rate);
return 0;
} catch (const std::exception& e) {
std::cerr << "Error: " << e.what() << std::endl;
return 1;
}
}Example #2: Real-time Speech Enhancement
Coming Soon.