Add an Android example and enhance the other examples with CLI argument parsing.
This commit is contained in:
parent
f6c12257bb
commit
269c60fde0
7 changed files with 222 additions and 53 deletions
|
|
@ -9,13 +9,35 @@
|
|||
extern crate anyhow;
|
||||
extern crate cpal;
|
||||
extern crate ringbuf;
|
||||
extern crate structopt;
|
||||
|
||||
use cpal::traits::{DeviceTrait, HostTrait, StreamTrait};
|
||||
use ringbuf::RingBuffer;
|
||||
use structopt::StructOpt;
|
||||
|
||||
const LATENCY_MS: f32 = 150.0;
|
||||
#[derive(Debug, StructOpt)]
|
||||
#[structopt(name = "beep", about = "Simple example of audio playback.")]
|
||||
struct Opt {
|
||||
#[cfg(all(
|
||||
any(target_os = "linux", target_os = "dragonfly", target_os = "freebsd"),
|
||||
feature = "jack"
|
||||
))]
|
||||
#[structopt(short, long)]
|
||||
jack: bool,
|
||||
|
||||
#[structopt(short, long, default_value = "150.0")]
|
||||
latency: f32,
|
||||
|
||||
#[structopt(default_value = "default")]
|
||||
input_device: String,
|
||||
|
||||
#[structopt(default_value = "default")]
|
||||
output_device: String,
|
||||
}
|
||||
|
||||
fn main() -> Result<(), anyhow::Error> {
|
||||
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"),
|
||||
|
|
@ -23,10 +45,7 @@ fn main() -> Result<(), anyhow::Error> {
|
|||
))]
|
||||
// Manually check for flags. Can be passed through cargo with -- e.g.
|
||||
// cargo run --release --example beep --features jack -- --jack
|
||||
let host = if std::env::args()
|
||||
.collect::<String>()
|
||||
.contains(&String::from("--jack"))
|
||||
{
|
||||
let host = if opt.jack {
|
||||
cpal::host_from_id(cpal::available_hosts()
|
||||
.into_iter()
|
||||
.find(|id| *id == cpal::HostId::Jack)
|
||||
|
|
@ -43,21 +62,31 @@ fn main() -> Result<(), anyhow::Error> {
|
|||
))]
|
||||
let host = cpal::default_host();
|
||||
|
||||
// Default devices.
|
||||
let input_device = host
|
||||
.default_input_device()
|
||||
.expect("failed to get default input device");
|
||||
let output_device = host
|
||||
.default_output_device()
|
||||
.expect("failed to get default output device");
|
||||
println!("Using default input device: \"{}\"", input_device.name()?);
|
||||
println!("Using default output device: \"{}\"", output_device.name()?);
|
||||
// Find devices.
|
||||
let input_device = if opt.input_device == "default" {
|
||||
host.default_input_device()
|
||||
} else {
|
||||
host.input_devices()?
|
||||
.find(|x| x.name().map(|y| y == opt.input_device).unwrap_or(false))
|
||||
}
|
||||
.expect("failed to find input device");
|
||||
|
||||
let output_device = if opt.output_device == "default" {
|
||||
host.default_output_device()
|
||||
} else {
|
||||
host.output_devices()?
|
||||
.find(|x| x.name().map(|y| y == opt.output_device).unwrap_or(false))
|
||||
}
|
||||
.expect("failed to find output device");
|
||||
|
||||
println!("Using input device: \"{}\"", input_device.name()?);
|
||||
println!("Using output device: \"{}\"", output_device.name()?);
|
||||
|
||||
// We'll try and use the same configuration between streams to keep it simple.
|
||||
let config: cpal::StreamConfig = input_device.default_input_config()?.into();
|
||||
|
||||
// Create a delay in case the input and output devices aren't synced.
|
||||
let latency_frames = (LATENCY_MS / 1_000.0) * config.sample_rate.0 as f32;
|
||||
let latency_frames = (opt.latency / 1_000.0) * config.sample_rate.0 as f32;
|
||||
let latency_samples = latency_frames as usize * config.channels as usize;
|
||||
|
||||
// The buffer to share samples
|
||||
|
|
@ -111,7 +140,7 @@ fn main() -> Result<(), anyhow::Error> {
|
|||
// Play the streams.
|
||||
println!(
|
||||
"Starting the input and output streams with `{}` milliseconds of latency.",
|
||||
LATENCY_MS
|
||||
opt.latency
|
||||
);
|
||||
input_stream.play()?;
|
||||
output_stream.play()?;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue