Skip to content

Commit adffb9f

Browse files
committed
audiosettings: add helper service to set ALSA defaults on RPI
Added a helper method that installs a `systemd` service meant to automatically configure ALSA with the HDMI 0 port when using the ARM `vc4hdmi` RPI driver. The service will run once then disable itself. Since the default ALSA configuration on RaspiOS goes through the analog output (Heaphones) when the `vc4-kms-v3d` overlay is present, if we want to configure the HDMI output by default there needs to be an additional configuration. The additional configuration needs also to be applied when only one audio output is present, since the ALSA config is a bit more involved. There are a couple of reasons for which is a service: - the `vc4hdmi` device index can be different depending on the Pi model. Pi0(w)/Pi400 has no analog output by default and CM models may not have it also) - the card name can also be different (Pi4 with 2 devices vs Pi3/Pi2/Pi1 with 1 device) Note that the service will configure the 1st HDMI port/device, on Pi models with multiple HDMI ports (Pi4/Pi 400/Pi5/CM4) the user will still need to use the Audio settings dialog to change it if they want to. There may be a way to detect which HDMI port is active and has audio, but that's not implemented.
1 parent 083362d commit adffb9f

File tree

2 files changed

+91
-0
lines changed

2 files changed

+91
-0
lines changed

Diff for: scriptmodules/supplementary/audiosettings.sh

+30
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,36 @@ function _bcm2835_alsa_internal_audiosettings() {
159159
fi
160160
}
161161

162+
# Adds a service to generate the ALSA configuration on HDMI 0 for a vc4-hdmi device (RPI configured with 'vc4-kms-v3d').
163+
# The service will disable itself afterwards, but can be enabled if re-running the configuration is desired.
164+
# If the RetroPie ALSA configuration is found, the servie will not overwrite it.
165+
function alsa_defaults_service_audiosettings() {
166+
local service="retropie-alsa-config.service"
167+
168+
mkdir -p "$md_inst"
169+
cp -f "$md_data/alsa-defaults.sh" "$md_inst"
170+
cat << EOF > "/usr/lib/systemd/system/$service"
171+
[Unit]
172+
Description=Configure ALSA default card on HDMI 0
173+
ConditionPathExists=!/etc/alsa/conf.d/99-retropie.conf
174+
Before=getty.target
175+
After=sound.target
176+
177+
[Service]
178+
Type=oneshot
179+
TimeoutSec=120
180+
ExecStart=$md_inst/alsa-defaults.sh
181+
ExecStartPost=/usr/bin/systemctl disable $service
182+
183+
[Install]
184+
WantedBy=multi-user.target
185+
EOF
186+
# remove the RetroPie ALSA config file so it's created on next boot by the service
187+
rm -f /etc/alsa/conf.d/99-retropie.conf
188+
systemctl -q enable $service
189+
printMsgs "Installed the ALSA configuration service ($service)"
190+
191+
}
162192
# configure the default ALSA soundcard based on chosen card index and type
163193
function _asoundrc_save_audiosettings() {
164194
[[ -z "$1" ]] && return
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#!/bin/sh
2+
3+
# Sets the default ALSA audio card to the 1st vc4-hdmi device found
4+
# The ALSA configuration is written to '/etc/alsa/conf.d/99-retropie.conf'
5+
6+
CONF_FILE=/etc/alsa/conf.d/99-retropie.conf
7+
8+
# test if we don't already have the audio configured
9+
if [ -f "$CONF_FILE" ]; then
10+
echo RetroPie audio card configuration already present, skipping configuration
11+
exit 0
12+
fi
13+
14+
# test if we have any `vc4-hdmi` cards present, otherwise exit
15+
card_index="$(grep vc4hdmi /proc/asound/cards | cut -d' ' -f 2 | head -n1)"
16+
card_name="$(cat /proc/asound/card"${card_index}"/id)"
17+
if [ -z "$card_index" ]; then
18+
echo No vc4-hdmi audio devices present, skipping configuration
19+
fi
20+
21+
echo "Found a vc4-hdmi sound card on slot $card_index, configuring it"
22+
23+
tmpfile="$(mktemp)"
24+
cat << EOF > "$tmpfile"
25+
pcm.hdmi${card_index} {
26+
type asym
27+
playback.pcm {
28+
type plug
29+
slave.pcm "hdmi:${card_name}"
30+
}
31+
}
32+
ctl.!default {
33+
type hw
34+
card $card_index
35+
}
36+
pcm.softvolume {
37+
type softvol
38+
slave.pcm "hdmi${card_index}"
39+
control.name "HDMI Playback Volume"
40+
control.card ${card_index}
41+
}
42+
43+
pcm.softmute {
44+
type softvol
45+
slave.pcm "softvolume"
46+
control.name "HDMI Playback Switch"
47+
control.card ${card_index}
48+
resolution 2
49+
}
50+
51+
pcm.!default {
52+
type plug
53+
slave.pcm "softmute"
54+
}
55+
EOF
56+
mv -f "$tmpfile" "$CONF_FILE" || {
57+
echo "Failed to save configuration file $CONF_FILE!"
58+
exit 1
59+
}
60+
chmod 0644 "$CONF_FILE"
61+
echo "ALSA configuration saved in $CONF_FILE"

0 commit comments

Comments
 (0)