Skip to content
Browse files

Use python alsaaudio instead of shell aumix

  • Loading branch information...
1 parent 0f5196c commit c73df2f154467f254b7447b51a8a2516ab4d25d6 @akkana committed
Showing with 10 additions and 25 deletions.
  1. +10 −25 vol
View
35 vol
@@ -17,35 +17,21 @@ def Usage() :
print "Usage:", os.path.basename(sys.argv[0]), "[-v|-h]"
sys.exit(0)
-# Python alas doesn't have a simple way to do this.
-def system_readline (cmdstr) :
- fp = os.popen(cmdstr)
- s = fp.readline()
- fp.close()
- return s
-
-# Python is unbelievably bad at parsing integers!
-# There's apparently no built-in way to pull 70 out of the string "70,".
-def str2int(s) :
- pos = 0
- if s[0] in '-+' :
- pos = 1
- while pos < len(s):
- if s[pos] not in '1234567890':
- return int(s[0:pos])
- pos = pos + 1
+# First find a mixer. Use the first one.
+try :
+ mixer = alsaaudio.Mixer('Master', 0)
+except alsaaudio.ALSAAudioError :
+ sys.stderr.write("No such mixer\n")
+ sys.exit(1)
# Get the current system volume.
def get_volume() :
- volstr = string.split(system_readline("aumix -vq"), " ")
- return (str2int(volstr[1]), str2int(volstr[2]))
+ return mixer.getvolume()[0]
# Change the system volume (both channels at once).
def change_volume(scale) :
- newval = scale.get_adjustment().value
- # Force PCM to something semi-sane so that the volume won't clip.
- # This used to add a -W 100 but on some kernels that fails.
- os.system("aumix -w 78 -v " + str(int(newval)))
+ newval = int(scale.get_adjustment().value)
+ mixer.setvolume(newval, alsaaudio.MIXER_CHANNEL_ALL)
def key_press(w, event) :
if event.string == "q" :
@@ -88,6 +74,5 @@ if len(sys.argv) > 1 :
elif sys.argv[1] != "-h" :
Usage()
-(vLeft, vRight) = get_volume()
-create_window((vLeft + vRight)/2, vertical)
+create_window(get_volume(), vertical)

0 comments on commit c73df2f

Please sign in to comment.
Something went wrong with that request. Please try again.