Switch from structopt to clap to reduce dependencies.

This commit is contained in:
Alex Moon 2020-12-22 09:25:36 -07:00
parent 269c60fde0
commit e76e1de3a3
4 changed files with 112 additions and 33 deletions

View file

@ -18,7 +18,7 @@ thiserror = "1.0.2"
anyhow = "1.0.12" anyhow = "1.0.12"
hound = "3.4" hound = "3.4"
ringbuf = "0.2" ringbuf = "0.2"
structopt = "0.3.21" clap = { version = "2.33.3", default-features = false }
[target.'cfg(target_os = "windows")'.dependencies] [target.'cfg(target_os = "windows")'.dependencies]
winapi = { version = "0.3", features = ["audiosessiontypes", "audioclient", "coml2api", "combaseapi", "debug", "devpkey", "handleapi", "ksmedia", "mmdeviceapi", "objbase", "profileapi", "std", "synchapi", "winbase", "winuser"] } winapi = { version = "0.3", features = ["audiosessiontypes", "audioclient", "coml2api", "combaseapi", "debug", "devpkey", "handleapi", "ksmedia", "mmdeviceapi", "objbase", "profileapi", "std", "synchapi", "winbase", "winuser"] }
@ -60,20 +60,12 @@ crate-type = ["cdylib"]
[[example]] [[example]]
name = "beep" name = "beep"
path = "examples/beep.rs"
crate-type = ["bin"]
[[example]] [[example]]
name = "enumerate" name = "enumerate"
path = "examples/enumerate.rs"
crate-type = ["bin"]
[[example]] [[example]]
name = "feedback" name = "feedback"
path = "examples/feedback.rs"
crate-type = ["bin"]
[[example]] [[example]]
name = "record_wav" name = "record_wav"
path = "examples/record_wav.rs"
crate-type = ["bin"]

View file

