Open audio in the OA Viewer in Rust
Current source-owned tutorial. API availability differs by language.
Start the walkthrough · Jump to complete source
ViewerMode::Audio turns the level-0 viewer into a player: waveform, spectrum, or Mel-spectrogram visualization plus playback through the default output device.
Run
cargo run -p sdk --release --example tu_viewer_audio -- [file.flac] [waveform|spectrum|mel]
Both arguments are optional: the file defaults to asset/audio/oaNarration.flac and the view to waveform.
1. ViewerConfig selects the mode
The config record carries the mode, the media path, the audio visualization, and the window geometry. Viewer::new(config).run_standalone() owns everything else:
1let config = ViewerConfig {2 mode: ViewerMode::Audio,3 path: path.to_owned(),4 audio_view,5 title: "oa::Viewer · Audio".to_owned(),6 width: 960,7 height: 360,8 ..ViewerConfig::default()9};1011Viewer::new(config).run_standalone()12
audio_view accepts ViewerAudioView::Waveform, Spectrum, or Mel; the second CLI argument maps onto the same variants.
2. Controls
| Input | Action |
|---|---|
Space | Play / pause |
L | Toggle loop |
W / S / M | Waveform / spectrum / Mel view |
← / → | Seek ∓ / ± 5 s |
↑ / ↓ | Seek ± / ∓ 25 s |
Q / Esc | Quit |
What you built
A GPU-rendered audio player with three analysis views from one config struct. The full source is sdk/rs/tutorials/vision/tu_viewer_audio/tu_viewer_audio.rs; the Python form is at walkthrough-python.
Example
1// ═══════════════════════════════════════════════════════════════════════════2// OA Tutorial: oa::Viewer · Audio playback and analysis3// Level-0 API · oa::Viewer, ViewerMode::Audio4// ═══════════════════════════════════════════════════════════════════════════5//6// Opens a WAV/FLAC/MP3 audio file in the Viewer's audio mode, which renders7// an interactive waveform, spectrum, or Mel-spectrogram visualization and8// plays the audio through the default CPAL output device.9//10// Controls:11// Space · play/pause; L · loop12// W / S / M · waveform / spectrum / Mel13// Left / Right · seek backward / forward by 5 seconds14// Up / Down · seek forward / backward by 25 seconds15// Q / Esc · quit16//17// usage:18// cargo run --example tu_viewer_audio -- [file.flac]19// cargo run --example tu_viewer_audio -- [file.flac] waveform20// cargo run --example tu_viewer_audio -- [file.flac] spectrum21// cargo run --example tu_viewer_audio -- [file.flac] mel22//23// Defaults: asset/audio/oaNarration.flac, waveform view.24//25// Donor: sdk/cpp/tutorials/vision/tuViewerAudio.cpp26// ═══════════════════════════════════════════════════════════════════════════2728use oa::{Result, Viewer, ViewerAudioView, ViewerConfig, ViewerMode};2930fn main() -> Result<()> {31 let args: Vec<String> = std::env::args().collect();32 let default_path = oa::Path::asset_rel("audio/oaNarration.flac");33 let default_str = default_path.to_string_lossy().into_owned();34 let path = args.get(1).map(String::as_str).unwrap_or(&default_str);3536 let audio_view = match args.get(2).map(String::as_str) {37 Some("spectrum") => ViewerAudioView::Spectrum,38 Some("mel") => ViewerAudioView::Mel,39 _ => ViewerAudioView::Waveform,40 };4142 let config = ViewerConfig {43 mode: ViewerMode::Audio,44 path: path.to_owned(),45 audio_view,46 title: "oa::Viewer · Audio".to_owned(),47 width: 960,48 height: 360,49 ..ViewerConfig::default()50 };5152 Viewer::new(config).run_standalone()53}54
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.