diff --git a/scriptmodules/supplementary/audiosettings.sh b/scriptmodules/supplementary/audiosettings.sh index ada47c8b8c..b001542deb 100644 --- a/scriptmodules/supplementary/audiosettings.sh +++ b/scriptmodules/supplementary/audiosettings.sh @@ -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 diff --git a/scriptmodules/supplementary/audiosettings/alsa-defaults.sh b/scriptmodules/supplementary/audiosettings/alsa-defaults.sh new file mode 100755 index 0000000000..009eb05bc9 --- /dev/null +++ b/scriptmodules/supplementary/audiosettings/alsa-defaults.sh @@ -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"