From 01eeaa5417993f977701c36758724966ce061e24 Mon Sep 17 00:00:00 2001 From: Alexander Rosenberg Date: Sat, 24 Aug 2024 16:13:26 -0700 Subject: [PATCH] Add sound-control.sh --- sound-control.sh | 96 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100755 sound-control.sh diff --git a/sound-control.sh b/sound-control.sh new file mode 100755 index 0000000..ddf7b42 --- /dev/null +++ b/sound-control.sh @@ -0,0 +1,96 @@ +#!/usr/bin/env zsh + +setopt rcquotes + +function print_help { + echo "usage: sound-control.sh [-n] [-h] " + 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