7
7
//! precisely synchronised.
8
8
9
9
use clap:: Parser ;
10
- use cpal:: traits:: { DeviceTrait , HostTrait , StreamTrait } ;
10
+ use cpal:: {
11
+ traits:: { DeviceTrait , HostTrait , StreamTrait } ,
12
+ HostUnavailable ,
13
+ } ;
11
14
use ringbuf:: {
12
15
traits:: { Consumer , Producer , Split } ,
13
16
HeapRb ,
@@ -28,58 +31,57 @@ struct Opt {
28
31
#[ arg( short, long, value_name = "DELAY_MS" , default_value_t = 150.0 ) ]
29
32
latency : f32 ,
30
33
31
- /// Use the JACK host
32
- #[ cfg( all(
33
- any(
34
- target_os = "linux" ,
35
- target_os = "dragonfly" ,
36
- target_os = "freebsd" ,
37
- target_os = "netbsd"
38
- ) ,
39
- feature = "jack"
40
- ) ) ]
41
- #[ arg( short, long) ]
42
- #[ allow( dead_code) ]
34
+ /// Use the JACK host. Requires `--features jack`.
35
+ #[ arg( long, default_value_t = false ) ]
43
36
jack : bool ,
37
+
38
+ /// Use the PulseAudio host. Requires `--features pulseaudio`.
39
+ #[ arg( long, default_value_t = false ) ]
40
+ pulseaudio : bool ,
44
41
}
45
42
46
43
fn main ( ) -> anyhow:: Result < ( ) > {
47
44
let opt = Opt :: parse ( ) ;
48
45
49
- // Conditionally compile with jack if the feature is specified.
50
- #[ cfg( all(
51
- any(
52
- target_os = "linux" ,
53
- target_os = "dragonfly" ,
54
- target_os = "freebsd" ,
55
- target_os = "netbsd"
56
- ) ,
57
- feature = "jack"
46
+ // Jack/PulseAudio support must be enabled at compile time, and is
47
+ // only available on some platforms.
48
+ #[ allow( unused_mut, unused_assignments) ]
49
+ let mut jack_host_id = Err ( HostUnavailable ) ;
50
+ #[ allow( unused_mut, unused_assignments) ]
51
+ let mut pulseaudio_host_id = Err ( HostUnavailable ) ;
52
+
53
+ #[ cfg( any(
54
+ target_os = "linux" ,
55
+ target_os = "dragonfly" ,
56
+ target_os = "freebsd" ,
57
+ target_os = "netbsd"
58
58
) ) ]
59
+ {
60
+ #[ cfg( feature = "jack" ) ]
61
+ {
62
+ jack_host_id = Ok ( cpal:: HostId :: Jack ) ;
63
+ }
64
+
65
+ #[ cfg( feature = "pulseaudio" ) ]
66
+ {
67
+ pulseaudio_host_id = Ok ( cpal:: HostId :: PulseAudio ) ;
68
+ }
69
+ }
70
+
59
71
// Manually check for flags. Can be passed through cargo with -- e.g.
60
72
// cargo run --release --example beep --features jack -- --jack
61
73
let host = if opt. jack {
62
- cpal:: host_from_id ( cpal:: available_hosts ( )
63
- . into_iter ( )
64
- . find ( |id| * id == cpal:: HostId :: Jack )
65
- . expect (
66
- "make sure --features jack is specified. only works on OSes where jack is available" ,
67
- ) ) . expect ( "jack host unavailable" )
74
+ jack_host_id
75
+ . and_then ( cpal:: host_from_id)
76
+ . expect ( "make sure `--features jack` is specified, and the platform is supported" )
77
+ } else if opt. pulseaudio {
78
+ pulseaudio_host_id
79
+ . and_then ( cpal:: host_from_id)
80
+ . expect ( "make sure `--features pulseaudio` is specified, and the platform is supported" )
68
81
} else {
69
82
cpal:: default_host ( )
70
83
} ;
71
84
72
- #[ cfg( any(
73
- not( any(
74
- target_os = "linux" ,
75
- target_os = "dragonfly" ,
76
- target_os = "freebsd" ,
77
- target_os = "netbsd"
78
- ) ) ,
79
- not( feature = "jack" )
80
- ) ) ]
81
- let host = cpal:: default_host ( ) ;
82
-
83
85
// Find devices.
84
86
let input_device = if opt. input_device == "default" {
85
87
host. default_input_device ( )
@@ -161,8 +163,8 @@ fn main() -> anyhow::Result<()> {
161
163
output_stream. play ( ) ?;
162
164
163
165
// Run for 3 seconds before closing.
164
- println ! ( "Playing for 3 seconds... " ) ;
165
- std:: thread:: sleep ( std:: time:: Duration :: from_secs ( 3 ) ) ;
166
+ println ! ( "Playing for 10 seconds... " ) ;
167
+ std:: thread:: sleep ( std:: time:: Duration :: from_secs ( 10 ) ) ;
166
168
drop ( input_stream) ;
167
169
drop ( output_stream) ;
168
170
println ! ( "Done!" ) ;
0 commit comments