Getting Audio Out Working on the Raspberry Pi

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

Getting Audio Out Working on the Raspberry Pi

Enabling the Sound Module

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.

Getting Audio Out Working on the Raspberry Pi Code

    • /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 from amixer 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 use amixer set PCM ...
    • You can mute /unmute the sound via these commands: amixer set PCM mute and 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% 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.

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

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:

Source: Getting Audio Out Working on the Raspberry Pi


About The Author

Ibrar Ayyub

I am an experienced technical writer holding a Master's degree in computer science from BZU Multan, Pakistan University. With a background spanning various industries, particularly in home automation and engineering, I have honed my skills in crafting clear and concise content. Proficient in leveraging infographics and diagrams, I strive to simplify complex concepts for readers. My strength lies in thorough research and presenting information in a structured and logical format.

Follow Us:
LinkedinTwitter

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top