From 39cd5d0084c8c524880c80db92eef01da4c285e0 Mon Sep 17 00:00:00 2001 From: mitchmindtree Date: Sat, 22 Jun 2019 00:10:40 +0200 Subject: [PATCH] Update examples for addition of new StreamEvent API --- examples/beep.rs | 11 ++++++++++- examples/feedback.rs | 11 ++++++++++- examples/record_wav.rs | 11 ++++++++++- 3 files changed, 30 insertions(+), 3 deletions(-) diff --git a/examples/beep.rs b/examples/beep.rs index fe5dfb6..c6b70e0 100644 --- a/examples/beep.rs +++ b/examples/beep.rs @@ -17,7 +17,16 @@ fn main() -> Result<(), failure::Error> { (sample_clock * 440.0 * 2.0 * 3.141592 / sample_rate).sin() }; - event_loop.run(move |_, data| { + event_loop.run(move |id, event| { + let data = match event { + cpal::StreamEvent::Data(data) => data, + cpal::StreamEvent::Close(cpal::StreamCloseCause::Error(err)) => { + eprintln!("stream {:?} closed due to an error: {}", id, err); + return; + } + _ => return, + }; + match data { cpal::StreamData::Output { buffer: cpal::UnknownTypeOutputBuffer::U16(mut buffer) } => { for sample in buffer.chunks_mut(format.channels as usize) { diff --git a/examples/feedback.rs b/examples/feedback.rs index 18dd62a..4c067dd 100644 --- a/examples/feedback.rs +++ b/examples/feedback.rs @@ -49,7 +49,16 @@ fn main() -> Result<(), failure::Error> { // Run the event loop on a separate thread. std::thread::spawn(move || { - event_loop.run(move |id, data| { + event_loop.run(move |id, event| { + let data = match event { + cpal::StreamEvent::Data(data) => data, + cpal::StreamEvent::Close(cpal::StreamCloseCause::Error(err)) => { + eprintln!("stream {:?} closed due to an error: {}", id, err); + return; + } + _ => return, + }; + match data { cpal::StreamData::Input { buffer: cpal::UnknownTypeInputBuffer::F32(buffer) } => { assert_eq!(id, input_stream_id); diff --git a/examples/record_wav.rs b/examples/record_wav.rs index 61489f0..74c10aa 100644 --- a/examples/record_wav.rs +++ b/examples/record_wav.rs @@ -30,7 +30,16 @@ fn main() -> Result<(), failure::Error> { let writer_2 = writer.clone(); let recording_2 = recording.clone(); std::thread::spawn(move || { - event_loop.run(move |_, data| { + event_loop.run(move |id, event| { + let data = match event { + cpal::StreamEvent::Data(data) => data, + cpal::StreamEvent::Close(cpal::StreamCloseCause::Error(err)) => { + eprintln!("stream {:?} closed due to an error: {}", id, err); + return; + } + _ => return, + }; + // If we're done recording, return early. if !recording_2.load(std::sync::atomic::Ordering::Relaxed) { return;