Realm

Open audio in the OA Viewer in Rust

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

Rust · Python

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};
10
11Viewer::new(config).run_standalone()
12

audio_view accepts ViewerAudioView::Waveform, Spectrum, or Mel; the second CLI argument maps onto the same variants.

2. Controls

InputAction
SpacePlay / pause
LToggle loop
W / S / MWaveform / spectrum / Mel view
← / →Seek ∓ / ± 5 s
↑ / ↓Seek ± / ∓ 25 s
Q / EscQuit

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 analysis
3// Level-0 API · oa::Viewer, ViewerMode::Audio
4// ═══════════════════════════════════════════════════════════════════════════
5//
6// Opens a WAV/FLAC/MP3 audio file in the Viewer's audio mode, which renders
7// an interactive waveform, spectrum, or Mel-spectrogram visualization and
8// plays the audio through the default CPAL output device.
9//
10// Controls:
11// Space · play/pause; L · loop
12// W / S / M · waveform / spectrum / Mel
13// Left / Right · seek backward / forward by 5 seconds
14// Up / Down · seek forward / backward by 25 seconds
15// Q / Esc · quit
16//
17// usage:
18// cargo run --example tu_viewer_audio -- [file.flac]
19// cargo run --example tu_viewer_audio -- [file.flac] waveform
20// cargo run --example tu_viewer_audio -- [file.flac] spectrum
21// cargo run --example tu_viewer_audio -- [file.flac] mel
22//
23// Defaults: asset/audio/oaNarration.flac, waveform view.
24//
25// Donor: sdk/cpp/tutorials/vision/tuViewerAudio.cpp
26// ═══════════════════════════════════════════════════════════════════════════
27
28use oa::{Result, Viewer, ViewerAudioView, ViewerConfig, ViewerMode};
29
30fn 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);
35
36 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 };
41
42 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 };
51
52 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.