cpal/examples/music.rs

48 lines
1.2 KiB
Rust
Raw Normal View History

2014-12-15 12:01:37 +00:00
extern crate cpal;
extern crate vorbis;
2015-03-25 10:21:10 +00:00
use std::io::Cursor;
2014-12-15 12:01:37 +00:00
fn main() {
2014-12-17 08:16:26 +00:00
let mut channel = cpal::Voice::new();
2014-12-22 13:16:47 +00:00
channel.play();
2014-12-15 12:01:37 +00:00
2015-03-25 10:21:10 +00:00
let mut decoder = vorbis::Decoder::new(Cursor::new(&include_bytes!("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
2015-03-26 09:03:13 +00:00
let mut data = &data[..];
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;
}
2014-12-22 13:16:47 +00:00
{
let mut buffer = channel.append_data(channels, cpal::SamplesRate(rate as u32),
data.len());
let mut buffer = buffer.iter_mut();
loop {
let next_sample = match data.get(0) {
Some(s) => *s,
None => continue 'main
};
if let Some(output) = buffer.next() {
2015-07-22 12:28:45 +00:00
*output = next_sample;
data = &data[1..];
2014-12-22 13:16:47 +00:00
} else {
break;
}
}
2014-12-15 14:29:59 +00:00
}
2014-12-22 13:16:47 +00:00
channel.play();
2014-12-15 12:01:37 +00:00
}
}
}