Page 1 of 1
Muting sound output using GPIO and Python
Posted: Sun Jan 13, 2013 5:13 am
by rbeck
Hello,
I have just purchased my rpi and very new to python and linux in general. I have been able to get my audio to stream from my iPad using shairport to rpi. However I am having difficulties trying to control the volume/mute on the rpi from the GPIO inputs. Can this be done using python? I tried writing a program to start by muting the output by importing the alsaaudio module and trying the following.
import alsaaudio
m = alsaaudio.Mixer()
m.setmute(1)
I was going to add the GPIO inputs after I am able to get this to work. Am I going about this correctly? Should I be using something other than python?
Please help.
rbeck
Re: Muting sound output using GPIO and Python
Posted: Sun Jan 13, 2013 1:04 pm
by -rst-
Well... What application do you use that plays the stream? Does calling the alsa-mixer from the command-line (if at all possible) affect the volume of the playing app output (I am not exactly sure how these components link up in real life)? If it does, there should be no issue calling the mixer from Python code (provided that the Python mixer library works as expected). And obviously, if you get that bit of code to work, it is just some more coding work to get it together with GPIO ...you might want to do a similar tester app to get to grips with GPIO in Python before combining these two parts.
Re: Muting sound output using GPIO and Python
Posted: Mon Jan 14, 2013 2:57 am
by rbeck
I believe shairport is playing the stream. I can also call the mixer from the command prompt. If I start the mixer I can adjust the master volume from my headless keyboard. Now I have to figure it out in python. I get very confused with the difference between pyalsa, alsaaudio??? Can I this be achived with python? Should I used something else?
Re: Muting sound output using GPIO and Python
Posted: Mon Jan 14, 2013 2:45 pm
by -rst-
Not very familiar with these, but looks like 'pyalsa' and 'pyalsaaudio' are two sort of competing Python libraries for the same task... pyalsa (
http://pypi.python.org/pypi/pyalsa) does not seem to have mixer support for now, so would be best to go for pyalsaaudio (
http://pyalsaaudio.sourceforge.net/pyalsaaudio.html).
Since there are handy GPIO libraries (and good examples) for Python and looks like controlling the alsa mixer should work from Python as well, I think Python is a good choice for this!
After you install pyalsaaudio, just find out your mixer device name, run the Python interpreter and try this:
Code: Select all
>>> import alsaaudio
>>> m = alsaaudio.Mixer('<your mixer name here>')
>>> m.setmute(1)
and see (well, hear

if the output gets muted (
http://pyalsaaudio.sourceforge.net/liba ... er-objects)...
Re: Muting sound output using GPIO and Python
Posted: Tue Jan 15, 2013 3:03 am
by rbeck
Thanks rst,
I was close. The name was all that was missing in the alsaaudo.Mixer('PCM'). That however took me a little messing around to figure out the name. Whats funny is that when I go to unmute with m.setmute(0) the mixer the rpi crashes. I may try a fresh install. Anyway thank you for putting me on the right track.
Re: Muting sound output using GPIO and Python
Posted: Tue Jan 15, 2013 10:48 am
by -rst-
There could of course be a bug somewhere in the chain... So the setmute(1) worked? You could of course look at setvolume() to get ahead, if unmuting cannot be fixed for the moment.