Skip to content

Instantly share code, notes, and snippets.

@ablwr
Created April 21, 2018 03:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ablwr/c02ccd4f6c48bd90614647ec9dbd3380 to your computer and use it in GitHub Desktop.
Save ablwr/c02ccd4f6c48bd90614647ec9dbd3380 to your computer and use it in GitHub Desktop.
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