From 97fc4edb8ba82b55f36d53ef81f0e9dcf0319465 Mon Sep 17 00:00:00 2001 From: mbodmer Date: Sun, 23 Oct 2022 20:59:45 +0200 Subject: [PATCH] Bump linux deps, simplify examples, remove nix dep (#712) * bump linux dependencies, dev-dependencies, anyhow, hound, ringbuf * update to clap v4 and simplify examples with clap_derive * have libjack installed when running with --all-features * don't force the patch version for libc * allow room for version resolution with nix * nix dependency not used anymore, remove it --- .github/workflows/cpal.yml | 2 + Cargo.toml | 17 ++++--- examples/beep.rs | 67 ++++++-------------------- examples/feedback.rs | 96 +++++++++----------------------------- examples/record_wav.rs | 64 +++++-------------------- 5 files changed, 55 insertions(+), 191 deletions(-) diff --git a/.github/workflows/cpal.yml b/.github/workflows/cpal.yml index 8dc4c2a..eaf606e 100644 --- a/.github/workflows/cpal.yml +++ b/.github/workflows/cpal.yml @@ -12,6 +12,8 @@ jobs: run: sudo apt update - name: Install alsa run: sudo apt-get install libasound2-dev + - name: Install libjack + run: sudo apt-get install libjack-jackd2-dev libjack-jackd2-0 - name: Install stable uses: actions-rs/toolchain@v1 with: diff --git a/Cargo.toml b/Cargo.toml index 6590da2..0d1416d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,14 +13,14 @@ edition = "2021" asio = ["asio-sys", "num-traits"] # Only available on Windows. See README for setup instructions. [dependencies] -thiserror = "1.0.2" -dasp_sample = "0.11.0" +thiserror = "1.0" +dasp_sample = "0.11" [dev-dependencies] -anyhow = "1.0.12" -hound = "3.4" -ringbuf = "0.2" -clap = { version = "3.1", default-features = false, features = ["std"] } +anyhow = "1.0" +hound = "3.5" +ringbuf = "0.3" +clap = { version = "4.0", features = ["derive"] } [target.'cfg(target_os = "android")'.dev-dependencies] ndk-glue = "0.7" @@ -34,10 +34,9 @@ once_cell = "1.12" [target.'cfg(any(target_os = "linux", target_os = "dragonfly", target_os = "freebsd", target_os = "netbsd"))'.dependencies] alsa = "0.6" -nix = "0.25" -libc = "0.2.65" +libc = "0.2" parking_lot = "0.12" -jack = { version = "0.9", optional = true } +jack = { version = "0.10", optional = true } [target.'cfg(any(target_os = "macos", target_os = "ios"))'.dependencies] core-foundation-sys = "0.8.2" # For linking to CoreFoundation.framework and handling device name `CFString`s. diff --git a/examples/beep.rs b/examples/beep.rs index 4128c48..e0266aa 100644 --- a/examples/beep.rs +++ b/examples/beep.rs @@ -1,16 +1,18 @@ -extern crate anyhow; -extern crate clap; -extern crate cpal; - -use clap::arg; +use anyhow; +use clap::Parser; use cpal::{ traits::{DeviceTrait, HostTrait, StreamTrait}, - SizedSample, + FromSample, Sample, SizedSample, }; -use cpal::{FromSample, Sample}; -#[derive(Debug)] +#[derive(Parser, Debug)] +#[command(version, about = "CPAL beep example", long_about = None)] struct Opt { + /// The audio device to use + #[arg(short, long, default_value_t = String::from("default"))] + device: String, + + /// Use the JACK host #[cfg(all( any( target_os = "linux", @@ -20,56 +22,13 @@ struct Opt { ), feature = "jack" ))] + #[arg(short, long)] + #[allow(dead_code)] 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(); + let opt = Opt::parse(); // Conditionally compile with jack if the feature is specified. #[cfg(all( diff --git a/examples/feedback.rs b/examples/feedback.rs index 50697f6..eae9fa2 100644 --- a/examples/feedback.rs +++ b/examples/feedback.rs @@ -6,18 +6,26 @@ //! Uses a delay of `LATENCY_MS` milliseconds in case the default input and output streams are not //! precisely synchronised. -extern crate anyhow; -extern crate clap; -extern crate cpal; -extern crate ringbuf; - -use anyhow::Context; -use clap::arg; +use clap::Parser; use cpal::traits::{DeviceTrait, HostTrait, StreamTrait}; -use ringbuf::RingBuffer; +use ringbuf::HeapRb; -#[derive(Debug)] +#[derive(Parser, Debug)] +#[command(version, about = "CPAL feedback example", long_about = None)] struct Opt { + /// The input audio device to use + #[arg(short, long, value_name = "IN", default_value_t = String::from("default"))] + input_device: String, + + /// The output audio device to use + #[arg(short, long, value_name = "OUT", default_value_t = String::from("default"))] + output_device: String, + + /// Specify the delay between input and output + #[arg(short, long, value_name = "DELAY_MS", default_value_t = 150.0)] + latency: f32, + + /// Use the JACK host #[cfg(all( any( target_os = "linux", @@ -27,75 +35,13 @@ struct Opt { ), feature = "jack" ))] + #[arg(short, long)] + #[allow(dead_code)] jack: bool, - - latency: f32, - input_device: String, - output_device: String, -} - -impl Opt { - fn from_args() -> anyhow::Result { - let app = clap::Command::new("feedback") - .arg(arg!( - -l --latency [DELAY_MS] "Specify the delay between input and output [default: 150]")) - .arg(arg!([IN] "The input audio device to use")) - .arg(arg!([OUT] "The output 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 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", - target_os = "netbsd" - ), - 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", - target_os = "netbsd" - )), - not(feature = "jack") - ))] - Ok(Opt { - latency, - input_device, - output_device, - }) - } } fn main() -> anyhow::Result<()> { - let opt = Opt::from_args()?; + let opt = Opt::parse(); // Conditionally compile with jack if the feature is specified. #[cfg(all( @@ -159,7 +105,7 @@ fn main() -> anyhow::Result<()> { let latency_samples = latency_frames as usize * config.channels as usize; // The buffer to share samples - let ring = RingBuffer::new(latency_samples * 2); + let ring = HeapRb::::new(latency_samples * 2); let (mut producer, mut consumer) = ring.split(); // Fill the samples with 0.0 equal to the length of the delay. diff --git a/examples/record_wav.rs b/examples/record_wav.rs index 27fd40e..4841bfa 100644 --- a/examples/record_wav.rs +++ b/examples/record_wav.rs @@ -2,20 +2,21 @@ //! //! The input data is recorded to "$CARGO_MANIFEST_DIR/recorded.wav". -extern crate anyhow; -extern crate clap; -extern crate cpal; -extern crate hound; - -use clap::arg; +use clap::Parser; use cpal::traits::{DeviceTrait, HostTrait, StreamTrait}; use cpal::{FromSample, Sample}; use std::fs::File; use std::io::BufWriter; use std::sync::{Arc, Mutex}; -#[derive(Debug)] +#[derive(Parser, Debug)] +#[command(version, about = "CPAL record_wav example", long_about = None)] struct Opt { + /// The audio device to use + #[arg(short, long, default_value_t = String::from("default"))] + device: String, + + /// Use the JACK host #[cfg(all( any( target_os = "linux", @@ -25,56 +26,13 @@ struct Opt { ), feature = "jack" ))] + #[arg(short, long)] + #[allow(dead_code)] jack: bool, - - device: String, -} - -impl Opt { - fn from_args() -> Self { - let app = clap::Command::new("record_wav").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() -> Result<(), anyhow::Error> { - let opt = Opt::from_args(); + let opt = Opt::parse(); // Conditionally compile with jack if the feature is specified. #[cfg(all(