Skip to content

Commit

Permalink
Make script more robust based on PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
pabera committed Dec 25, 2023
1 parent 701f8e3 commit a62c036
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 10 deletions.
2 changes: 1 addition & 1 deletion documentation/builders/components/soundcards/hifiberry.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
Run the following command to install any HiFiBerry board. Make sure you reboot your device afterwards.

```
$ cd; cd ~/RPi-Jukebox-RFID/installation/components && chmod +x setup_hifiberry.sh && sudo ./setup_hifiberry.sh
$ cd; cd ~/RPi-Jukebox-RFID/installation/components && chmod +x setup_hifiberry.sh && sudo -E ./setup_hifiberry.sh
```

Optional: You can register your sound card with ALSA. Define the following variable before running the script: `CONFIGURE_ALSA=true`
Expand Down
36 changes: 27 additions & 9 deletions installation/components/setup_hifiberry.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ check_existing_hifiberry() {
read -p "Do you want to proceed with a new configuration? This will remove the existing one. (y/n): " yn
case $yn in
[Yy]* )
remove_existing_hifiberry
return 0;;
[Nn]* )
echo "Exiting without making changes.";
Expand Down Expand Up @@ -70,6 +69,8 @@ case $choice in
*) echo "Invalid selection. Exiting."; exit 1;;
esac

remove_existing_hifiberry

echo "Disabling onboard sound..."
sed -i '/dtparam=audio=on/c\dtparam=audio=off' /boot/config.txt

Expand All @@ -83,18 +84,34 @@ if grep -qx 'dtoverlay=vc4-kms-v3d' /boot/config.txt; then
sed -i '/dtoverlay=vc4-kms-v3d/c\dtoverlay=vc4-kms-v3d,noaudio' /boot/config.txt
fi

# Check for CONFIGURE_ALSA environment variable
if [ "${CONFIGURE_ALSA}" == "true" ]; then
if [ -f /etc/asound.conf ]; then
echo "Backing up existing asound.conf..."
cp /etc/asound.conf "/etc/asound.conf.backup.$(date +%Y%m%d%H%M%S)"
fi
if [ -z "${CONFIGURE_ALSA}" ]; then
echo "CONFIGURE_ALSA not set. Skipping configuration of sound settings in asound.conf."
else
if [ "${CONFIGURE_ALSA}" == "true" ]; then
if [ -f /etc/asound.conf ]; then
echo "Backing up existing asound.conf..."
cp /etc/asound.conf "/etc/asound.conf.backup.$(date +%Y%m%d%H%M%S)"
fi

aplay -l

echo "Configuring sound settings in asound.conf..."
# Prompt the user for the card number
while true; do
read -p "Which card number is your HifiBerry sound card? " card_number

# Check if the input is a number
if [[ "$card_number" =~ ^[0-9]+$ ]]; then
break
else
echo "Please enter a valid number."
fi
done

echo "Configuring sound settings in asound.conf..."
cat > /etc/asound.conf << EOF
pcm.hifiberry {
type softvol
slave.pcm "plughw:0"
slave.pcm "plughw:$card_number"
control.name "HifiBerry"
control.card 0
}
Expand All @@ -104,6 +121,7 @@ pcm.!default {
slave.pcm "hifiberry"
}
EOF
fi
fi

echo "Configuration complete. Please restart your device."

0 comments on commit a62c036

Please sign in to comment.