X11 volume control


Please note that this blog has been moved.

Now it has its own domain: mynixworld.info🙂

If you want to read the latest version of this article (recommended) please click here and I open the page for you.

In Ubuntu I had a volume control applet that allows me to change the volume or to toggle the volume (mute/unmute). There are many plugins/applets capable of doing this (on almost any Linux DM) but if you are like me then you will prefer to keep your desktop as lightweight as possible. Instead of loading an special applet at session start-up just to change the volume few times (if no time at all) per day I think that the following solution will give you the same results without keeping your RAM and CPU occupied by a resident daemon which you are not using at all (or maybe just few times per day):

  • create a simple bash script (eg: volumctrl) that will handle the volume control stuff
  • create a keyboard shortcut (eg: keyboard function key Raise Volume or any other that suit to you) that we will use for raising the volume; point the shortcut to our script with the following parameter: volumctrl “>”
  • create a keyboard shortcut (eg: keyboard function key Lower Volume or any other that suit to you) that we will use for lowering the volume; point the shortcut to our script with the following parameter: volumctrl “<“
  • create a keyboard shortcut (eg: keyboard function key Mute Volume or any other that suit to you) that we will use for toggling the volume (mute/unmute); point the shortcut to our script with the following parameter: volumctrl “”
#!/bin/bash

case $1 in
">" ) cmd="5%+"
icon="audio-volume-high.png"
status="increased with $cmd"
;;
"<" ) cmd="5%-"
icon="audio-volume-low.png"
status="decreased with $cmd"
;;
"" ) cmd="toggle"
toggle=$( amixer -c 0 sget Master|awk '/Front.*[Left|Right]/ && /[[off]]$/ {};END {print $NF}')
if [ "$toggle" != "[off]" ];then
icon="audio-volume-muted.png"
status="muted"
else
icon="audio-volume-medium.png"
status="unmuted"
fi
;;
esac

amixer set Master "$cmd" >/dev/null 2>&1

notify-send -i "/usr/share/icons/Tango/48x48/status/$icon" -t 1000 "Volume control" "Volume has been $status"

Note: to make it working you will need the “amixer – command-line mixer for ALSA soundcard driver” otherwise you have to adapt the script based on other command-line tool you might use.

So anytime you press any of the above keyboard shortcuts the volume will be increasing, decreasing or toggled to mute/unmute. Also a notification balloon will inform you about this:

increase volume
When muting the volume the balloon will looks like:

volumctrl
Important: The icons shown above depends on your DM (mine comes from Xfce Tango icon set) but you can easily adapt the above script to use your preferred icons.

About Eugen Mihailescu

Always looking to learn more about *nix world, about the fundamental concepts of arithmetic, algebra and geometry. I am also passionate about programming, database and systems administration.
This entry was posted in linux, X11 and tagged , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s