I want to deliver sound from my Raspberry Pi’s (RPi) Audio Output 3.5mm jack. I’ll need to get audio drivers working on Audio Out, and to test it, I’ll need some sound files and players. I’m choosing the Advanced Linux Sound Architecture (ALSA) drivers because its widely supported and because ALSA not only provides audio but Musical Instrument Digital Interface (MIDI) functionality to Linux. I’ll also be using the popular command line MP3 players, mpg321 and the WAV player that comes with ALSA, aplay.
To get things going, I installed ALSA, a MP3 tools, and a WAV to MP3 conversion tool via the following commands:
sudo apt-get install alsa-utils
sudo apt-get install mpg321
sudo apt-get install lame
Enabling the Sound Module
Reboot the RP and when it comes back up, its time to load the Sound Drivers. This will be done via loadable kernel module (LKM) which are object file that contains code to extend the Linux kernel.
lsmod is a command on Linux systems which prints the contents of the
snd-bcm2835 module appears to be already installed. RPi has a Broadcom BCM2835 system on a chip (SoC) which is a High Definition 1080p Embedded Multimedia Applications Processor.
snd-bcm2835 is the sound driver. If
lsmod doesn’t list the
snd-bcn2835 module, then it can be installed via the following command:
sudo modprobe snd-bcm2835
Enabling Audio Output
By default, the RPi audio output is set to automatically select the digital HDMI interface if its being used, otherwise the analog audio output. You can force it to use a specific interface via the sound mixer controls.
amixer allows command-line control of the mixer for the ALSA driver.
You can force the RPi to use a specific interface using the command
amixer cset numid=3 N where the
N parameter means the following: 0=auto, 1=analog, 2=hdmi. Therefore, to force the Raspberry Pi to use the analog output:
amixer cset numid=3 1
With this done, you should be ready for a simple test. Plug a speaker into the (RPi) Audio Output 3.5mm jack. I used a simple battery powered iHM60 iHome speaker. The jack will not deliver much power, so the speaker needs to be powered.
To test the RPi audio, you can play a WAV file (download this … excellent for user-error notification) with
mpg321 for MP3 files, or use the
speaker-test command if you don’t have a WAV/MP3 file.
speaker-test -t sine -f 440 -c 2 -s 1
mpg321 "Mannish Boy.mp3"
More on the ALSA Sound Drivers and Utilities
While ALSA is a powerful tool, it documentation appears is very weak. Also, it appears that the capabilities of ALSA drivers and utilities are very dependent on the hardware used. The best sources of documentation that I found include Advanced Linux Sound Architecture (ALSA) project homepage, archlinux Advanced Linux Sound Architecture, and ALSA-sound-mini-HOWTO.
You can find useful information in the directory /proc, which is a “virtual” file system (meaning that it does not exist in real life, but merely is a mapping to various processes and tasks in your computer).
/proc/modulesgives information about loaded kernel modules. The command
lsmod | grep sndwill list modules relevant to the sound system.
- You can check the existence of a soundcard by looking at
amixer command can provide useful information (sometimes):
- You can look at the mixer settings by typing
amixerwithout any arguments. This command lists the mixer settings of the various parts of the soundcard. The output from
amixercan greatly differ from card to card. Unfortunately you can’t find much documentation on how to interpret the out.
- The RPi doesn’t have a “Master” control only “PCM”. So commands like
amixer set Master...will not work. You must use
amixer set PCM ...
- You can mute /unmute the sound via these commands:
amixer set PCM muteand
amixer set PCM unmute
- As of August 2012, there appears to be a known bug in RPi ALSA driver that ignores volume settings at the start of playback and always plays at max volume. Therefore, commands like
amixer set PCM 50% unmutewill not set the volume to 50%, at least until this bug is fixed. Maybe this isn’t really a bug but a limitation of the hardware because there is a workaround for this …. see below.
The RPi built-in sound chips don’t have a “master volume” control, and as a result, you must control the volume via software. I guess the RPi views itself as a preamplifier (preamp) and volume controls would be supplied down stream. ALSA provides a software volume control using the softvol plugin.
/etc/asound.conf file is a configuration files for ALSA drivers (system-wide). The main use of this configuration file is to add functionality. It allows you to create “virtual devices” that pre or post-process audio streams. Any properly written ALSA program can use these virtual devices as though they were normal devices.
For more detail: Getting Audio Out Working on the Raspberry Pi