Skip to content

Commit 739b623

Browse files
mbodmerImUrX
authored andcommitted
pipewire: update beep example to support pipewire
Signed-off-by: mbodmer <[email protected]>
1 parent 483e344 commit 739b623

File tree

1 file changed

+38
-3
lines changed

1 file changed

+38
-3
lines changed

examples/beep.rs

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,26 @@ struct Opt {
2525
#[arg(short, long)]
2626
#[allow(dead_code)]
2727
jack: bool,
28+
/// Use the Pipewire host
29+
#[cfg(all(
30+
any(
31+
target_os = "linux",
32+
target_os = "dragonfly",
33+
target_os = "freebsd",
34+
target_os = "netbsd"
35+
),
36+
feature = "pipewire"
37+
))]
38+
#[allow(dead_code)]
39+
pipewire: bool,
2840
}
2941

3042
fn main() -> anyhow::Result<()> {
3143
let opt = Opt::parse();
3244

3345
// Conditionally compile with jack if the feature is specified.
46+
// Manually check for flags. Can be passed through cargo with -- e.g.
47+
// cargo run --release --example beep --features jack -- --jack
3448
#[cfg(all(
3549
any(
3650
target_os = "linux",
@@ -40,8 +54,6 @@ fn main() -> anyhow::Result<()> {
4054
),
4155
feature = "jack"
4256
))]
43-
// Manually check for flags. Can be passed through cargo with -- e.g.
44-
// cargo run --release --example beep --features jack -- --jack
4557
let host = if opt.jack {
4658
cpal::host_from_id(cpal::available_hosts()
4759
.into_iter()
@@ -53,14 +65,37 @@ fn main() -> anyhow::Result<()> {
5365
cpal::default_host()
5466
};
5567

68+
// Conditionally compile with PipeWire if the feature is specified.
69+
#[cfg(all(
70+
any(
71+
target_os = "linux",
72+
target_os = "dragonfly",
73+
target_os = "freebsd",
74+
target_os = "netbsd"
75+
),
76+
feature = "pipewire"
77+
))]
78+
// Manually check for flags. Can be passed through cargo with -- e.g.
79+
// cargo run --release --example beep --features pipewire -- --pipewire
80+
let host = if opt.pipewire {
81+
cpal::host_from_id(cpal::available_hosts()
82+
.into_iter()
83+
.find(|id| *id == cpal::HostId::PipeWire)
84+
.expect(
85+
"make sure --features pipewire is specified. only works on OSes where PipeWire is available",
86+
)).expect("PipeWire host unavailable")
87+
} else {
88+
cpal::default_host()
89+
};
90+
5691
#[cfg(any(
5792
not(any(
5893
target_os = "linux",
5994
target_os = "dragonfly",
6095
target_os = "freebsd",
6196
target_os = "netbsd"
6297
)),
63-
not(feature = "jack")
98+
not(any(feature = "jack", feature = "pipewire"))
6499
))]
65100
let host = cpal::default_host();
66101

0 commit comments

Comments
 (0)