Tuesday 17 June 2014

Enabling USB audio card in Raspberry Pi

The Raspberry Pi has an on-board audio jack, but the on-board audio is generated by a PWM output with little filtering done, but the sound quality, volume, and recording capabilities can be added by using a USB sound card (these are pretty cheap too).

I bought 4€ USB sound card from Amazon, but pretty much any other can be used, it has a stereo output and has a MIC input, which is perfect for voice-controlled projects (like the one I'm currently working on, remember Jarvis?).  The audio card features an USB 2.0 type A connector and it powers up from the USB 5VDC power supply.  I have not measured the current consumption (yet).


Preparing the installation


I'm running Raspbian-based RetroPie on a USB stick, the following should not change anything from your configured settings, however this installation can be done on any Raspbian-based images.
sudo apt-get update
sudo apt-get upgrade

After 5-10 minutes you should have your OS upgraded and ready to go.


Recognizing the USB audio card


Sources:

ALSA driver by default does not allows the USB audio card to be sound device #0 (default), to change this we need to edit the /etc/modprobe.d/alsa-base.conf (as sudo) and change the following line (normally commented-out):

#options snd-usb-audio index=-2

Remove the comment and change index to 0

options snd-usb-audio index=0

After rebooting or restarting ALSA, the audio card should be listed as the default sound device.

As pointed out in Adafruit, there are different supported chipsets, mine was a C-Media based (CM108).


pi@raspberrypi ~ $ dmesg | grep C-Media
3.352853] usb 1-1.2: Manufacturer: C-Media Electronics Inc. 
3.370053] input: C-Media Electronics Inc. 
USB PnP Sound Device as /devices/platform/bcm2708_usb/usb1/1-1/1-1.2/1-1.2:1.3/input/input0
3.386302] hid-generic 0003:0D8C:013C.0001: input,hidraw0: USB HID v1.00 Device [C-Media Electronics Inc. 
USB PnP Sound Device] on usb-bcm2708_usb-1.2/input3
pi@raspberrypi ~ $ lsusb
Bus 001 Device 002: ID 0424:9512 Standard Microsystems Corp.
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 001 Device 003: ID 0424:ec00 Standard Microsystems Corp.
Bus 001 Device 004: ID 0d8c:013c C-Media Electronics, Inc. CM108 Audio Controller
Bus 001 Device 005: ID 125f:c13a A-DATA Technology Co., Ltd.
pi@raspberrypi ~ $ cat /proc/asound/cards
0 [Device]: USB-Audio - USB PnP Sound Device
C-Media Electronics Inc. USB PnP Sound Device at usb-bcm2708_usb-1.2, full spee
1 [ALSA]: BRCM bcm2835 ALSbcm2835 ALSA - bcm2835 ALSA bcm2835 ALSA
pi@raspberrypi ~ $ aplay -L
Discard all samples (playback) or generate zero samples (capture)
sysdefault:CARD=Device
USB PnP Sound Device, USB Audio
Default Audio Device
front:CARD=Device,DEV=0
USB PnP Sound Device, USB Audio
Front speakers
surround40:CARD=Device,DEV=0
USB PnP Sound Device, USB Audio
4.0 Surround output to Front and Rear speakers
surround41:CARD=Device,DEV=0
USB PnP Sound Device, USB Audio
4.1 Surround output to Front, Rear and Subwoofer speakers
surround50:CARD=Device,DEV=0
USB PnP Sound Device, USB Audio
5.0 Surround output to Front, Center and Rear speakers
surround51:CARD=Device,DEV=0

USB PnP Sound Device, USB Audio
5.1 Surround output to Front, Center, Rear and Subwoofer speakers
surround71:CARD=Device,DEV=0
USB PnP Sound Device, USB Audio
7.1 Surround output to Front, Center, Side, Rear and Woofer speakers
iec958:CARD=Device,DEV=0
USB PnP Sound Device, USB Audio
IEC958 (S/PDIF) Digital Audio Output
sysdefault:CARD=ALSA
bcm2835 ALSA, bcm2835 ALSA
Default Audio Device
pi@raspberrypi ~ $ arecord -l
**** List of CAPTURE Hardware Devices ****
card 0: Device [USB PnP Sound Device], device 0: USB Audio [USB Audio]
 Subdevices: 1/1
 Subdevice #0: subdevice #0

The last step is adding the following configuration to the asound.conf file, used by ALSA.
pi@raspberrypi ~ $ cat /etc/asound.conf
pcm.!default {
 type plug
 slave {
   pcm "hw:0,0"
 }
}
ctl.!default {
 type hw
 card 0
}

Testing the setup

I have hooked the USB audio card with a TPA2005D1 audio amplifier (roughly 6€) from Sparkfun and a 0.5 Watt  magnetic speaker.  I have to work on the sound quality to reduce some noise, change the audio amplifier's resistors to increase the gain, maybe throw in a potentiometer to allow changing the volume, and use speakers with a higher wattage.


To test the installation we can use the built-in functions provided by the OS.
pi@raspberrypi ~ $ speaker-test
speaker-test 1.0.25
Playback device is default
Stream parameters are 48000Hz, S16_LE, 1 channels
Using 16 octaves of pink noise
Playback open error: -16,Device or resource busy
Had to close the emulationstation scripts running, then I was able to hear the sound:


pi@raspberrypi ~ $ speaker-test -c2 -D hw:0,0
speaker-test 1.0.25
Playback device is hw:0,0
Stream parameters are 48000Hz, S16_LE, 2 channels
Using 16 octaves of pink noise
Rate set to 48000Hz (requested 48000Hz)
Buffer size range from 96 to 262144
Period size range from 48 to 131072
Using max buffer size 262144
Periods = 4
was set period_size = 65536
was set buffer_size = 262144
0 - Front Left
1 - Front Right
Time per period = 5.564981
0 - Front Left
1 - Front Right
Time per period = 5.559085
0 - Front Left
1 - Front Right
And to test the recording I just connected a detachable mic from a headset.
pi@raspberrypi ~ $ arecord -d 10 test.wav
Recording WAVE 'test.wav' : Unsigned 8 bit, Rate 8000 Hz, Mono
^CAborted by signal Interrupt...

$ aplay test.wav
Playing WAVE 'test.wav' : Unsigned 8 bit, Rate 8000 Hz, Mono

No comments: