-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathset_asound_conf
executable file
·353 lines (340 loc) · 9.28 KB
/
set_asound_conf
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
#!/bin/bash
MM="/usr/local/MirrorCommand"
ASOUND_TMPL="${MM}/etc/asound.conf.tmpl"
ASOUND="/etc/asound.conf"
CARDS="/proc/asound/cards"
OUTSTR="__AUDIO_OUT__"
INPSTR="__AUDIO_IN__"
INPUT=
OUTPUT=
# Change these if you are not using a USB microphone or audio
#
# String to look for in /proc/asound/cards for audio output device
OUTGREP="USB-Audio"
# String to look for in /proc/asound/cards for audio input device
INGREP="USB-Audio"
usage() {
printf "\nUsage: set_asound_conf [-c] [-e] [-l] [-n] [-o outfile] [-q] [-r] [-t template] [-u]"
printf "\nWhere:"
printf "\n\t-c indicates perform a check and notify if update needed"
printf "\n\t-e indicates perform an update of /etc/asound.conf"
printf "\n\t-l indicates list audio input/output configured devices"
printf "\n\t-n indicates non-interactive execution (guess at settings)"
printf "\n\t-o 'outfile' specifies an alternate output file for generated asound.conf"
printf "\n\t-q indicates quiet execution"
printf "\n\t-r indicates restore original /etc/asound.conf"
printf "\n\t-t 'template' specifies an alternate asound.conf template to use"
printf "\n\t-u indicates display this usage message and exit"
printf "\n\nDefault generated output is the file /etc/asound.conf"
printf "\nDefault asound.conf template can be found in:"
printf "\n\t/usr/local/MirrorCommand/etc/asound.conf.tmpl\n\n"
exit 0
}
set_vol_card() {
VOL="/usr/local/bin/vol"
if [ "$1" ]
then
if [ -f ${VOL} ]
then
OUTCARD=$1
((OUTCARD--))
VOLCARD=`grep ^CARD= ${VOL} | awk -F '=' ' { print $2 } '`
[ ${VOLCARD} -eq ${OUTCARD} ] || {
[ "${QUT}" ] || {
echo "Updating ${VOL} with audio output card number"
}
cat ${VOL} | sed -e "s/CARD=${VOLCARD}/CARD=${OUTCARD}/" > /tmp/v$$
sudo cp /tmp/v$$ ${VOL}
rm -f /tmp/v$$
}
else
[ "${QUT}" ] || {
echo "/usr/local/bin/vol does not exist"
echo "Skipping vol card default setting"
}
fi
else
[ "${QUT}" ] || {
echo "No argument supplied to set_vol_card()"
echo "Skipping vol card default setting"
}
fi
}
UPD=
CHK=
LST=
REV=
QUT=
SEL=1
while getopts celno:qrt:u flag; do
case $flag in
c)
CHK=1
;;
e)
UPD=1
;;
l)
LST=1
;;
n)
SEL=
;;
o)
ASOUND="${OPTARG}"
;;
q)
QUT=1
;;
r)
REV=1
;;
t)
ASOUND_TMPL="${OPTARG}"
;;
u)
usage
;;
esac
done
shift $(( OPTIND - 1 ))
[ "$LST" ] && {
INP=
OUT=
grep card /etc/asound.conf | while read card
do
[ "$INP" ] || {
echo "Audio Output:"
aplay -l | grep "$card"
INP=1
echo ""
continue
}
[ "$OUT" ] || {
echo "Audio Input:"
arecord -l | grep "$card"
OUT=1
echo ""
continue
}
done
exit 0
}
[ -f ${ASOUND_TMPL} ] || {
[ "${QUT}" ] || {
echo "Cannot locate ${ASOUND_TMPL}. Exiting."
}
exit 0
}
if [ "$REV" ]
then
[ -f ${ASOUND}.orig ] && {
sudo cp ${ASOUND}.orig ${ASOUND}
sudo rm -f ${ASOUND}.orig
}
else
# Present a selection dialog to allow user to choose audio input and output
echo ""
device_options=()
while read entry
do
devnum=`echo ${entry} | awk ' { print $1 } '`
[ "${devnum}" -eq "${devnum}" ] 2> /dev/null || continue
device=`echo ${entry} | awk -F ':' ' { print $2 } '`
device=`echo ${device} | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//'`
device_options+=("${device}")
done < <(cat ${CARDS})
if [ "${SEL}" ]
then
device_options+=("Select For Me" "None" "Quit")
PS3="${BOLD}Please enter the device you wish to use for AUDIO OUTPUT (numeric or text): ${NORMAL}"
select opt in "${device_options[@]}"
do
case "$opt,$REPLY" in
"Select For Me",*|*,"Select For Me"|"select for me",*|*,"select for me")
OUTPUT=`grep ${OUTGREP} ${CARDS} | grep "DAC" | awk ' { print $1 } '`
[ "${OUTPUT}" ] || {
OUTPUT=`grep '\[Headphones' ${CARDS} | awk ' { print $1 } '`
}
[ "${OUTPUT}" ] || {
OUTPUT=`grep ${OUTGREP} ${CARDS} | grep "Audio Device" | awk ' { print $1 } '`
}
[ "${OUTPUT}" ] && {
OUTPUT=`echo ${OUTPUT} | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//'`
}
[ "${OUTPUT}" ] || OUTPUT=1
;;
"None",*|*,"None"|"none",*|*,"none")
OUTPUT=1
break
;;
"Quit",*|*,"Quit"|"quit",*|*,"quit")
exit 0
;;
[0-9]*,*|*,[0-9]*)
OUTPUT=${REPLY}
((OUTPUT--))
break
;;
*)
printf "\nInvalid entry. Please try again"
printf "\nEnter either a number or text of one of the menu entries\n"
;;
esac
done
echo ""
PS3="${BOLD}Please enter the device you wish to use for AUDIO INPUT (numeric or text): ${NORMAL}"
select opt in "${device_options[@]}"
do
case "$opt,$REPLY" in
"Select For Me",*|*,"Select For Me"|"select for me",*|*,"select for me")
INPUT=`grep ${INGREP} ${CARDS} | grep "Webcam" | awk ' { print $1 } '`
[ "${INPUT}" ] || {
INPUT=`grep '\[Microphone' ${CARDS} | awk ' { print $1 } '`
}
[ "${INPUT}" ] || {
INPUT=`grep ${INGREP} ${CARDS} | grep "Audio Device" | awk ' { print $1 } '`
}
[ "${INPUT}" ] && {
INPUT=`echo ${INPUT} | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//'`
}
[ "${INPUT}" ] || {
if [ "${OUTPUT}" == "0" ]
then
INPUT=1
else
INPUT=0
fi
}
;;
"None",*|*,"None"|"none",*|*,"none")
if [ "${OUTPUT}" == "0" ]
then
INPUT=1
else
INPUT=0
fi
break
;;
"Quit",*|*,"Quit"|"quit",*|*,"quit")
exit 0
;;
[0-9]*,*|*,[0-9]*)
INPUT=${REPLY}
((INPUT--))
break
;;
*)
printf "\nInvalid entry. Please try again"
printf "\nEnter either a number or text of one of the menu entries\n"
;;
esac
done
else
OUTPUT=`grep ${OUTGREP} ${CARDS} | grep "DAC" | awk ' { print $1 } '`
[ "${OUTPUT}" ] || {
OUTPUT=`grep '\[Headphones' ${CARDS} | awk ' { print $1 } '`
}
[ "${OUTPUT}" ] || {
OUTPUT=`grep ${OUTGREP} ${CARDS} | grep "Audio Device" | awk ' { print $1 } '`
}
[ "${OUTPUT}" ] && {
OUTPUT=`echo ${OUTPUT} | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//'`
}
[ "${OUTPUT}" ] || OUTPUT=1
INPUT=`grep ${INGREP} ${CARDS} | grep "Webcam" | awk ' { print $1 } '`
[ "${INPUT}" ] || {
INPUT=`grep '\[Microphone' ${CARDS} | awk ' { print $1 } '`
}
[ "${INPUT}" ] || {
INPUT=`grep ${INGREP} ${CARDS} | grep "Audio Device" | awk ' { print $1 } '`
}
[ "${INPUT}" ] && {
INPUT=`echo ${INPUT} | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//'`
}
[ "${INPUT}" ] || {
if [ "${OUTPUT}" == "0" ]
then
INPUT=1
else
INPUT=0
fi
}
fi
# Generate a new asound.conf from the template
cat ${ASOUND_TMPL} | \
sed -e "s/${INPSTR}/${INPUT}/" -e "s/${OUTSTR}/${OUTPUT}/" > /tmp/asound$$
SAME=
[ -f ${ASOUND} ] && {
diff /tmp/asound$$ ${ASOUND} > /dev/null
[ $? -eq 0 ] && SAME=1
}
if [ "${CHK}" ]
then
if [ -f ${ASOUND} ]
then
if [ "${SAME}" ]
then
[ "${QUT}" ] || {
echo "${ASOUND} is identical to a generated asound.conf. No need to update."
}
exit 0
else
[ "${QUT}" ] || {
echo "${ASOUND} differs with generated asound.conf as follows:"
diff /tmp/asound$$ ${ASOUND}
}
exit 1
fi
else
[ "${QUT}" ] || {
echo "No ${ASOUND} detected. Check not performed."
echo "Use 'set_asound_conf -e' to generate and update ${ASOUND}"
}
exit 1
fi
else
if [ "${UPD}" ]
then
if [ -f ${ASOUND} ]
then
if [ "${SAME}" ]
then
[ "${QUT}" ] || {
echo "${ASOUND} is identical to a generated asound.conf. No need to update."
}
set_vol_card ${OUTPUT}
else
[ -f ${ASOUND}.orig ] || {
[ "${QUT}" ] || {
echo "Copying original ${ASOUND} to ${ASOUND}.orig"
}
sudo cp ${ASOUND} ${ASOUND}.orig
}
[ "${QUT}" ] || {
echo "Installing generated asound.conf as ${ASOUND}"
}
sudo cp /tmp/asound$$ ${ASOUND}
set_vol_card ${OUTPUT}
fi
else
sudo cp /tmp/asound$$ ${ASOUND}
set_vol_card ${OUTPUT}
fi
else
if [ "${SAME}" ]
then
[ "${QUT}" ] || {
echo "${ASOUND} is identical to a generated asound.conf. No need to update."
}
else
[ "${QUT}" ] || {
echo "Copying generated asound.conf to ${ASOUND}.gen$$"
}
sudo cp /tmp/asound$$ ${ASOUND}.gen$$
fi
fi
fi
rm -f /tmp/asound$$
fi
exit 0