Files
src/bin/volume
T

147 lines
3.4 KiB
Bash
Raw Normal View History

2022-05-18 17:44:50 -04:00
#!/bin/sh
argv0="$0"
# ADDING A NEW SOUND SERVER/CLIENT/WHATEVERIDK
# `volume a` and `volume s` are the system dependent functions.
# `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 15:47:10 -04:00
! command -v stris >/dev/null 2>/dev/null \
&& printf "%b: Missing stris.\n" "$argv0" \
&& exit 1 \
|| true
2022-05-18 17:44:50 -04:00
2022-06-06 15:47:10 -04:00
VOLUME_TMP_FILE="$HOME/.volume-previous"
2022-05-18 17:44:50 -04:00
unknown_system(){
printf "%s: Unknown system.\n" "$argv0" 1>&2
exit 1
}
usage(){
printf "\
Usage: %b {function} (operand)\n" "$argv0"
printf "\
Functions:
'+' - shortcut to \"relative \$2\"
'-' - shortcut to \"relative -\$2\"
absolute - change sound output to absolute unit
help,'' - print this help output
mute - toggle mute/unmute
relative - change sound output relative to current status
2022-06-06 15:47:10 -04:00
status - print current sound output in system unit \
2022-05-18 17:44:50 -04:00
"
printf "\
The options are matched with [char]*. So \"%b help\"
works the same as \"%b h\".
" "$argv0" "$argv0"
exit 1
}
# $1 becomes 'help' if previously empty
[ -n "$1" ] \
&& argv1="$1" \
|| argv1=help
case "$argv1" in
#
# SYSTEM DEPENDENT
#
(a*)
2022-06-06 15:47:10 -04:00
[ -n "$2" ] && [ -z "$3" ] && stris int "$2" || usage
2022-05-18 17:44:50 -04:00
2022-06-06 15:47:10 -04:00
if [ "$(uname)" = "NetBSD" ]; then
2022-05-18 17:44:50 -04:00
# NetBSD 9.2 STABLE 2021-07
audioctl -w play.gain=$2 >/dev/null
2022-06-06 15:47:10 -04:00
elif command -v pactl >/dev/null; then
2022-05-18 17:44:50 -04:00
# pactl 15.0 compiled+linked with libpulse 15.0.0
pactl set-sink-volume @DEFAULT_SINK@ $2%
2022-06-06 15:47:10 -04:00
else unknown_system; fi
2022-05-18 17:44:50 -04:00
exit 0 ;;
(s*)
[ -z "$2" ] || usage
2022-06-06 15:47:10 -04:00
if [ "$(uname)" = "NetBSD" ]; then
2022-05-18 17:44:50 -04:00
# hacky
audioctl -a \
| grep "play\.gain" \
| cut -d '=' -f 2
2022-06-06 15:47:10 -04:00
elif command -v pactl >/dev/null; then
2022-05-18 17:44:50 -04:00
# really hacky, gets the job done
# gets the volume % of Lchan specifically
pactl get-sink-volume @DEFAULT_SINK@ \
| sed q \
| cut -d '/' -f 2 \
| xargs echo \
| sed s/'%'//
2022-06-06 15:47:10 -04:00
else unknown_system; fi
2022-05-18 17:44:50 -04:00
exit 0 ;;
#
# SYSTEM independent
#
(+)
2022-06-06 15:47:10 -04:00
[ -n "$2" ] && [ -z "$3" ] && stris uint "$2" \
2022-05-18 17:44:50 -04:00
&& "$argv0" r "$2" \
|| usage
;;
(-)
2022-06-06 15:47:10 -04:00
[ -n "$2" ] && [ -z "$3" ] && stris uint "$2" \
2022-05-18 17:44:50 -04:00
&& "$argv0" r -"$2" \
|| usage
;;
(m*)
[ -z "$2" ] || usage
# restore previous volume if there is one
if [ -e "$VOLUME_TMP_FILE" ]; then
"$argv0" a "$(cat "$VOLUME_TMP_FILE")" \
&& rm "$VOLUME_TMP_FILE" \
&& printf "Unmuted.\n" \
&& exit 0 \
|| printf "Error restoring previous volume.\n" \
>/dev/stderr \
&& 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" "$("$argv0" s)" \
| dd >"$VOLUME_TMP_FILE" 2>/dev/null
then
printf "Error writing to file.\n" >/dev/stderr
false
fi
# and then of course mute
"$argv0" a 0
printf "Muted.\n"
exit 0 ;;
(r*)
2022-06-06 15:47:10 -04:00
[ -n "$2" ] && [ -z "$3" ] && stris int "$2" \
2022-05-18 17:44:50 -04:00
|| usage
2022-06-06 15:47:10 -04:00
if ! newval=$(add $("$argv0" s) $2) || ! stris int "$newval"; then
2022-05-18 17:44:50 -04:00
printf "%b: Error finding new value for volume.\n" "$argv0"
exit 1
fi
2022-06-06 15:47:10 -04:00
"$argv0" a $newval
2022-05-18 17:44:50 -04:00
exit $? ;;
(h*) usage ;;
(*) usage ;;
esac