Switch from structopt to clap to reduce dependencies.
This commit is contained in:
parent
269c60fde0
commit
e76e1de3a3
4 changed files with 112 additions and 33 deletions
10
Cargo.toml
10
Cargo.toml
|
|
@ -18,7 +18,7 @@ thiserror = "1.0.2"
|
|||
anyhow = "1.0.12"
|
||||
hound = "3.4"
|
||||
ringbuf = "0.2"
|
||||
structopt = "0.3.21"
|
||||
clap = { version = "2.33.3", default-features = false }
|
||||
|
||||
[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"] }
|
||||
|
|
@ -60,20 +60,12 @@ crate-type = ["cdylib"]
|
|||
|
||||
[[example]]
|
||||
name = "beep"
|
||||
path = "examples/beep.rs"
|
||||
crate-type = ["bin"]
|
||||
|
||||
[[example]]
|
||||
name = "enumerate"
|
||||
path = "examples/enumerate.rs"
|
||||
crate-type = ["bin"]
|
||||
|
||||
[[example]]
|
||||
name = "feedback"
|
||||
path = "examples/feedback.rs"
|
||||
crate-type = ["bin"]
|
||||
|
||||
[[example]]
|
||||
name = "record_wav"
|
||||
path = "examples/record_wav.rs"
|
||||
crate-type = ["bin"]
|
||||
|
|
|
|||
|
|
@ -1,24 +1,48 @@
|
|||
extern crate anyhow;
|
||||
extern crate clap;
|
||||
extern crate cpal;
|
||||
extern crate structopt;
|
||||
|
||||
use cpal::traits::{DeviceTrait, HostTrait, StreamTrait};
|
||||
use structopt::StructOpt;
|
||||
|
||||
#[derive(Debug, StructOpt)]
|
||||
#[structopt(name = "beep", about = "Simple example of audio playback.")]
|
||||
#[derive(Debug)]
|
||||
struct Opt {
|
||||
#[cfg(all(
|
||||
any(target_os = "linux", target_os = "dragonfly", target_os = "freebsd"),
|
||||
feature = "jack"
|
||||
))]
|
||||
#[structopt(short, long)]
|
||||
jack: bool,
|
||||
|
||||
#[structopt(default_value = "default")]
|
||||
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<()> {
|
||||
let opt = Opt::from_args();
|
||||
|
||||
|
|
|
|||
|
|
@ -7,36 +7,75 @@
|
|||
//! precisely synchronised.
|
||||
|
||||
extern crate anyhow;
|
||||
extern crate clap;
|
||||
extern crate cpal;
|
||||
extern crate ringbuf;
|
||||
extern crate structopt;
|
||||
|
||||
use anyhow::Context;
|
||||
use cpal::traits::{DeviceTrait, HostTrait, StreamTrait};
|
||||
use ringbuf::RingBuffer;
|
||||
use structopt::StructOpt;
|
||||
|
||||
#[derive(Debug, StructOpt)]
|
||||
#[structopt(name = "beep", about = "Simple example of audio playback.")]
|
||||
#[derive(Debug)]
|
||||
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();
|
||||
impl Opt {
|
||||
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.
|
||||
#[cfg(all(
|
||||
|
|
|
|||
|
|
@ -3,30 +3,54 @@
|
|||
//! The input data is recorded to "$CARGO_MANIFEST_DIR/recorded.wav".
|
||||
|
||||
extern crate anyhow;
|
||||
extern crate clap;
|
||||
extern crate cpal;
|
||||
extern crate hound;
|
||||
extern crate structopt;
|
||||
|
||||
use cpal::traits::{DeviceTrait, HostTrait, StreamTrait};
|
||||
use std::fs::File;
|
||||
use std::io::BufWriter;
|
||||
use std::sync::{Arc, Mutex};
|
||||
use structopt::StructOpt;
|
||||
|
||||
#[derive(Debug, StructOpt)]
|
||||
#[structopt(name = "beep", about = "Simple example of audio playback.")]
|
||||
#[derive(Debug)]
|
||||
struct Opt {
|
||||
#[cfg(all(
|
||||
any(target_os = "linux", target_os = "dragonfly", target_os = "freebsd"),
|
||||
feature = "jack"
|
||||
))]
|
||||
#[structopt(short, long)]
|
||||
jack: bool,
|
||||
|
||||
#[structopt(default_value = "default")]
|
||||
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> {
|
||||
let opt = Opt::from_args();
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue