44
55use clap:: Parser ;
66use cpal:: traits:: { DeviceTrait , HostTrait , StreamTrait } ;
7- use cpal:: { FromSample , Sample } ;
7+ use cpal:: { FromSample , HostUnavailable , Sample } ;
88use std:: fs:: File ;
99use std:: io:: BufWriter ;
1010use std:: sync:: { Arc , Mutex } ;
@@ -20,58 +20,70 @@ struct Opt {
2020 #[ arg( long, default_value_t = 3 ) ]
2121 duration : u64 ,
2222
23- /// Use the JACK host
24- #[ cfg( all(
25- any(
26- target_os = "linux" ,
27- target_os = "dragonfly" ,
28- target_os = "freebsd" ,
29- target_os = "netbsd"
30- ) ,
31- feature = "jack"
32- ) ) ]
33- #[ arg( short, long) ]
34- #[ allow( dead_code) ]
23+ /// Use the JACK host. Requires `--features jack`.
24+ #[ arg( long, default_value_t = false ) ]
3525 jack : bool ,
26+
27+ /// Use the PulseAudio host. Requires `--features pulseaudio`.
28+ #[ arg( long, default_value_t = false ) ]
29+ pulseaudio : bool ,
30+
31+ /// Use the Pipewire host. Requires `--features pipewire`
32+ #[ arg( long, default_value_t = false ) ]
33+ pipewire : bool ,
3634}
3735
3836fn main ( ) -> Result < ( ) , anyhow:: Error > {
3937 let opt = Opt :: parse ( ) ;
4038
41- // Conditionally compile with jack if the feature is specified.
42- #[ cfg( all(
43- any(
44- target_os = "linux" ,
45- target_os = "dragonfly" ,
46- target_os = "freebsd" ,
47- target_os = "netbsd"
48- ) ,
49- feature = "jack"
39+ // Jack/PulseAudio support must be enabled at compile time, and is
40+ // only available on some platforms.
41+ #[ allow( unused_mut, unused_assignments) ]
42+ let mut jack_host_id = Err ( HostUnavailable ) ;
43+ #[ allow( unused_mut, unused_assignments) ]
44+ let mut pulseaudio_host_id = Err ( HostUnavailable ) ;
45+ #[ allow( unused_mut, unused_assignments) ]
46+ let mut pipewire_host_id = Err ( HostUnavailable ) ;
47+ #[ cfg( any(
48+ target_os = "linux" ,
49+ target_os = "dragonfly" ,
50+ target_os = "freebsd" ,
51+ target_os = "netbsd"
5052 ) ) ]
53+ {
54+ #[ cfg( feature = "jack" ) ]
55+ {
56+ jack_host_id = Ok ( cpal:: HostId :: Jack ) ;
57+ }
58+
59+ #[ cfg( feature = "pulseaudio" ) ]
60+ {
61+ pulseaudio_host_id = Ok ( cpal:: HostId :: PulseAudio ) ;
62+ }
63+ #[ cfg( feature = "pipewire" ) ]
64+ {
65+ pipewire_host_id = Ok ( cpal:: HostId :: PipeWire ) ;
66+ }
67+ }
68+
5169 // Manually check for flags. Can be passed through cargo with -- e.g.
52- // cargo run --release --example beep --features jack -- --jack
70+ // cargo run --release --example record_wav --features jack -- --jack
5371 let host = if opt. jack {
54- cpal:: host_from_id ( cpal:: available_hosts ( )
55- . into_iter ( )
56- . find ( |id| * id == cpal:: HostId :: Jack )
57- . expect (
58- "make sure --features jack is specified. only works on OSes where jack is available" ,
59- ) ) . expect ( "jack host unavailable" )
72+ jack_host_id
73+ . and_then ( cpal:: host_from_id)
74+ . expect ( "make sure `--features jack` is specified, and the platform is supported" )
75+ } else if opt. pulseaudio {
76+ pulseaudio_host_id
77+ . and_then ( cpal:: host_from_id)
78+ . expect ( "make sure `--features pulseaudio` is specified, and the platform is supported" )
79+ } else if opt. pipewire {
80+ pipewire_host_id
81+ . and_then ( cpal:: host_from_id)
82+ . expect ( "make sure `--features pipewire` is specified, and the platform is supported" )
6083 } else {
6184 cpal:: default_host ( )
6285 } ;
6386
64- #[ cfg( any(
65- not( any(
66- target_os = "linux" ,
67- target_os = "dragonfly" ,
68- target_os = "freebsd" ,
69- target_os = "netbsd"
70- ) ) ,
71- not( feature = "jack" )
72- ) ) ]
73- let host = cpal:: default_host ( ) ;
74-
7587 // Set up the input device and stream with the default input config.
7688 let device = if let Some ( device) = opt. device {
7789 let id = & device. parse ( ) . expect ( "failed to parse input device id" ) ;
0 commit comments