Simplify beep example
This commit is contained in:
parent
e2b259c958
commit
be84701f82
|
@ -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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue