Thursday, February 7, 2013

pavolume.sh - control the active sink volume for pulseaudio

If you have multiple audio output devices, and you use keyboard bindings to control the master volume -> your system may get confused which master volume you want and select the first one by default. This script finds the current default active sink and changes the volume with it.

Copy it to a bin dir like `/usr/local/bin` execute with :
  • pavolume.sh --up
  • pavolume.sh --down
  • pavolume.sh --togmute  (this will change the mute status to the opposite of what it is currently - useful for a single key that does both)
  • pavolume.sh --sync   (this will sync all of your current apps using the current active/default output to the same volume level)

In the beggining of the script you'll see four useful settings/variables :
  1. The 'osd' variable. If you want to display an OSD message when changing volume set to yes. It's setup for kde and the KOSD package - so you might have to install that.
  2. The 'inc' variable. This is the increment that each volume up/down request will preform. 5 is default.
  3. The 'capvol' variable. This will cap the max volume to 100% (since pulseaudio by default allows you to go over this limit). Defaults to 'yes'.
  4. The 'maxvol' variable. This will cap the maximum volume to another percent (capvol overrides this if set to yes). Pulse will keep increasing the volume to several thousand percent, so here you can set a much more respectable number. Defaults to '200'.
  5. The 'autosync' variable. This will automatically sync all your sink-inputs (apps) with the current volume of your output (speakers) whenever you change the volume. This is useful if you manage multiple outputs and have issues with the app volumes becoming out of sync with the output (they should move up/down together). If you like keeping apps at different volumes then you should change this to no, and maybe bind a keystroke for manual sync if you so desire.

If you wish to use kosd it's in portage for Gentoo:
  • emerge -av kosd
  • After installation you have to restart kde to get it to load. 

If you wish to remove the volume cap (IE - have your max volume over 100%) you will need to apply the following patch to kosd. If you're using Gentoo (and you probably should be), all you need to do is put this patch in the `/etc/portage/patches/kde-misc/kosd/` directory.

You can also download the maxvol.patch here : http://pastebin.com/yCgVtmUZ




  • Afterwards go to systemsettings -> Shortcuts and Gestures -> Custom Shortcuts -> Add new shortcuts bound to the commands listed above.

You can also download the script here : http://pastebin.com/ZqWt3A5q

10 comments:

  1. Thank you! Worked on ubuntu 13.10 as well, with a couple of changes. I was also able to assign shortcuts to up/down/mute with a non-media keyboard! :-)

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. Thanks for this script!

    I noticed a small bug if there is a sink with an index > 9 (e.g. a bluetooth device that you frequently disconnect and reconnect).

    If the default sink is 1 and there is another one indexed 10, getCurVol return the voulme of both. This happens because
    pacmd list-sinks |grep 'index: 1'
    matches 'index: 10' as well. To avoid that the -w option of grep can be used, so the function should read:

    function getCurVol {

    curVol=`pacmd list-sinks |grep -wA 15 'index: '${active_sink}'' |grep 'volume:' |egrep -v 'base volume:' |awk -F : '{print $3}' |grep -o -P '.{0,3}%'|sed s/.$// |tr -d ' '`

    }

    [I removed my previous comment because of a typo. Couldn't an edit button.]

    ReplyDelete
    Replies
    1. Thanks for the comment; I have updated it with the -w flag on grep, as well as making it compatible with new pulseaudio pactrl commands (no longer utilizes the --)

      Delete
  4. Changes for Ubuntu 16.04:
    1. Escape * in active_sink regex: awk '/\* ......
    2. Change all set-sink-volumes from -- X% to "X%" (-- is no longer valid)
    Also had to dos2unix it for some reason.

    ReplyDelete
  5. line 41: qdbus: command not found

    but it shows the OSD.

    I just set osd to no, and it keeps showing the OSD.

    Appart from that it works.

    Thnx!

    btw I am running XUbuntu 18.4

    ReplyDelete
  6. try this:
    pactl set-sink-volume @DEFAULT_SINK@ +5%

    ReplyDelete
  7. This script required small changes to work with my ubuntu 18.04 and awesome window manger.

    My fork of the script: https://gist.github.com/mrozo/4d90cb21979520004db8ad6d8691d489

    This script rocks ;]

    ReplyDelete
  8. For anyone else still finding this, I have a fork of this on GitHub with more features and bug fixes: https://github.com/marioortizmanero/polybar-pulseaudio-control

    ReplyDelete