-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathalsa.sh
executable file
·113 lines (100 loc) · 2.5 KB
/
alsa.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
#!/bin/sh
# path: /home/klassiker/.local/share/repos/shell/alsa.sh
# author: klassiker [mrdotx]
# github: https://github.com/mrdotx/shell
# date: 2024-12-21T07:13:44+0100
# speed up script and avoid language problems by using standard c
LC_ALL=C
LANG=C
# config
config_path="$HOME/.config/alsa"
config_file="asoundrc"
analog_filter="analog"
message_title="Volume"
# help
script=$(basename "$0")
help="$script [-h/--help] -- script to change alsa audio output
Usage:
$script [-inc/-dec/-abs/-mute] [percent]
Settings:
[-inc] = increase in percent (0-100)
[-dec] = decrease in percent (0-100)
[-abs] = absolute volume in percent (0-100)
[percent] = how much percent to increase/decrease the volume
[-mute] = mute volume
Examples:
$script -inc 5
$script -dec 5
$script -abs 36
$script -mute"
notification() {
volume="$(amixer get "$1" \
| tail -1 \
| cut -d'[' -f2 \
| sed 's/%]*//' \
)"
if amixer get "$2" | tail -1 | grep "\[off\]" >/dev/null; then
volume=0 \
volume_indicator="MUTE"
else
volume=$((volume /= ${3:-1}))
volume=$((volume *= ${3:-1}))
volume_indicator="$volume%"
fi
notify-send \
-t 2000 \
-u low \
"$message_title $volume_indicator" \
-h string:x-canonical-private-synchronous:"$message_title" \
-h int:value:"$volume"
}
get_analog() {
card=$(grep -m1 "card" "$config_path/$config_file" \
| sed 's/^ //' \
)
if ! aplay -l \
| grep -m1 -i -e "^$card.*$analog_filter" > /dev/null 2>&1; then
device_mixer="PCM"
device_mute="IEC958"
else
device_mixer="Master"
device_mute="Master"
fi
}
set_volume() {
if [ "$#" -eq 2 ] \
&& [ "$2" -ge 0 ] > /dev/null 2>&1 \
&& [ "$2" -le 100 ] > /dev/null 2>&1; then
amixer -q set "$device_mixer" "$2%$1"
notification "$device_mixer" "$device_mute" "$2"
else
printf "%s\n" "$help"
exit 1
fi
}
case "$1" in
-h | --help)
printf "%s\n" "$help"
;;
-inc)
get_analog
set_volume "+" "$2"
;;
-dec)
get_analog
set_volume "-" "$2"
;;
-abs)
get_analog
set_volume "" "$2"
;;
-mute)
get_analog
amixer -q set "$device_mute" toggle
notification "$device_mixer" "$device_mute"
;;
*)
printf "%s\n" "$help"
exit 1
;;
esac