Introduction: Test Sound Card and Speakers in Raspberry Pi

Introduction

The Raspberry Pi makes a great MP3 player. There are a lot of MP3 Player software that could be installed in Raspberry Pi. Before I learned about these MP3 software, I feel it is beneficial to learn about how Raspberry Pi handles the audio card hardware and drivers . Linux and Audio is a very difficult topic. I think I managed to understand only 5 to 10% of Linux Audio! In this instructable, I will share what I learned. In my terminology, the meaning of Audio and Sound are the same and interchangeable. After completing this instructable., you may be interested in my "Modify the Capability of your Sound Card in Raspberry Pi" instructable.

Scope

This instructable will cover the following:

  • How to install Advanced Linux Sound Architecture (ALSA) utilities
  • Use ALSA utilities to test sound card and sound device

This instructables will NOT cover the following

  • PulseAudio
  • OSS

Specifications

My Raspberry Pi:

  • Raspberry Pi 2
  • Rasbian based on Debian Version 8.0 (a.k.a Jessie)
  • Advanced Linux Sound Architecture Driver Version k4.1.10-v7+
  • Pulse Audio and OSS are not installed
  • 2 speakers connected to Raspberry Pi's audio/video 3.5mm socket.

Step 1: Uninstall PulseAudio and OSS

PulseAudio is a sound server. It is a software layer that uses the ALSA. However, ALSA does not depend on PulseAudio. Sound files can be played using only ALSA.

Open Sound System (OSS) was the predecessor to ALSA. A few applications use only OSS.

To simplify my learning of ALSA, I removed PulseAudio and OSS so as not to confuse matters.

Use Package Manager to check whether PulseAudio is installed:

dpkg -l | grep ^ii.*pulseaudio

If pulseaudio is installed, uninstall it:

sudo apt-get purge pulseaudio<br>

Step 2: Identify the Kernel Module That Drives the Sound Card

In Linux, a software driver is known as a kernel module.

Open terminal like LXTerminal

cat /proc/asound/modules

The kernel module name is snd_bcm2835.

Get information about this snd_bcm2835:

modinfo snd_bcm2835

snd_bcm2835 is the kernel module that drives broadcom sound chip. It is provided by ALSA.

Step 3: Understand Two Fundamental ALSA Concepts

ALSA creates the following concepts:

  • ALSA Card
  • ALSA Device

ALSA Card refers to the hardware audio card.

A Hardware audio card can have multiple capabilities like:

  • Sending sound to a speaker
  • Receiving sound from a microphone

An ALSA device refers to a specific capability described above. Therefore an ALSA Card will have one ALSA device (to send sound) and may have another ALSA device (to receive sound) and another ALSA Device for some other capability.

Step 4: Understand ALSA Sound Card Hardware Address

The Raspberry Pi may have more than one sound card installed:

  • The Broadcom sound chip
  • A USB Sound Card

ALSA needs a way to identify them so that it knows where to send the sound to. ALSA cards are identied by the following format:

ALSA Card id

ALSA card id can be in either one of the following format:

  • Numeric
  • Name

In addition, because an ALSA card may have more than one ALSA devices, ALSA needs to know which device of the card to send sound to. ALSA devices are identified by the following format:

ALSA Card id,ALSA Device Id

The ALDA Device Id is always numeric.

ALSA applications works only at the device level. Therefore, we need to specify the ALSA device identity. However, this is not sufficient. ALSA needs to know the interface to receive the sound from ALSA applications. The device name that ALSA expects is in following format:

ALSA Interface:ALSA Card id,ALSA Device Id

An ALSA interface is basically an access protocol. The two built-in ALSA interfaces are:

  • hw
  • plughw

hw provides direct communication to the hardware device.
plughw provides translation from a standardized protocol to one which is supported by the device.

Examples of sound hardware address:

  1. hw:0,1
  2. hw:CM,1
  3. plughw:0,1
  4. plughw:CM,1

Step 5: Identify Soundcard Chip's Card Id Using Linux Virtual Proc Filesystem

Open console like LXTerminal

cat /proc/asound/cards

My Raspberry Pi is using only one sound card. The sound card is a Broadcom chip. This is Raspberry Pi's built-in sound card.

The ALSA Card Id in numeric form is 0.

The ALSA Card Id in the alphabetic form is located in square parenthesis, which in my case is ALSA.

Step 6: Identify Soundcard Chip's Device Id Using Linux Virtual Proc Filesystem

Open Terminal

ls -l /proc/asound/card*

There should only be one folder beginning with word card (card0) in my case as there is only sound card installed. The card0 folder has 2 folders beginning with the words "pcm". A pcm folder represent a playback device. Therefore the broadcom ALSA card has 2 ALSA devices.

Identify the first device id:

cat /proc/asound/card0/pcm0p/info

The first device has id 0.

Identify the second device id:

cat /proc/asound/card0/pcm1p/info

The second device has id 0

Step 7: Install ALSA Utlities

ALSA Utilities is required for configuring and testing the sound card. The next two steps requires that ALSA utilities be installed.

Install the ALSA utilities.

Open terminal:

sudo apt-get install alsa-utils

Step 8: Understand ALSA Native Application Vs ALSA

The previous step installed ALSA utilities. ALSA utilities are ALSA native applications. ALSA native applications uses ALSA. ALSA refers to kernel modules (a.k.a device drivers) and API library.

Step 9: Adjust the Volume to Make It Louder

There are 2 parts:

  1. Adjust hardware speaker volume
  2. Adjust ALSA volume

Adjust hardware speaker volume using the knob or buttons built into the speakers. Make sure to increase the volume. Some speakers may not have any knobs or buttons.

Adjust ALSA volume using alsa-mixer program.

Open terminal:

alsamixer

Use the UP arrow to increase the volume.

Press ESC button to exit the program.

Step 10: Test the Sound Card Output Capablity

User speaker-test program to test the Sound Card Output Capablity:

speaker-test -D<interface id:card id,device id> -c2 -twav

where card id and device id were identified in the previos step. For interface id, I will use "hw".

Open terminal

speaker-test -Dhw:0,0 -c2 -twav

OR

speaker-test -Dhw:ALSA,0 -c2 -twav

If you hear a lady's voice saying " FRONT LEFT on one speaker and FRONT RIGHT on the other speaker", then your sound card is good and its software driver was loaded. If software driver was loaded but speaker is not producing sound, then check your speaker connections and whether you forgot to switch ON the speaker.

Comments

author
skimorc (author)2016-05-26

where is the 5.1 audio!!!! =(

author
KnightOfPi (author)2016-01-27

Hi. Great tutorial, but i did need to enter

sudo amixer -c 0 cset numid=3 1

in step 10 to enable sound output.

author
DIY Hacks and How Tos (author)2015-10-17

Nice application for a Raspberry Pi