update/improve audioctrl

This commit is contained in:
speedie 2023-02-26 00:05:38 +01:00
parent cc646e1c01
commit 87cbcf5343

View file

@ -7,130 +7,94 @@
# Requires pactl for pulseaudio/pipewire or amixer for alsa
# Additionally requires pulsemixer for pulseaudio/pipewire or alsamixer for alsa if the mixer functionality is used
BINDIR="$(dirname $(command -v speedwm-audioctrl))/"
[ -z "$STATUS" ] && STATUS="speedwm_status"
[ -z "$NOTIF_SYSTEM" ] && NOTIF_SYSTEM="notify-send"
MUTE() {
# Mute for pulseaudio/pipewire
if [ "$AUDIO" = "pulse" ]; then
if [ -e "${BINDIR}pactl" ]; then
pactl set-sink-mute @DEFAULT_SINK@ toggle
else
amixer set Master toggle
fi
fi
[ "$AUDIO" = "pulse" ] && \
command -v pactl > /dev/null && \
pactl set-sink-mute @DEFAULT_SINK@ toggle || amixer set Master toggle
kill -38 $(pidof status)
pidof "$STATUS" > /dev/null && \
kill -38 $(pidof $STATUS)
}
RAISE() {
if [ "$AUDIO" = "pulse" ]; then
if [ -e "${BINDIR}pactl" ]; then
if [ "$(pactl get-sink-volume @DEFAULT_SINK@ | awk '{ print $5;exit }' | sed 's/%//g')" = "110" ]; then
pactl set-sink-volume @DEFAULT_SINK@ 100%
else
pactl set-sink-volume @DEFAULT_SINK@ +10%
test ${BINDIR}notify-send && notify-send " $(pactl get-sink-volume @DEFAULT_SINK@ | awk '{ print $5;exit }')"
fi
fi
else
if [ "$(amixer -c 0 get Master | tail -n 1 | sed -r "s/.*\[(.*)%\].*/\1/")" = "100" ]; then
a=$a
else
amixer -c 0 set Master 7%+
ls ${BINDIR}notify-send && notify-send " $(amixer -c 0 get Master | tail -n 1 | sed -r "s/.*\[(.*)%\].*/\1/")%"
fi
fi
PULSE() {
command -v pactl > /dev/null && pactl set-sink-volume @DEFAULT_SINK@ +${NUM}%
command -v notify-send > /dev/null && notify-send " $(pactl get-sink-volume @DEFAULT_SINK@ | awk '{ print $5;exit }')"
}
kill -38 $(pidof status)
ALSA() {
command -v amixer > /dev/null && amixer -c 0 set Master ${NUM}%+
command -v notify-send > /dev/null && notify-send " $(amixer -c 0 get Master | tail -n 1 | sed -r "s/.*\[(.*)%\].*/\1/")%"
}
[ "$AUDIO" = "pulse" ] && PULSE || ALSA
pidof "$STATUS" > /dev/null && \
kill -38 $(pidof $STATUS)
}
LOWER() {
if [ "$AUDIO" = "pulse" ]; then
if [ -e "${BINDIR}pactl" ]; then
if [ "$(pactl get-sink-volume @DEFAULT_SINK@ | awk '{ print $5;exit }' | sed 's/%//g')" != "0" ]; then
pactl set-sink-volume @DEFAULT_SINK@ -10%
test ${BINDIR}notify-send && notify-send " $(pactl get-sink-volume @DEFAULT_SINK@ | awk '{ print $5;exit }')"
fi
PULSE() {
command -v pactl > /dev/null && pactl set-sink-volume @DEFAULT_SINK@ -${NUM}%
command -v notify-send > /dev/null && notify-send " $(pactl get-sink-volume @DEFAULT_SINK@ | awk '{ print $5;exit }')"
}
fi
else
if [ "$(amixer -c 0 get Master | tail -n 1 | sed -r "s/.*\[(.*)%\].*/\1/")" = "0" ]; then
a=$a
else
amixer -c 0 set Master 7%-
test ${BINDIR}notify-send && notify-send " $(amixer -c 0 get Master | tail -n 1 | sed -r "s/.*\[(.*)%\].*/\1/")%"
fi
fi
ALSA() {
command -v amixer > /dev/null && amixer -c 0 set Master ${NUM}%-
command -v notify-send > /dev/null && notify-send " $(amixer -c 0 get Master | tail -n 1 | sed -r "s/.*\[(.*)%\].*/\1/")%"
}
kill -38 $(pidof status)
[ "$AUDIO" = "pulse" ] && PULSE || ALSA
pidof "$STATUS" > /dev/null && \
kill -38 $(pidof $STATUS)
}
GETVOL() {
if [ "$AUDIO" = "pulse" ]; then
if [ -e "${BINDIR}pactl" ]; then
pactl get-sink-volume @DEFAULT_SINK@ | awk '{ print $5;exit }'
fi
else
echo "$(amixer -c 0 get Master | tail -n 1 | sed -r "s/.*\[(.*)%\].*/\1/")%"
fi
[ "$AUDIO" = "pulse" ] && command -v pactl > /dev/null && pactl get-sink-volume @DEFAULT_SINK@ | awk '{ print $5;exit }' || echo "$(amixer -c 0 get Master | tail -n 1 | sed -r "s/.*\[(.*)%\].*/\1/")%"
}
GETMUTE() {
if [ "$AUDIO" = "pulse" ]; then
if [ -e "${BINDIR}pactl" ]; then
pactl get-sink-mute @DEFAULT_SINK@ | sed "s/Mute: //; s/no/Not muted/; s/yes/Muted/g"
fi
fi
}
SWITCH() {
ls $HOME/.local/share/audioctrl-status || echo "0" > $HOME/.local/share/audioctrl-status
if [ "$(cat $HOME/.local/share/audioctrl-status)" = "0" ]; then
amixer -c 0 sset 'Auto-Mute Mode' Enabled ; echo "1" > $HOME/.local/share/audioctrl-status
if [ -e "${BINDIR}notify-send" ]; then
notify-send " Switched to headphones."
fi
else
amixer -c 0 sset 'Auto-Mute Mode' Disabled ; echo "0" > $HOME/.local/share/audioctrl-status
if [ -e "${BINDIR}notify-send" ]; then
notify-send " Switched to speakers."
fi
fi
[ "$AUDIO" = "pulse" ] && command -v pactl > /dev/null && pactl get-sink-mute @DEFAULT_SINK@ | sed "s/Mute: //; s/no/Not muted/; s/yes/Muted/g"
}
RUNMIXER() {
if [ "$AUDIO" = "pulse" ]; then
if [ -e "${BINDIR}pulsemixer" ]; then
pulsemixer
elif [ -e "${BINDIR}alsamixer" ]; then
alsamixer
fi
fi
command -v pulsemixer > /dev/null && pulsemixer && exit 0
command -v alsamixer > /dev/null && alsamixer && exit 0
}
AUDIO=$2
case "$2" in
"") AUDIO=alsa
if [ -e "${BINDIR}pulsemixer" ]; then
AUDIO=pulse
else
if [ -e "${BINDIR}pactl" ]; then
AUDIO=pulse
fi
fi
;;
esac
GET_AUDIO_BACKEND() {
command -v alsamixer > /dev/null && AUDIO=alsa
command -v amixer > /dev/null && AUDIO=alsa
command -v pulsemixer > /dev/null && AUDIO=pulse
command -v pactl > /dev/null && AUDIO=pulse
command -v pulseaudio > /dev/null && AUDIO=pulse
command -v pipewire > /dev/null && AUDIO=pulse
}
[ -z "$3" ] && GET_AUDIO_BACKEND || AUDIO="$3"
[ -z "$2" ] && NUM="5" || NUM="$2"
case "$1" in
"-mute") MUTE && GETMUTE > /tmp/speedwm-audioctrl-mutestatus ;;
"-remute") remute=true ; MUTE && GETMUTE > /tmp/speedwm-audioctrl-mutestatus ; MUTE && GETMUTE > /tmp/speedwm-audioctrl-mutestatus ; remute="" ; exit 0 ;;
"-raise") RAISE ;;
"-lower") LOWER ;;
"-switch") SWITCH && exit 0 ;;
"-getvol") GETVOL && exit 0 ;;
"-getmute") GETMUTE > /tmp/speedwm-audioctrl-mutestatus && exit 0 ;;
"-getbackend") printf "$AUDIO\n" && exit 0 ;;
"-runmixer") RUNMIXER && exit 0 ;;
"-help") printf "speedwm-audioctrl\n-mute | Toggle mute\n-raise | Raise the volume by 7\n-lower | Lower the volume by 7\n-switch | Toggle output\n-getvol | Get current volume in percentage\n-getmute | Get mute status\n-getbackend | Get audio backend (ALSA, PulseAudio, etc.)\n-runmixer | Run the audio mixer detected on the system\n-remute | Mute and unmute.\n-help | Display this help screen\nNo arguments | Display this help screen\n"; exit 0 ;;
"") $0 -help && exit 0 ;;
"-mute") MUTE ;;
"-raise") RAISE ;;
"-lower") LOWER ;;
"-get_vol") GETVOL && exit 0 ;;
"-get_mute") GETMUTE&& exit 0 ;;
"-get_backend") printf "$AUDIO\n" && exit 0 ;;
"-run_mixer") RUNMIXER && exit 0 ;;
"-help") printf "speedwm-audioctrl\n-mute - Toggle mute\n-raise - Raise the volume\n-lower - Lower the volume\n-switch - Toggle output\n-get_vol - Get current volume in percentage\n-get_mute - Get mute status\n-get_backend - Get audio backend (ALSA/Pulseaudio)\n-run_mixer - Run the audio mixer detected on the system\n-help - Display this help screen\nNo arguments - Display this help screen\n"; exit 0 ;;
"") $0 -help && exit 0 ;;
esac
# compatibility arguments
case "$1" in
"-getvol") $0 -get_vol && exit 0 ;;
"-getmute") $0 -get_mute && exit 0 ;;
"-getbackend") $0 -get_backend && exit 0 ;;
"-runmixer") RUNMIXER ;;
esac