diff --git a/examples/beep.rs b/examples/beep.rs index 16f8865..6240805 100644 --- a/examples/beep.rs +++ b/examples/beep.rs @@ -1,38 +1,18 @@ extern crate cpal; -// TODO: manual replacement for unstable `std::iter::iterate` -struct Iter { - value: f32, -} - -impl Iterator for Iter { - type Item = f32; - - fn next(&mut self) -> Option { - self.value += 0.03; - Some(self.value) - } -} - fn main() { let mut channel = cpal::Voice::new(); - // producing a sinusoid - let mut data_source = Iter { value: 0.0 } - .map(|angle| { - let angle = angle.sin(); - - let max: u16 = std::u16::MAX; - let value = (max as f32 / 2.0) + (angle * (max as f32 / 2.0)); - value as u16 - }); + // Produce a sinusoid of maximum amplitude. + let max = std::u16::MAX as f32; + let mut data_source = (0u64..).map(|t| t as f32 * 0.03) + .map(|t| ((t.sin() * 0.5 + 0.5) * max) as u16); loop { { let mut buffer = channel.append_data(1, cpal::SamplesRate(44100), 32768); - for sample in buffer.iter_mut() { - let value = data_source.next().unwrap(); + for (sample, value) in buffer.iter_mut().zip(&mut data_source) { *sample = value; } }