rc/bin/volcont
2020-12-23 17:02:34 +01:00

167 lines
3.9 KiB
Bash
Executable file

#!/bin/sh
# finds the active sink for pulse audio and increments the volume. useful when you have multiple audio outputs and have a key bound to vol-up and down
# Taken from http://customlinux.blogspot.fr/2013/02/pavolumesh-control-active-sink-volume.html
# and slightly adapted by lhark to work with notify-send
notify='dunst' # no/yes/dunst
notiftime='400'
notifid='42'
inc='2'
capvol='yes'
maxvol='200'
autosync='no'
icon_up='audio-volume-high-symbolic'
icon_down='audio-volume-low-symbolic'
icon_mute='audio-volume-muted-symbolic'
main_sink=$(pacmd list-sinks |awk '/* index:/{print $3}')
active_sinks=$(pacmd list-sinks |awk '/index:/{print $NF}' | tr '\n' ' ' | sed 's/ $/\n/g')
limit=$((100 - inc))
maxlimit=$((maxvol - inc))
updateSinks () {
active_sinks=$(pacmd list-sinks |awk '/index:/{print $NF}' | tr '\n' ' ' | sed 's/ $/\n/g')
}
volUp () {
updateSinks
getCurVol
for sink in ${active_sinks}; do
if [ ${capvol} = 'yes' ]; then
if [ "${curVol}" -le 100 ] && [ "${curVol}" -ge ${limit} ]; then
pactl set-sink-volume "${sink}" 100%
elif [ "${curVol}" -lt ${limit} ]; then
pactl set-sink-volume "${sink}" +${inc}%
fi
elif [ "${curVol}" -le ${maxvol} ] && [ "${curVol}" -ge ${maxlimit} ]; then
pactl set-sink-volume "${sink}" ${maxvol}%
elif [ "${curVol}" -lt ${maxlimit} ]; then
pactl set-sink-volume "${sink}" +${inc}%
fi
done
getCurVol
if [ ${notify} = 'yes' ]; then
notify-send "Volume : ${curVol}%" -t ${notiftime}
elif [ "$notify" = 'dunst' ]; then
dunstify -r "$notifid" -t "$notiftime" -i "$icon_up" "Volume: $curVol%"
fi
if [ ${autosync} = 'yes' ]; then
volSync
fi
}
volDown () {
updateSinks
for sink in ${active_sinks}; do
pactl set-sink-volume "${sink}" -${inc}%
done
getCurVol
if [ ${notify} = 'yes' ]; then
notify-send "Volume : ${curVol}%" -t ${notiftime}
elif [ "$notify" = 'dunst' ]; then
dunstify -r "$notifid" -t "$notiftime" -i "$icon_down" "Volume: $curVol%"
fi
if [ ${autosync} = 'yes' ]; then
volSync
fi
}
volSync () {
updateSinks
getCurVol
for each in ${active_sinks}; do
pactl set-sink-volume "${each}" "${curVol}"%
done
}
getCurVol () {
curVol=$(pacmd list-sinks |grep -A 15 'index: '"${main_sink}"'' |grep 'volume:' |egrep -v 'base volume:' |awk -F : '{print $3}' |grep -o -P '.{0,3}%'|sed s/.$// | tr -d ' ')
}
volMute () {
case "$1" in
mute)
for sink in ${active_sinks}; do
pactl set-sink-mute "${sink}" 1
done
curVol=0
status=1
;;
unmute)
for sink in ${active_sinks}; do
pactl set-sink-mute "${sink}" 0
done
getCurVol
status=0
;;
esac
if [ ${notify} = 'yes' ]; then
notify-send "$([ "${status}" = 1 ] && echo "Sound muted" || echo "Sound unmuted")" -t ${notiftime}
elif [ "$notify" = 'dunst' ]; then
[ "$status" = 1 ] && icon="$icon_mute" || icon="$icon_up"
dunstify -r "$notifid" -t "$notiftime" -i "$icon" "$([ "${status}" = 1 ] && echo "Sound muted" || echo "Sound unmuted")"
fi
}
volMuteStatus () {
curStatus=$(pacmd list-sinks |grep -A 15 'index: '"${main_sink}"'' |awk '/muted/{ print $2}')
if [ "${curStatus}" = 'yes' ]; then
volMute unmute
else
volMute mute
fi
}
volHelp () {
echo "Finds the active sink for pulse audio and manages the volume."
echo "Options :"
echo ""
echo " --down"
echo " --help"
echo " --mute"
echo " --sync Sync all sinks volumes"
echo " --toggle"
echo " --unmute"
echo " --up"
}
case "$1" in
--up)
volUp
;;
--down)
volDown
;;
--toggle)
volMuteStatus
;;
--help)
volHelp
;;
--mute)
volMute mute
;;
--unmute)
volMute unmute
;;
--sync)
volSync
;;
esac