# python audio bindings

I'm trying to get python audio input on my raspberry pi. So far I've installed the following:

My inquiry looks similar to this one, but I'm not using an audio card, I just have a webcam plugged directly into my usb drive. I was able to get a recording via:

arecord -D plughw:1,0 -f cd -d 10 test.wav


and then play it back no problem. Next I wanted to get the audio into python.

I've tried PyAudio via the first two lines of the example on the PyAudio documentation:

import pyaudio
pyaudio.PyAudio()


which returns

ALSA lib confmisc.c:1286:(snd_func_refer) Unable to find definition 'cards.BRCM bcm2835 AL.pcm.front.0:CARD=0'


and a bunch more ASLA errors. I've also tried a snippet using alsaaudio:

import alsaaudio
inp = alsaaudio.PCM(alsaaudio.PCM_CAPTURE,alsaaudio.PCM_NONBLOCK)


which returns:

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
alsaaudio.ALSAAudioError: No such file or directory


Do I need to do something special to configure my ALSA installation to detect my webcam? I'm thinking about trying to use PyUSB to connect directly to the webcam, but that seems too low level to be fun. Does anyone know how to get PyAudio working through the above error? Is there a different package that I might try?

• you can ignore the ALSA output you are getting whilst initializing PyAudio(). – abolotnov Feb 3 '13 at 17:29

I don't know if it's exactly your same problem, but i had some issues recording from a usb mic, and i resolved them with some python i found on the internet:

import alsaaudio, time, audioop
# Open the device in nonblocking capture mode. The last argument could
# just as well have been zero for blocking mode. Then we could have
# left out the sleep call in the bottom of the loop
card = 'sysdefault:CARD=Microphone'
inp = alsaaudio.PCM(alsaaudio.PCM_CAPTURE,alsaaudio.PCM_NONBLOCK, card)

# Set attributes: Mono, 8000 Hz, 16 bit little endian samples
inp.setchannels(1)
inp.setrate(16000)
inp.setformat(alsaaudio.PCM_FORMAT_S16_LE)

# The period size controls the internal number of frames per period.
# The significance of this parameter is documented in the ALSA api.
# For our purposes, it is suficcient to know that reads from the device
# will return this many frames. Each frame being 2 bytes long.
# This means that the reads below will return either 320 bytes of data
# or 0 bytes of data. The latter is possible because we are in nonblocking
# mode.
inp.setperiodsize(160)