Volume change notifications in Openbox

If you’re here, you probably want to know how to configure notifications for volume change actions, just like it is in most of current less-geeky GNU/Linux distributions and window managers.

So if you press the volume up/volume down button, you want to see some notification with a beautiful icon and current volume percentage, like this: Volume notification

And if you press the mute button you are expecting something comparable to: Muted volume notification

A note of introduction

I’m using Arch Linux , Openbox window manager and xfce4-notifyd (though it should work exactly the same with standard notification-deamon, I encourage you to try xfce4-notifyd which is cool and easily configurable thanks to xfce4-notifyd-config) and ALSA.

But most of the following instructions should work also with other distributions (minor changes may be needed), window managers (possibly more changes) or with OSS (lots of changes, I guess).

Our magic script

I’ll give you the whole code first (a bash script) and then try to explain it.

So here’s the code:

#!/bin/bash

step=3

if [[ $# -eq 1 ]]; then
  case $1 in
    "up")
      amixer set Master $step+;;
    "down")
      amixer set Master $step-;;
    "toggle")
      amixer set Master toggle;;
    *)
      echo "Invalid option";;
  esac
fi

muted=`amixer get Master|tail -n1|sed -E 's/.*\[([a-z]+)\]/\1/'`
volume=`amixer get Master|tail -n1|sed -E 's/.*\[([0-9]+)\%\].*/\1/'`

if [[ $muted == "off" ]]; then
  notify-send "Muted" -t 1000 -i notification-audio-volume-muted -h int:value:$volume
else
  notify-send "Volume" -t 500 -i notification-audio-volume-medium -h int:value:$volume
fi

The script accepts 3 possible arguments: up (increase volume), down (decrease volume), toggle (mute/unmute). If it’s run without any arguments it just sends the proper notification.

Now more about the most magic lines

Changing volume and muting

These lines (respectively) increase (by $step), decrease (by $step) and mute/unmute the Master channel.

amixer set Master $step+
amixer set Master $step-
amixer set Master toggle

The part:

muted=`amixer get Master|tail -n1|sed -E 's/.*\[([a-z]+)\]/\1/'`
volume=`amixer get Master|tail -n1|sed -E 's/.*\[([0-9]+)\%\].*/\1/'`

determines whether the channel is muted and sets the muted variable to “on” or “off” and sets the volume variable to the current volume (in percent). These lines just extract some parts of the amixer get Master command which output looks similar to this one:

Simple mixer control 'Master',0
  Capabilities: pvolume pvolume-joined pswitch pswitch-joined penum
  Playback channels: Mono
  Limits: Playback 0 - 74
  Mono: Playback 30 [41%] [-44.00dB] [on]

If in your case it looks differently, you may need to change these lines a bit.

And finally:

notify-send "Muted" -t 1000 -i notification-audio-volume-muted -h int:value:$volume

and

notify-send "Volume" -t 500 -i notification-audio-volume-medium -h int:value:$volume

are responsible for creating the notifications. The -t parameter sets the timeout (in milliseconds), so you may want to change the value according to your own preferences.

Setting key bindings

Put the script in some place listed in your $PATH. In my case it’s in ~/bin (you may want to put it e.g. in /usr/local/bin/ to make it available for all users). Let’s assume you called the script volume-change.sh. Run:

chmod +x /path/to/your/location/volume-change.sh

and test it from the console:

cd /path/to/your/location
./volume-change.sh
./volume-change.sh up
./volume-change.sh down
./volume-change.sh toggle

If it works, please proceed. If it doesn’t, I wish you happy debugging ;)

Now, open ~/.config/openbox/rc.xml in your favorite editor and find the (…) section. You need to modify it so that it includes the following lines:

<!-- Volume control -->
<keybind key="XF86AudioRaiseVolume">
  <action name="Execute">
    <command>volume-change.sh up</command>
  </action>
</keybind>
<keybind key="XF86AudioLowerVolume">
  <action name="Execute">
    <command>volume-change.sh down</command>
  </action>
</keybind>
<keybind key="XF86AudioMute">
  <action name="Execute">
    <command>volume-change.sh toggle</command>
  </action>
</keybind>

And that’s it. Now reconfigure (from the Openbox menu: System -> Reconfigure Openbox) or restart Openbox. Hopefully, from now on, pressing the volume control buttons should display proper notifications on your screen.