Skip to content

Commit c61d92e

Browse files
authored
Merge pull request #3769 from cmitu/audiosettings-pw
audiosettings: add PipeWire support
2 parents 10a90d3 + 3fd5a3c commit c61d92e

File tree

1 file changed

+48
-22
lines changed

1 file changed

+48
-22
lines changed

scriptmodules/supplementary/audiosettings.sh

Lines changed: 48 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ function gui_audiosettings() {
3030
# The list of ALSA cards/devices depends on the 'snd-bcm2385' module parameter 'enable_compat_alsa'
3131
# * enable_compat_alsa: true - single soundcard, output is routed based on the `numid` control
3232
# * enable_compat_alsa: false - one soundcard per output type (HDMI/Headphones)
33-
# If PulseAudio is enabled, then try to configure it and leave ALSA alone
34-
if _pa_cmd_audiosettings systemctl -q --user is-enabled pulseaudio.socket; then
33+
# When PulseAudio/PipeWire is enabled, try to configure it and leave ALSA alone
34+
if _pa_cmd_audiosettings systemctl -q --user is-enabled {pulseaudio,pipewire-pulse}.socket; then
3535
_pulseaudio_audiosettings
3636
elif aplay -l | grep -q "bcm2835 ALSA"; then
3737
_bcm2835_alsa_compat_audiosettings
@@ -76,9 +76,13 @@ function _bcm2835_alsa_compat_audiosettings() {
7676
M "Mixer - adjust output volume"
7777
R "Reset to default"
7878
)
79-
# If PulseAudio is installed, add an option to enable it
80-
hasPackage "pulseaudio" && options+=(P "Enable PulseAudio")
81-
79+
# If PulseAudio (PipeWire) is installed, add an option to enable it
80+
local sound_server="PulseAudio"
81+
if hasPackage "wireplumber"; then
82+
options+=(P "Enable PipeWire")
83+
else
84+
hasPackage "pulseaudio" && options+=(P "Enable PulseAudio")
85+
fi
8286
choice=$("${cmd[@]}" "${options[@]}" 2>&1 >/dev/tty)
8387
if [[ -n "$choice" ]]; then
8488
case "$choice" in
@@ -110,8 +114,8 @@ function _bcm2835_alsa_compat_audiosettings() {
110114
_reset_alsa_audiosettings
111115
;;
112116
P)
113-
_toggle_pulseaudio_audiosettings "on"
114-
printMsgs "dialog" "PulseAudio enabled"
117+
_toggle_${sound_server,,} audiosettings "on"
118+
printMsgs "dialog" "${sound_server} enabled"
115119
;;
116120
esac
117121
fi
@@ -134,8 +138,14 @@ function _bcm2835_alsa_internal_audiosettings() {
134138
R "Reset to default"
135139
)
136140

137-
# If PulseAudio is installed, add an option to enable it
138-
hasPackage "pulseaudio" && options+=(P "Enable PulseAudio")
141+
# If PulseAudio (PipeWire) is installed, add an option to enable it
142+
local sound_server="PulseAudio"
143+
if hasPackage "wireplumber"; then
144+
options+=(P "Enable PipeWire")
145+
sound_server="PipeWire"
146+
else
147+
hasPackage "pulseaudio" && options+=(P "Enable PulseAudio")
148+
fi
139149

140150
choice=$("${cmd[@]}" "${options[@]}" 2>&1 >/dev/tty)
141151
if [[ -n "$choice" ]]; then
@@ -152,8 +162,8 @@ function _bcm2835_alsa_internal_audiosettings() {
152162
_reset_alsa_audiosettings
153163
;;
154164
P)
155-
_toggle_pulseaudio_audiosettings "on"
156-
printMsgs "dialog" "PulseAudio enabled"
165+
_toggle_${sound_server,,}_audiosettings "on"
166+
printMsgs "dialog" "$sound_server enabled"
157167
;;
158168
esac
159169
fi
@@ -231,29 +241,31 @@ EOF
231241
}
232242

233243
function _pulseaudio_audiosettings() {
234-
local cmd=(dialog --backtitle "$__backtitle" --menu "Set audio output (PulseAudio)" 22 86 16)
235244
local options=()
236245
local sink_index
237246
local sink_label
247+
local sound_server="PulseAudio"
238248

239-
# Check if PulseAudio is running, otherwise 'pacmd' will not work
240-
if ! _pa_cmd_audiosettings pacmd stat>/dev/null; then
241-
printMsgs "dialog" "PulseAudio is enabled, but not running\nAudio settings cannot be set right now"
249+
# Check if PulseAudio is running, otherwise 'pactl' will not work
250+
if ! _pa_cmd_audiosettings pactl info >/dev/null; then
251+
printMsgs "dialog" "PulseAudio is present, but not running.\nAudio settings cannot be set right now."
242252
return
243253
fi
244254
while read sink_index sink_label; do
245255
options+=("$sink_index" "$sink_label")
246-
done < <(_pa_cmd_audiosettings pacmd list-sinks | \
247-
awk -F [:=] '/index/ { idx=$2;
256+
done < <(_pa_cmd_audiosettings pactl list sinks | \
257+
awk -F [:=] 'BEGIN {idx=0}; /Name:/ {
248258
do {getline} while($0 !~ "alsa.name");
249259
gsub(/"|bcm2835[^a-zA-Z]+/, "", $2);
250-
print idx,$2 }'
260+
print idx,$2;
261+
idx++}'
251262
)
252-
263+
_pa_cmd_audiosettings pactl info | grep -i pipewire >/dev/null && sound_server="PipeWire"
264+
local cmd=(dialog --backtitle "$__backtitle" --menu "Set audio output ($sound_server)" 22 86 16)
253265
options+=(
254266
M "Mixer - adjust output volume"
255267
R "Reset to default"
256-
P "Disable PulseAudio"
268+
P "Disable $sound_server"
257269
)
258270
choice=$("${cmd[@]}" "${options[@]}" 2>&1 >/dev/tty)
259271
if [[ -n "$choice" ]]; then
@@ -275,8 +287,8 @@ function _pulseaudio_audiosettings() {
275287
printMsgs "dialog" "Audio settings reset to defaults"
276288
;;
277289
P)
278-
_toggle_pulseaudio_audiosettings "off"
279-
printMsgs "dialog" "PulseAudio disabled"
290+
_toggle_${sound_server,,}_audiosettings "off"
291+
printMsgs "dialog" "${sound_server} disabled"
280292
;;
281293
esac
282294
fi
@@ -296,6 +308,20 @@ function _toggle_pulseaudio_audiosettings() {
296308
fi
297309
}
298310

311+
function _toggle_pipewire_audiosettings() {
312+
local state=$1
313+
314+
if [[ "$state" == "on" ]]; then
315+
_pa_cmd_audiosettings systemctl --user unmask pipewire-pulse.socket pipewire.socket
316+
_pa_cmd_audiosettings systemctl --user start pipewire.service pipewire-pulse.service wireplumber.service
317+
fi
318+
319+
if [[ "$state" == "off" ]]; then
320+
_pa_cmd_audiosettings systemctl --user mask pipewire-pulse.socket pipewire.socket
321+
_pa_cmd_audiosettings systemctl --user stop pipewire.service pipewire-pulse.service wireplumber.service
322+
fi
323+
}
324+
299325
# Run PulseAudio commands as the calling user
300326
function _pa_cmd_audiosettings() {
301327
[[ -n "$@" ]] && sudo -u "$user" XDG_RUNTIME_DIR=/run/user/$SUDO_UID "$@" 2>/dev/null

0 commit comments

Comments
 (0)