snd (4489B)
1 #!/bin/sh 2 AUDIO_ACT="get" 3 AUDIO_STEPS="5" 4 AUDIO_NOTIF=0 5 6 while getopts ':ngidmGIDMs:' flag; do 7 case "${flag}" in 8 n) AUDIO_NOTIF=1 ;; 9 g) AUDIO_ACT="get" ;; 10 i) AUDIO_ACT="inc" ;; 11 d) AUDIO_ACT="dec" ;; 12 m) AUDIO_ACT="mute" ;; 13 G) AUDIO_ACT="mic-get" ;; 14 I) AUDIO_ACT="mic-inc" ;; 15 D) AUDIO_ACT="mic-dec" ;; 16 M) AUDIO_ACT="mic-mute" ;; 17 s) AUDIO_STEPS="${OPTARG}" ;; 18 *) 19 printf 'Usage: %s [-g] [-d] [-i] [-m] [-G] [-D] [-I] [-M] [-s audio steps (in %%)]\n' "$0" 20 printf ' -g: get volume\n' 21 printf ' -i: increase volume\n' 22 printf ' -d: decrease volume\n' 23 printf ' -m: mute volume\n' 24 printf ' -G: get mic volume\n' 25 printf ' -I: increase mic volume\n' 26 printf ' -D: decrease mic volume\n' 27 printf ' -M: mute mic volume\n' 28 exit 2 29 ;; 30 esac 31 done 32 33 vol() { 34 case "$(uname)" in 35 "Linux"*) 36 if pactl stat >/dev/null 2>&1; then 37 DEFAULT_SINK=$(pactl get-default-sink) 38 DEFAULT_SOURCE=$(pactl get-default-source) 39 case "$1" in 40 get) pactl get-sink-volume $DEFAULT_SINK | awk '/%/{sub(/%/, "", $5);print $5}' ;; 41 inc) pactl set-sink-volume $DEFAULT_SINK "+$2%" ;; 42 dec) pactl set-sink-volume $DEFAULT_SINK "-$2%" ;; 43 mute) pactl set-sink-mute $DEFAULT_SINK toggle ;; 44 ismuted) pactl get-sink-mute $DEFAULT_SINK | grep -o 'yes' ;; 45 mic-get) pactl get-source-volume $DEFAULT_SOURCE | awk '/%/{sub(/%/, "", $5);print $5}' ;; 46 mic-inc) pactl set-source-volume $DEFAULT_SINK "+$2%" ;; 47 mic-dec) pactl set-source-volume $DEFAULT_SINK "-$2%" ;; 48 mic-mute) pactl set-source-mute $DEFAULT_SINK toggle ;; 49 mic-ismuted) pactl get-source-mute $DEFAULT_SOURCE | grep -o 'yes' ;; 50 esac 51 else 52 case "$1" in 53 get) amixer get Master on | grep -o '[0-9]*%' | sed 's/%//' | sed -n '1p' ;; 54 inc) amixer set Master "$2%+" on -q ;; 55 dec) amixer set Master "$2%-" on -q ;; 56 mute) amixer set Master 1+ toggle -q ;; 57 ismuted) amixer get Master on | grep -o '\[off\]' ;; 58 mic-get) amixer get Capture on | grep -o '[0-9]*%' | sed 's/%//' | sed -n '1p' ;; 59 mic-inc) amixer set Capture "$2%+" on -q ;; 60 mic-dec) amixer set Capture "$2%-" on -q ;; 61 mic-mute) amixer set Capture 1+ toggle -q ;; 62 mic-ismuted) amixer get Capture on | grep -o '\[off\]' ;; 63 esac 64 fi 65 ;; 66 "FreeBSD"*) 67 case "$1" in 68 get) mixer -s vol | cut -d ':' -f 2 ;; 69 inc) mixer -s vol "+$2%" ;; 70 dec) mixer -s vol "-$2%" ;; 71 mute) 72 if [ "$(vol get)" -eq 0 ]; then 73 mixer -s vol 75 74 else 75 mixer -s vol 0 76 fi 77 ;; 78 ismuted) [ "$(vol get)" -eq 0 ] && echo yes ;; 79 mic-get) mixer -s mic | cut -d ':' -f 2 ;; 80 mic-inc) mixer -s mic "+$2%" ;; 81 mic-dec) mixer -s mic "-$2%" ;; 82 mic-mute) 83 if [ "$(vol mic-get)" -eq 0 ]; then 84 mixer -s mic 35 85 else 86 mixer -s mic 0 87 fi 88 ;; 89 mic-ismuted) [ "$(vol mic-get)" -eq 0 ] && echo yes ;; 90 esac 91 ;; 92 esac 93 } 94 95 notif() { 96 case "$AUDIO_ACT" in 97 mic) 98 VOLUME=$(vol mic-get) 99 IS_MUTED=$(vol mic-ismuted) 100 VOLUME_ICON_0="" 101 VOLUME_ICON_1="" 102 VOLUME_ICON_2="" 103 VOLUME_ICON_3="" 104 ;; 105 *) 106 VOLUME=$(vol get) 107 IS_MUTED=$(vol ismuted) 108 VOLUME_ICON_0="ﱝ" 109 VOLUME_ICON_1="" 110 VOLUME_ICON_2="" 111 VOLUME_ICON_3="" 112 ;; 113 esac 114 if [ "$IS_MUTED" ] || [ "$VOLUME" -eq 0 ]; then 115 VOLUME="Muted" 116 VOLUME_ICON=$VOLUME_ICON_0 117 elif [ "$VOLUME" -lt 30 ]; then 118 VOLUME_ICON=$VOLUME_ICON_1 119 elif [ "$VOLUME" -lt 70 ]; then 120 VOLUME_ICON=$VOLUME_ICON_2 121 else 122 VOLUME_ICON=$VOLUME_ICON_3 123 fi 124 notify "$VOLUME_ICON $VOLUME" 125 } 126 127 case "$AUDIO_ACT" in 128 inc) 129 CURRENT_VOLUME=$(vol get) 130 REMAINDER=$((CURRENT_VOLUME%AUDIO_STEPS)) 131 AUDIO_STEPS=$((AUDIO_STEPS-REMAINDER)) 132 ;; 133 dec) 134 CURRENT_VOLUME=$(vol get) 135 REMAINDER=$((CURRENT_VOLUME%AUDIO_STEPS)) 136 AUDIO_STEPS=$((AUDIO_STEPS+REMAINDER)) 137 ;; 138 mic-inc) 139 CURRENT_VOLUME=$(vol mic-get) 140 REMAINDER=$((CURRENT_VOLUME%AUDIO_STEPS)) 141 AUDIO_STEPS=$((AUDIO_STEPS-REMAINDER)) 142 ;; 143 mic-dec) 144 CURRENT_VOLUME=$(vol mic-get) 145 REMAINDER=$((CURRENT_VOLUME%AUDIO_STEPS)) 146 AUDIO_STEPS=$((AUDIO_STEPS+REMAINDER)) 147 ;; 148 esac 149 150 case "$AUDIO_ACT" in 151 get) 152 vol get 153 ;; 154 inc) 155 vol inc "$AUDIO_STEPS" 156 ;; 157 dec) 158 vol dec "$AUDIO_STEPS" 159 ;; 160 mute) 161 vol mute 162 ;; 163 mic-get) 164 vol mic-get 165 ;; 166 mic-inc) 167 vol mic-inc "$AUDIO_STEPS" 168 ;; 169 mic-dec) 170 vol mic-dec "$AUDIO_STEPS" 171 ;; 172 mic-mute) 173 vol mic-mute 174 ;; 175 esac 176 177 [ "$AUDIO_NOTIF" -eq 1 ] && notif