Reputation: 47
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.
Upvotes: 2
Views: 1150
Reputation: 114548
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.
Upvotes: 0
Reputation: 2778
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)
Upvotes: 1