1
0
src/bin/volume

147 lines
3.3 KiB
Plaintext
Raw Normal View History

2022-05-18 15:44:50 -06: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 13:47:10 -06:00
VOLUME_TMP_FILE="$HOME/.volume-previous"
2022-05-18 15:44:50 -06:00
2022-06-26 14:34:09 -06:00
system_determine(){
if [ "$(uname)" = NetBSD ]; then
printf "NetBSD\n"
elif command -v pactl >/dev/null 2>&1; then
printf "PulseAudio\n"
fi
}
2022-05-18 15:44:50 -06: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
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
}
# $1 becomes 'help' if previously empty
[ -n "$1" ] \
&& argv1="$1" \
|| argv1=help
case "$argv1" in
#
# SYSTEM DEPENDENT
#
2022-06-26 14:34:09 -06:00
a*) [ -n "$2" ] || usage
case "$(system_determine)" in
NetBSD)
2022-05-18 15:44:50 -06:00
# NetBSD 9.2 STABLE 2021-07
audioctl -w play.gain=$2 >/dev/null
2022-06-26 14:34:09 -06:00
;;
PulseAudio)
2022-05-18 15:44:50 -06:00
# pactl 15.0 compiled+linked with libpulse 15.0.0
pactl set-sink-volume @DEFAULT_SINK@ $2%
2022-06-26 14:34:09 -06:00
;;
*) unknown_system ;;
esac; exit 0 ;;
2022-05-18 15:44:50 -06:00
2022-06-26 14:34:09 -06:00
s*) [ -z "$2" ] || usage
case "$(system_determine)" in
NetBSD) # hacky
2022-05-18 15:44:50 -06:00
audioctl -a \
| grep "play\.gain" \
| cut -d '=' -f 2
2022-06-26 14:34:09 -06:00
;;
PulseAudio)
2022-05-18 15:44:50 -06: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-26 14:34:09 -06:00
;;
*) unknown_system ;;
esac; exit 0 ;;
2022-05-18 15:44:50 -06:00
#
# SYSTEM independent
#
2022-06-26 14:34:09 -06:00
+) "$argv0" r "$2" ;;
-) "$argv0" r -"$2" ;;
d*)
printf '$0: %s\nAudio system detected: %s\n' \
"$argv0" \
"$(system_determine)"
exit 0 ;;
m*) [ -z "$2" ] || usage
2022-05-18 15:44:50 -06:00
# restore previous volume if there is one
if [ -e "$VOLUME_TMP_FILE" ]; then
2022-06-26 14:34:09 -06:00
xargs "$argv0" 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" "$("$argv0" 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
"$argv0" a 0
printf "Muted.\n"
exit 0 ;;
2022-06-26 14:34:09 -06:00
r*)
2022-07-15 20:11:18 -06:00
set -x
argv2="$(printf "%s\n" "$2" | tr - _)"
! newval=$(printf "%s %s + p\n" $("$argv0" s) "$argv2" | dc) \
2022-06-26 14:34:09 -06:00
&& printf "%b: Error finding new value for volume.\n" "$argv0" \
&& exit 1
"$argv0" a "$newval"
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