2

I'm trying to add on volume to a current set volume, in this case we will say it's 80%. Using the alsaaudio module in Python, there is a function called getvolume

#declare alsaaudio
am = alsaaudio.Mixer()
#get current volume
current_volume = am.getvolume()

getvolume or current_volume in my case dumps a list such as [80L] for 80% volume. I am trying to add volume on to the current one like this,

#adds 5 on to the current_volume    
increase = current_volume + 5
am.setvolume(increase)

But my problem is, since it's a list I cannot strip or replace characters and since I am relatively new to Python, have no clue on how to strip the characters in the list and then add 5 on to that integer after converting.

I created a run-able example here:

import alsaaudio
am = alsaaudio.Mixer()
current_volume = am.getvolume()
print(repr(current_volume), type(current_volume), type(current_volume[0]))

It prints: ('[45L]', <type 'list'>, <type 'long'>), even though this issue is solved, thanks for your responses.

Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
Daquaney
  • 47
  • 7
  • For starters, fix your formatting. Whitespace is crucial in Python. – Mad Physicist Apr 18 '18 at 02:53
  • Secondly, show an actual runnable example and the resulting error with stacktrace. Ihave no idea what [80L] is supposed to mean. It seems to be invalid syntax (I just checked to make sure I'm not missing something). – Mad Physicist Apr 18 '18 at 02:55
  • 1
    Now that I've fixed your formatting for you, the most useful piece of information you can show is the result of `print(repr(current_volume), type(current_volume))`. That should clarify any ambiguity about what that value is, so I can presumably answer your question. – Mad Physicist Apr 18 '18 at 03:01
  • The "format [%L]" is something you made up. Please show the actual output. Kudos for responding quickly though. Could you make it `print(repr(current_volume), type(current_volume), type(current_volume[0]))`? – Mad Physicist Apr 18 '18 at 03:12
  • Thanks for the update. Could you provide the printout I asked for though? – Mad Physicist Apr 18 '18 at 03:20
  • You should probably mention that you are using Python 2 in the tags. `80L` is very much a Python 2 thing. I haven't used it for so long, I'd forgotten all about it. Kudos on a well posed question. – Mad Physicist Apr 18 '18 at 03:24

2 Answers2

1

Mixer.getvolume([direction])

Returns a list with the current volume settings for each channel. The list elements are integer percentages.

https://www.programcreek.com/python/example/91452/alsaaudio.Mixer

    mixer = alsaaudio.Mixer()
    value = mixer.getvolume()[0]
    value = value + 5
    if value > 100:
        value = 100
    mixer.setvolume(value)
Gang
  • 2,658
  • 3
  • 17
  • 38
0

According to the docs, Mixer.getvolume returns a list of integer percentages, with one element for each channel. The docs for Mixer.setvolume are less clear, but apear to imply that the first argument is an integer.

If my interpretation is correct, and you have only one channel, you can use list indexing to get the first element of the list as an integer. The other steps are as you show in the question. You may want to make sure that the incremented result is less than or equal to 100. The min function provides a standard idiom to do that:

import alsaaudio

am = alsaaudio.Mixer()
current_volume = am.getvolume()
new_volume = min(current_volume[0] + 5, 100)
am.setvolume(new_volume)

I've submitted issue #58 to pyalsaaudio to get the docs clarified a bit.

Mad Physicist
  • 107,652
  • 25
  • 181
  • 264