From 624d1f8ce6403eaac29ca52b5f4750ea077ec031 Mon Sep 17 00:00:00 2001 From: Mitchell Hentges Date: Thu, 26 Sep 2019 17:16:09 -0700 Subject: [PATCH 1/2] Uses ringbuffer in feedback example --- Cargo.toml | 1 + examples/feedback.rs | 17 ++++++++++------- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 00f9614..d83e3e7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,6 +18,7 @@ num-traits = "0.2.6" [dev-dependencies] hound = "3.4" +ringbuf = "0.1.6" [target.'cfg(target_os = "windows")'.dependencies] winapi = { version = "0.3", features = ["audiosessiontypes", "audioclient", "coml2api", "combaseapi", "debug", "devpkey", "handleapi", "ksmedia", "mmdeviceapi", "objbase", "std", "synchapi", "winuser"] } diff --git a/examples/feedback.rs b/examples/feedback.rs index 42d36bc..a71e7ea 100644 --- a/examples/feedback.rs +++ b/examples/feedback.rs @@ -8,8 +8,10 @@ extern crate cpal; extern crate failure; +extern crate ringbuf; use cpal::traits::{DeviceTrait, EventLoopTrait, HostTrait}; +use ringbuf::RingBuffer; const LATENCY_MS: f32 = 150.0; @@ -37,12 +39,13 @@ fn main() -> Result<(), failure::Error> { let latency_frames = (LATENCY_MS / 1_000.0) * format.sample_rate.0 as f32; let latency_samples = latency_frames as usize * format.channels as usize; - // The channel to share samples. - let (tx, rx) = std::sync::mpsc::sync_channel(latency_samples * 2); + // The buffer to share samples + let ring = RingBuffer::new(latency_samples * 2); + let (mut producer, mut consumer) = ring.split(); // Fill the samples with 0.0 equal to the length of the delay. for _ in 0..latency_samples { - tx.send(0.0)?; + producer.push(0.0).unwrap(); } // Play the streams. @@ -66,7 +69,7 @@ fn main() -> Result<(), failure::Error> { assert_eq!(id, input_stream_id); let mut output_fell_behind = false; for &sample in buffer.iter() { - if tx.try_send(sample).is_err() { + if producer.push(sample).is_err() { output_fell_behind = true; } } @@ -78,7 +81,7 @@ fn main() -> Result<(), failure::Error> { assert_eq!(id, output_stream_id); let mut input_fell_behind = None; for sample in buffer.iter_mut() { - *sample = match rx.try_recv() { + *sample = match consumer.pop() { Ok(s) => s, Err(err) => { input_fell_behind = Some(err); @@ -86,8 +89,8 @@ fn main() -> Result<(), failure::Error> { }, }; } - if let Some(err) = input_fell_behind { - eprintln!("input stream fell behind: {}: try increasing latency", err); + if let Some(_) = input_fell_behind { + eprintln!("input stream fell behind: try increasing latency"); } }, _ => panic!("we're expecting f32 data"), From 1676bf20b61fccb994ad487416e16cded48ffe1c Mon Sep 17 00:00:00 2001 From: Mitchell Hentges Date: Fri, 27 Sep 2019 10:18:23 -0700 Subject: [PATCH 2/2] Adds comment about latency-push unwrap() --- examples/feedback.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/examples/feedback.rs b/examples/feedback.rs index a71e7ea..f77f26e 100644 --- a/examples/feedback.rs +++ b/examples/feedback.rs @@ -45,6 +45,8 @@ fn main() -> Result<(), failure::Error> { // Fill the samples with 0.0 equal to the length of the delay. for _ in 0..latency_samples { + // The ring buffer has twice as much space as necessary to add latency here, + // so this should never fail producer.push(0.0).unwrap(); }