1
0

make work better

This commit is contained in:
dtb 2022-06-26 16:34:09 -04:00
parent 59936a7c21
commit 19282627fc

View File

@ -14,13 +14,16 @@ argv0="$0"
# `volume s` must output the current volume in integer form in whatever units # `volume s` must output the current volume in integer form in whatever units
# are being used by `volume a` on the given system. # are being used by `volume a` on the given system.
! command -v stris >/dev/null 2>/dev/null \
&& printf "%b: Missing stris.\n" "$argv0" \
&& exit 1 \
|| true
VOLUME_TMP_FILE="$HOME/.volume-previous" VOLUME_TMP_FILE="$HOME/.volume-previous"
system_determine(){
if [ "$(uname)" = NetBSD ]; then
printf "NetBSD\n"
elif command -v pactl >/dev/null 2>&1; then
printf "PulseAudio\n"
fi
}
unknown_system(){ unknown_system(){
printf "%s: Unknown system.\n" "$argv0" 1>&2 printf "%s: Unknown system.\n" "$argv0" 1>&2
exit 1 exit 1
@ -34,6 +37,7 @@ Functions:
'+' - shortcut to \"relative \$2\" '+' - shortcut to \"relative \$2\"
'-' - shortcut to \"relative -\$2\" '-' - shortcut to \"relative -\$2\"
absolute - change sound output to absolute unit absolute - change sound output to absolute unit
debug - show debugging information
help,'' - print this help output help,'' - print this help output
mute - toggle mute/unmute mute - toggle mute/unmute
relative - change sound output relative to current status relative - change sound output relative to current status
@ -56,29 +60,27 @@ case "$argv1" in
# #
# SYSTEM DEPENDENT # SYSTEM DEPENDENT
# #
(a*) a*) [ -n "$2" ] || usage
[ -n "$2" ] && [ -z "$3" ] && stris int "$2" || usage case "$(system_determine)" in
NetBSD)
if [ "$(uname)" = "NetBSD" ]; then
# NetBSD 9.2 STABLE 2021-07 # NetBSD 9.2 STABLE 2021-07
audioctl -w play.gain=$2 >/dev/null audioctl -w play.gain=$2 >/dev/null
;;
elif command -v pactl >/dev/null; then PulseAudio)
# pactl 15.0 compiled+linked with libpulse 15.0.0 # pactl 15.0 compiled+linked with libpulse 15.0.0
pactl set-sink-volume @DEFAULT_SINK@ $2% pactl set-sink-volume @DEFAULT_SINK@ $2%
;;
*) unknown_system ;;
esac; exit 0 ;;
else unknown_system; fi s*) [ -z "$2" ] || usage
exit 0 ;; case "$(system_determine)" in
(s*) NetBSD) # hacky
[ -z "$2" ] || usage
if [ "$(uname)" = "NetBSD" ]; then
# hacky
audioctl -a \ audioctl -a \
| grep "play\.gain" \ | grep "play\.gain" \
| cut -d '=' -f 2 | cut -d '=' -f 2
;;
elif command -v pactl >/dev/null; then PulseAudio)
# really hacky, gets the job done # really hacky, gets the job done
# gets the volume % of Lchan specifically # gets the volume % of Lchan specifically
pactl get-sink-volume @DEFAULT_SINK@ \ pactl get-sink-volume @DEFAULT_SINK@ \
@ -86,32 +88,31 @@ case "$argv1" in
| cut -d '/' -f 2 \ | cut -d '/' -f 2 \
| xargs echo \ | xargs echo \
| sed s/'%'// | sed s/'%'//
;;
*) unknown_system ;;
esac; exit 0 ;;
else unknown_system; fi
exit 0 ;;
# #
# SYSTEM independent # SYSTEM independent
# #
(+) +) "$argv0" r "$2" ;;
[ -n "$2" ] && [ -z "$3" ] && stris uint "$2" \ -) "$argv0" r -"$2" ;;
&& "$argv0" r "$2" \
|| usage d*)
;; printf '$0: %s\nAudio system detected: %s\n' \
(-) "$argv0" \
[ -n "$2" ] && [ -z "$3" ] && stris uint "$2" \ "$(system_determine)"
&& "$argv0" r -"$2" \ exit 0 ;;
|| usage
;; m*) [ -z "$2" ] || usage
(m*)
[ -z "$2" ] || usage
# restore previous volume if there is one # restore previous volume if there is one
if [ -e "$VOLUME_TMP_FILE" ]; then if [ -e "$VOLUME_TMP_FILE" ]; then
"$argv0" a "$(cat "$VOLUME_TMP_FILE")" \ xargs "$argv0" a <"$VOLUME_TMP_FILE" \
&& rm "$VOLUME_TMP_FILE" \ && rm "$VOLUME_TMP_FILE" \
&& printf "Unmuted.\n" \ && printf "Unmuted.\n" \
&& exit 0 \ && exit 0 \
|| printf "Error restoring previous volume.\n" \ || printf "Error restoring previous volume.\n" \
>/dev/stderr \ 1>&2 \
&& exit 1 && exit 1
fi fi
@ -120,10 +121,10 @@ case "$argv1" in
# dd used rather than shell redirect so it's easy to determine # dd used rather than shell redirect so it's easy to determine
# whether or not the file write worked # whether or not the file write worked
if ! printf "%b" "$("$argv0" s)" \ if ! printf "%b" "$("$argv0" s)" \
| dd >"$VOLUME_TMP_FILE" 2>/dev/null | dd of="$VOLUME_TMP_FILE" 2>/dev/null
then then
printf "Error writing to file.\n" >/dev/stderr printf "Error writing to file.\n" 1>&2
false exit 1
fi fi
# and then of course mute # and then of course mute
@ -131,16 +132,13 @@ case "$argv1" in
printf "Muted.\n" printf "Muted.\n"
exit 0 ;; exit 0 ;;
(r*)
[ -n "$2" ] && [ -z "$3" ] && stris int "$2" \
|| usage
if ! newval=$(add $("$argv0" s) $2) || ! stris int "$newval"; then r*)
printf "%b: Error finding new value for volume.\n" "$argv0" ! newval=$(printf "%s %s + p\n" $("$argv0"s) "$2" | dc) \
exit 1 && printf "%b: Error finding new value for volume.\n" "$argv0" \
fi && exit 1
"$argv0" a $newval "$argv0" a "$newval"
exit $? ;; exit $? ;;
(h*) usage ;;
(*) usage ;; *) usage ;;
esac esac