Created
April 21, 2018 03:19
-
-
Save ablwr/c02ccd4f6c48bd90614647ec9dbd3380 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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