I recently bought a little USB “sound card” at my local Microcenter, and I’ve gotten it playing nicely the my Raspberry pi via pyaudio. It’s a lot like this nice USB audio input from Adafruit, though that’s not the exact unit I’m using. As a little sample, here’s the final output.
My long-term goals for the device include recording and playing-back audio from the air (say, evening nets that I usually miss), decoding morse/PSK31, and other such digital tasks. But just getting the sound card and the pi to play nicely together was tricky enough, so I figure it was worth documenting. This walkthrough assumes you have basic familiarity with using the terminal.
First things first – the RPi seems to be happiest when you plug in new USB devices with the RPi powered off. So, make sure the RPi is fully shut down, plug in the USB sound card, and power things up again.
It’s important that we know what chipset the USB device is using, so we know what to look for and how to configure ours settings later. To identify the chipset, we’ll look at the system startup messages using the dmesg command:
The dmesg command shows you the last several hundred commands in the kernal ring buffer. To be honest, I don’t know what the kernal ring buffer is, but I do know this command shows the system messages from startup, which will be very useful to us. We’ll look for the lines telling us that the computer has recognized and identified an attached USB device. And rather than look through hundreds of lines by hand, we can use the handy grep search tool to do the work for us. The three most common chipsets seem to be the CM108, CM109, and C-Media chipsets, so we’ll grep for each of these in turn:
Success! So we know we have a C-Media USB device, which will come in handy in the next step. We can also verify that the device is set up as a capture device by running arecord -l:
If you try opening the the input file and get a whole bunch of errors back in the terminal as below, don’t worry: the ALSA audio system is particularly noisy in its errors,and is mostly telling you it has information about sound sources that don’t exit.
You can edit your alsa config file at /usr/share/alsa/alsa.conf (sudo nano /usr/share/alsa/alsa.conf) and comment out the lines related to the errors. For example, if you get the line “ALSA lib pcm.c:2217(snd_pcm_open_noupdate) Uknown PCM surround 71“you’ll want to comment out the line about surround 71. In this case, that’s the line which reads pcm.surround71 cards.pcm.surround71. I had almost a dozen of these lines to comment out.
We’d also like to make the USB input device our default device, or at least higher priority than other input devices. We can do this by editing /etc/modprobe.d/alsa.conf using sudo nano /etc/modprobe.d/alsa.conf. (If this returns a blank file, try sudo nano /etc/modeprobe.d/alsa-base.conf)
We’ll want to change the line which reads “options snd-usb-audio index=-2” to “options snd-usb-audio index=0”
Note that pyaudio will crash if the audio buffer isn’t cleared fast enough; this is an unresolved issue, with some discussion here.