Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

audiosettings: add helper service to set ALSA defaults on RPI #3759

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions scriptmodules/supplementary/audiosettings.sh
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,36 @@ function _bcm2835_alsa_internal_audiosettings() {
fi
}

# Adds a service to generate the ALSA configuration on HDMI 0 for a vc4-hdmi device (RPI configured with 'vc4-kms-v3d').
# The service will disable itself afterwards, but can be enabled if re-running the configuration is desired.
# If the RetroPie ALSA configuration is found, the service will not overwrite it.
function alsa_defaults_service_audiosettings() {
local service="retropie-alsa-config.service"

mkdir -p "$md_inst"
cp -f "$md_data/alsa-defaults.sh" "$md_inst"
cat << EOF > "/usr/lib/systemd/system/$service"
[Unit]
Description=Configure ALSA default card on HDMI 0
ConditionPathExists=!/etc/alsa/conf.d/99-retropie.conf
Before=getty.target
After=sound.target

[Service]
Type=oneshot
TimeoutSec=120
ExecStart=$md_inst/alsa-defaults.sh
ExecStartPost=/usr/bin/systemctl disable $service

[Install]
WantedBy=multi-user.target
EOF
# remove the RetroPie ALSA config file so it's created on next boot by the service
rm -f /etc/alsa/conf.d/99-retropie.conf
systemctl -q enable $service
printMsgs "console" "Installed the ALSA configuration service ($service)"

}
# configure the default ALSA soundcard based on chosen card index and type
function _asoundrc_save_audiosettings() {
[[ -z "$1" ]] && return
Expand Down
61 changes: 61 additions & 0 deletions scriptmodules/supplementary/audiosettings/alsa-defaults.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#!/bin/sh

# Sets the default ALSA audio card to the 1st vc4-hdmi device found
# The ALSA configuration is written to '/etc/alsa/conf.d/99-retropie.conf'

CONF_FILE=/etc/alsa/conf.d/99-retropie.conf

# test if we don't already have the audio configured
if [ -f "$CONF_FILE" ]; then
echo RetroPie audio card configuration already present, skipping configuration
exit 0
fi

# test if we have any `vc4-hdmi` cards present, otherwise exit
card_index="$(grep vc4hdmi /proc/asound/cards | cut -d' ' -f 2 | head -n1)"
card_name="$(cat /proc/asound/card"${card_index}"/id)"
if [ -z "$card_index" ]; then
echo No vc4-hdmi audio devices present, skipping configuration
fi

echo "Found a vc4-hdmi sound card on slot $card_index, configuring it"

tmpfile="$(mktemp)"
cat << EOF > "$tmpfile"
pcm.hdmi${card_index} {
type asym
playback.pcm {
type plug
slave.pcm "hdmi:${card_name}"
}
}
ctl.!default {
type hw
card $card_index
}
pcm.softvolume {
type softvol
slave.pcm "hdmi${card_index}"
control.name "HDMI Playback Volume"
control.card ${card_index}
}

pcm.softmute {
type softvol
slave.pcm "softvolume"
control.name "HDMI Playback Switch"
control.card ${card_index}
resolution 2
}

pcm.!default {
type plug
slave.pcm "softmute"
}
EOF
mv -f "$tmpfile" "$CONF_FILE" || {
echo "Failed to save configuration file $CONF_FILE!"
exit 1
}
chmod 0644 "$CONF_FILE"
echo "ALSA configuration saved in $CONF_FILE"