Update examples for addition of new StreamEvent API

This commit is contained in:
mitchmindtree 2019-06-22 00:10:40 +02:00
parent e41baa248b
commit 39cd5d0084
3 changed files with 30 additions and 3 deletions

View File

@ -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) {

View File

@ -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);

View File

@ -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;