Skip to content

Instantly share code, notes, and snippets.

@pawelkl-zz
Created May 6, 2013 17:00
Show Gist options
  • Save pawelkl-zz/5526408 to your computer and use it in GitHub Desktop.
Save pawelkl-zz/5526408 to your computer and use it in GitHub Desktop.
i3 volume control
#!/bin/bash
STEPS=10
STEP=$((65536/$STEPS))
SINK=1
MAXVOL=65536
STATE_FILE=.mute
ICON_NAME=""
MUTED=""
getMasterVolume ()
{
pacmd list-sinks | awk 'BEGIN{i=1} /^[[:space:]]*volume:[[:space:]]*/ {if (i==2) { printf("%d",$3)} ;i=i+1}'
}
setMasterVolume () {
pacmd set-sink-volume $SINK $1 > /dev/null
osd
}
volumeUp ()
{
NEWVOL=$(($(getMasterVolume)*655 + $STEP))
[[ $NEWVOL -gt $MAXVOL ]] && NEWVOL=65536
setMasterVolume $NEWVOL
}
volumeDown ()
{
NEWVOL=$(($(getMasterVolume)*655 - $STEP))
[[ $NEWVOL -lt 0 ]] && NEWVOL=0
setMasterVolume $NEWVOL
}
mute () {
# [[ -e $STATE_FILE ]] && rm -f $STATE_FILE;
echo $(getMasterVolume) > $STATE_FILE;
pacmd set-sink-mute $SINK 1 > /dev/null
# volume=$(getMasterVolume)
# notify-send "Muted" -t 950 -i notification-audio-volume-muted -h int:value:$volume
MUTED=1
osd
}
unmute () {
pacmd set-sink-mute $SINK 0 > /dev/null
# VOL=0
# [[ -e $STATE_FILE ]] &&
VOL=$(cat $STATE_FILE)
rm -f $STATE_FILE;
# echo $VOL
# [[ VOL -gt 0 ]] && VOL=$(getMasterVolume)
# echo "restored" + $VOL
setMasterVolume $VOL
# volume=$(getMasterVolume)
# notify-send "Volume" -t 950 -i notification-audio-volume-medium -h int:value:$volume
}
state() {
pacmd list-sinks | awk 'BEGIN{i=1} /^[[:space:]]*muted:[[:space:]]*/ {if (i==2) { printf("%s",$2)} ;i=i+1}'
}
# Main Interface
up () { volumeUp; }
down () { volumeDown; }
toggle () {
STATE=$(state)
[[ $STATE = "yes" ]] && unmute || mute;
# && rm -f $STATE_FILE;
echo $(state);
}
get () { getMasterVolume; }
set () { setMasterVolume $1; }
osd ()
{
volume=$(getMasterVolume)
if [ "$ICON_NAME" = "" ]; then
if [ "$volume" = "0" ]; then
ICON_NAME="notification-audio-volume-off"
else
if [ "$volume" -lt "33" ]; then
ICON_NAME="notification-audio-volume-low"
else
if [ "$volume" -lt "67" ]; then
ICON_NAME="notification-audio-volume-medium"
else
ICON_NAME="notification-audio-volume-high"
fi
fi
fi
fi
[[ $MUTED = 1 ]] && ICON_NAME="notification-audio-volume-muted";
echo $MUTED
notify-send "Volume $volume" -t 1 -i $ICON_NAME -h int:value:$volume -h string:synchronous:volume
# echo $volume;
}
getconky(){ [[ -e $STATE_FILE ]] && printf "%-3s" get || echo " x";}
$@
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment