1
0

separate system-independent parts of volume(1)

This commit is contained in:
dtb 2022-08-21 16:16:25 -04:00
parent 01fee33d0f
commit daca2efe30
2 changed files with 38 additions and 71 deletions

View File

@ -1,10 +1,9 @@
#!/bin/sh
set -e
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
@ -16,22 +15,18 @@ argv0="$0"
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(){
printf "%s: Unknown system.\n" "$argv0" 1>&2
if [ "$(uname)" = NetBSD ]; then
VOLUME_SYSTEM=netbsd
elif command -v pactl >/dev/null 2>&1; then
VOLUME_SYSTEM=pulseaudio
else
printf "%s: Unknown system.\n" "$0"
exit 1
}
fi
usage(){
printf "\
Usage: %b {function} (operand)\n" "$argv0"
Usage: %b {function} (operand)\n" "$0"
printf "\
Functions:
'+' - shortcut to \"relative \$2\"
@ -51,63 +46,32 @@ works the same as \"%b h\".
exit 1
}
# $1 becomes 'help' if previously empty
[ -n "$1" ] \
&& argv1="$1" \
|| argv1=help
case "$1" in
case "$argv1" in
#
# SYSTEM DEPENDENT
#
a*) [ -n "$2" ] || usage
case "$(system_determine)" in
NetBSD)
# NetBSD 9.2 STABLE 2021-07
audioctl -w play.gain=$2 >/dev/null
;;
PulseAudio)
# pactl 15.0 compiled+linked with libpulse 15.0.0
pactl set-sink-volume @DEFAULT_SINK@ $2%
;;
*) unknown_system ;;
esac; exit 0 ;;
+) "$0" r "$2" ;;
-) "$0" r -"$2" ;;
s*) [ -z "$2" ] || usage
case "$(system_determine)" in
NetBSD) # hacky
audioctl -a \
| grep "play\.gain" \
| cut -d '=' -f 2
;;
PulseAudio)
# 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/'%'//
;;
*) unknown_system ;;
esac; exit 0 ;;
#
# SYSTEM independent
#
+) "$argv0" r "$2" ;;
-) "$argv0" r -"$2" ;;
a*)
nonzero $2 && ! nonzero $3 && str isdigit "$2" \
|| usage
"$0".$VOLUME_SYSTEM "$@"
;;
s*)
! nonzero $2 \
|| usage
"$0".$VOLUME_SYSTEM s
;;
d*)
printf '$0: %s\nAudio system detected: %s\n' \
"$argv0" \
"$(system_determine)"
exit 0 ;;
"$0" \
$VOLUME_SYSTEM
;;
m*) [ -z "$2" ] || usage
# restore previous volume if there is one
if [ -e "$VOLUME_TMP_FILE" ]; then
xargs "$argv0" a <"$VOLUME_TMP_FILE" \
xargs "$0" a <"$VOLUME_TMP_FILE" \
&& rm "$VOLUME_TMP_FILE" \
&& printf "Unmuted.\n" \
&& exit 0 \
@ -120,7 +84,7 @@ m*) [ -z "$2" ] || usage
# dd used rather than shell redirect so it's easy to determine
# whether or not the file write worked
if ! printf "%b" "$("$argv0" s)" \
if ! printf "%b" "$("$0" s)" \
| dd of="$VOLUME_TMP_FILE" 2>/dev/null
then
printf "Error writing to file.\n" 1>&2
@ -128,18 +92,21 @@ m*) [ -z "$2" ] || usage
fi
# and then of course mute
"$argv0" a 0
"$0" a 0
printf "Muted.\n"
exit 0 ;;
r*)
set -x
argv2="$(printf "%s\n" "$2" | tr - _)"
! newval=$(printf "%s %s + p\n" $("$argv0" s) "$argv2" | dc) \
&& printf "%b: Error finding new value for volume.\n" "$argv0" \
&& exit 1
"$argv0" a "$newval"
nonzero $2 && ! nonzero $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)"
exit $? ;;
*) usage ;;

View File

@ -1,3 +1,3 @@
int main(int argc, char *argv[]){
return argc > 1;
return !(argc > 1);
}