volume (3109B)
1 #!/usr/bin/env bash
2 # Copyright (C) 2014 Julien Bonjean <julien@bonjean.info>
3 # Copyright (C) 2014 Alexander Keller <github@nycroth.com>
4
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
9
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 #------------------------------------------------------------------------
19
20 # The second parameter overrides the mixer selection
21 # For PulseAudio users, eventually use "pulse"
22 # For Jack/Jack2 users, use "jackplug"
23 # For ALSA users, you may use "default" for your primary card
24 # or you may use hw:# where # is the number of the card desired
25 if [[ -z "$MIXER" ]] ; then
26 MIXER="default"
27 if command -v pulseaudio >/dev/null 2>&1 && pulseaudio --check ; then
28 # pulseaudio is running, but not all installations use "pulse"
29 if amixer -D pulse info >/dev/null 2>&1 ; then
30 MIXER="pulse"
31 fi
32 fi
33 [ -n "$(lsmod | grep jack)" ] && MIXER="jackplug"
34 MIXER="${2:-$MIXER}"
35 fi
36
37 # The instance option sets the control to report and configure
38 # This defaults to the first control of your selected mixer
39 # For a list of the available, use `amixer -D $Your_Mixer scontrols`
40 if [[ -z "$SCONTROL" ]] ; then
41 SCONTROL="${BLOCK_INSTANCE:-$(amixer -D $MIXER scontrols |
42 sed -n "s/Simple mixer control '\([^']*\)',0/\1/p" |
43 head -n1
44 )}"
45 fi
46
47 # The first parameter sets the step to change the volume by (and units to display)
48 # This may be in in % or dB (eg. 5% or 3dB)
49 if [[ -z "$STEP" ]] ; then
50 STEP="${1:-5%}"
51 fi
52
53 #------------------------------------------------------------------------
54
55 capability() { # Return "Capture" if the device is a capture device
56 amixer -D $MIXER get $SCONTROL |
57 sed -n "s/ Capabilities:.*cvolume.*/Capture/p"
58 }
59
60 volume() {
61 amixer -D $MIXER get $SCONTROL $(capability)
62 }
63
64 format() {
65
66 perl_filter='if (/.*\[(\d+%)\] (\[(-?\d+.\d+dB)\] )?\[(on|off)\]/)'
67 perl_filter+='{CORE::say $4 eq "off" ? "MUTE" : "'
68 # If dB was selected, print that instead
69 perl_filter+=$([[ $STEP = *dB ]] && echo '$3' || echo '$1')
70 perl_filter+='"; exit}'
71 output=$(perl -ne "$perl_filter")
72 echo "$LABEL$output"
73 }
74
75 #------------------------------------------------------------------------
76
77 case $BLOCK_BUTTON in
78 3) amixer -q -D $MIXER sset $SCONTROL $(capability) toggle ;; # right click, mute/unmute
79 4) amixer -q -D $MIXER sset $SCONTROL $(capability) ${STEP}+ unmute ;; # scroll up, increase
80 5) amixer -q -D $MIXER sset $SCONTROL $(capability) ${STEP}- unmute ;; # scroll down, decrease
81 esac
82
83 volume | format