7

More particularly: How can I use python with ALSA to capture audio input?

I'm thinking about doing an oled-based spectrum analyzer for Pi-based audioplayer. One part of the solution is to read the audio currently playing. I've tried this using ALSA in Python:

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

At this point the script dies:

alsaaudio.ALSAAudioError: No such file or directory

Adding a card like this doesn't work either:

card = 'sysdefault:CARD=ALSA'
inp = alsaaudio.PCM(alsaaudio.PCM_CAPTURE, alsaaudio.PCM_NONBLOCK, card)

I've seen various related posts on raspbian, but no solution.

4

I don't believe the Raspberry Pi has the necessary hardware to capture audio input. You can confirm this running alsamixer.

enter image description here

That would explain why alsaaudio.ALSAAudioError exception was thrown by alsaaudio.PCM.__init__ method.

Explore third party USB devices

I've enjoyed an article entitled Speech Recognition with the Raspberry Pi; which, outlined the steps needed for adding audio input. Raspberry Pi into an audio spying device is also helpful with USB-microphone installs. The underlining theme is to ensure that the base system is configured & ready to capture audio through ALSA before developing an application.

Module alsaaudio is well documented

Alsaaudio ships with test suites to verify that you local install is fully functional.

bash> cd pyalsaaudio-0.7
bash> python recordtest.py -c NAME_OF_USB_CARD output.wav

The NAME_OF_THE_USB_CARD should be a name listed by alsaaudio.cards()'s method; which, is the same name for the third parameter for alsaaudio.PCM class.

  • Mhm. Running alsamixer I do not get the capture controls message shown above. Instead, a volume control is shown? – andig Dec 18 '12 at 12:08
  • Press F4 for capture controls. F3 for playback & F5 for all controls. Use F6 to switch between sound cards. – emcconville Dec 18 '12 at 14:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.