Merge pull request #40 from ruud-v-a/beep

Simplify beep example
This commit is contained in:
tomaka 2015-04-27 18:11:36 +02:00
commit f71af2bed4
1 changed files with 5 additions and 25 deletions

View File

@ -1,38 +1,18 @@
extern crate cpal; 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<f32> {
self.value += 0.03;
Some(self.value)
}
}
fn main() { fn main() {
let mut channel = cpal::Voice::new(); let mut channel = cpal::Voice::new();
// producing a sinusoid // Produce a sinusoid of maximum amplitude.
let mut data_source = Iter { value: 0.0 } let max = std::u16::MAX as f32;
.map(|angle| { let mut data_source = (0u64..).map(|t| t as f32 * 0.03)
let angle = angle.sin(); .map(|t| ((t.sin() * 0.5 + 0.5) * max) as u16);
let max: u16 = std::u16::MAX;
let value = (max as f32 / 2.0) + (angle * (max as f32 / 2.0));
value as u16
});
loop { loop {
{ {
let mut buffer = channel.append_data(1, cpal::SamplesRate(44100), 32768); let mut buffer = channel.append_data(1, cpal::SamplesRate(44100), 32768);
for sample in buffer.iter_mut() { for (sample, value) in buffer.iter_mut().zip(&mut data_source) {
let value = data_source.next().unwrap();
*sample = value; *sample = value;
} }
} }