1
0
src/niceties/volume

114 lines
2.5 KiB
Plaintext
Raw Normal View History

2022-05-18 15:44:50 -06:00
#!/bin/sh
set -e
2022-05-18 15:44:50 -06:00
argv0="$0"
# ADDING A NEW SOUND SERVER/CLIENT/WHATEVERIDK
# `volume a $2` must set the volume to $2, an integer value. This should be a
# percentage, 100% being the loudest possible volume that can be output by
# hardware without quality loss. Percentages above 100% will be allowable in
# software if using percentage units. If percentage units are implausible to
# implement, system units are okay (e.g. 0-$arbitrary scale versus 0-100).
# `volume s` must output the current volume in integer form in whatever units
# are being used by `volume a` on the given system.
2022-06-06 13:47:10 -06:00
VOLUME_TMP_FILE="$HOME/.volume-previous"
2022-05-18 15:44:50 -06:00
2023-12-19 00:16:49 -07:00
if uname | xargs strcmp NetBSD; then
VOLUME_SYSTEM=netbsd
elif command -v pactl >/dev/null 2>&1; then
VOLUME_SYSTEM=pulseaudio
else
printf "%s: Unknown system.\n" "$0"
2022-05-18 15:44:50 -06:00
exit 1
fi
2022-05-18 15:44:50 -06:00
usage(){
printf "\
Usage: %b {function} (operand)\n" "$0"
2022-05-18 15:44:50 -06:00
printf "\
Functions:
'+' - shortcut to \"relative \$2\"
'-' - shortcut to \"relative -\$2\"
absolute - change sound output to absolute unit
2022-06-26 14:34:09 -06:00
debug - show debugging information
2022-05-18 15:44:50 -06:00
help,'' - print this help output
mute - toggle mute/unmute
relative - change sound output relative to current status
2022-06-06 13:47:10 -06:00
status - print current sound output in system unit \
2022-05-18 15:44:50 -06:00
"
printf "\
The options are matched with [char]*. So \"%b help\"
works the same as \"%b h\".
" "$argv0" "$argv0"
exit 1
}
case "$1" in
+) "$0" r "$2" ;;
-) "$0" r -"$2" ;;
a*)
2022-10-23 00:23:58 -06:00
str isvalue $2 && ! str isvalue $3 && str isdigit "$2" \
|| usage
"$0".$VOLUME_SYSTEM "$@"
;;
s*)
2022-10-23 00:23:58 -06:00
! str isvalue $2 \
|| usage
"$0".$VOLUME_SYSTEM s
;;
2022-06-26 14:34:09 -06:00
d*)
printf '$0: %s\nAudio system detected: %s\n' \
"$0" \
$VOLUME_SYSTEM
;;
2022-06-26 14:34:09 -06:00
2022-10-23 00:23:58 -06:00
m*) ! str isvalue "$2" || usage
2022-05-18 15:44:50 -06:00
# restore previous volume if there is one
2022-10-23 00:23:58 -06:00
if test -e "$VOLUME_TMP_FILE"; then
xargs "$0" a <"$VOLUME_TMP_FILE" \
2022-05-18 15:44:50 -06:00
&& rm "$VOLUME_TMP_FILE" \
&& printf "Unmuted.\n" \
&& exit 0 \
|| printf "Error restoring previous volume.\n" \
2022-06-26 14:34:09 -06:00
1>&2 \
2022-05-18 15:44:50 -06:00
&& exit 1
fi
# otherwise, make new file with previous volume
# dd used rather than shell redirect so it's easy to determine
# whether or not the file write worked
if ! printf "%b" "$("$0" s)" \
2022-06-26 14:34:09 -06:00
| dd of="$VOLUME_TMP_FILE" 2>/dev/null
2022-05-18 15:44:50 -06:00
then
2022-06-26 14:34:09 -06:00
printf "Error writing to file.\n" 1>&2
exit 1
2022-05-18 15:44:50 -06:00
fi
# and then of course mute
"$0" a 0
2022-05-18 15:44:50 -06:00
printf "Muted.\n"
exit 0 ;;
2022-06-26 14:34:09 -06:00
r*)
2022-10-23 00:23:58 -06:00
str isvalue $2 && ! str isvalue $3 \
&& str isdigit "$(printf "%s\n" "$2" | sed 's/^\-//')" \
|| usage
"$0" a \
"$(printf "%s %s + p\n" \
$("$0" s) \
"$(printf "%s\n" $2 | tr - _)" \
| dc)"
2022-05-18 15:44:50 -06:00
exit $? ;;
2022-06-26 14:34:09 -06:00
*) usage ;;
2022-05-18 15:44:50 -06:00
esac