use std::{thread::sleep, time::Duration};
use cpal::traits::{DeviceTrait, HostTrait, StreamTrait};
use ringbuf::{
HeapRb,
traits::{Consumer, Producer, Split},
};
fn main() {
let h = cpal::default_host();
let i = h.default_input_device().unwrap();
let o = h.default_output_device().unwrap();
let mut i_conf = i
.supported_input_configs()
.unwrap()
.next()
.unwrap()
.with_max_sample_rate()
.config();
let mut o_conf = o
.supported_output_configs()
.unwrap()
.next()
.unwrap()
.with_max_sample_rate()
.config();
const LEN: usize = 600;
i_conf.buffer_size = cpal::BufferSize::Fixed(LEN as u32);
o_conf.buffer_size = cpal::BufferSize::Fixed(LEN as u32);
let buf = HeapRb::<f32>::new(LEN * 2);
let (mut tx, mut rx) = buf.split();
let is = i
.build_input_stream(
&i_conf,
move |data: &[f32], _: &cpal::InputCallbackInfo| {
tx.push_slice(data);
},
move |_err| {},
None,
)
.unwrap();
let os = o
.build_output_stream(
&o_conf,
move |data: &mut [f32], _: &cpal::OutputCallbackInfo| {
rx.pop_slice(data);
},
move |_err| {},
None,
)
.unwrap();
is.play().unwrap();
os.play().unwrap();
sleep(Duration::from_secs(20));
}