#!/bin/sh #{{{ detect detect () { if pgrep -x pulseaudio >/dev/null; then PULSE='y' ; unset PIPEWIRE SNDIO ALSA elif pgrep -x pipewire >/dev/null; then PIPEWIRE='y' ; unset PULSE SNDIO ALSA elif pgrep -x sndiod >/dev/null; then SNDIO='y' ; unset PIPEWIRE PULSE ALSA else ALSA='y' ; unset PIPEWIRE PULSE SNDIO fi } #}}} #{{{ up up () { [ -n "$SNDIO" ] && sndioctl output.level=+"$(printf "%0.02f" "$(( amt * 0.01 ))")" [ -n "$PULSE$PIPEWIRE" ] && pactl set-sink-volume $(pactl get-default-sink) "+${amt}%" [ -n "$ALSA" ] && amixer sset Master "${amt}%+" } #}}} #{{{ down down () { [ -n "$SNDIO" ] && sndioctl output.level=-"$(printf "%0.02f" "$(( amt * 0.01 ))")" [ -n "$PULSE$PIPEWIRE" ] && pactl set-sink-volume $(pactl get-default-sink) "-${amt}%" [ -n "$ALSA" ] && amixer sset Master "${amt}%-" } #}}} #{{{ mute mute () { case "$1" in spr) [ -n "$SNDIO" ] && sndioctl output.mute=! [ -n "$PULSE$PIPEWIRE" ] && pactl set-sink-mute $(pactl get-default-sink) toggle [ -n "$ALSA" ] && amixer sset Master toggle ;; mic) [ -n "$SNDIO" ] && sndioctl input.mute=! [ -n "$PULSE$PIPEWIRE" ] && pactl set-source-mute $(pactl get-default-source) toggle [ -n "$PIPEWIRE$ALSA" ] && amixer sset Capture toggle ;; esac } #}}} #{{{ get get () { case "$1" in volu) [ -n "$SNDIO" ] && printf "%s%%" `sndioctl output.level | rev | cut -d'.' -f1 | rev | cut -c 1,2` [ -n "$PULSE$PIPEWIRE" ] && printf "%s%%\n" "$(pamixer --get-volume)" [ -n "$ALSA" ] && amixer sget Master | grep '\[[0-9]*\%\]' | sed "s/ /\n/g" | grep '%' | sed 's/\[//;s/\]//g' ;; mute) [ -n "$SNDIO" ] && sndioctl output.mute | cut -d'=' -f2 | sed 's/1/[off]/;s/0/[on]/' [ -n "$PULSE$PIPEWIRE" ] && pamixer --get-mute | sed 's/[Ff]alse/\[on\]/;s/[Tt]rue/\[off\]/' [ -n "$ALSA" ] && amixer sget Master | grep '\[o[fn]' | cut -d' ' -f8 | head -1 ;; both) [ -n "$SNDIO" ] && printf "%s%s%%" `sndioctl output.mute | cut -d'=' -f2 | sed 's/1/[off]/;s/0/[on]/'` `sndioctl output.level | rev | cut -d'.' -f1 | rev | cut -c 1,2` [ -n "$PULSE$PIPEWIRE" ] && printf "%s%s%%\n" `pamixer --get-mute | sed 's/[Ff]alse/\[on\]/;s/[Tt]rue/\[off\]/'` "$(pamixer --get-volume)" [ -n "$ALSA" ] && printf "%s%s" `amixer sget Master | grep '\[o[fn]' | cut -d' ' -f8 | head -1` `amixer sget Master | grep '\[[0-9]*\%\]' | sed "s/ /\n/g" | grep '%' | sed 's/\[//;s/\]//g'` ;; esac } #}}} #{{{ setact setact() { if [ -n "${action+x}" ] && [ "$action" != "$1" ]; then echo "Running $1 with $action..." echo "Incompatible options given. Only one action may be specified per run." return 1 else action="$1" fi } #}}} #{{{ printhelp printhelp () { cat << EOF volsv: volume setter v, sets the volume despite the sound server supported sound servers are sndio, pulseaudio, pipewire, and alsa (no bluealsa) Actions: -i x Increase the volume by x (5% if not given) -d x Decrease the volume by x (5% if not given) -t Mute the default output -m Mute the default mic -v Get the volume of the default output -g Get the mute status of the default output -a Get the volume and mute status of the default output EOF } #}}} #{{{ getopts while getopts "hidtmvga" o; do case "${o}" in i) eval nextopt=\${$OPTIND} if [ -n "$nextopt" -a "$nextopt" != "-*" ]; then OPTIND=$((OPTIND + 1)) amt=$nextopt fi case "$amt" in ''|*[!0-9]*) amt=5 ;; esac setact up || exit 1 ;; d) eval nextopt=\${$OPTIND} if [ -n "$nextopt" -a "$nextopt" != "-*" ]; then OPTIND=$((OPTIND + 1)) amt=$nextopt fi case "$amt" in ''|*[!0-9]*) amt=5 ;; esac setact down || exit 1 ;; t) setact mute || exit 1 ;; m) setact micm || exit 1 ;; v) setact getv || exit 1 ;; g) setact getm || exit 1 ;; a) setact geta || exit 1 ;; *) printhelp ; exit 1 ;; esac done #}}} detect #{{{ action case "$action" in up) up ;; down) down ;; mute) mute spr ;; micm) mute mic ;; getv) get volu ;; getm) get mute ;; geta) get both ;; *) printhelp ;; esac #}}}