cpal/examples/beep.rs

43 lines
934 B
Rust
Raw Normal View History

2014-12-11 15:28:26 +00:00
extern crate cpal;
2015-04-04 07:06:46 +00:00
// 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)
}
}
2014-12-11 15:28:26 +00:00
fn main() {
2014-12-17 08:16:26 +00:00
let mut channel = cpal::Voice::new();
2014-12-11 16:23:33 +00:00
2014-12-11 17:09:48 +00:00
// producing a sinusoid
2015-04-04 07:06:46 +00:00
let mut data_source = Iter { value: 0.0 }
.map(|angle| {
let angle = angle.sin();
2015-04-04 07:06:46 +00:00
let max: u16 = std::u16::MAX;
let value = (max as f32 / 2.0) + (angle * (max as f32 / 2.0));
value as u16
2014-12-11 18:02:04 +00:00
});
2014-12-11 16:23:33 +00:00
loop {
2014-12-22 13:16:47 +00:00
{
let mut buffer = channel.append_data(1, cpal::SamplesRate(44100), 32768);
2014-12-22 13:16:47 +00:00
for sample in buffer.iter_mut() {
let value = data_source.next().unwrap();
*sample = value;
}
}
2014-12-22 13:16:47 +00:00
channel.play();
2014-12-11 16:23:33 +00:00
}
2014-12-11 15:28:26 +00:00
}