Topic: PulseAudio Volume Control with Media Keys
So I know there are many ways to change the volume when one is running ALSA. However, I like running PulseAudio, whose volume really shouldn't be changed with an ALSA volume control. So I decided to find a way to change the volume through the command line. I researched and found pactl, which lets the person mute, unmute, and change the volume, among a whole lot of other things. The only problem was that pactl doesn't accept percentage increments like ALSA volume changers do. That is, it requires an absolute volume between 0 and 65536. Therefore, I hacked up a little script which changes the volume in 5% increments (this can be changed). Here is the script:
#!/bin/bash
declare -i CURVOL=`cat ~/.volume` #Reads in the current volume
if [[ $1 == "increase" ]]
then
CURVOL=$(($CURVOL + 3277)) #3277 is 5% of the total volume, you can change this to suit your needs.
fi
if [[ $1 == "decrease" ]]
then
CURVOL=$(($CURVOL - 3277))
fi
if [[ $1 == "mute" ]]
then
pactl set-sink-mute 0 1
exit
fi
if [[ $1 == "unmute" ]]
then
pactl set-sink-mute 0 0
exit
fi
if [[ $CURVOL -le 65540 && $CURVOL -ge 0 ]] # Check to see if the volume is a valid number (65540 was needed in this case because of how I rounded the increment)
then
pactl set-sink-volume 0 $CURVOL
echo $CURVOL > .volume # Write the new volume to disk to be read the next time the script is run.
exit
fiTo use, simply copy and paste the code to a new file and save it somewhere you can find it. Now, mark it executable.
GRAPHICAL WAY
1) Right-click on the file
2) Click on Properties
3) Click on the Permissions tab
4) Check the box at the bottom
COMMAND-LINE WAY
1)
cd to the directory containing the script
2)
chmod +x scriptname-------------------------------------------------------------------------------------------------------------------------------------------------------------------
Now, all you have to do is create a keyboard shortcut in openbox's config file (~/.config/openbox/rc.xml).
The "Volume Up" key is called
XF86AudioRaiseVolumeThe "Volume Down" key is called
XF86AudioLowerVolumeThe mute key is called
XF86AudioMuteThat should be it! Now you can control your PulseAudio volume using your media keys
Feel free to leave comments or questions!
- Chaanakya
EDIT: Forgot to put up the mute/unmute script. Because there is no mute toggle for PulseAudio, you need another script for that. Here's the script:
#!/bin/bash
MUTE=`cat ~/.mute`
test "$MUTE" = "1"
RETURN=$?
if [[ $RETURN -eq 0 ]]
then
/path/to/previous/script unmute
echo "0" > ~/.mute
exit
fi
if [[ $RETURN -eq 1 ]]
then
/path/to/previous/script mute
echo "1" > ~/.mute
exit
fiTo use this script, simply repeat the scripts you did for the previous script. However, make sure to replace /path/to/previous/script with the actual path. That should be it! ![]()
Last edited by chaanakya (2011-01-29 22:49:15)
Join SpiderOak using this link and get an extra 1 GB free: https://spideroak.com/download/referral 660e787ff1