Remove EventLoop and port the ALSA backend
This commit is contained in:
parent
700bef79d9
commit
c97d1dd3fa
8 changed files with 519 additions and 951 deletions
|
|
@ -1,37 +1,34 @@
|
|||
extern crate anyhow;
|
||||
extern crate cpal;
|
||||
|
||||
use cpal::traits::{DeviceTrait, EventLoopTrait, HostTrait};
|
||||
use cpal::traits::{DeviceTrait, StreamTrait, HostTrait};
|
||||
|
||||
fn main() -> Result<(), anyhow::Error> {
|
||||
let host = cpal::default_host();
|
||||
let device = host.default_output_device().expect("failed to find a default output device");
|
||||
let format = device.default_output_format()?;
|
||||
let event_loop = host.event_loop();
|
||||
let stream_id = event_loop.build_output_stream(&device, &format)?;
|
||||
event_loop.play_stream(stream_id.clone())?;
|
||||
|
||||
let sample_rate = format.sample_rate.0 as f32;
|
||||
let channels = format.channels;
|
||||
let mut sample_clock = 0f32;
|
||||
|
||||
// Produce a sinusoid of maximum amplitude.
|
||||
let mut next_value = || {
|
||||
let mut next_value = move || {
|
||||
sample_clock = (sample_clock + 1.0) % sample_rate;
|
||||
(sample_clock * 440.0 * 2.0 * 3.141592 / sample_rate).sin()
|
||||
};
|
||||
|
||||
event_loop.run(move |id, result| {
|
||||
let stream = device.build_output_stream(&format, move |result| {
|
||||
let data = match result {
|
||||
Ok(data) => data,
|
||||
Err(err) => {
|
||||
eprintln!("an error occurred on stream {:?}: {}", id, err);
|
||||
eprintln!("an error occurred on stream: {}", err);
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
match data {
|
||||
cpal::StreamData::Output { buffer: cpal::UnknownTypeOutputBuffer::U16(mut buffer) } => {
|
||||
for sample in buffer.chunks_mut(format.channels as usize) {
|
||||
for sample in buffer.chunks_mut(channels as usize) {
|
||||
let value = ((next_value() * 0.5 + 0.5) * std::u16::MAX as f32) as u16;
|
||||
for out in sample.iter_mut() {
|
||||
*out = value;
|
||||
|
|
@ -39,7 +36,7 @@ fn main() -> Result<(), anyhow::Error> {
|
|||
}
|
||||
},
|
||||
cpal::StreamData::Output { buffer: cpal::UnknownTypeOutputBuffer::I16(mut buffer) } => {
|
||||
for sample in buffer.chunks_mut(format.channels as usize) {
|
||||
for sample in buffer.chunks_mut(channels as usize) {
|
||||
let value = (next_value() * std::i16::MAX as f32) as i16;
|
||||
for out in sample.iter_mut() {
|
||||
*out = value;
|
||||
|
|
@ -47,7 +44,7 @@ fn main() -> Result<(), anyhow::Error> {
|
|||
}
|
||||
},
|
||||
cpal::StreamData::Output { buffer: cpal::UnknownTypeOutputBuffer::F32(mut buffer) } => {
|
||||
for sample in buffer.chunks_mut(format.channels as usize) {
|
||||
for sample in buffer.chunks_mut(channels as usize) {
|
||||
let value = next_value();
|
||||
for out in sample.iter_mut() {
|
||||
*out = value;
|
||||
|
|
@ -56,5 +53,10 @@ fn main() -> Result<(), anyhow::Error> {
|
|||
},
|
||||
_ => (),
|
||||
}
|
||||
});
|
||||
})?;
|
||||
stream.play()?;
|
||||
|
||||
std::thread::sleep(std::time::Duration::from_millis(1000));
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue