forked from Rust-SDL2/rust-sdl2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
audio-queue-squarewave.rs
49 lines (38 loc) · 1.11 KB
/
audio-queue-squarewave.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
45
46
47
48
49
extern crate sdl2;
use sdl2::audio::AudioSpecDesired;
use std::time::Duration;
fn gen_wave(bytes_to_write: i32) -> Vec<i16> {
// Generate a square wave
let tone_volume = 1_000i16;
let period = 48_000 / 256;
let sample_count = bytes_to_write;
let mut result = Vec::new();
for x in 0..sample_count {
result.push(if (x / period) % 2 == 0 {
tone_volume
} else {
-tone_volume
});
}
result
}
fn main() -> Result<(), String> {
let sdl_context = sdl2::init()?;
let audio_subsystem = sdl_context.audio()?;
let desired_spec = AudioSpecDesired {
freq: Some(48_000),
channels: Some(2),
// mono -
samples: None, // default sample size
};
let device = audio_subsystem.open_queue::<i16, _>(None, &desired_spec)?;
let target_bytes = 48_000 * 4;
let wave = gen_wave(target_bytes);
device.queue_audio(&wave)?;
// Start playback
device.resume();
// Play for 2 seconds
std::thread::sleep(Duration::from_millis(2_000));
// Device is automatically closed when dropped
Ok(())
}