From a1a90ae4f8f6d1573fcce2322a1401246b0025c4 Mon Sep 17 00:00:00 2001 From: tolelom <98kimsungmin@naver.com> Date: Wed, 25 Mar 2026 11:11:55 +0900 Subject: [PATCH] feat(audio): add audio_demo example with sine wave playback Co-Authored-By: Claude Sonnet 4.6 --- Cargo.toml | 1 + examples/audio_demo/Cargo.toml | 7 ++++++ examples/audio_demo/src/main.rs | 38 +++++++++++++++++++++++++++++++++ 3 files changed, 46 insertions(+) create mode 100644 examples/audio_demo/Cargo.toml create mode 100644 examples/audio_demo/src/main.rs diff --git a/Cargo.toml b/Cargo.toml index b59c19d..434ed69 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,6 +17,7 @@ members = [ "examples/ibl_demo", "crates/voltex_physics", "crates/voltex_audio", + "examples/audio_demo", ] [workspace.dependencies] diff --git a/examples/audio_demo/Cargo.toml b/examples/audio_demo/Cargo.toml new file mode 100644 index 0000000..14fa21f --- /dev/null +++ b/examples/audio_demo/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "audio_demo" +version = "0.1.0" +edition = "2021" + +[dependencies] +voltex_audio.workspace = true diff --git a/examples/audio_demo/src/main.rs b/examples/audio_demo/src/main.rs new file mode 100644 index 0000000..764e2a8 --- /dev/null +++ b/examples/audio_demo/src/main.rs @@ -0,0 +1,38 @@ +use voltex_audio::{AudioClip, AudioSystem}; + +fn generate_sine_clip(freq: f32, duration: f32, sample_rate: u32) -> AudioClip { + let num_samples = (sample_rate as f32 * duration) as usize; + let mut samples = Vec::with_capacity(num_samples); + for i in 0..num_samples { + let t = i as f32 / sample_rate as f32; + samples.push((t * freq * 2.0 * std::f32::consts::PI).sin() * 0.3); + } + AudioClip::new(samples, sample_rate, 1) +} + +fn main() { + println!("=== Voltex Audio Demo ==="); + println!("Generating 440Hz sine wave (2 seconds)..."); + + let clip = generate_sine_clip(440.0, 2.0, 44100); + let clip2 = generate_sine_clip(660.0, 1.5, 44100); + + println!("Initializing audio system..."); + let audio = match AudioSystem::new(vec![clip, clip2]) { + Ok(a) => a, + Err(e) => { + eprintln!("Failed to init audio: {}", e); + return; + } + }; + + println!("Playing 440Hz tone..."); + audio.play(0, 0.5, false); + std::thread::sleep(std::time::Duration::from_secs(1)); + + println!("Playing 660Hz tone on top..."); + audio.play(1, 0.3, false); + std::thread::sleep(std::time::Duration::from_secs(2)); + + println!("Done!"); +}