My goal is to successfully output sound from my Raspberry Piâs audio jack. To achieve this, Iâll need to configure the audio drivers for Audio Out and test them, which requires some sound files and compatible players. Given its broad support and additional MIDI capabilities, Iâve opted for the Advanced Linux Sound Architecture (ALSA) drivers for this endeavor. Iâll also employ the well-known command-line MP3 player mpg321, as well as aplay, the WAV player bundled with ALSA.
To get things going, I installed ALSA, 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
Type reboot to restart the RP system, and as the RP comes back with the sound drivers, it is time to load them. This will be done through loadable kernel modules (LKM), which are object files that enclose code that modifies the Linux kernel. lsmod
 is a command on Linux systems that displays the content of /proc/modules
 file.
Letâs focus on the snd-bcm2835
module, this seems to be installed. RPi has a Broadcom BCM2835 system on a chip (SoC) that is a High Definition 1080p Embedded Multimedia Applications Processor. snd-bcm2835
 is the driver for the sound. 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 Raspberry Piâs audio output is configured to dynamically switch to the digital HDMI interface when itâs in use, while falling back to the analog audio output otherwise. However, you can override this auto-selection by utilizing the sound mixer controls to specify a preferred interface. For command-line control of the mixer under ALSA, the utility  amixer
 is available, allowing you to manipulate the audio settings as needed.
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
Sound Check
With the audio configuration now set, youâre prepared to conduct a basic test. Connect a speaker to the Raspberry Piâs 3.5mm audio output jack. In my case, I utilized a low-power, battery-powered iHM60 iHome speaker. Keep in mind that the jack will not provide a significant amount of power, so itâs crucial to power the speaker externally.
To test the RPi audio, you can play a WAV file (download this ⊠excellent for user-error notification) with aplay
, mpg321
for MP3 files, or use the speaker-test
command if you donât have a WAV/MP3 file.
aplay numnuts.wav
speaker-test -t sine -f 440 -c 2 -s 1
mpg321 "Mannish Boy.mp3"
More on the ALSA Sound Drivers and Utilities
Although ALSA is a robust tool, its documentation appears to be somewhat sparse. Furthermore, it appears that the capabilities of ALSA drivers and utilities are heavily dependent on the specific hardware in use. The most reliable sources of documentation that I found include the Advanced Linux Sound Architecture (ALSA) project homepage, the Arch Linux ALSA documentation, and the ALSA-sound-mini-HOWTO.
The `/proc` directory is a valuable resource, functioning as a virtual file system. Although it doesnât represent a physical location, itâs a symbolic representation of various processes and tasks operating within your computer.
-
/proc/modules
gives information about loaded kernel modules.  The commandÂlsmod | grep snd
will list modules relevant to the sound system.- You can check the existence of a soundcard by looking  at
cat /proc/asound/cards
.
The amixer
command can provide useful information (sometimes):
-
- You can look at the mixer settings by typing
amixer
without any arguments. This command lists the mixer settings of the various parts of the soundcard. The output fromamixer
can 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 useamixer set PCM ...
- You can mute /unmute the sound via these commands:
amixer set PCM mute
andamixer 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% unmute
will 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.
- You can look at the mixer settings by typing
Volume Control
Unlike traditional sound systems, the Raspberry Piâs built-in audio components do not feature a master volume control. Instead, volume adjustments must be made through software. It appears the RPi is designed as a preamplifier (preamp), with volume controls typically implemented downstream. Fortunately, ALSA provides a software volume control solution via the softvol plugin.
The /etc/asound.conf
 file serves as a central configuration file for ALSA drivers, encompassing system-wide settings. The primary purpose of this file is to extend the capabilities of ALSA by enabling the creation of âvirtual devicesâ that can preprocess or post-process audio streams. Any ALSA-compatible program can utilize these virtual devices as if they were standard devices, unlocking additional audio functionality.
WAV and MP3 Conversion
The MP3 player mpg321
 can convert MP3 files to WAV files but the WAV player, aplay
, can not do a conversion.  To make a MP3 file from a WAV file, youâll need the tool lame
.
-
- To convert from WAV to MP3:Â
lame input.wav output.mp3
- To convert from MP3 to WAV: Â
mpg321 -w output.wav input.mp3
- To convert from WAV to MP3:Â
Bottomeline
Although itâs possible to get ALSA up and running on the Raspberry Pi, the available resources suggest that itâs only partially supported, probably buggy, and lacks proper documentation. If your primary goal is simply to output audio from the device (like my situation), youâll likely be fine. However, if you aim to leverage ALSA for advanced sound processing, be prepared to encounter significant frustration.
Epilogue
Curiously, this particular post has garnered approximately 25% of all views on my blog. Iâm uncertain about the reason behind this popularity, but I hypothesize that many users are attempting to turn the Raspberry Pi into a media player and are searching for technical solutions to their issues.
At this point in time, others have done some additional postings and they are more instructive than my post. You should check out: