forked from ExPixel/miniaudio-rs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
simple-playback-decoder.rs
50 lines (40 loc) · 1.76 KB
/
simple-playback-decoder.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
50
use miniaudio::{Decoder, Device, DeviceConfig, DeviceType};
pub fn main() {
let mut decoder = Decoder::from_file("miniaudio/examples/assets/exit.wav", None)
.expect("failed to initialize decoder from file");
let mut config = DeviceConfig::new(DeviceType::Playback);
config.playback_mut().set_format(decoder.output_format());
config
.playback_mut()
.set_channels(decoder.output_channels());
config.set_sample_rate(decoder.output_sample_rate());
// config.set_data_callback(move |_device, _output, _frames| {
// println!("ignored");
// });
// This stop callback can go on the config because it is cloneable.
config.set_stop_callback(|_device| {
println!("Device Stopped.");
});
let mut device = Device::new(None, &config).expect("failed to open playback device");
// Unlike `SyncDecoder`, Decoder is not cloneable so we have to use a version of the data
// callback that doesn't require everything that we pass into it to be cloneable. So here we
// use a device specific data callback.
device.set_data_callback(move |_device, output, _frames| {
decoder.read_pcm_frames(output);
});
device.start().expect("failed to start device");
println!("Device Backend: {:?}", device.context().backend());
wait_for_enter();
println!("Shutting Down...");
}
/// Shows a prompt and waits for input on stdin.
fn wait_for_enter() {
use std::io::Write;
print!("Press ENTER/RETURN to exit...");
// Make sure the line above is displayed:
std::io::stdout().flush().expect("failed to flush stdout");
// Just read some random line off of stdin and discard it:
std::io::stdin()
.read_line(&mut String::new())
.expect("failed to wait for line");
}