Skip to content

Instantly share code, notes, and snippets.

@andrelaszlo
Created September 4, 2011 21:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrelaszlo/1193541 to your computer and use it in GitHub Desktop.
Save andrelaszlo/1193541 to your computer and use it in GitHub Desktop.
Control the alsa volume (up/down/mute/set to x%) from the command line or in scripts
#!/bin/bash
AMIX=$(which amixer)
CHANNEL="Master"
MAX_VOL=65536
AFTER_MUTE=$(($MAX_VOL/5))
VOL_STEP=$(($MAX_VOL/20))
function get_volume {
$AMIX | egrep -i "^ *front left" | cut -f 6 -d " " | head -n 1
}
function get_muted {
MUTE=$($AMIX | egrep -i "^ *front left" | cut -f 8 -d " " | head -n 1)
if [[ $MUTE == "[on]" ]]; then
echo "false"
else
echo "true"
fi
}
function volume_percent {
echo $((100*$(get_volume)/$MAX_VOL))
}
function set_volume {
$AMIX set $CHANNEL $1 > /dev/null
#echo "New volume: $1"
}
function volume_up {
set_volume $(($(get_volume)+$VOL_STEP))
}
function volume_down {
if [[ $(volume_percent) != 0 ]]; then
set_volume $(($(get_volume)-$VOL_STEP))
fi
}
function volume_mute {
if [[ "$(get_muted)" == "true" ]]; then
$AMIX set $CHANNEL unmute > /dev/null
if [[ $(volume_percent) == 0 ]]; then
set_volume $AFTER_MUTE
fi
else
$AMIX set $CHANNEL mute > /dev/null
fi
}
function volume_status {
PERCENT=$(volume_percent)
if [[ "$(get_muted)" == "true" ]]; then
echo "Volume $PERCENT% (muted)"
else
echo "Volume $PERCENT%"
fi
}
function update_qtile {
python2 -c "from libqtile.command import Client;\
Client().widget['volume'].update(\"$1\")"
}
function display_help {
echo "Usage:"
echo "$0 [up | down | mute | set {0-100} | help ]"
echo "$0 without arguments will display current volume"
}
case "$1" in
up)
volume_up
volume_status
;;
down)
volume_down
volume_status
;;
mute)
volume_mute
volume_status
;;
help)
display_help
;;
set)
if [[ ($2 -lt 101) && ($2 -gt -1) ]]; then
NEW_VOL=$((($2*MAX_VOL)/100))
set_volume $NEW_VOL
volume_status
else
display_help
fi
;;
*)
volume_status
esac
#update_qtile "$(volume_status)"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment