feat(audio): add audio_demo example with sine wave playback
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -17,6 +17,7 @@ members = [
|
|||||||
"examples/ibl_demo",
|
"examples/ibl_demo",
|
||||||
"crates/voltex_physics",
|
"crates/voltex_physics",
|
||||||
"crates/voltex_audio",
|
"crates/voltex_audio",
|
||||||
|
"examples/audio_demo",
|
||||||
]
|
]
|
||||||
|
|
||||||
[workspace.dependencies]
|
[workspace.dependencies]
|
||||||
|
|||||||
7
examples/audio_demo/Cargo.toml
Normal file
7
examples/audio_demo/Cargo.toml
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
[package]
|
||||||
|
name = "audio_demo"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
voltex_audio.workspace = true
|
||||||
38
examples/audio_demo/src/main.rs
Normal file
38
examples/audio_demo/src/main.rs
Normal file
@@ -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!");
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user