Newer
Older
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
use sdl2::{
audio::{AudioCallback, AudioSpecDesired},
AudioSubsystem, Sdl,
};
use std::time::Duration;
struct SquareWave {
phase_inc: f32,
phase: f32,
volume: f32,
}
impl AudioCallback for SquareWave {
type Channel = f32;
fn callback(&mut self, out: &mut [f32]) {
for x in out.iter_mut() {
// this is a square wave with 50% of down
// and 50% of up values
*x = if self.phase <= 0.5 {
self.volume
} else {
-self.volume
};
self.phase = (self.phase + self.phase_inc) % 1.0;
}
}
}
pub struct Audio {
pub audio_subsystem: AudioSubsystem,
}
impl Audio {
pub fn new(sdl: &Sdl) -> Self {
let audio_subsystem = sdl.audio().unwrap();
let desired_spec = AudioSpecDesired {
freq: Some(44100),
channels: Some(1),
samples: None,
};
let device = audio_subsystem
.open_playback(None, &desired_spec, |spec| SquareWave {
phase_inc: 440.0 / spec.freq as f32,
phase: 0.0,
volume: 0.25,
})
.unwrap();
// starts the playback by resuming the audio
// device's activity
device.resume();
std::thread::sleep(Duration::from_millis(2000));
Self { audio_subsystem }
}
}