cpal/examples/music.rs

44 lines
1.2 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();
let mut decoder = vorbis::Decoder::new(BufReader::new(include_bin!("mozart_symfony_40.ogg")))
.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 14:29:59 +00:00
let mut data = data.iter();
let mut next_sample = None;
2014-12-15 12:01:37 +00:00
2014-12-15 14:29:59 +00:00
loop {
let mut buffer = channel.append_data(channels, cpal::SamplesRate(rate as u32));
let mut buffer = buffer.samples();
loop {
if next_sample.is_none() {
match data.next() {
Some(sample) => {
next_sample = Some(*sample as u16)
},
None => {
continue 'main;
}
2014-12-15 14:29:59 +00:00
}
}
if let Some(output) = buffer.next() {
*output = next_sample.take().unwrap();
} else {
break;
}
2014-12-15 14:29:59 +00:00
}
2014-12-15 12:01:37 +00:00
}
}
}