Files
dotfiles/i3/scripts/pa-volume

117 lines
2.3 KiB
Plaintext
Raw Normal View History

2014-04-19 04:22:05 +02:00
#!/bin/bash
2014-07-29 02:11:38 +02:00
# name of the sink. execute pactl list sinks to get a list
SINKNAME="alsa_output.pci-0000_00_1b.0.analog-stereo"
# this is the worst
2016-04-12 19:40:12 +02:00
SINK=$(( $(pactl list sinks | grep "Name: " | grep -n "${SINKNAME}"$ | grep -o "^[[:digit:]]*") -1))
2014-04-19 04:22:05 +02:00
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 ))
2014-07-26 20:37:59 +02:00
echo $newvol
fi
if [[ $newvol -gt 100 ]]; then
echo "capping at 100 percent"
newvol=100
fi
2014-11-07 17:37:04 +01:00
if [[ $newvol -lt 0 ]]; then
echo "capping at 0 percent"
newvol=0
fi
echo "newvol $newvol"
else
newvol="$1"
fi
2015-11-01 01:37:32 +01:00
pactl set-sink-volume $SINKNAME $(( $newvol * 65536 / 100 ))
2014-04-19 04:22:05 +02:00
}
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() {
2017-09-09 14:37:14 +02:00
pactl set-sink-mute $SINKNAME 1
2014-04-19 04:22:05 +02:00
}
unmute() {
2017-09-09 14:37:14 +02:00
pactl set-sink-mute $SINKNAME 0
2014-04-19 04:22:05 +02:00
}
mute-toggle() {
2017-09-09 14:37:14 +02:00
pactl set-sink-mute $SINKNAME toggle
2014-04-19 04:22:05 +02:00
}
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"
}
case "$1" in
"get-vol")
echo $(getvol)
;;
"set-vol")
if [[ -z "$2" ]] ; then
usage
else
setvol "$2"
fi
hook="$3"
2014-04-19 04:22:05 +02:00
;;
"mute")
mute
hook="$2"
2014-04-19 04:22:05 +02:00
;;
"unmute")
unmute
hook="$2"
2014-04-19 04:22:05 +02:00
;;
"mute-toggle")
mute-toggle
hook="$2"
2014-04-19 04:22:05 +02:00
;;
"is-muted")
echo $(ismuted)
;;
"status")
echo $(status)
;;
*)
usage
2014-04-19 04:22:05 +02:00
;;
esac
if [[ -n "$hook" ]] ; then
echo "volume changed, executing hook: $hook"
$hook
fi