Skip to content

Commit c2012e4

Browse files
authored
Merge pull request #1 from mbodmer/pipewire-host
pipewire: update beep example to support pipewire
2 parents 11f4737 + 6e07ed0 commit c2012e4

File tree

1 file changed

+24
-25
lines changed

1 file changed

+24
-25
lines changed

examples/beep.rs

+24-25
Original file line numberDiff line numberDiff line change
@@ -7,40 +7,27 @@ use cpal::traits::{DeviceTrait, HostTrait, StreamTrait};
77

88
#[derive(Debug)]
99
struct Opt {
10-
#[cfg(all(
11-
any(target_os = "linux", target_os = "dragonfly", target_os = "freebsd"),
12-
feature = "jack"
13-
))]
10+
#[allow(dead_code)]
1411
jack: bool,
15-
12+
#[allow(dead_code)]
13+
pipewire: bool,
1614
device: String,
1715
}
1816

1917
impl Opt {
2018
fn from_args() -> Self {
2119
let app = clap::Command::new("beep").arg(arg!([DEVICE] "The audio device to use"));
22-
#[cfg(all(
23-
any(target_os = "linux", target_os = "dragonfly", target_os = "freebsd"),
24-
feature = "jack"
25-
))]
2620
let app = app.arg(arg!(-j --jack "Use the JACK host"));
21+
let app = app.arg(arg!(-p --pipewire "Use the PipeWire host"));
22+
2723
let matches = app.get_matches();
2824
let device = matches.value_of("DEVICE").unwrap_or("default").to_string();
2925

30-
#[cfg(all(
31-
any(target_os = "linux", target_os = "dragonfly", target_os = "freebsd"),
32-
feature = "jack"
33-
))]
34-
return Opt {
26+
Opt {
3527
jack: matches.is_present("jack"),
28+
pipewire: matches.is_present("pipewire"),
3629
device,
37-
};
38-
39-
#[cfg(any(
40-
not(any(target_os = "linux", target_os = "dragonfly", target_os = "freebsd")),
41-
not(feature = "jack")
42-
))]
43-
Opt { device }
30+
}
4431
}
4532
}
4633

@@ -65,10 +52,22 @@ fn main() -> anyhow::Result<()> {
6552
cpal::default_host()
6653
};
6754

68-
#[cfg(any(
69-
not(any(target_os = "linux", target_os = "dragonfly", target_os = "freebsd")),
70-
not(feature = "jack")
71-
))]
55+
// Conditionally compile with PipeWire if the feature is specified.
56+
#[cfg(all(target_os = "linux", feature = "pipewire"))]
57+
// Manually check for flags. Can be passed through cargo with -- e.g.
58+
// cargo run --release --example beep --features pipewire -- --pipewire
59+
let host = if opt.pipewire {
60+
cpal::host_from_id(cpal::available_hosts()
61+
.into_iter()
62+
.find(|id| *id == cpal::HostId::PipeWire)
63+
.expect(
64+
"make sure --features pipewire is specified. only works on OSes where PipeWire is available",
65+
)).expect("PipeWire host unavailable")
66+
} else {
67+
cpal::default_host()
68+
};
69+
70+
#[cfg(not(any(feature = "jack", feature = "pipewire")))]
7271
let host = cpal::default_host();
7372

7473
let device = if opt.device == "default" {

0 commit comments

Comments
 (0)