You are not logged in.
I wrote this script to work with my laptop's multimedia keys. It requires python, pygtk, and alsa-utils. It is a non-interactive program that modifies the volume and then pops up a little gui notifier similar to programs like keytouch (but much simpler and less bloated). It was designed to work by binding multimedia keys (using programs like xbindkeys or any WM's/DE's keybinding apps) to the script and that is probably the only real convenient use.
Here is what it looks like (pops up in the middle of the desktop on top of everything else). Don't worry about the color of the bar and such, it is programmed with gtk2, so it will inherit whatever your gtk theme looks like:
Here is the script. To use: just dump into a text file called "changevol", chmod +x it, and put it in the PATH somewhere:
#!/usr/bin/env python
import pygtk
import gtk
import gobject
import commands
import sys
import os
import re
import getopt
def err(msg):
print msg
sys.exit(1)
def usage():
print '''
Usage: changevol [options] [argument]
Options:
-i, --increase increase volume by `argument'
-d, --decrease decrease volume by `argument'
-c, --control specify the mixer control by `argument' (default Master)
use only to modify a mixer other than Master
-t, --toggle toggle mute on and off
-s, --status display current volume status without modifying it
-q, --quiet don't display the gtk progressbar, just change the volume
-b, --backlight adjust the backlight using xbacklight
-h, --help display this help message
Note:
Volume increases and decreases won't be exact due to amixer oddities.
'''
sys.exit(0)
class GetVolInfo():
def __init__(self, command):
self.amixeroutput = commands.getoutput(command)
if re.compile("off]$", re.M).search(self.amixeroutput, 1):
self.realvol = "0"
self.endlabel = "Mute"
else:
self.tempvolarray1 = self.amixeroutput.split("[")
self.tempvolarray2 = self.tempvolarray1[1].split("%")
self.realvol = self.tempvolarray2[0]
self.endlabel = self.realvol + " %"
self.percent = float(self.realvol)/100
self.label = "Volume " + self.endlabel
class ProgressBar:
def timeout_callback(self):
gtk.main_quit()
return False
def __init__(self, fraction, label):
self.window = gtk.Window(gtk.WINDOW_POPUP)
self.window.set_border_width(0)
self.window.set_default_size(180, -1)
self.window.set_position(gtk.WIN_POS_CENTER)
timer = gobject.timeout_add(1000, self.timeout_callback)
self.bar = gtk.ProgressBar()
self.bar.set_fraction(fraction)
self.bar.set_orientation(gtk.PROGRESS_LEFT_TO_RIGHT)
self.bar.set_text(label)
self.bar.show()
self.window.add(self.bar)
self.window.show()
#Run through parameters, set variables and such
CONTROL = "Master"
AMIXEROPTION = "unset"
QUIET = "NO"
BACKLIGHT = "NO"
if (len(sys.argv) < 2):
usage()
try:
opts, args = getopt.getopt(sys.argv[1:], "bqhtsc:i:d:", ["backlight" "quiet", "help", "toggle", "status", "control=", "increase=", "decrease="])
except getopt.GetoptError:
err("Incorrect usage, see changevol --help.")
if (len(opts) == 0):
err("Incorrect usage, see --help.")
for opt, arg in opts:
if opt in ("-h", "--help"):
usage()
elif opt in ("-q", "--quiet"):
QUIET = "YES"
elif opt in ("-b", "--backlight"):
BACKLIGHT = "YES"
elif opt in ("-t", "--toggle"):
AMIXEROPTION = "toggle"
elif opt in ("-s", "--status"):
INCREMENT = "0"
AMIXEROPTION = INCREMENT + "%+"
elif opt in ("-c", "--control"):
CONTROL = arg
elif opt in ("-i", "--increase"):
INCREMENT = arg
AMIXEROPTION = INCREMENT + "%+"
elif opt in ("-d", "--decrease"):
INCREMENT = arg
AMIXEROPTION = INCREMENT + "%-"
else:
err("Incorrect usage, see --help")
if (AMIXEROPTION == "unset"):
err("No volume changing action has been dictated. See changevol --help.")
command = "amixer set " + CONTROL + " " + AMIXEROPTION
#Execution
volume = GetVolInfo(command)
if (QUIET == "NO"):
ProgressBar(volume.percent, volume.label)
gtk.main()
if (BACKLIGHT == "YES"):
os.execv('/usr/bin/xbacklight', ['placeholder', '-set', volume.realvol])
Here is the usage synopsis (it can be accessed with changevol --help):
Usage: changevol [options] [argument]
Options:
-i, --increase increase volume by `argument'
-d, --decrease decrease volume by `argument'
-c, --control specify the mixer control by `argument' (default Master)
use only to modify a mixer other than Master-t, --toggle toggle mute on and off
-s, --status display current volume status without modifying it
-q, --quiet don't display the gtk progressbar, just change the volume
-b, --backlight adjust the backlight using xbacklight
-h, --help display this help messageNote:
Volume increases and decreases won't be exact due to amixer oddities.
Edit: Changed mute handling a bit.
Edit2: Added a -c, --control option for an amixer interface other than Master
Edit3: Added a -s, --status option to just display the current volume status without modifying it
Edit4: Added a -q, --quiet option to not show the progress bar and just change the volume
Edit5: Added a -b, --backlight option to adjust the backlight along with the volume change (someone asked for it, so why the hell not right?)
Last edited by bruenig (2008-02-03 06:43:07)
Offline
very nice. as soon as a boot up my laptop tonight i'll be playin with this. thanks bruenig
archlinux - please read this and this — twice — then ask questions.
--
http://rsontech.net | http://github.com/rson
Offline
Thank you for great script. Maybe you should make name of mixer control configurable. For example I don't have 'Master' control, instead I use 'Front'.
Offline
Great stuff... thanks!
thayer williams ~ cinderwick.ca
Offline
Thank you for great script. Maybe you should make name of mixer control configurable. For example I don't have 'Master' control, instead I use 'Front'.
I have changed the script in the first post to provide that option, tell me if it works.
Last edited by bruenig (2007-12-30 20:43:54)
Offline
very awesome!!
Thanks
Offline
A handy script, thanks!
Offline
I gave it a spin but every time I get the same answer: "No volume changing action has been dictated."
Can you provide an example of issuing the command using xbindkeys? or even what I should be issuing on the command line? I must be doing something wrong ... and I do not feel like studying the code to figure it out since you can give me the answer just as fast
Offline
I gave it a spin but every time I get the same answer: "No volume changing action has been dictated."
Can you provide an example of issuing the command using xbindkeys? or even what I should be issuing on the command line? I must be doing something wrong ... and I do not feel like studying the code to figure it out since you can give me the answer just as fast
The usage synopsis is in the first post or available by just doing "changevol" or changevol -h|--help.
But to increase the volume by 5:
changevol -i 5
To decrease the volume by 5:
changevol -d 5
To toggle mute on and off:
changevol -t
If you need to specify a mixer other than "Master" which is the default mixer, you use the -c flag, so say you needed to use the mixer "Front" to increase by 5:
changevol -c Front -i 5
I see there is a problem in the code where it says No volume changing action has been dictated when I want it to say Incorrect usage, see changevol --help. I will try to fix that. Still learning python.
Edit: I have changed it to see if getopts returns no options and then if so print back the Incorrect usage error message. That should do a lot to direct people to the right, direction. I have also, changed the No volume changing action one, to also direct to --help.
Last edited by bruenig (2007-12-31 03:27:37)
Offline
Thanks for the reply.
Well ... this is most interesting. I issued the command: changevol -c Front -i 1 and what I got was the message I posted.
I executed the script on the command line like this: python volctl.py changevol -c -i 1
where volctl.py is the file where I saved your script, but no joy.
p.s. I just did another test with volctl.py -c -i 1 and that works just fine. Thanks! /happy camper/
Last edited by ralvez (2007-12-31 03:26:32)
Offline
Thanks for the reply.
Well ... this is most interesting. I issued the command: changevol -c Front -i 1 and what I got was the message I posted.
I executed the script on the command line like this: python volctl.py changevol -c -i 1
where volctl.py is the file where I saved your script, but no joy.p.s. I just did another test with volctl.py -c -i 1 and that works just fine. Thanks!
/happy camper/
Oh see, the initial post assumes that you actually name the script "changevol" on your system. But also, you should be able to leave the "-c" flag out unless you need some other mixer than "Master". And according to your last post you are just doing -c but without specifying another mixer. So just volctl.py -i 1 (or changevol -i 5) would be identical.
Last edited by bruenig (2007-12-31 03:30:53)
Offline
Yes. I realized that afterwards.
Thanks again. Wonderful program. I have been about to code something like that for awhile ... good timing!!
R
Offline
sergejx wrote:Thank you for great script. Maybe you should make name of mixer control configurable. For example I don't have 'Master' control, instead I use 'Front'.
I have changed the script in the first post to provide that option, tell me if it works.
Yes, it works. Thank you.
Offline
hm, Is there a way to call amixer to definitely set the volume to a certain percent? This works almost as I expect it to, bruenigvol -i increases, bruenigvol -d decreases but it changes not in units of 1 or 5 but in units of 3, so "bruenigvol -i 1" would increase from 87 to 90, or "bruenigvol -d 5" would decrease from 90 to 81 or 86, it seems kinda random. The volume of the music coming out the speakers is only slightly decreased, but the actual percentage fluctuates a bit.
PS: Could you please add some type of status message? Maybe changevol -s/--status would popup with the volume bar and display the current volume, etc but not modify it
EDIT: I should read more seeing as you said that it wouldn't be exact, right there in the help! Silly me...
Last edited by zenix (2008-01-11 00:11:13)
Offline
hm, Is there a way to call amixer to definitely set the volume to a certain percent? This works almost as I expect it to, bruenigvol -i increases, bruenigvol -d decreases but it changes not in units of 1 or 5 but in units of 3, so "bruenigvol -i 1" would increase from 87 to 90, or "bruenigvol -d 5" would decrease from 90 to 81 or 86, it seems kinda random. The volume of the music coming out the speakers is only slightly decreased, but the actual percentage fluctuates a bit.
PS: Could you please add some type of status message? Maybe changevol -s/--status would popup with the volume bar and display the current volume, etc but not modify it
EDIT: I should read more
seeing as you said that it wouldn't be exact, right there in the help! Silly me...
Ha yeah, amixer is weird. Someone explained to me at one point why for some reason it is unable to change precisely but I have forgotten why.
Also, I modified the script in the first post to work with -s, --status. Tell me if it works.
Last edited by bruenig (2008-01-12 05:37:46)
Offline
I'm happy to report that it works just as expected. I did a bit of searching yesterday, why didn't you use something like pyalsa or pyalsaaudio?
Offline
I'm happy to report that it works just as expected. I did a bit of searching yesterday, why didn't you use something like pyalsa or pyalsaaudio?
No idea such a thing existed. I will look into it.
Edit: Adding another library for a script as trivial as this seems unnecessary. Because I am not doing anything really complex with alsa, the pyalsa wrapper would just be unneeded bloat really.
Last edited by bruenig (2008-01-15 17:53:46)
Offline
I don't know when this started but I am getting this error now when trying to use the -t mute option.
Traceback (most recent call last):
File "/home/bmartin/.bin/changevol", line 116, in <module>
volume = GetVolInfo(command)
File "/home/bmartin/.bin/changevol", line 45, in __init__
self.tempvolarray2 = self.tempvolarray1[1].split("%")
IndexError: list index out of range
Offline
request fulfilled. thanks again!
Last edited by rson451 (2008-01-31 00:19:49)
archlinux - please read this and this — twice — then ask questions.
--
http://rsontech.net | http://github.com/rson
Offline
I don't know when this started but I am getting this error now when trying to use the -t mute option.
Traceback (most recent call last): File "/home/bmartin/.bin/changevol", line 116, in <module> volume = GetVolInfo(command) File "/home/bmartin/.bin/changevol", line 45, in __init__ self.tempvolarray2 = self.tempvolarray1[1].split("%") IndexError: list index out of range
change the channel from Master to PCM or Front
Zygfryd Homonto
Offline
I have the same error.. tried changing to PCM but no luck
Offline
I have the same error.. tried changing to PCM but no luck
Same problem here also. I have mine set to PCM.
Offline
To the people with problems: I don't know a lick about python. I actually wrote this trying to make myself learn it, but got bored of it. If someone else knows what the hell is going on, please fix it.
Offline
great script, worked for me without problem, tnx!
Offline
if (BACKLIGHT == "YES"):
self.label = "Backlight " + self.endlabel
else:
self.label = "Volume " + self.endlabel
I added that where you had
self.label = "Volume " + self.endlabel
and it worked...
Offline