Update the beep example

This commit is contained in:
Pierre Krieger 2015-09-10 12:00:52 +02:00
parent 48282a068d
commit 9d56c4c616
1 changed files with 7 additions and 10 deletions

View File

@ -6,31 +6,28 @@ fn main() {
let mut channel = cpal::Voice::new(&endpoint, &format).unwrap(); let mut channel = cpal::Voice::new(&endpoint, &format).unwrap();
// Produce a sinusoid of maximum amplitude. // Produce a sinusoid of maximum amplitude.
let mut data_source = (0u64..).map(|t| t as f32 * 0.03) let mut data_source = (0u64..).map(|t| t as f32 * 440.0 * 2.0 * 3.141592 / format.samples_rate.0 as f32) // 440 Hz
.map(|t| t.sin()); .map(|t| t.sin());
loop { loop {
match channel.append_data(32768) { match channel.append_data(32768) {
cpal::UnknownTypeBuffer::U16(mut buffer) => { cpal::UnknownTypeBuffer::U16(mut buffer) => {
for (sample, value) in buffer.chunks_mut(2).zip(&mut data_source) { for (sample, value) in buffer.chunks_mut(format.channels.len()).zip(&mut data_source) {
let value = ((value * 0.5 + 0.5) * std::u16::MAX as f32) as u16; let value = ((value * 0.5 + 0.5) * std::u16::MAX as f32) as u16;
sample[0] = value; for out in sample.iter_mut() { *out = value; }
sample[1] = value;
} }
}, },
cpal::UnknownTypeBuffer::I16(mut buffer) => { cpal::UnknownTypeBuffer::I16(mut buffer) => {
for (sample, value) in buffer.chunks_mut(2).zip(&mut data_source) { for (sample, value) in buffer.chunks_mut(format.channels.len()).zip(&mut data_source) {
let value = (value * std::i16::MAX as f32) as i16; let value = (value * std::i16::MAX as f32) as i16;
sample[0] = value; for out in sample.iter_mut() { *out = value; }
sample[1] = value;
} }
}, },
cpal::UnknownTypeBuffer::F32(mut buffer) => { cpal::UnknownTypeBuffer::F32(mut buffer) => {
for (sample, value) in buffer.chunks_mut(2).zip(&mut data_source) { for (sample, value) in buffer.chunks_mut(format.channels.len()).zip(&mut data_source) {
sample[0] = value; for out in sample.iter_mut() { *out = value; }
sample[1] = value;
} }
}, },
} }