use clap::Parser;
use cpal::{
traits::{DeviceTrait, HostTrait, StreamTrait},
Error, ErrorKind, HostId, InputCallbackInfo, OutputCallbackInfo, Sample, StreamConfig,
};
use ringbuf::{
traits::{Consumer, Producer, Split},
HeapRb,
};
#[derive(Parser, Debug)]
#[command(version, about = "CPAL feedback example", long_about = None)]
struct Opt {
#[arg(short, long, value_name = "IN")]
input_device: Option<String>,
#[arg(short, long, value_name = "OUT")]
output_device: Option<String>,
#[arg(short, long, value_name = "DELAY_MS", default_value_t = 150.0)]
latency: f32,
#[arg(long, default_value_t = false)]
jack: bool,
#[arg(long, default_value_t = false)]
pulseaudio: bool,
}
fn main() -> anyhow::Result<()> {
let opt = Opt::parse();
#[allow(unused_mut, unused_assignments)]
let mut jack_host_id: Result<HostId, Error> = Err(ErrorKind::HostUnavailable.into());
#[allow(unused_mut, unused_assignments)]
let mut pulseaudio_host_id: Result<HostId, Error> = Err(ErrorKind::HostUnavailable.into());
#[cfg(any(
target_os = "linux",
target_os = "dragonfly",
target_os = "freebsd",
target_os = "netbsd"
))]
{
#[cfg(feature = "jack")]
{
jack_host_id = Ok(HostId::Jack);
}
#[cfg(feature = "pulseaudio")]
{
pulseaudio_host_id = Ok(HostId::PulseAudio);
}
}
let host = if opt.jack {
jack_host_id
.and_then(cpal::host_from_id)
.expect("make sure `--features jack` is specified, and the platform is supported")
} else if opt.pulseaudio {
pulseaudio_host_id
.and_then(cpal::host_from_id)
.expect("make sure `--features pulseaudio` is specified, and the platform is supported")
} else {
cpal::default_host()
};
let input_device = if let Some(device) = opt.input_device {
let id = &device.parse().expect("failed to parse input device id");
host.device_by_id(id)
} else {
host.default_input_device()
}
.expect("failed to find input device");
let output_device = if let Some(device) = opt.output_device {
let id = &device.parse().expect("failed to parse output device id");
host.device_by_id(id)
} else {
host.default_output_device()
}
.expect("failed to find output device");
println!("Using input device: \"{}\"", input_device.id()?);
println!("Using output device: \"{}\"", output_device.id()?);
let config: StreamConfig = input_device.default_input_config()?.into();
let latency_frames = (opt.latency / 1_000.0) * config.sample_rate as f32;
let latency_samples = latency_frames as usize * config.channels as usize;
let ring = HeapRb::<f32>::new(latency_samples * 2);
let (mut producer, mut consumer) = ring.split();
for _ in 0..latency_samples {
producer.try_push(f32::EQUILIBRIUM).unwrap();
}
let input_data_fn = move |data: &[f32], _: &InputCallbackInfo| {
if producer.push_slice(data) < data.len() {
eprintln!("output stream fell behind: try increasing latency");
}
};
let output_data_fn = move |data: &mut [f32], _: &OutputCallbackInfo| {
let read = consumer.pop_slice(data);
if read < data.len() {
data[read..].fill(f32::EQUILIBRIUM);
eprintln!("input stream fell behind: try increasing latency");
}
};
println!("Attempting to build both streams with f32 samples and `{config:?}`.");
let input_stream = input_device.build_input_stream(config, input_data_fn, err_fn, None)?;
let output_stream = output_device.build_output_stream(config, output_data_fn, err_fn, None)?;
println!("Successfully built streams.");
println!(
"Starting the input and output streams with `{}` milliseconds of latency.",
opt.latency
);
input_stream.play()?;
output_stream.play()?;
println!("Playing for 10 seconds... ");
std::thread::sleep(std::time::Duration::from_secs(10));
drop(input_stream);
drop(output_stream);
println!("Done!");
Ok(())
}
fn err_fn(err: Error) {
match err.kind() {
ErrorKind::DeviceChanged | ErrorKind::Xrun | ErrorKind::RealtimeDenied => {
eprintln!("{err}")
}
_ => eprintln!("Stream error: {err}"),
}
}