Reputation:
I have attached a USB up to a RPi and know it works because I am able to record audio through the microphone using the command:
arecord test.wav -D sysdefault:CARD=1
Now I want to write a Python program that can stream the data from the usb device. I wrote the following code (which sets the input_device_index = 1 in the .open()):
import pyaudio
import numpy as np
RATE = 44100
CHUNK = 1024
p = pyaudio.PyAudio()
stream = p.open(format=pyaudio.paInt16, channels=1, rate=RATE, input=True, input_device_index=1, frames_per_buffer=CHUNK)
while (True):
data = np.fromstring(stream.read(CHUNK),dtype=np.int16)
stream.stop_stream()
stream.close()
p.terminate()
When I run the code I get the following error though:
"ALSA lib confmisc.c:1281:(snd_func_refer) Unable to find definition 'cards.bcm2835.pcm.front.0:CARD=0'
ALSA lib conf.c:4528:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directory
ALSA lib conf.c:5007:(snd_config_expand) Evaluate error: No such file or directory
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM front
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.rear
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.center_lfe
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.side
ALSA lib confmisc.c:1281:(snd_func_refer) Unable to find definition 'cards.bcm2835.pcm.surround51.0:CARD=0'
.........
a failed in 'src/hostapi/alsa/pa_linux_alsa.c', line: 2818
Traceback (most recent call last):
File "RealTimePlaybackTest.py", line 9, in <module>
stream = p.open(format=pyaudio.paInt16, channels=1, rate=RATE, input=True, input_device_index=1, frames_per_buffer=CHUNK)
File "build/bdist.linux-armv7l/egg/pyaudio.py", line 750, in open
File "build/bdist.linux-armv7l/egg/pyaudio.py", line 441, in __init__
IOError: [Errno -9998] Invalid number of channels
"The whole error is in this gist
This confuses me though because the error states that the CARD=0 even though I set the index = 1 in the code. How can I go about changing it so the CARD=1?
Upvotes: 2
Views: 1750
Reputation: 33
You can get the Card and Device number for all capture devices using
arecord -l
My USB mic lists as
card 1: Device [USB PnP Sound Device], device 0: USB Audio [USB Audio]
The Card is specified in
/usr/share/alsa/alsa.conf
So for my capture device on Card 1, I set this
defaults.ctl.card 1
defaults.pcm.card 1
As you noted, the Device number is specified in the call to open a stream; p.open(input_device_index=0).
Upvotes: 2
Reputation:
I found the solution. Card number is not the same as index number. I needed to run the following code to get the index number:
import pyaudio
p = pyaudio.PyAudio()
for i in range(p.get_device_count()):
dev = p.get_device_info_by_index(i)
print((i,dev['name'],dev['maxInputChannels']))
Upvotes: 1