Files
dotfiles/i3/scripts/pa-volume

138 lines
3.0 KiB
Bash
Executable File

#!/bin/bash
# name of the sink. execute pactl list sinks to get a list
SINKNAME="$(pactl info | grep '^Default Sink:' | cut -d ' ' -f 3-)"
# name of the sink. execute pactl list sinks to get a list
SOURCENAME="$(pactl info | grep '^Default Source:' | cut -d ' ' -f 3-)"
#SINKNAME="alsa_output.usb-Logitech_Logitech_Wireless_Headset_88C626354D45-00.analog-stereo"
# this is the worst
SINK=$(( $(pactl list sinks | grep "Name: " | grep -n "${SINKNAME}"$ | grep -o "^[[:digit:]]*") -1))
SOURCE=$(( $(pactl list sources | grep "Name: " | grep -n "${SOURCENAME}"$ | grep -o "^[[:digit:]]*") -1))
getvol() {
echo $(pactl list sinks | grep "^[[:space:]]*Volume" | head -n $(( $SINK + 1 )) | tail -n 1 | grep -o "[[:digit:]]*%" | head -n 1 | cut -d "%" -f 1)
}
setvol() {
if [[ $1 =~ [+-][0-9]+ ]] ; then
oldvol="$(getvol)"
echo "oldvol $oldvol"
delta="$(echo "$1" | cut -c 2-)"
echo "delta $delta"
if [[ "$(echo "$1" | cut -c 1)" == "+" ]] ; then
echo "+"
newvol=$(( $oldvol + $delta ))
else
echo "-"
newvol=$(( $oldvol - $delta ))
echo $newvol
fi
if [[ $newvol -gt 100 ]]; then
echo "capping at 100 percent"
newvol=100
fi
if [[ $newvol -lt 0 ]]; then
echo "capping at 0 percent"
newvol=0
fi
echo "newvol $newvol"
else
newvol="$1"
fi
pactl set-sink-volume $SINKNAME $(( $newvol * 65536 / 100 ))
}
ismuted() {
muted=$(pactl list sinks | grep "^[[:space:]]*Mute" | head -n $(( $SINK + 1 )) | tail -n 1 | cut -d " " -f 2)
if [[ $muted == "no" ]]; then
echo 0
else
echo 1
fi
}
mute() {
pactl set-sink-mute $SINKNAME 1
}
unmute() {
pactl set-sink-mute $SINKNAME 0
}
mute-toggle() {
pactl set-sink-mute $SINKNAME toggle
}
mute-toggle-mic() {
pactl set-source-mute $SOURCENAME toggle
}
status() {
if [[ $(ismuted) == "1" ]] ; then
echo "mute"
return
fi
echo "$(getvol)%"
}
usage() {
echo "Usage:"
echo
echo "$0 get-vol"
echo "$0 set-vol VOL_PERC"
}
update_status_bar_sink() {
~/.i3/scripts/bar-update "volume_status output"
}
update_status_bar_source() {
~/.i3/scripts/bar-update "volume_status input"
}
case "$1" in
"sink")
echo $SINKNAME
echo $SINK
;;
"get-vol")
echo $(getvol)
;;
"set-vol")
if [[ -z "$2" ]] ; then
usage
else
setvol "$2"
fi
update_status_bar_sink
;;
"mute")
mute
update_status_bar_sink
;;
"unmute")
unmute
update_status_bar_sink
;;
"mute-toggle")
mute-toggle
update_status_bar_sink
;;
"mute-toggle-mic")
mute-toggle-mic
update_status_bar_source
;;
"is-muted")
echo $(ismuted)
;;
"status")
echo $(status)
;;
*)
usage
esac