Add sound-control.sh

This commit is contained in:
Alexander Rosenberg 2024-08-24 16:13:26 -07:00
parent 416c07e329
commit 01eeaa5417
Signed by: Zander671
GPG Key ID: 5FD0394ADBD72730

96
sound-control.sh Executable file
View File

@ -0,0 +1,96 @@
#!/usr/bin/env zsh
setopt rcquotes
function print_help {
echo "usage: sound-control.sh [-n] [-h] <command>"
echo 'Options:'
echo ' -h: print this message, then exit'
echo ' -n: don''t notify the volume'
echo 'Commands:'
echo ' up: increase the volume (default: 5%)'
echo ' down: decrease the volume (default: 5%)'
echo ' set: set the volume'
echo ' mute: toggle the mute with no argument, or set it with one'
echo ' help: print this message, then exit'
echo ' notify: notify the volume, then exit (do nothing with -n)'
}
let notify=1
function notify_vol {
let vol="$(pamixer --get-volume)"
local icon
if [[ "$(pamixer --get-mute)" == 'true' ]]; then
icon='󰸈'
elif (( ${vol} > 50 )); then
icon='󰕾'
elif (( ${vol} >= 0 )); then
icon='󰖀'
else
icon=''
fi
notify-send -h string:x-canonical-private-synchronous:sound-control-sh \
-t 4000 \
-u normal \
-a "Volume" \
-h int:value:"${vol}" \
"${icon}"
}
function check_number {
if ! [[ -z "${1}" ]] && ! [[ "${1}" =~ '^-?[0-9]+%?$' ]]; then
echo "error: not a valid number: \"${1}\""
exit 1
fi
}
if [[ "${1}" == '-n' ]]; then
shift 1
notify=0
fi
case "${1}" in
-h|help|'')
print_help
;;
up)
check_number "${2}"
pamixer -i "${${2:-5}%'%'}"
notify_vol
;;
down)
check_number "${2}"
pamixer -d "${${2:-5}%'%'}"
notify_vol
;;
set)
(( ${#} >= 2 )) || { echo 'error: no input value'; exit 1 }
check_number "${2}"
pamixer --set-volume "${2%'%'}"
notify_vol
;;
mute)
if (( ${#} >= 2 )); then
if [[ "${2:l}" =~ '^y(es?)?$' ]]; then
pamixer -m
elif [[ "${2:l}" =~ '^no?$' ]]; then
pamixer -u
else
echo "error: must be one of 'yes' or 'no': \"${2}\""
exit 1
fi
else
pamixer -t
fi
notify_vol
;;
notify)
notify_vol
;;
*)
echo "error: unknown command: \"${1}\""
print_help
exit 1
;;
esac