Realm

Decode, normalize, and save audio in Rust

Current source-owned tutorial. API availability differs by language.

Rust · Python

Start the walkthrough · Jump to complete source

The smallest complete OA audio pipeline: decode a FLAC file, normalize its peak level, inspect the result, and write it back as WAV. This walkthrough follows the same experiment as the C++ tuCoreEngine tutorial. The output below is from the Rust run with OA 0.18.0 on Intel Iris Xe with Mesa 26.3.1.

Run in Rust

From the OASDK root, with oa-dev as a sibling checkout:

cargo run --release --example tu_audio_basics

1. Resolve the input and output paths

Path::asset_rel resolves a file inside the SDK asset tree, honoring OA_ASSET when set. Path::var_rel resolves under the var output tree so generated media stays out of the source checkout. The tutorial creates the output directory before writing.

1let asset = Path::asset_rel("audio/oaNarration.flac");
2let output = Path::var_rel("tutorial/audio/oaNarrationNormalized.wav");
3if let Some(parent) = output.parent() {
4 std::fs::create_dir_all(&parent)?;
5}
6

2. Decode the file

decode_file probes the container, decodes every stream, and uploads planar FP32 samples on this thread's default runtime. The bundled narration clip is mono at 24 kHz.

1let audio = decode_file(&asset)?;
2

3. Normalize the peak level

Audio::normalize rescales the signal so its global peak sits at target_db. The default Peak mode matches the Python call.

1let clean = audio.normalize(NormalizeAudioConfig {
2 target_db: -3.0,
3 ..NormalizeAudioConfig::default()
4})?;
5

4. Save and inspect

save_wav_f32 encodes the FP32 buffers as interleaved WAV and writes the file. channels, sample_rate, and duration_seconds report the decoded shape of the normalized result.

1save_wav_f32(&output, &clean)?;
2println!(
3 "Channels: {} Sample-rate: {} Duration: {:.2} s",
4 clean.channels(),
5 clean.sample_rate(),
6 clean.duration_seconds()
7);
8println!("Saved: {}", output.display());
9
1Channels: 1 Sample-rate: 24000 Duration: 12.52 s
2Saved: var/tutorial/audio/oaNarrationNormalized.wav
3

The Python program decodes and normalizes the same clip and prints the same summary.

Source

Rust example · Full Python walkthrough

Example

1//! Decode, normalize, and save one audio file with the OA audio API.
2//!
3//! Mirrors sdk/cpp/tutorials/core/tuCoreEngine.cpp, the basic Audio DSP
4//! tutorial.
5
6use anyhow::{Context, Result};
7use oa::Path;
8use oa::audio::{NormalizeAudioConfig, decode_file, save_wav_f32};
9
10fn main() -> Result<()> {
11 let asset = Path::asset_rel("audio/oaNarration.flac");
12 let output = Path::var_rel("tutorial/audio/oaNarrationNormalized.wav");
13 if let Some(parent) = output.parent() {
14 std::fs::create_dir_all(&parent)
15 .with_context(|| format!("failed to create {}", parent.display()))?;
16 }
17
18 let audio = decode_file(&asset)?;
19 let clean = audio.normalize(NormalizeAudioConfig {
20 target_db: -3.0,
21 ..NormalizeAudioConfig::default()
22 })?;
23 save_wav_f32(&output, &clean)?;
24
25 println!(
26 "Channels: {} Sample-rate: {} Duration: {:.2} s",
27 clean.channels(),
28 clean.sample_rate(),
29 clean.duration_seconds()
30 );
31 println!("Saved: {}", output.display());
32 Ok(())
33}
34

Run Rust tutorials from the OA SDK checkout with cargo run --release --example <name>. Run Python with the environment containing the OA native extension. Consult each program's arguments and capability checks before running it.