Add pactl support
This commit is contained in:
parent
d85fe4b419
commit
f12dbece18
@ -1,18 +1,32 @@
|
||||
#!/bin/sh
|
||||
|
||||
# Has some undocumented features but they're functional (as far as I know)
|
||||
# and system-independent. just shortcuts for me really.
|
||||
|
||||
argv0="$0"
|
||||
|
||||
! which stris >/dev/null 2>/dev/null \
|
||||
&& printf "%b: Missing stris.\n" \
|
||||
# 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.
|
||||
|
||||
! command -v stris >/dev/null 2>/dev/null \
|
||||
&& printf "%b: Missing stris.\n" "$argv0" \
|
||||
&& exit 1 \
|
||||
|| true
|
||||
|
||||
VOLUME_TMP_FILE="$HOME/.volume-previous"
|
||||
|
||||
usage() {
|
||||
unknown_system(){
|
||||
printf "%s: Unknown system.\n" "$argv0" 1>&2
|
||||
exit 1
|
||||
}
|
||||
|
||||
usage(){
|
||||
printf "\
|
||||
Usage: %b [function] (argument)\n" "$argv0"
|
||||
printf "\
|
||||
@ -36,28 +50,57 @@ works the same as \"%b h\".
|
||||
|| argv1=s
|
||||
|
||||
case "$argv1" in
|
||||
(+) # shortcut
|
||||
#
|
||||
# SYSTEM DEPENDENT
|
||||
#
|
||||
(a*)
|
||||
[ -n "$2" ] && [ -z "$3" ] && stris int "$2" || usage
|
||||
|
||||
if [ "$(uname)" = "NetBSD" ]; then
|
||||
# NetBSD 9.2 STABLE 2021-07
|
||||
audioctl -w play.gain=$2 >/dev/null
|
||||
|
||||
elif command -v pactl >/dev/null; then
|
||||
# pactl 15.0 compiled+linked with libpulse 15.0.0
|
||||
pactl set-sink-volume @DEFAULT_SINK@ $2%
|
||||
|
||||
else unknown_system; fi
|
||||
exit 0 ;;
|
||||
(s*)
|
||||
[ -z "$2" ] || usage
|
||||
|
||||
if [ "$(uname)" = "NetBSD" ]; then
|
||||
# hacky
|
||||
audioctl -a \
|
||||
| grep "play\.gain" \
|
||||
| cut -d '=' -f 2
|
||||
|
||||
elif command -v pactl >/dev/null; then
|
||||
# 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/'%'//
|
||||
|
||||
else unknown_system; fi
|
||||
exit 0 ;;
|
||||
#
|
||||
# SYSTEM independent
|
||||
#
|
||||
(+)
|
||||
[ -n "$2" ] && [ -z "$3" ] && stris uint "$2" \
|
||||
&& "$argv0" r "$2" \
|
||||
|| usage
|
||||
;;
|
||||
(-) # shortcut
|
||||
(-)
|
||||
[ -n "$2" ] && [ -z "$3" ] && stris uint "$2" \
|
||||
&& "$argv0" r -"$2" \
|
||||
|| usage
|
||||
;;
|
||||
(a*)
|
||||
[ -n "$2" ] && [ -z "$3" ] && stris int "$2" || usage
|
||||
case "$(uname)" in
|
||||
(NetBSD)
|
||||
# 9.2 STABLE 2021-07
|
||||
audioctl -w play.gain=$2 >/dev/null
|
||||
;;
|
||||
(*) unknown_system
|
||||
esac
|
||||
exit 0 ;;
|
||||
(h*) usage ;;
|
||||
(m*) # system independent
|
||||
(m*)
|
||||
[ -z "$2" ] || usage
|
||||
# restore previous volume if there is one
|
||||
if [ -e "$VOLUME_TMP_FILE" ]; then
|
||||
@ -74,8 +117,8 @@ case "$argv1" in
|
||||
|
||||
# dd used rather than shell redirect so it's easy to determine
|
||||
# whether or not the file write worked
|
||||
if ! printf "%b" "$("$argv0" s)" | dd of="$VOLUME_TMP_FILE" \
|
||||
2>/dev/null
|
||||
if ! printf "%b" "$("$argv0" s)" \
|
||||
| dd >"$VOLUME_TMP_FILE" 2>/dev/null
|
||||
then
|
||||
printf "Error writing to file.\n" >/dev/stderr
|
||||
false
|
||||
@ -86,22 +129,15 @@ case "$argv1" in
|
||||
printf "Muted.\n"
|
||||
|
||||
exit 0 ;;
|
||||
(r*) # system independent
|
||||
[ -n "$2" ] && [ -z "$3" ] && stris int "$2" || usage
|
||||
(r*)
|
||||
[ -n "$2" ] && [ -z "$3" ] && stris int "$2" \
|
||||
|| usage
|
||||
|
||||
if ! newval=$(add $("$argv0" s) $2) || ! stris int "$newval"; then
|
||||
printf "%b: Error finding new value for volume.\n"
|
||||
printf "%b: Error finding new value for volume.\n" "$argv0"
|
||||
exit 1
|
||||
fi
|
||||
"$argv0" a $newval
|
||||
exit $? ;;
|
||||
(s*)
|
||||
[ -z "$2" ] || usage
|
||||
case "$(uname)" in
|
||||
(NetBSD)
|
||||
audioctl -a | grep "play\.gain" | cut -d '=' -f 2
|
||||
;;
|
||||
(*) unknown_system
|
||||
esac
|
||||
exit 0 ;;
|
||||
(*) usage
|
||||
esac
|
||||
|
Loading…
Reference in New Issue
Block a user