-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvol
executable file
·78 lines (61 loc) · 1.55 KB
/
vol
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
#!/usr/bin/env bash
MIX=amixer
declare -i LO=0
declare -i HI=100
usage ()
{
echo "Usage: `basename $0` [ - | + | N ]" >&2
echo " where N is a whole number between $LO and $HI, inclusive." >&2
exit 1
}
# Zero or one argument
[ $# -le 1 ] || usage
# Argument must be one of: empty, -, +, number
[[ $1 == ?(-|+|+([0-9])) ]] || usage
ARG="$1"
# Number argument
if [[ $ARG == +([0-9]) ]]; then
# Strip leading zeros
while [[ $ARG == 0+([0-9]) ]]; do
ARG=${ARG#0}
done
# Must be between LO and HI
(( ARG >= LO && ARG <= HI )) || usage
fi
EXE=$(which $MIX)
if [ -z "$EXE" ]; then
echo "Error: $MIX not found. Try \"sudo apt-get install alsa-utils\" first." >&2
exit 2
fi
GET=$($EXE cget numid=1)
declare -i MIN=$(echo $GET | /bin/grep -E -o -e ',min=[^,]+' | /bin/grep -E -o -e '[0-9-]+')
declare -i MAX=$(echo $GET | /bin/grep -E -o -e ',max=[^,]+' | /bin/grep -E -o -e '[0-9-]+')
declare -i VAL=$(echo $GET | /bin/grep -E -o -e ': values=[0-9+-]+' | /bin/grep -E -o -e '[0-9-]+')
if (( MIN >= MAX || VAL < MIN || VAL > MAX )); then
echo "Error: could not get the right values from $MIX output." >&2
exit 3
fi
declare -i LEN=0
(( LEN = MAX - MIN ))
declare -i ABS=0
(( ABS = VAL - MIN ))
declare -i PCT=0
(( PCT = 100 * ABS / LEN ))
if [ ! -z "$ARG" ]; then
declare -i OLD=$PCT
declare -i ADJ=3
if [[ "$ARG" == "+" ]]; then
(( PCT += ADJ ))
elif [[ "$ARG" == "-" ]]; then
(( PCT -= ADJ ))
else
PCT=$ARG
fi
if [[ "$PCT" != "$OLD" ]]; then
(( ABS = PCT * LEN / 100 ))
(( VAL = MIN + ABS ))
$EXE -q cset numid=1 -- $VAL
fi
fi
echo $PCT
exit 0