Take the 2-minute tour ×
Raspberry Pi Stack Exchange is a question and answer site for users and developers of hardware and software for Raspberry Pi. It's 100% free, no registration required.

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.

share|improve this question
    
add comment

1 Answer 1

up vote 4 down vote accepted

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.

share|improve this answer
    
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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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