Draft for switching to futures
This commit is contained in:
parent
47fec7e846
commit
be8310da51
5 changed files with 322 additions and 184 deletions
|
|
@ -1,16 +1,25 @@
|
|||
extern crate cpal;
|
||||
extern crate futures;
|
||||
|
||||
use futures::Future;
|
||||
use futures::stream::Stream;
|
||||
|
||||
fn main() {
|
||||
let endpoint = cpal::get_default_endpoint().expect("Failed to get default endpoint");
|
||||
let format = endpoint.get_supported_formats_list().unwrap().next().expect("Failed to get endpoint format");
|
||||
let mut channel = cpal::Voice::new(&endpoint, &format).expect("Failed to create a channel");
|
||||
|
||||
let event_loop = cpal::EventLoop::new();
|
||||
|
||||
let (mut voice, stream) = cpal::Voice::new(&endpoint, &format, &event_loop).expect("Failed to create a voice");
|
||||
|
||||
// Produce a sinusoid of maximum amplitude.
|
||||
let mut data_source = (0u64..).map(|t| t as f32 * 440.0 * 2.0 * 3.141592 / format.samples_rate.0 as f32) // 440 Hz
|
||||
.map(|t| t.sin());
|
||||
let samples_rate = format.samples_rate.0 as f32;
|
||||
let mut data_source = (0u64..).map(move |t| t as f32 * 440.0 * 2.0 * 3.141592 / samples_rate) // 440 Hz
|
||||
.map(move |t| t.sin());
|
||||
|
||||
loop {
|
||||
match channel.append_data(32768) {
|
||||
voice.play();
|
||||
stream.for_each(move |buffer| -> Result<_, ()> {
|
||||
match buffer {
|
||||
cpal::UnknownTypeBuffer::U16(mut buffer) => {
|
||||
for (sample, value) in buffer.chunks_mut(format.channels.len()).zip(&mut data_source) {
|
||||
let value = ((value * 0.5 + 0.5) * std::u16::MAX as f32) as u16;
|
||||
|
|
@ -30,8 +39,10 @@ fn main() {
|
|||
for out in sample.iter_mut() { *out = value; }
|
||||
}
|
||||
},
|
||||
}
|
||||
};
|
||||
|
||||
channel.play();
|
||||
}
|
||||
Ok(())
|
||||
}).forget();
|
||||
|
||||
event_loop.run();
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue