1
0
This commit is contained in:
dtb 2022-06-06 15:47:10 -04:00
parent 4088cb1b7b
commit 75449d26da
2 changed files with 72 additions and 30 deletions

View File

@ -14,13 +14,12 @@ 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.
VOLUME_TMP_FILE="$HOME/.volume-previous" ! command -v stris >/dev/null 2>/dev/null \
&& printf "%b: Missing stris.\n" "$argv0" \
&& exit 1 \
|| true
if [ "$(uname)" = "NetBSD" ]; then VOLUME_TMP_FILE="$HOME/.volume-previous"
backend="NetBSD"
elif command -v pactl >/dev/null; then
backend="pulseaudio"
fi
unknown_system(){ unknown_system(){
printf "%s: Unknown system.\n" "$argv0" 1>&2 printf "%s: Unknown system.\n" "$argv0" 1>&2
@ -38,16 +37,13 @@ Functions:
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
status - print current sound output in system unit status - print current sound output in system unit \
" "
printf "\ printf "\
The options are matched with [char]*. So \"%b help\" The options are matched with [char]*. So \"%b help\"
works the same as \"%b h\". works the same as \"%b h\".
" "$argv0" "$argv0" " "$argv0" "$argv0"
printf "\
The backend detected is %b.
" "$backend"
exit 1 exit 1
} }
@ -61,34 +57,28 @@ case "$argv1" in
# SYSTEM DEPENDENT # SYSTEM DEPENDENT
# #
(a*) (a*)
[ -n "$2" ] && [ -z "$3" ] || usage [ -n "$2" ] && [ -z "$3" ] && stris int "$2" || usage
case "$backend" in if [ "$(uname)" = "NetBSD" ]; then
NetBSD)
# 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
;;
pulseaudio) elif command -v pactl >/dev/null; then
# 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 else unknown_system; fi
esac
exit 0 ;; exit 0 ;;
(s*) (s*)
[ -z "$2" ] || usage [ -z "$2" ] || usage
case "$backend" in if [ "$(uname)" = "NetBSD" ]; then
NetBSD)
# hacky # hacky
audioctl -a \ audioctl -a \
| grep "play\.gain" \ | grep "play\.gain" \
| cut -d '=' -f 2 | cut -d '=' -f 2
;;
pulseaudio) elif command -v pactl >/dev/null; then
# 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@ \
@ -96,21 +86,19 @@ case "$argv1" in
| cut -d '/' -f 2 \ | cut -d '/' -f 2 \
| xargs echo \ | xargs echo \
| sed s/'%'// | sed s/'%'//
;;
*) unknown_system ;; else unknown_system; fi
esac
exit 0 ;; exit 0 ;;
# #
# SYSTEM independent # SYSTEM independent
# #
(+) (+)
[ -n "$2" ] && [ -z "$3" ] \ [ -n "$2" ] && [ -z "$3" ] && stris uint "$2" \
&& "$argv0" r "$2" \ && "$argv0" r "$2" \
|| usage || usage
;; ;;
(-) (-)
[ -n "$2" ] && [ -z "$3" ] \ [ -n "$2" ] && [ -z "$3" ] && stris uint "$2" \
&& "$argv0" r -"$2" \ && "$argv0" r -"$2" \
|| usage || usage
;; ;;
@ -144,14 +132,14 @@ case "$argv1" in
exit 0 ;; exit 0 ;;
(r*) (r*)
[ -n "$2" ] && [ -z "$3" ] \ [ -n "$2" ] && [ -z "$3" ] && stris int "$2" \
|| usage || usage
if ! newval="$(printf "%s %s + p\n" "$("$argv0" s)" "$(printf "%s\n" "$2" | sed 's/^-/_/')" | dc)"; then if ! newval=$(add $("$argv0" s) $2) || ! stris int "$newval"; then
printf "%b: Error finding new value for volume.\n" "$argv0" printf "%b: Error finding new value for volume.\n" "$argv0"
exit 1 exit 1
fi fi
"$argv0" a "$newval" "$argv0" a $newval
exit $? ;; exit $? ;;
(h*) usage ;; (h*) usage ;;
(*) usage ;; (*) usage ;;

54
man/volume.1 Normal file
View File

@ -0,0 +1,54 @@
.TH VOLUME 1
.SH NAME
volume \(en change the current audio volume
.SH SYNOPSIS
volume {function} [operand]
.SH DESCRIPTION
Volume provides a common interface for elementary configuration of the
current system audio volume.
.PP
Volume is not a replacement for the system-specific audio interface.
.SH FUNCTIONS
.SS +
Shortcut to "volume r ".
.SS -
Shortcut to "volume r -".
.SS a
Change sound output to absolute unit.
.SS h
Print help output.
.SS m
Toggle audio mute.
.SS r
Change sound output relative to current status.
.SS s
Print current sound output level in system unit.
.SH BUGS
Many.
.SH COPYRIGHT
Public domain.