2014-12-11 15:28:26 +00:00
|
|
|
extern crate cpal;
|
|
|
|
|
|
|
|
fn main() {
|
2016-01-15 17:05:29 +00:00
|
|
|
let endpoint = cpal::get_default_endpoint().expect("Failed to get default endpoint");
|
|
|
|
let format = endpoint.get_supported_formats_list().unwrap().next().expect("Failed to get endpoint format");
|
|
|
|
let mut channel = cpal::Voice::new(&endpoint, &format).expect("Failed to create a channel");
|
2014-12-11 16:23:33 +00:00
|
|
|
|
2015-04-27 15:33:02 +00:00
|
|
|
// Produce a sinusoid of maximum amplitude.
|
2015-09-10 10:00:52 +00:00
|
|
|
let mut data_source = (0u64..).map(|t| t as f32 * 440.0 * 2.0 * 3.141592 / format.samples_rate.0 as f32) // 440 Hz
|
2015-08-20 12:38:25 +00:00
|
|
|
.map(|t| t.sin());
|
2014-12-11 16:51:58 +00:00
|
|
|
|
2014-12-11 16:23:33 +00:00
|
|
|
loop {
|
2015-08-20 12:38:25 +00:00
|
|
|
match channel.append_data(32768) {
|
|
|
|
cpal::UnknownTypeBuffer::U16(mut buffer) => {
|
2015-09-10 10:00:52 +00:00
|
|
|
for (sample, value) in buffer.chunks_mut(format.channels.len()).zip(&mut data_source) {
|
2015-08-20 12:38:25 +00:00
|
|
|
let value = ((value * 0.5 + 0.5) * std::u16::MAX as f32) as u16;
|
2015-09-10 10:00:52 +00:00
|
|
|
for out in sample.iter_mut() { *out = value; }
|
2015-08-20 12:38:25 +00:00
|
|
|
}
|
|
|
|
},
|
2014-12-11 16:51:58 +00:00
|
|
|
|
2015-08-20 12:38:25 +00:00
|
|
|
cpal::UnknownTypeBuffer::I16(mut buffer) => {
|
2015-09-10 10:00:52 +00:00
|
|
|
for (sample, value) in buffer.chunks_mut(format.channels.len()).zip(&mut data_source) {
|
2015-08-20 12:38:25 +00:00
|
|
|
let value = (value * std::i16::MAX as f32) as i16;
|
2015-09-10 10:00:52 +00:00
|
|
|
for out in sample.iter_mut() { *out = value; }
|
2015-08-20 12:38:25 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
cpal::UnknownTypeBuffer::F32(mut buffer) => {
|
2015-09-10 10:00:52 +00:00
|
|
|
for (sample, value) in buffer.chunks_mut(format.channels.len()).zip(&mut data_source) {
|
|
|
|
for out in sample.iter_mut() { *out = value; }
|
2015-08-20 12:38:25 +00:00
|
|
|
}
|
|
|
|
},
|
2014-12-11 16:51:58 +00:00
|
|
|
}
|
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
|
|
|
}
|