From 9d56c4c6162350c7e43c8d5a71c5c29a61ac7c93 Mon Sep 17 00:00:00 2001 From: Pierre Krieger Date: Thu, 10 Sep 2015 12:00:52 +0200 Subject: [PATCH] Update the beep example --- examples/beep.rs | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/examples/beep.rs b/examples/beep.rs index 09e3312..83b52b2 100644 --- a/examples/beep.rs +++ b/examples/beep.rs @@ -6,31 +6,28 @@ fn main() { let mut channel = cpal::Voice::new(&endpoint, &format).unwrap(); // Produce a sinusoid of maximum amplitude. - let mut data_source = (0u64..).map(|t| t as f32 * 0.03) + 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()); loop { match channel.append_data(32768) { cpal::UnknownTypeBuffer::U16(mut buffer) => { - for (sample, value) in buffer.chunks_mut(2).zip(&mut data_source) { + 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; - sample[0] = value; - sample[1] = value; + for out in sample.iter_mut() { *out = value; } } }, cpal::UnknownTypeBuffer::I16(mut buffer) => { - for (sample, value) in buffer.chunks_mut(2).zip(&mut data_source) { + for (sample, value) in buffer.chunks_mut(format.channels.len()).zip(&mut data_source) { let value = (value * std::i16::MAX as f32) as i16; - sample[0] = value; - sample[1] = value; + for out in sample.iter_mut() { *out = value; } } }, cpal::UnknownTypeBuffer::F32(mut buffer) => { - for (sample, value) in buffer.chunks_mut(2).zip(&mut data_source) { - sample[0] = value; - sample[1] = value; + for (sample, value) in buffer.chunks_mut(format.channels.len()).zip(&mut data_source) { + for out in sample.iter_mut() { *out = value; } } }, }