2014-12-11 15:28:26 +00:00
|
|
|
extern crate cpal;
|
|
|
|
|
|
|
|
fn main() {
|
2014-12-11 16:23:33 +00:00
|
|
|
let mut channel = cpal::Channel::new();
|
|
|
|
|
2014-12-11 18:07:58 +00:00
|
|
|
assert!(channel.get_samples_format() == cpal::SampleFormat::U16);
|
|
|
|
|
2014-12-11 17:09:48 +00:00
|
|
|
// producing a sinusoid
|
2014-12-11 16:51:58 +00:00
|
|
|
let mut data_source =
|
|
|
|
std::iter::iterate(0.0f32, |f| f + 0.03)
|
|
|
|
.map(|angle| {
|
|
|
|
use std::num::FloatMath;
|
|
|
|
use std::num::Int;
|
|
|
|
|
|
|
|
let angle = angle.sin();
|
|
|
|
|
|
|
|
let max: u16 = Int::max_value();
|
|
|
|
let value = (max as f32 / 2.0) + (angle * (max as f32 / 2.0));
|
|
|
|
value as u16
|
2014-12-11 18:02:04 +00:00
|
|
|
});
|
2014-12-11 16:51:58 +00:00
|
|
|
|
2014-12-11 16:23:33 +00:00
|
|
|
loop {
|
2014-12-11 18:02:04 +00:00
|
|
|
let mut buffer = channel.append_data();
|
2014-12-11 16:51:58 +00:00
|
|
|
|
2014-12-11 18:02:04 +00:00
|
|
|
for sample in buffer.chunks_mut(4) {
|
|
|
|
let value = data_source.next().unwrap();
|
|
|
|
|
|
|
|
let mut writer = std::io::BufWriter::new(sample);
|
|
|
|
writer.write_le_u16(value).unwrap();
|
|
|
|
writer.write_le_u16(value).unwrap();
|
2014-12-11 16:51:58 +00:00
|
|
|
}
|
2014-12-11 16:23:33 +00:00
|
|
|
}
|
2014-12-11 15:28:26 +00:00
|
|
|
}
|