9

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.

2

2 Answers 2

5

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.

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

I hit this error with code which works perfectly with my USB mic on Ubuntu but refused to work on RaspberryPi.

First, since the question was asked, card is outdated and replaced with device.

Second, I had to run alsaaudio.pcms(alsaaudio.PCM_CAPTURE) and use EXACT FULL line of that output as device. In my case it was full line "hw:CARD=SHG7980,DEV=0". Just 'SHG7980`, '0' or whatsoever else didn't work.

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