Start a hosting plan from $3.92/mo and get a free year on Tuts+ (normally $180)
My previous tutorial on the Raspberry Pi, Using a Raspberry Pi as an AirPlay Receiver, made use of the device’s built-in 3.5mm jack for audio output. This, however, produces sound that is mediocre at best and if you’re wanting to use the Raspberry Pi for any amount of audio listening, it’s going to have to produce a better quality of audio. We’re not going to squeeze much more out of the built-in components so, in this tutorial, I’ll explain how to set up and configure a cheap USB sound card as the Raspberry Pi’s default audio output device, providing a better quality, and volume, audio.
Before You Begin
This tutorial will follow on from my previous tutorial, Using a Raspberry Pi as an AirPlay Receiver. It will be expected that you have completed this tutorial and your Raspberry Pi is currently able to act as an AirPlay receiver, albeit with its built-in audio output.
USB Audio Device
Back in the good old days, most audio devices needed their own set of drivers and software to work. This often proved very frustrating for end-users and developers alike.
The reason for this is that many devices from different manufacturers actually use the same chipset, the physical components that make up the device. This is what the drivers and software are for and you’ll often find that a number of different audio devices from many manufacturers will use the exact same chipset, the only difference between them will usually just be the branding.
Nowadays, most modern operating systems and audio devices have no such requirements as most chipsets work to an accepted standard, and since most manufacturers will make use of the same small number of chipsets, providing widespread support is far easier. There are certainly a small number of more specialised audio devices that require additional software but any generic audio output device is usually just plug-and-play
This is applicable not only to Mac OS X and Windows, but also Linux as well, with Raspbian being able to support devices via the AlsaProject, a set of widely compatible audio drivers for Linux.
Tip: Not all USB audio output devices are supported, though you can check the AlsaProject Compatibility List to ensure the device you’re planning to use should be compatible.
What I Recommend
The USB audio device I will be using with my Raspberry Pi is a generic one I found on Amazon, costing just $2. It’s called, quite vaguely, the 7.1 Channel USB External Sound Card Audio Adapter. The reason for using this particular USB audio device are as follows:
- It’s cheap
- It doesn’t need much power
- It’s small enough to be easily used
- It’s supported
This particular audio device uses the C-Media audio chipset, something that is supported by AlsaProject in Raspian.
When using any USB audio device, you’ll need to make sure that your Raspberry Pi is being powered enough that it is able to support a USB-powered device. If you’re running your Raspberry Pi from a mains adapter then you should be all set.
Tip: USB audio devices are usually a little larger than a memory stick and will probably take up more physical space, blocking access to the second USB port. If you’re needing to use both USB ports at the same time, I would recommend using a USB extension cable and connecting your USB audio device that way, instead.
Now that I’ve covered the requirements for sourcing a suitable USB audio device, I will show you how to configure it.
The following steps will be performed within the command line on the Raspberry Pi. If you’re familiar with SSH and know how to access your Raspberry Pi using it, you can perform the following steps without needing to be physically at your Raspberry Pi.
Before continuing, make sure your Raspberry Pi is switched off and then connect the USB audio device to its USB port. Remember, if you need access to both USB ports then use the USB audio device in conjunction with a USB extension cable, preventing the second port from being blocked.
Power up your Raspberry Pi and, once it has booted, open the LXTerminal app. (Again, if you’re familiar with SSH then you can simply connect and perform the remaining commands remotely).
First, I’ll make sure that the USB audio device is being detected by both the hardware and software. Enter the following command and press enter:
This will display information regarding attached USB devices. As you can see, the last device listed in the screenshot above is the USB audio device labelled as C-Media Electronics, Inc. Audio Adapter. So far, so good.
I will use a command to display the currently set audio device, which will still be the built-in audio. This command displays various information regarding the current audio device.
I don’t want to use the built-in audio device, instead I’d like to use the USB audio device I’ve connected. I need to edit a configuration file so that whenever the Raspberry Pi boots, it will default to using the USB audio device as the preferred method of audio output.
The configuration file that I need to edit is called alsa-base.conf. This controls how any attached audio devices are dealt with. Using this configuration file, I can set the order at which to use an attached audio device, making the USB audio device the first one to use and then fall back to the others if it isn’t detected. To do this, enter:
$ sudo nano /etc/modprobe.d/alsa-base.conf
This will launch the text editor nano with the configuration file so I can edit it accordingly. It might look a little complicated or hard to understand, but all I need to do is add a single character. Look for the following line:
options snd-usb-audio index=-2
At the moment, this line causes any USB audio devices that are detected to be pushed down the order list and not be used by default. To override this, simply add a pound/hash symbol in front of the line so it reads like so:
# options snd-usb-audio index=-2
Press Ctrl Z and save the changes, then restart your Raspberry Pi by entering:
$ sudo reboot
With the Raspberry Pi restarted, re-run the following command:
Instead of the same information I was given earlier, I should now have a completely different set. You’ll notice that I also have information regarding audio capture, further confirming I’m using the USB sound device as my default audio output option.
Oddly, it is still stated that the output is in mono. This can be ignored as it’s actually not correct, the output will indeed be stereo, as you’ll see (or hear) shortly.
Now that I’ve set the USB audio device as the default audio output for the Raspberry Pi, it’s time to test it to ensure it is working. Connect a set of speakers to the USB audio device and then start playing audio through them.
Raspian includes a number of test sound clips I can use to check that everything is working properly. Enter the following command:
$ aplay /usr/share/scratch/Media/Sounds/Vocals/Singer1.wav
If everything went well, you should have heard a quick clip of an opera singer’s voice. The clip is incredibly low quality and mono, so I’ll doubly make sure everything is working and use a clip that is in stereo.
I’m going to test this further by using an MP3 file, but I first need to install some software that’s able to do this. I can use the utility mpg321 to do this. Installing it is very straightforward, simply use:
$ sudo apt-get -y install mp321
Raspian will automatically install the utility and any required additional bits and pieces it might need to work.
I’ve created a small MP3 clip that pans from left to right, ensuring that any stereo audio will work correctly. To download it, enter:
$ wget https://cdn.tutsplus.com/mac.tutsplus.com/uploads/2013/10/test-song.mp3
This is what it should sound like:
After the song is downloaded, enter:
$ mpg321 test-song.mp3
You should hear a short music clip that pans audio from left to right.
Should the audio output be too loud or quiet, we can adjust this using another utility, called alsamixer.
To start it, simply enter the name in the command line, like so:
This presents a more graphical view of the volume and information regarding the USB audio device. Using the arrow keys on your keyboard, select the volume column and adjust the volume higher or lower, dependent on your needs. Where possible, keep the volume level below 80–90% to avoid any distortion.
Using a dedicated USB audio device will mean better audio quality and more control, as well as better volume as many devices are able to output far better than the Raspberry Pi’s built-in audio.
By the end of this tutorial, you should now be able to configure a new USB audio device and set it as the Raspberry Pi’s default audio output option when using Raspian. If you were using your Raspberry Pi as an AirPlay receiver, you don’t need to reconfigure any settings, simply start the AirPlay service back up and audio will be outputted to the USB audio device automatically.