Skip to content

Commit 1b10b95

Browse files
committed
[add] sound playback demo.
1 parent fa36b76 commit 1b10b95

1 file changed

Lines changed: 52 additions & 0 deletions

File tree

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#![allow(clippy::needless_return)]
2+
//! Audio demo exercising `AudioContext` transport controls.
3+
//!
4+
//! This demo validates that `AudioContext` can play a decoded `SoundBuffer`
5+
//! through the output device and that `SoundInstance` transport operations
6+
//! (play/pause/stop/looping) behave as expected.
7+
8+
use std::time::Duration;
9+
10+
use lambda::audio::{
11+
AudioContextBuilder,
12+
SoundBuffer,
13+
};
14+
15+
fn main() {
16+
const SLASH_VORBIS_STEREO_48000_OGG: &[u8] = include_bytes!(concat!(
17+
env!("CARGO_MANIFEST_DIR"),
18+
"/../../crates/lambda-rs-platform/assets/audio/slash_vorbis_stereo_48000.ogg"
19+
));
20+
21+
let buffer =
22+
SoundBuffer::from_ogg_bytes(SLASH_VORBIS_STEREO_48000_OGG).unwrap();
23+
24+
let mut context = AudioContextBuilder::new()
25+
.with_label("sound-playback-transport")
26+
.with_sample_rate(buffer.sample_rate())
27+
.with_channels(buffer.channels())
28+
.build()
29+
.unwrap();
30+
31+
let mut instance = context.play_sound(&buffer).unwrap();
32+
std::thread::sleep(Duration::from_millis(250));
33+
34+
instance.pause();
35+
std::thread::sleep(Duration::from_millis(250));
36+
37+
instance.play();
38+
std::thread::sleep(Duration::from_millis(250));
39+
40+
instance.stop();
41+
std::thread::sleep(Duration::from_millis(250));
42+
43+
instance.play();
44+
std::thread::sleep(Duration::from_millis(300));
45+
46+
instance.set_looping(true);
47+
std::thread::sleep(Duration::from_secs(2));
48+
49+
instance.set_looping(false);
50+
std::thread::sleep(Duration::from_millis(300));
51+
return;
52+
}

0 commit comments

Comments
 (0)