forked from bobrathbone/piradio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathselect_audio.sh
229 lines (191 loc) · 5.8 KB
/
select_audio.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#!/bin/bash
# Raspberry Pi Internet Radio
# $Id: select_audio.sh,v 1.13 2016/08/06 07:24:51 bob Exp $
#
# Author : Bob Rathbone
# Site : http://www.bobrathbone.com
#
# This program is used to select the sound card to be used.
# It also configures /etc/mpd.conf
#
# License: GNU V3, See https://www.gnu.org/copyleft/gpl.html
#
# Disclaimer: Software is provided as is and absolutly no warranties are implied or given.
# The authors shall not be liable for any loss or damage however caused.
#
# This program uses whiptail. Set putty terminal to use UTF-8 charachter set
# for best results
BOOTCONFIG=/boot/config.txt
MPDCONFIG=/etc/mpd.conf
DEFAULTRCS=/etc/default/rcS
# Audio types
JACK=1 # Audio Jack or Sound Cards
HDMI=2 # HDMI
TYPE=${JACK}
# dtoverlay parameter in /etc/config.txt
DTOVERLAY=""
# Device and Name
DEVICE="0,0"
NAME="Onboard jack"
MIXER="hardware"
# Wheezy not supported
cat /etc/os-release | grep -i wheezy 2>&1 >/dev/null
if [[ $? == 0 ]]; then # Don't seperate from above
echo "This prograqm is not supported on Debian Wheezy!"
echo "Exiting program."
exit 1
fi
selection=1
while [ $selection != 0 ]
do
ans=$(whiptail --title "Select audio output" --menu "Choose your option" 18 75 10 \
"1" "On-board audio output jack" \
"2" "HDMI output" \
"3" "USB DAC" \
"4" "HiFiBerry DAC" \
"5" "HiFiBerry DAC plus" \
"6" "HiFiBerry DAC Digi" \
"7" "HiFiBerry Amp" \
"8" "IQAudio DAC" \
"9" "IQAudio DAC plus and Digi/AMP " \
"10" "Manually configure" 3>&1 1>&2 2>&3)
exitstatus=$?
if [[ $exitstatus != 0 ]]; then
exit 0
fi
if [[ ${ans} == '1' ]]; then
DESC="On-board audio output Jack"
elif [[ ${ans} == '2' ]]; then
DESC="HDMI output"
TYPE=${HDMI}
elif [[ ${ans} == '3' ]]; then
DESC="USB DAC"
NAME=${DESC}
DEVICE="1,0"
MIXER="software"
elif [[ ${ans} == '4' ]]; then
DESC="HiFiBerry DAC or Light"
NAME=${DESC}
DTOVERLAY="hifiberry-dac"
DEVICE="1,0"
MIXER="software"
elif [[ ${ans} == '5' ]]; then
DESC="HiFiBerry DAC Plus"
NAME=${DESC}
DTOVERLAY="hifiberry-dacplus"
DEVICE="1,0"
MIXER="software"
elif [[ ${ans} == '6' ]]; then
DESC="HiFiBerry Digi"
NAME=${DESC}
DTOVERLAY="hifiberry-digi"
DEVICE="1,0"
MIXER="software"
elif [[ ${ans} == '7' ]]; then
DESC="HiFiBerry Amp"
NAME=${DESC}
DTOVERLAY="hifiberry-amp"
DEVICE="1,0"
MIXER="software"
elif [[ ${ans} == '8' ]]; then
DESC="IQAudio DAC"
NAME=${DESC}
DTOVERLAY="iqaudio-dacplus"
DEVICE="1,0"
MIXER="software"
elif [[ ${ans} == '9' ]]; then
DESC="IQAudio DAC plus or DigiAMP"
NAME="IQAudio DAC+"
DTOVERLAY="iqaudio-dacplus"
DEVICE="1,0"
MIXER="software"
elif [[ ${ans} == '10' ]]; then
DESC="Manual configuration"
fi
whiptail --title "$DESC selected" --yesno "Is this correct?" 10 60
selection=$?
done
echo "${DESC} selected"
# Install sysvinit-init if not already installed
PKG="sysvinit-core"
dpkg-query --status ${PKG} 2>&1 >/dev/null
if [[ $? != 0 ]]; then # Don't seperate from above
echo "Installing ${PKG} package"
sudo apt-get --yes install ${PKG}
fi
# Install alsa-utils if not already installed
PKG="alsa-utils"
dpkg-query --status ${PKG} 2>&1 >/dev/null
if [[ $? != 0 ]]; then # Don't seperate from above
echo "Installing ${PKG} package"
sudo apt-get --yes install ${PKG}
fi
# Remove pulseaudio package
PKG="pulseaudio"
if [[ -x /usr/bin/${PKG} ]]; then # Don't seperate from above
echo "Removing ${PKG} package"
sudo apt-get --yes remove ${PKG} 2>&1 >/dev/null
fi
# Select HDMI or audio jack/DACs Alsa output
if [[ ${TYPE} == ${HDMI} ]]; then
echo "Configuring HDMI as output"
sudo amixer cset numid=3 2 2>&1 >/dev/null
else
sudo amixer cset numid=3 1 2>&1 >/dev/null
fi
# Configure the Alsa sound mixer for maximum volume
amixer cset numid=1 100% 2>&1 >/dev/null
if [[ $? != 0 ]]; then # Don't seperate from above
echo "Failed to configure Alsa mixer"
fi
# Configure the Alsa sound mixer on second card for 80 volume
aplay -l | grep "card 1" 2>&1 >/dev/null
if [[ $? == 0 ]]; then # Don't seperate from above
amixer -c1 cset numid=1 80% 2>&1 >/dev/null
if [[ $? != 0 ]]; then # Don't seperate from above
echo "Failed to configure second Alsa mixer"
fi
fi
# Save original configuration
if [[ ! -f ${MPDCONFIG}.orig ]]; then
sudo cp -f -p ${MPDCONFIG} ${MPDCONFIG}.orig
fi
# Configure name device and mixer type in mpd.conf
sudo cp -f -p ${MPDCONFIG}.orig ${MPDCONFIG}
sudo sed -i -e "0,/^\sname/{s/\sname.*/\tname\t\t\"${NAME}\"/}" ${MPDCONFIG}
sudo sed -i -e "0,/device/{s/.*device.*/\tdevice\t\t\"hw:${DEVICE}\"/}" ${MPDCONFIG}
sudo sed -i -e "0,/mixer_type/{s/.*mixer_type.*/\tmixer_type\t\"${MIXER}\"/}" ${MPDCONFIG}
# Bind address to any
sudo sed -i -e "0,/bind_to_address/{s/.*bind_to_address.*/bind_to_address\t\t\"any\"/}" ${MPDCONFIG}
##sudo sed -i 0,'/#bind_to_address/{s/localhost/any/}' ${MPDCONFIG}
##sudo sed -i 0,'/#bind_to_address/{s/#bind_to_address/bind_to_address/}' ${MPDCONFIG}
# Force file checks on reboot
if [[ ! -f ${DEFAULTRCS}.orig ]]; then
sudo cp -f -p ${DEFAULTRCS} ${DEFAULTRCS}.orig
sudo sed -i -e 's/\#FSCKFIX=no/FSCKFIX=yes/' ${DEFAULTRCS}
fi
# Delete existing dtoverlays
sudo sed -i '/dtoverlay=iqaudio/d' ${BOOTCONFIG}
sudo sed -i '/dtoverlay=hifiberry/d' ${BOOTCONFIG}
# Add dtoverlay for sound cards
if [[ ${DTOVERLAY} != "" ]]; then
TEMP="/tmp/awk$$"
sudo awk -v s="dtoverlay=${DTOVERLAY}" '/^dtoverlay/{f=1;$0=s}7;END{if(!f)print s}' ${BOOTCONFIG} > ${TEMP}
sudo cp -f ${TEMP} ${BOOTCONFIG}
sudo rm -f ${TEMP}
fi
# Remove redundant packages
echo "Removing redundant packages"
sudo apt-get -y autoremove 2>&1 >/dev/null
echo "${DESC} configured"
sleep 2
# Reboot dialogue
if (whiptail --title "Audio selection" --yesno "Reboot Raspberry Pi (Recommended)" 10 60) then
echo "Rebooting Raspberry Pi"
sudo reboot
else
echo
echo "You chose not to reboot!"
echo "Changes made will not become active until the next reboot"
fi
# End of script