-
Notifications
You must be signed in to change notification settings - Fork 472
Expand file tree
/
Copy pathmixer-demo.rs
More file actions
143 lines (111 loc) · 4.51 KB
/
Copy pathmixer-demo.rs
File metadata and controls
143 lines (111 loc) · 4.51 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/// Demonstrates the simultaneous mixing of music and sound effects.
extern crate sdl2;
use sdl2::mixer::{InitFlag, AUDIO_S16LSB, DEFAULT_CHANNELS};
use std::env;
use std::path::Path;
fn main() -> Result<(), String> {
let args: Vec<_> = env::args().collect();
if args.len() < 2 {
println!("Usage: ./demo music.[mp3|wav|ogg] [sound-effect.[mp3|wav|ogg]]")
} else {
let sound_file = args.get(2).map(Path::new);
demo(Path::new(&args[1]), sound_file)?;
}
Ok(())
}
fn demo(music_file: &Path, sound_file: Option<&Path>) -> Result<(), String> {
println!("linked version: {}", sdl2::mixer::get_linked_version());
let sdl = sdl2::init()?;
let _audio = sdl.audio()?;
let timer = sdl.timer()?;
let frequency = 44_100;
let format = AUDIO_S16LSB; // signed 16 bit samples, in little-endian byte order
let channels = DEFAULT_CHANNELS; // Stereo
let chunk_size = 1_024;
sdl2::mixer::open_audio(frequency, format, channels, chunk_size)?;
let _mixer_context =
sdl2::mixer::init(InitFlag::MP3 | InitFlag::FLAC | InitFlag::MOD | InitFlag::OGG)?;
// Number of mixing channels available for sound effect `Chunk`s to play
// simultaneously.
sdl2::mixer::allocate_channels(4);
{
let n = sdl2::mixer::get_chunk_decoders_number();
println!("available chunk(sample) decoders: {}", n);
for i in 0..n {
println!(" decoder {} => {}", i, sdl2::mixer::get_chunk_decoder(i));
}
}
{
let n = sdl2::mixer::get_music_decoders_number();
println!("available music decoders: {}", n);
for i in 0..n {
println!(" decoder {} => {}", i, sdl2::mixer::get_music_decoder(i));
}
}
println!("query spec => {:?}", sdl2::mixer::query_spec());
let music = sdl2::mixer::Music::from_file(music_file)?;
fn hook_finished() {
println!("play ends! from rust cb");
}
sdl2::mixer::Music::hook_finished(hook_finished);
println!("music => {:?}", music);
println!("music type => {:?}", music.get_type());
println!("music volume => {:?}", sdl2::mixer::Music::get_volume());
println!("play => {:?}", music.play(1));
{
let sound_chunk = match sound_file {
Some(sound_file_path) => sdl2::mixer::Chunk::from_file(sound_file_path)
.map_err(|e| format!("Cannot load sound file: {:?}", e))?,
None => {
// One second of 500Hz sine wave using equation A * sin(2 * PI * f * t)
// (played at half the volume to save people's ears).
let buffer = (0..frequency)
.map(|i| {
(0.1 * i16::MAX as f32
* (2.0 * std::f32::consts::PI * 500.0 * (i as f32 / frequency as f32))
.sin()) as i16
})
.collect();
sdl2::mixer::Chunk::from_raw_buffer(buffer)
.map_err(|e| format!("Cannot get chunk from buffer: {:?}", e))?
}
};
println!("chunk volume => {:?}", sound_chunk.get_volume());
println!("playing sound twice");
sdl2::mixer::Channel::all().play(&sound_chunk, 1)?;
// This delay is needed because when the `Chunk` goes out of scope,
// the sound effect stops playing. Delay long enough to hear the
// sound.
timer.delay(5_000);
println!("played sound");
}
timer.delay(10_000);
println!("fading out ... {:?}", sdl2::mixer::Music::fade_out(4_000));
timer.delay(5_000);
println!(
"fading in from pos ... {:?}",
music.fade_in_from_pos(1, 10_000, 100.0)
);
timer.delay(5_000);
sdl2::mixer::Music::halt();
println!("playing sound from memory...");
let sound_as_bytes = include_bytes!("../assets/sine.wav");
let chunk = sdl2::mixer::Chunk::from_bytes(sound_as_bytes)?;
sdl2::mixer::Channel::all().play(&chunk, 0)?;
timer.delay(1_000);
println!("playing music from static memory...");
let music_as_bytes = include_bytes!("../assets/sine.wav");
let mus = sdl2::mixer::Music::from_static_bytes(music_as_bytes)?;
mus.play(-1);
timer.delay(1_000);
sdl2::mixer::Music::halt();
timer.delay(0_500);
println!("playing music from owned memory...");
let mus = sdl2::mixer::Music::from_owned_bytes(music_as_bytes.clone().into())?;
mus.play(-1);
timer.delay(1_000);
sdl2::mixer::Music::halt();
timer.delay(1_000);
println!("quitting sdl");
Ok(())
}