* add timeout paramter to DeviceTrait stream builders This makes it possible for the library client to handle situations where the host takes too long to respond or even blocks forever, eg. a retry can be applied. if the timeout passed is "None" the current behaviour is applied, eg. blocking forever. if the timeout passed is a Duration then it will be applied to the Device Streams. This is only implemented for ALSA, the other hosts will ignore the parameter for now. Signed-off-by: Marc Bodmer <marc.bodmer@securiton.ch>
180 lines
5.4 KiB
Rust
180 lines
5.4 KiB
Rust
extern crate anyhow;
|
|
extern crate clap;
|
|
extern crate cpal;
|
|
|
|
use clap::arg;
|
|
use cpal::{
|
|
traits::{DeviceTrait, HostTrait, StreamTrait},
|
|
SizedSample,
|
|
};
|
|
use cpal::{FromSample, Sample};
|
|
|
|
#[derive(Debug)]
|
|
struct Opt {
|
|
#[cfg(all(
|
|
any(
|
|
target_os = "linux",
|
|
target_os = "dragonfly",
|
|
target_os = "freebsd",
|
|
target_os = "netbsd"
|
|
),
|
|
feature = "jack"
|
|
))]
|
|
jack: bool,
|
|
|
|
device: String,
|
|
}
|
|
|
|
impl Opt {
|
|
fn from_args() -> Self {
|
|
let app = clap::Command::new("beep").arg(arg!([DEVICE] "The audio device to use"));
|
|
#[cfg(all(
|
|
any(
|
|
target_os = "linux",
|
|
target_os = "dragonfly",
|
|
target_os = "freebsd",
|
|
target_os = "netbsd"
|
|
),
|
|
feature = "jack"
|
|
))]
|
|
let app = app.arg(arg!(-j --jack "Use the JACK host"));
|
|
let matches = app.get_matches();
|
|
let device = matches.value_of("DEVICE").unwrap_or("default").to_string();
|
|
|
|
#[cfg(all(
|
|
any(
|
|
target_os = "linux",
|
|
target_os = "dragonfly",
|
|
target_os = "freebsd",
|
|
target_os = "netbsd"
|
|
),
|
|
feature = "jack"
|
|
))]
|
|
return Opt {
|
|
jack: matches.is_present("jack"),
|
|
device,
|
|
};
|
|
|
|
#[cfg(any(
|
|
not(any(
|
|
target_os = "linux",
|
|
target_os = "dragonfly",
|
|
target_os = "freebsd",
|
|
target_os = "netbsd"
|
|
)),
|
|
not(feature = "jack")
|
|
))]
|
|
Opt { device }
|
|
}
|
|
}
|
|
|
|
fn main() -> anyhow::Result<()> {
|
|
let opt = Opt::from_args();
|
|
|
|
// Conditionally compile with jack if the feature is specified.
|
|
#[cfg(all(
|
|
any(
|
|
target_os = "linux",
|
|
target_os = "dragonfly",
|
|
target_os = "freebsd",
|
|
target_os = "netbsd"
|
|
),
|
|
feature = "jack"
|
|
))]
|
|
// Manually check for flags. Can be passed through cargo with -- e.g.
|
|
// cargo run --release --example beep --features jack -- --jack
|
|
let host = if opt.jack {
|
|
cpal::host_from_id(cpal::available_hosts()
|
|
.into_iter()
|
|
.find(|id| *id == cpal::HostId::Jack)
|
|
.expect(
|
|
"make sure --features jack is specified. only works on OSes where jack is available",
|
|
)).expect("jack host unavailable")
|
|
} else {
|
|
cpal::default_host()
|
|
};
|
|
|
|
#[cfg(any(
|
|
not(any(
|
|
target_os = "linux",
|
|
target_os = "dragonfly",
|
|
target_os = "freebsd",
|
|
target_os = "netbsd"
|
|
)),
|
|
not(feature = "jack")
|
|
))]
|
|
let host = cpal::default_host();
|
|
|
|
let device = if opt.device == "default" {
|
|
host.default_output_device()
|
|
} else {
|
|
host.output_devices()?
|
|
.find(|x| x.name().map(|y| y == opt.device).unwrap_or(false))
|
|
}
|
|
.expect("failed to find output device");
|
|
println!("Output device: {}", device.name()?);
|
|
|
|
let config = device.default_output_config().unwrap();
|
|
println!("Default output config: {:?}", config);
|
|
|
|
match config.sample_format() {
|
|
cpal::SampleFormat::I8 => run::<i8>(&device, &config.into()),
|
|
cpal::SampleFormat::I16 => run::<i16>(&device, &config.into()),
|
|
// cpal::SampleFormat::I24 => run::<I24>(&device, &config.into()),
|
|
cpal::SampleFormat::I32 => run::<i32>(&device, &config.into()),
|
|
// cpal::SampleFormat::I48 => run::<I48>(&device, &config.into()),
|
|
cpal::SampleFormat::I64 => run::<i64>(&device, &config.into()),
|
|
cpal::SampleFormat::U8 => run::<u8>(&device, &config.into()),
|
|
cpal::SampleFormat::U16 => run::<u16>(&device, &config.into()),
|
|
// cpal::SampleFormat::U24 => run::<U24>(&device, &config.into()),
|
|
cpal::SampleFormat::U32 => run::<u32>(&device, &config.into()),
|
|
// cpal::SampleFormat::U48 => run::<U48>(&device, &config.into()),
|
|
cpal::SampleFormat::U64 => run::<u64>(&device, &config.into()),
|
|
cpal::SampleFormat::F32 => run::<f32>(&device, &config.into()),
|
|
cpal::SampleFormat::F64 => run::<f64>(&device, &config.into()),
|
|
sample_format => panic!("Unsupported sample format '{sample_format}'"),
|
|
}
|
|
}
|
|
|
|
pub fn run<T>(device: &cpal::Device, config: &cpal::StreamConfig) -> Result<(), anyhow::Error>
|
|
where
|
|
T: SizedSample + FromSample<f32>,
|
|
{
|
|
let sample_rate = config.sample_rate.0 as f32;
|
|
let channels = config.channels as usize;
|
|
|
|
// Produce a sinusoid of maximum amplitude.
|
|
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| eprintln!("an error occurred on stream: {}", err);
|
|
|
|
let stream = device.build_output_stream(
|
|
config,
|
|
move |data: &mut [T], _: &cpal::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;
|
|
}
|
|
}
|
|
}
|