generated from tversteeg/library-github-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsdl2.rs
44 lines (32 loc) · 1011 Bytes
/
sdl2.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
use sdl2::audio::{AudioCallback, AudioSpecDesired};
use std::time::Duration;
const SAMPLE_RATE: usize = 44_100;
struct Sample {
mixer: usfx::Mixer,
}
impl AudioCallback for Sample {
type Channel = f32;
fn callback(&mut self, out: &mut [f32]) {
self.mixer.generate(out)
}
}
fn main() {
let sdl_context = sdl2::init().unwrap();
let audio_subsystem = sdl_context.audio().unwrap();
let desired_spec = AudioSpecDesired {
freq: Some(SAMPLE_RATE as i32),
channels: Some(1),
samples: None,
};
// Create a default sample, which is a sine-wave of 441 hz
let sample = usfx::Sample::default();
// Create a mixer that will play our sample
let mut mixer = usfx::Mixer::new(SAMPLE_RATE);
// Play the sample
mixer.play(sample);
let device = audio_subsystem
.open_playback(None, &desired_spec, |_spec| Sample { mixer })
.unwrap();
device.resume();
std::thread::sleep(Duration::from_millis(5_000));
}