Is anyone successfully using Python for audio on the Raspberry Pi?
I'm interested in doing Python development on the RP but I'm running into an interesting issue where the unit freezes(!) when I actually try to write audio data to the audio card.
I have successfully run e.g. aplay /usr/share/sounds/alsa/Front_Center.wav and heard the sound come out of the headphone jack (as I assigned it) so I'm fairly sure the unit is set up correctly for sound.
However, in Python the unit becomes unresponsive when try to output audio. I should add that I'm doing all my development through ssh into the "box", I don't know if the unit is unresponsive to its keyboard if it were plugged in... but all my ssh connections freeze and I can't even ping the unit any more.
Here's the code:
Code: Select all
#!/usr/bin/python
import ossaudiodev
import sys
import wave
def play(filename):
print "opening file"
sound_file = wave.open(filename,'rb')
print "getting parameters"
(nc, sw, fr, nf, comptype, compname) = sound_file.getparams()
print "parameters were", (nc, sw, fr, nf, comptype, compname)
print "opening audio"
sound = ossaudiodev.open('w')
print "setting parameters"
sound.setparameters(ossaudiodev.AFMT_S16_NE, nc, fr)
print "readframes"
data = sound_file.readframes(nf)
print "closing file"
sound_file.close()
print "writing data"
sound.write(data)
# Never gets here, freezes on the previous line.
print "closing sound device"
sound.close()
if __name__ == '__main__':
if len(sys.argv) is 2:
play(sys.argv[1])
else:
print 'Usage: %s filename' % sys.argv[0]
Thanks in advance!