SEARCH

Enter your search query in the box above ^, or use the forum search tool.

You are not logged in.

#1 2011-01-28 22:37:47

chaanakya
#! Die Hard
Registered: 2010-12-30
Posts: 940

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
fi

To 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

XF86AudioRaiseVolume

The "Volume Down" key is called

XF86AudioLowerVolume

The mute key is called

XF86AudioMute

That should be it!  Now you can control your PulseAudio volume using your media keys smile  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
fi

To 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! smile

Last edited by chaanakya (2011-01-29 22:49:15)


Check out Musik - an easy-to-use text-to-music converter!
Join SpiderOak using this link and get an extra 1 GB free: https://spideroak.com/download/referral … 660e787ff1

Offline

Be excellent to each other!

#2 2011-02-15 13:31:13

wudu83
New Member
Registered: 2011-02-15
Posts: 1

Re: PulseAudio Volume Control with Media Keys

Thanks, that helped me but I want to have only one script and other things, so i modified yours.
- All in one script
- It creates the mute and volume file if not existing
- It creates them in .pulse (I don't want them in my home directory)
- limits the volume to a valid number (in your version the volume can get invalid numbers: 65537 to 65539)
- Volume can reach exactly 0 or 65536

Here's my script:

#!/bin/bash
#### Create ~/.pulse/mute if not exists
ls ~/.pulse/mute &> /dev/null
if [[ $? != 0 ]]
then
    echo "false" > ~/.pulse/mute
fi

####Create ~/.pulse/volume if not exists
ls ~/.pulse/volume &> /dev/null
if [[ $? != 0 ]]
then
    echo "65536" > ~/.pulse/volume
fi

CURVOL=`cat ~/.pulse/volume`     #Reads in the current volume
MUTE=`cat ~/.pulse/mute`          #Reads mute state

if [[ $1 == "increase" ]]
then
    CURVOL=$(($CURVOL + 3277)) #3277 is 5% of the total volume, you can change this to suit your needs.
    if [[ $CURVOL -ge 65536 ]]
    then
        CURVOL=65536        
    fi
elif [[ $1 == "decrease" ]]
then
    CURVOL=$(($CURVOL - 3277))
    if [[ $CURVOL -le 0 ]]
    then
        CURVOL=0        
    fi
elif [[ $1 == "mute" ]]
then
    if [[ $MUTE == "false" ]]
    then
        pactl set-sink-mute 0 1
        echo "true" > ~/.pulse/mute
    exit    
    else
        pactl set-sink-mute 0 0
        echo "false" > ~/.pulse/mute    
    exit
    fi
fi

pactl set-sink-volume 0 $CURVOL
echo $CURVOL > ~/.pulse/volume # Write the new volume to disk to be read the next time the script is run.

Last edited by wudu83 (2011-02-15 14:00:11)

Offline

#3 2011-02-15 14:05:08

chaanakya
#! Die Hard
Registered: 2010-12-30
Posts: 940

Re: PulseAudio Volume Control with Media Keys

Just as an fyi...volumes above 65536 aren't technically invalid...it's just that you're amplifying the signal.  I might have actually modified the script after I put this up, to let it go up to exactly 65536, but I'm not sure big_smile.  Anyway, thanks for the feedback/modifications smile


Check out Musik - an easy-to-use text-to-music converter!
Join SpiderOak using this link and get an extra 1 GB free: https://spideroak.com/download/referral … 660e787ff1

Offline

#4 2011-08-01 11:26:55

jeetu
New Member
Registered: 2011-08-01
Posts: 1

Re: PulseAudio Volume Control with Media Keys

I have modified the script so as too show volume bar with dzen2. You can check the modified script at https://bbs.archlinux.org/viewtopic.php?id=46608&p=2 post#30

Offline

#5 2012-02-10 18:18:45

uriel1998
Member
From: Dayton, OH
Registered: 2012-02-10
Posts: 36
Website

Re: PulseAudio Volume Control with Media Keys

I'm using a ruby script that actually changes all sinks at the same time - which comes in handy when you've suddenly started using an external USB speaker and all your keybound scripts work with only one sink.

I can't take credit for most of it - I forked and tweaked it a tiny bit.  The original script is here: https://gist.github.com/814634;  I couldn't get it to run because of something in the muting code, so I fixed that and added in some output in case someone wants to send it to notify-osd or somesuch.  Mine's at: https://gist.github.com/1791270.

Offline

#6 2012-06-26 00:30:19

arman
Member
Registered: 2011-12-08
Posts: 27

Re: PulseAudio Volume Control with Media Keys

Thank you for this; it is exactly that for which I am looking!! smile

Offline

#7 2013-05-31 18:33:27

folti
#! Member
Registered: 2013-05-26
Posts: 82

Re: PulseAudio Volume Control with Media Keys

bump.


http://crunchbang.org/forums/viewtopic. … 56#p312256


i'm having problems with this script. could you please help, wudu83 or chaanakya?


should ~/.pulse/mute and ~/.pulse/volume files have to be empty?

how to implement this solution?

Last edited by folti (2013-05-31 18:35:09)

Offline

#8 2013-05-31 19:22:30

xaos52
The Good Doctor
From: Planet of the @s
Registered: 2011-06-24
Posts: 4,602

Re: PulseAudio Volume Control with Media Keys

Yes.
Create empty files with the touch command:

touch ~/.pulse/.{mute,volume}

Offline

#9 2013-05-31 19:36:48

folti
#! Member
Registered: 2013-05-26
Posts: 82

Re: PulseAudio Volume Control with Media Keys

did i understand ok?

created
/home/folti/.pulse/mute
and
/home/folti/.pulse/volume

(here is the ls:)

folti@platon:~/.pulse$ ls
216c0d93934e66766ca72ac651a67716-card-database.tdb
216c0d93934e66766ca72ac651a67716-default-sink
216c0d93934e66766ca72ac651a67716-default-source
216c0d93934e66766ca72ac651a67716-device-volumes.tdb
216c0d93934e66766ca72ac651a67716-runtime
216c0d93934e66766ca72ac651a67716-stream-volumes.tdb
mute
volume
folti@platon:~/.pulse$ 

but, when i run the script zvuk:

folti@platon:~/scripts$ sh zvuk
zvuk: 4: zvuk: [[: not found
zvuk: 11: zvuk: [[: not found
/home/folti/.pulse/mute
/home/folti/.pulse/volume
zvuk: 19: zvuk: [[: not found
zvuk: 26: zvuk: [[: not found
zvuk: 33: zvuk: [[: not found
You have to specify a sink name/index and a volume
folti@platon:~/scripts$ 



i did

touch ~/.pulse/.{mute,volume}

and the uotput is the same:

folti@platon:~/scripts$ sh zvuk
zvuk: 4: zvuk: [[: not found
zvuk: 11: zvuk: [[: not found
/home/folti/.pulse/mute
/home/folti/.pulse/volume
zvuk: 19: zvuk: [[: not found
zvuk: 26: zvuk: [[: not found
zvuk: 33: zvuk: [[: not found
You have to specify a sink name/index and a volume
folti@platon:~/scripts$ 

i don't understand what am i doing wrong sad

Offline

#10 2013-05-31 19:41:25

folti
#! Member
Registered: 2013-05-26
Posts: 82

Re: PulseAudio Volume Control with Media Keys

funny thing is i have a lenovo e330 laptop which has same button for mute/unmute, and everything worked out of the box. i looked into laptop's rc.xml and there are NO DEFINITIONS of hotkeys to raise/lower/mute/unmute volume. and the notification window works as expected.

both laptop and desktop pc are running waldorf 11.


[SOLVED] on other topic i linked in previous post. didn't need this script.

Last edited by folti (2013-05-31 20:17:45)

Offline

Board footer

Powered by FluxBB

Copyright © 2012 CrunchBang Linux.
Proudly powered by Debian. Hosted by Linode.
Debian is a registered trademark of Software in the Public Interest, Inc.

Debian Logo