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;
// 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() {
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;
}
}