cpal/examples/music.rs

42 lines
1.1 KiB
Rust
Raw Normal View History

2014-12-15 12:01:37 +00:00
extern crate cpal;
extern crate vorbis;
use std::io::BufReader;
fn main() {
let mut channel = cpal::Channel::new();
2014-12-17 05:59:03 +00:00
let mut decoder = vorbis::Decoder::new(BufReader::new(include_bin!("music.ogg")))
2014-12-15 12:01:37 +00:00
.unwrap();
2014-12-15 14:29:59 +00:00
'main: for packet in decoder.packets() {
2014-12-15 12:01:37 +00:00
let packet = packet.unwrap();
2014-12-15 14:29:59 +00:00
let vorbis::Packet { channels, rate, data, .. } = packet;
2014-12-15 12:01:37 +00:00
2014-12-15 15:26:55 +00:00
let mut data = data.as_slice();
2014-12-15 12:01:37 +00:00
2014-12-15 14:29:59 +00:00
loop {
2014-12-15 15:26:55 +00:00
if data.len() == 0 {
continue 'main;
}
let mut buffer = channel.append_data(channels, cpal::SamplesRate(rate as u32), data.len());
2014-12-15 15:32:13 +00:00
let mut buffer = buffer.iter_mut();
2014-12-15 14:29:59 +00:00
loop {
2014-12-15 15:26:55 +00:00
let next_sample = match data.get(0) {
Some(s) => *s,
None => continue 'main
};
if let Some(output) = buffer.next() {
2014-12-15 15:26:55 +00:00
*output = next_sample as u16;
data = data.slice_from(1);
} else {
break;
}
2014-12-15 14:29:59 +00:00
}
2014-12-15 12:01:37 +00:00
}
}
}