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 #!/bin/sh
set -e
argv0="$0" argv0="$0"
# ADDING A NEW SOUND SERVER/CLIENT/WHATEVERIDK # 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 # `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 # percentage, 100% being the loudest possible volume that can be output by
# hardware without quality loss. Percentages above 100% will be allowable in # hardware without quality loss. Percentages above 100% will be allowable in
@ -16,22 +15,18 @@ argv0="$0"
VOLUME_TMP_FILE="$HOME/.volume-previous" VOLUME_TMP_FILE="$HOME/.volume-previous"
system_determine(){ if [ "$(uname)" = NetBSD ]; then
if [ "$(uname)" = NetBSD ]; then VOLUME_SYSTEM=netbsd
printf "NetBSD\n" elif command -v pactl >/dev/null 2>&1; then
elif command -v pactl >/dev/null 2>&1; then VOLUME_SYSTEM=pulseaudio
printf "PulseAudio\n" else
fi printf "%s: Unknown system.\n" "$0"
}
unknown_system(){
printf "%s: Unknown system.\n" "$argv0" 1>&2
exit 1 exit 1
} fi
usage(){ usage(){
printf "\ printf "\
Usage: %b {function} (operand)\n" "$argv0" Usage: %b {function} (operand)\n" "$0"
printf "\ printf "\
Functions: Functions:
'+' - shortcut to \"relative \$2\" '+' - shortcut to \"relative \$2\"
@ -51,63 +46,32 @@ works the same as \"%b h\".
exit 1 exit 1
} }
# $1 becomes 'help' if previously empty case "$1" in
[ -n "$1" ] \
&& argv1="$1" \
|| argv1=help
case "$argv1" in +) "$0" r "$2" ;;
# -) "$0" r -"$2" ;;
# 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 ;;
s*) [ -z "$2" ] || usage a*)
case "$(system_determine)" in nonzero $2 && ! nonzero $3 && str isdigit "$2" \
NetBSD) # hacky || usage
audioctl -a \ "$0".$VOLUME_SYSTEM "$@"
| grep "play\.gain" \ ;;
| cut -d '=' -f 2 s*)
;; ! nonzero $2 \
PulseAudio) || usage
# really hacky, gets the job done "$0".$VOLUME_SYSTEM s
# 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" ;;
d*) d*)
printf '$0: %s\nAudio system detected: %s\n' \ printf '$0: %s\nAudio system detected: %s\n' \
"$argv0" \ "$0" \
"$(system_determine)" $VOLUME_SYSTEM
exit 0 ;; ;;
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
xargs "$argv0" a <"$VOLUME_TMP_FILE" \ xargs "$0" a <"$VOLUME_TMP_FILE" \
&& rm "$VOLUME_TMP_FILE" \ && rm "$VOLUME_TMP_FILE" \
&& printf "Unmuted.\n" \ && printf "Unmuted.\n" \
&& exit 0 \ && exit 0 \
@ -120,7 +84,7 @@ m*) [ -z "$2" ] || usage
# 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" "$("$0" s)" \
| dd of="$VOLUME_TMP_FILE" 2>/dev/null | dd of="$VOLUME_TMP_FILE" 2>/dev/null
then then
printf "Error writing to file.\n" 1>&2 printf "Error writing to file.\n" 1>&2
@ -128,18 +92,21 @@ m*) [ -z "$2" ] || usage
fi fi
# and then of course mute # and then of course mute
"$argv0" a 0 "$0" a 0
printf "Muted.\n" printf "Muted.\n"
exit 0 ;; exit 0 ;;
r*) r*)
set -x nonzero $2 && ! nonzero $3 \
argv2="$(printf "%s\n" "$2" | tr - _)" && str isdigit "$(printf "%s\n" "$2" | sed 's/^\-//')" \
! newval=$(printf "%s %s + p\n" $("$argv0" s) "$argv2" | dc) \ || usage
&& printf "%b: Error finding new value for volume.\n" "$argv0" \
&& exit 1 "$0" a \
"$argv0" a "$newval" "$(printf "%s %s + p\n" \
$("$0" s) \
"$(printf "%s\n" $2 | tr - _)" \
| dc)"
exit $? ;; exit $? ;;
*) usage ;; *) usage ;;

View File

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