Skip to content

Instantly share code, notes, and snippets.

@KonradKuznicki
Last active December 14, 2015 22:58
Show Gist options
  • Save KonradKuznicki/5161902 to your computer and use it in GitHub Desktop.
Save KonradKuznicki/5161902 to your computer and use it in GitHub Desktop.
PulseAudio volume control in Bash
#!/bin/bash
SINK=0 # default sink
STEP=10 # %
POINTS=`pacmd list-sinks | awk '/volume steps/ {print $3}'`
ONE_PERCENT=`expr $POINTS / 100`
ISMUTED=`pacmd list-sinks | awk '/mute/ {print $2}' `
CURRENT_VOLUME=;
function live {
live_help
while read -sN1 key # 1 char (not delimiter), silent
do
# catch multi-char special key sequences
read -sN1 -t 0.0001 k1
read -sN1 -t 0.0001 k2
read -sN1 -t 0.0001 k3
key+=${k1}${k2}${k3}
case "$key" in
$'\e[A' | $'\e0A')
volume_sign +
;;
$'\e[B' | $'\e0B')
volume_sign -
;;
esac
done
}
function live_help {
echo "
Use your up down arow keys to adjust volume
Ctrl-c to quit
";
}
function volume {
if [ $1 -gt 99 ]; then
VOLUME=$POINTS
elif [ 0 -gt $1 ]; then
VOLUME=0
else
VOLUME=`expr $ONE_PERCENT \* $1`
fi
echo "set volume $VOLUME, $1%"
pacmd set-sink-volume $SINK $VOLUME > /dev/null
}
unction get_volume {
CURRENT_VOLUME=`pacmd list-sinks | awk '/\tvolume:/ {print $3}' | sed 's/%//'`
}
function volume_sign {
get_volume
volume $( expr $CURRENT_VOLUME $1 $STEP )
}
function mute {
pacmd set-sink-mute $SINK $1 > /dev/null
}
function toggle {
if [ "$ISMUTED" = "yes" ]; then
mute 0
else
mute 1
fi
}
function help {
echo "
Please provide correct input
default sink: $SINK
mute
unmute
toggle - toggle between mute states
+, up - volume up by $STEP
-, down - volume down by $STEP
live - enables live mode in wich every arow press modifies volume
"
}
# main
case "$1" in
"mute")
mute 1
;;
"unmute")
mute 0
;;
"toggle")
toggle
;;
"live")
live
;;
"+" | "up" )
volume_sign +
;;
"-" | "down" )
volume_sign -
;;
100 | [1-9][0-9] | [1-9] )
volume $1
;;
*)
help
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment