use clap::Parser;
use cpal::{
traits::{DeviceTrait, HostTrait, StreamTrait},
Device, Error, ErrorKind, FromSample, HostId, OutputCallbackInfo, Sample, SampleFormat,
SizedSample, StreamConfig, I24,
};
#[derive(Parser, Debug)]
#[command(version, about = "CPAL beep example", long_about = None)]
struct Opt {
#[arg(short, long)]
device: Option<String>,
#[arg(long, default_value_t = false)]
jack: bool,
#[arg(long, default_value_t = false)]
pulseaudio: bool,
#[arg(long, default_value_t = false)]
pipewire: 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());
#[allow(unused_mut, unused_assignments)]
let mut pipewire_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);
}
#[cfg(feature = "pipewire")]
{
pipewire_host_id = Ok(HostId::PipeWire);
}
}
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 if opt.pipewire {
pipewire_host_id
.and_then(cpal::host_from_id)
.expect("make sure `--features pipewire` is specified, and the platform is supported")
} else {
cpal::default_host()
};
let device = if let Some(device) = opt.device {
let id = &device.parse().expect("failed to parse device id");
host.device_by_id(id)
} else {
host.default_output_device()
}
.expect("failed to find output device");
println!("Output device: {}", device.id()?);
let config = device.default_output_config().unwrap();
println!("Default output config: {config:?}");
match config.sample_format() {
SampleFormat::I8 => run::<i8>(&device, config.into()),
SampleFormat::I16 => run::<i16>(&device, config.into()),
SampleFormat::I24 => run::<I24>(&device, config.into()),
SampleFormat::I32 => run::<i32>(&device, config.into()),
SampleFormat::I64 => run::<i64>(&device, config.into()),
SampleFormat::U8 => run::<u8>(&device, config.into()),
SampleFormat::U16 => run::<u16>(&device, config.into()),
SampleFormat::U32 => run::<u32>(&device, config.into()),
SampleFormat::U64 => run::<u64>(&device, config.into()),
SampleFormat::F32 => run::<f32>(&device, config.into()),
SampleFormat::F64 => run::<f64>(&device, config.into()),
sample_format => panic!("Unsupported sample format '{sample_format}'"),
}
}
pub fn run<T>(device: &Device, config: StreamConfig) -> Result<(), anyhow::Error>
where
T: SizedSample + FromSample<f32>,
{
let sample_rate = config.sample_rate as f32;
let channels = config.channels as usize;
let mut sample_clock = 0f32;
let mut next_value = move || {
sample_clock = (sample_clock + 1.0) % sample_rate;
(sample_clock * 440.0 * 2.0 * std::f32::consts::PI / sample_rate).sin()
};
let err_fn = |err: Error| match err.kind() {
ErrorKind::DeviceChanged | ErrorKind::Xrun | ErrorKind::RealtimeDenied => {
eprintln!("{err}")
}
_ => eprintln!("Stream error: {err}"),
};
let stream = device.build_output_stream(
config,
move |data: &mut [T], _: &OutputCallbackInfo| write_data(data, channels, &mut next_value),
err_fn,
None,
)?;
stream.play()?;
std::thread::sleep(std::time::Duration::from_millis(1000));
Ok(())
}
fn write_data<T>(output: &mut [T], channels: usize, next_sample: &mut dyn FnMut() -> f32)
where
T: Sample + FromSample<f32>,
{
for frame in output.chunks_mut(channels) {
let value: T = T::from_sample(next_sample());
for sample in frame.iter_mut() {
*sample = value;
}
}
}