@ -1,24 +1,48 @@
extern crate anyhow; extern crate anyhow;
extern crate clap;
extern crate cpal; extern crate cpal;
extern crate structopt;
use cpal::traits::{DeviceTrait, HostTrait, StreamTrait}; use cpal::traits::{DeviceTrait, HostTrait, StreamTrait};
use structopt::StructOpt;
#[derive(Debug, StructOpt)] #[derive(Debug)]
#[structopt(name = "beep", about = "Simple example of audio playback.")]
struct Opt { struct Opt {
#[cfg(all( #[cfg(all(
any(target_os = "linux", target_os = "dragonfly", target_os = "freebsd"), any(target_os = "linux", target_os = "dragonfly", target_os = "freebsd"),
feature = "jack" feature = "jack"
))] ))]
#[structopt(short, long)]
jack: bool, jack: bool,
#[structopt(default_value = "default")]
device: String, device: String,
} }
impl Opt {
fn from_args() -> Self {
let app = clap::App::new("beep").arg_from_usage("[DEVICE] 'The audio device to use'");
#[cfg(all(
any(target_os = "linux", target_os = "dragonfly", target_os = "freebsd"),
feature = "jack"
))]
let app = app.arg_from_usage("-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"),
feature = "jack"
))]
return Opt {
jack: matches.is_present("jack"),
device,
};
#[cfg(any(
not(any(target_os = "linux", target_os = "dragonfly", target_os = "freebsd")),
not(feature = "jack")
))]
Opt { device }
}
}
fn main() -> anyhow::Result<()> { fn main() -> anyhow::Result<()> {
let opt = Opt::from_args(); let opt = Opt::from_args();

View file

@ -7,36 +7,75 @@
//! precisely synchronised. //! precisely synchronised.
extern crate anyhow; extern crate anyhow;
extern crate clap;
extern crate cpal; extern crate cpal;
extern crate ringbuf; extern crate ringbuf;
extern crate structopt;
use anyhow::Context;
use cpal::traits::{DeviceTrait, HostTrait, StreamTrait}; use cpal::traits::{DeviceTrait, HostTrait, StreamTrait};
use ringbuf::RingBuffer; use ringbuf::RingBuffer;
use structopt::StructOpt;
#[derive(Debug, StructOpt)] #[derive(Debug)]
#[structopt(name = "beep", about = "Simple example of audio playback.")]
struct Opt { struct Opt {
#[cfg(all( #[cfg(all(
any(target_os = "linux", target_os = "dragonfly", target_os = "freebsd"), any(target_os = "linux", target_os = "dragonfly", target_os = "freebsd"),
feature = "jack" feature = "jack"
))] ))]
#[structopt(short, long)]
jack: bool, jack: bool,
#[structopt(short, long, default_value = "150.0")]
latency: f32, latency: f32,
#[structopt(default_value = "default")]
input_device: String, input_device: String,
#[structopt(default_value = "default")]
output_device: String, output_device: String,
} }
fn main() -> Result<(), anyhow::Error> { impl Opt {
let opt = Opt::from_args(); fn from_args() -> anyhow::Result<Self> {
let app = clap::App::new("beep")
.arg_from_usage(
"-l, --latency [DELAY_MS] 'Specify the delay between input and output [default: 150]'",
)
.arg_from_usage("[IN] 'The input audio device to use'")
.arg_from_usage("[OUT] 'The output audio device to use'");
#[cfg(all(
any(target_os = "linux", target_os = "dragonfly", target_os = "freebsd"),
feature = "jack"
))]
let app = app.arg_from_usage("-j, --jack 'Use the JACK host");
let matches = app.get_matches();
let latency: f32 = matches
.value_of("latency")
.unwrap_or("150")
.parse()
.context("parsing latency option")?;
let input_device = matches.value_of("IN").unwrap_or("default").to_string();
let output_device = matches.value_of("OUT").unwrap_or("default").to_string();
#[cfg(all(
any(target_os = "linux", target_os = "dragonfly", target_os = "freebsd"),
feature = "jack"
))]
return Ok(Opt {
jack: matches.is_present("jack"),
latency,
input_device,
output_device,
});
#[cfg(any(
not(any(target_os = "linux", target_os = "dragonfly", target_os = "freebsd")),
not(feature = "jack")
))]
Ok(Opt {
latency,
input_device,
output_device,
})
}
}
fn main() -> anyhow::Result<()> {
let opt = Opt::from_args()?;
// Conditionally compile with jack if the feature is specified. // Conditionally compile with jack if the feature is specified.
#[cfg(all( #[cfg(all(

View file

@ -3,30 +3,54 @@
//! The input data is recorded to "$CARGO_MANIFEST_DIR/recorded.wav". //! The input data is recorded to "$CARGO_MANIFEST_DIR/recorded.wav".
extern crate anyhow; extern crate anyhow;
extern crate clap;
extern crate cpal; extern crate cpal;
extern crate hound; extern crate hound;
extern crate structopt;
use cpal::traits::{DeviceTrait, HostTrait, StreamTrait}; use cpal::traits::{DeviceTrait, HostTrait, StreamTrait};
use std::fs::File; use std::fs::File;
use std::io::BufWriter; use std::io::BufWriter;
use std::sync::{Arc, Mutex}; use std::sync::{Arc, Mutex};
use structopt::StructOpt;
#[derive(Debug, StructOpt)] #[derive(Debug)]
#[structopt(name = "beep", about = "Simple example of audio playback.")]
struct Opt { struct Opt {
#[cfg(all( #[cfg(all(
any(target_os = "linux", target_os = "dragonfly", target_os = "freebsd"), any(target_os = "linux", target_os = "dragonfly", target_os = "freebsd"),
feature = "jack" feature = "jack"
))] ))]
#[structopt(short, long)]
jack: bool, jack: bool,
#[structopt(default_value = "default")]
device: String, device: String,
} }
impl Opt {
fn from_args() -> Self {
let app = clap::App::new("beep").arg_from_usage("[DEVICE] 'The audio device to use'");
#[cfg(all(
any(target_os = "linux", target_os = "dragonfly", target_os = "freebsd"),
feature = "jack"
))]
let app = app.arg_from_usage("-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"),
feature = "jack"
))]
return Opt {
jack: matches.is_present("jack"),
device,
};
#[cfg(any(
not(any(target_os = "linux", target_os = "dragonfly", target_os = "freebsd")),
not(feature = "jack")
))]
Opt { device }
}
}
fn main() -> Result<(), anyhow::Error> { fn main() -> Result<(), anyhow::Error> {
let opt = Opt::from_args(); let opt = Opt::from_args();