RFC: Rework the API [WIP] (#165)
* Rework the API to not use futures anymore * Add some comments * Update the MacOS backend * Restore the null implementation * Add an emscripten backend * Remove erroneously added feature * Fix to_f32 formula * [WIP] Alsa backend * Alsa backend compiling * Working ALSA backend * Fix tests * Move WASAPI endpoint to endpoint module * Fix WASAPI warnings * Rework the WASAPI backend * Check overflows for voice ID * Add comments and minor fixes to WASAPI backend * Add a changelog
This commit is contained in:
parent
4b019243c2
commit
6ae01f437c
13 changed files with 1472 additions and 1447 deletions
|
|
@ -1,23 +1,9 @@
|
|||
extern crate cpal;
|
||||
extern crate futures;
|
||||
|
||||
use futures::stream::Stream;
|
||||
use futures::task;
|
||||
use futures::task::Executor;
|
||||
use futures::task::Run;
|
||||
|
||||
use std::sync::Arc;
|
||||
use std::thread;
|
||||
use std::time::Duration;
|
||||
|
||||
struct MyExecutor;
|
||||
|
||||
impl Executor for MyExecutor {
|
||||
fn execute(&self, r: Run) {
|
||||
r.run();
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let endpoint = cpal::default_endpoint().expect("Failed to get default endpoint");
|
||||
let format = endpoint
|
||||
|
|
@ -27,18 +13,15 @@ fn main() {
|
|||
.expect("Failed to get endpoint format");
|
||||
|
||||
let event_loop = cpal::EventLoop::new();
|
||||
let executor = Arc::new(MyExecutor);
|
||||
|
||||
let (mut voice, stream) = cpal::Voice::new(&endpoint, &format, &event_loop)
|
||||
.expect("Failed to create a voice");
|
||||
let voice_id = event_loop.build_voice(&endpoint, &format).unwrap();
|
||||
event_loop.play(voice_id);
|
||||
|
||||
// Produce a sinusoid of maximum amplitude.
|
||||
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());
|
||||
|
||||
voice.play();
|
||||
task::spawn(stream.for_each(move |buffer| -> Result<_, ()> {
|
||||
event_loop.run(move |_, buffer| {
|
||||
match buffer {
|
||||
cpal::UnknownTypeBuffer::U16(mut buffer) => {
|
||||
for (sample, value) in buffer
|
||||
|
|
@ -75,16 +58,5 @@ fn main() {
|
|||
}
|
||||
},
|
||||
};
|
||||
|
||||
Ok(())
|
||||
})).execute(executor);
|
||||
|
||||
thread::spawn(move || loop {
|
||||
thread::sleep(Duration::from_millis(500));
|
||||
voice.pause();
|
||||
thread::sleep(Duration::from_millis(500));
|
||||
voice.play();
|
||||
});
|
||||
|
||||
event_loop.run();
|
||||
});
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue