-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathalsa.sh
More file actions
executable file
·129 lines (113 loc) · 3.02 KB
/
Copy pathalsa.sh
File metadata and controls
executable file
·129 lines (113 loc) · 3.02 KB
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
#!/bin/sh
# path: /home/klassiker/.local/share/repos/shell/alsa.sh
# author: klassiker [mrdotx]
# url: https://github.com/mrdotx/shell
# date: 2025-08-11T04:49:29+0200
# 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 [-i/--increase/-d/--decrease/-a/--absolute] [percent] [-n/--notify]
$script [-m/--mute/-u/--unmute/-t/--toggle] [-n/--notify]
Settings:
[-i/--increase] = increase in percent (0-100)
[-d/--decrease] = decrease in percent (0-100)
[-a/--absolute] = absolute volume in percent (0-100)
[percent] = percentage of increase/decrease/absolute volume
[-m/--mute] = mute audio
[-u/--unmute] = unmute audio
[-t/--toggle] = toggle mute status
[-n/--notify] = send status notification
Examples:
$script --increase 5
$script -d 5 -n
$script --absolute 36
$script -m
$script --unmute
$script --toggle --notify"
notification() {
case "$1" in
-n | --notify)
get_volume "$2"
notify-send \
-t 2000 \
-u low \
"$message_title $volume_indicator" \
-h string:x-canonical-private-synchronous:"$message_title" \
-h int:value:"$volume"
;;
esac
}
get_analog() {
card=$(grep -m1 "card" "$config_path/$config_file" \
| sed 's/^ //' \
)
! aplay -l \
| grep -m1 -i -e "^$card.*$analog_filter" > /dev/null 2>&1 \
&& device_mixer="PCM" \
&& device_mute="IEC958" \
&& return
device_mixer="Master"
device_mute="Master"
}
get_volume() {
amixer get "$device_mute" | tail -1 | grep "\[off\]" >/dev/null \
&& volume=0 \
&& volume_indicator="MUTE" \
&& return
volume="$(amixer get "$device_mixer" \
| tail -1 \
| cut -d'[' -f2 \
| sed 's/%]*//' \
)"
volume=$((volume /= ${1:-1}))
volume=$((volume *= ${1:-1}))
volume_indicator="$volume%"
}
set_volume() {
[ "$2" -ge 0 ] > /dev/null 2>&1 \
&& [ "$2" -le 100 ] > /dev/null 2>&1 \
&& get_analog \
&& amixer -q set "$device_mixer" "$2%$1" \
&& notification "$3" "$2"
}
set_mute() {
get_analog \
&& amixer -q set "$device_mute" "$1" \
&& notification "$2"
}
case "$1" in
-h | --help)
printf "%s\n" "$help"
;;
-i | --increase)
set_volume "+" "$2" "$3"
;;
-d | --decrease)
set_volume "-" "$2" "$3"
;;
-a | --absolute)
set_volume "" "$2" "$3"
;;
-m | --mute)
set_mute "mute" "$2"
;;
-u | --unmute)
set_mute "unmute" "$2"
;;
-t | --toggle)
set_mute "toggle" "$2"
;;
*)
printf "%s\n" "$help"
exit 1
;;
esac