Instantly share code, notes, and snippets.

Embed
What would you like to do?
extern crate cpal;
use cpal::{EventLoop, StreamData, UnknownTypeOutputBuffer};
fn main() {
let event_loop = EventLoop::new();
let device = cpal::default_output_device().expect("no output device available");
let format = device.default_output_format().unwrap();
let stream_id = event_loop.build_output_stream(&device, &format).unwrap();
event_loop.play_stream(stream_id);
let mut samples_written: u64 = 0;
event_loop.run(move |_stream_id, stream_data| {
let mut buffer = if let StreamData::Output{buffer} = stream_data {
if let UnknownTypeOutputBuffer::F32(buffer) = buffer {
buffer
} else {
panic!("got non f32 buffer");
}
} else {
panic!("got StreamData::Input, which we didn't request.");
};
for elem in buffer.iter_mut() {
let time = (samples_written / format.channels as u64) as f32 / format.sample_rate.0 as f32;
let t = time / (1.0/220.0) % 1.0;
if t < 0.5 {
*elem = 0.4;
} else {
*elem = -0.4;
}
samples_written+=1;
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment