11#! /bin/bash
22# ####### INSTALL SPOTIFY CONNECT ################
33# Install required packages
4- echo " Step 1: Installing required packages..."
4+ echo " Installing required packages..."
55sudo apt install -y apt-transport-https curl
66
77# Add GPG key and repository for raspotify
8- echo " Step 2: Adding GPG key and repository for raspotify..."
8+ echo " Adding GPG key and repository for raspotify..."
99curl -sSL https://dtcooper.github.io/raspotify/key.asc | sudo tee /usr/share/keyrings/raspotify-archive-keyrings.asc > /dev/null
1010echo ' deb [signed-by=/usr/share/keyrings/raspotify-archive-keyrings.asc] https://dtcooper.github.io/raspotify raspotify main' | sudo tee /etc/apt/sources.list.d/raspotify.list
1111
1212# Install raspotify package
13- echo " Step 3: Installing raspotify package..."
13+ echo " Installing raspotify package..."
1414sudo apt update
1515sudo apt install -y raspotify
1616
@@ -22,8 +22,60 @@ sudo apt install -y raspotify
2222# echo "Inside the configuration file, you can modify settings such as the device name and bitrate."
2323# echo "Remember to save your changes CTRL + X, Y, ENTER and restart the raspotify service after making any modifications."
2424
25+ # Script to set a USB DAC as the default audio device on Raspberry Pi
26+
27+ echo " Listing all available audio devices..."
28+ device_list=$( aplay -l | grep ' ^card' )
29+
30+ # Check if no devices were found
31+ if [ -z " $device_list " ]; then
32+ echo " No audio devices found. Please ensure your audio device is connected."
33+ exit 1
34+ fi
35+
36+ echo " $device_list "
37+ echo
38+
39+ # Create an array to store device card numbers
40+ declare -a card_numbers
41+ index=0
42+
43+ # Extract card numbers and populate the array
44+ while read -r line; do
45+ card_number=$( echo $line | awk ' {print $2}' | tr -d :)
46+ card_numbers[$index ]=$card_number
47+ let index++
48+ done <<< " $device_list"
49+
50+ # Display options to the user
51+ echo " Select the output device:"
52+ for i in " ${! card_numbers[@]} " ; do
53+ echo " [$i ] Card number: ${card_numbers[$i]} "
54+ done
55+
56+ # Ask the user to choose a device
57+ read -p " Enter your choice (0-${# card_numbers[@]} ): " choice
58+
59+ # Validate user input
60+ if ! [[ " $choice " =~ ^[0-9]+$ ]] || [ " $choice " -ge " ${# card_numbers[@]} " ]; then
61+ echo " Invalid selection. Exiting."
62+ exit 1
63+ fi
64+
65+ selected_card=${card_numbers[$choice]}
66+
67+ # Backup the original alsa.conf file
68+ sudo cp /usr/share/alsa/alsa.conf /usr/share/alsa/alsa.conf.backup
69+
70+ # Update the alsa.conf file with the selected card number
71+ sudo sed -i " s/defaults.ctl.card 0/defaults.ctl.card $selected_card /" /usr/share/alsa/alsa.conf
72+ sudo sed -i " s/defaults.pcm.card 0/defaults.pcm.card $selected_card /" /usr/share/alsa/alsa.conf
73+
74+ echo " The default audio device is set to card number $selected_card ."
75+
76+
2577# Restart the raspotify service
26- echo " Step 5: Restarting the raspotify service..."
78+ echo " Restarting the raspotify service..."
2779sudo systemctl restart raspotify
2880
2981echo " Raspotify setup completed. You can now connect to your Raspberry Pi via Spotify Connect."
0 commit comments