Raspberry Pi ALSA

From WA2IAC Wiki
Jump to: navigation, search

Contents

Introduction

Hot off the Press

(circa 5/29/2013) Many problems I've encountered have been caused by known problems in Raspbian (see below). Because fixes are in the pipeline, I choose to wait for them to get into the distro via updates. I'm keeping track of them here for my own benefit, you're welcome to ride along...

As of this date, some tres importante fixes still haven't made it into the pipeline, and I'm wondering now if they will -- for the version of Raspbian I'm running. I'll be reviewing this and updating shortly. I'm still pretty frustrated at not being able to use Raspbian for the purposes I have in mind... which in the meantime are hogging up other, less suitable, but more capable hardware.

Hot Links

02/09/13 Raspberry PI – getting audio working http://cagewebdev.com/index.php/raspberry-pi-getting-audio-working/

05/29/13 Stumbled across this: http://www.raspberrypi.org/phpBB3/viewtopic.php?f=66&t=7107 but beware of the dates!

mmap Kernal Update

The mmap method for driver communication is not supported in Raspbian at present. This has been solved thanks to Javier Martinez Canillas. The preceeding link will take you to the details. It was dated 4/27/2013 at this writing.

Without this kernel fix, at best, ALSA performance will be "challanged". You may also want to wait on this one.

Jack

Actually, it's "jackd" (the daemon). This popular package has known problems with Raspbian, and various fixes are in the pipeline.

Gstreamer

Gstreamer and friends (its associated stuff) has known problems on Raspbian. This is unfortunate, because gst123 seems to be the best solution if you want to have a single command-line player that can play mp3, ogg, and FLAC formats. I use it in applications I've built that run on other distros, so I'd like to use it on Raspberry Pi, but sadly that's not possible until the fixes are in.

More Introduction

(Circa 5/2013) The information on this page assumes the operating system is Raspbian, which is simply a port of Debian for ARM processors recompiled so that the entire distro supports the floating point processor on the Raspberry Pi. See other references to this wiki at the bottom.

Googling this topic produces lots of outdated junk, so I'm writing this page to be reliable for me.

At Present, some of this is lifted from here. I've been adding more fast.

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

Problems to Solve

So far, many/most operations require root privs. This is a giant step backwards. Note '#' prompt where root seems needed, else the prompt shown in examples is '$'. Of course, "$ sudo ..." is an alias for "# ...".

WA2IAC app-specific packages needed include:

liquidsoap, ogg-vorbis libraries and tools, gst123 [these need to be documented, and anything left out added], unfortunately this horse is already out of the barn. Complete step-by-step configuration cookbook instructions will have to await the next Pi!

WA2IAC app notes

To be added here.

/etc/alsa.conf

Restarting the Audio System

For most changes to /etc/asound.conf you will need to restart the sound server:

$ sudo /etc/init.d/alsa-utils restart

Early Observations

The stock /etc/asound.conf file looks like:

pcm.mmap0 {
    type mmap_emul;
    slave {
      pcm "hw:0,0";
    }
}

pcm.!default {
  type plug;
  slave {
    pcm mmap0;
  }
}

... the problem here appears to be that a 'plug' channel (?) is assigned by default, however, jack is not installed. Bummer! Causes many of the players to fail.

Status: non-functional -- see "Discussion Page"

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 /proc/modules file. It shows which loadable kernel modules are currently loaded. In my case, lsmod gives me:

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

This appears to have been necessary in the past, but not with my contemporary release of Raspberry Pi. Still, this is good to know, particularly if other distros are to be used.

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 (note root prompt!):

# amixer cset numid=3 1

Sound Check

[This section is totally retarded and needs to be re-written]

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 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"

New Material:

The Raspbian distro comes with a number of sound files that can be used for testing. Afterward, you may wish to remove them to conserve space.

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.

[This part is retarded; some useful to pointers to /proc need to be added] 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/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

[This section needs review; not checked by WA2IAC!] 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.

The /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. My RPi /etc/asound.conf file looks like this:

pcm.mmap0 {
    type mmap_emul;
    slave {
        pcm "hw:0.0";
    }
}

pcm.!default {
    type plug;
    slave {
        pcm mmap0;
    }
}


For most changes to /etc/asound.conf you will need to restart the sound server (ie. sudo /etc/init.d/alsa-utils restart) for the changes to take effect.

I attempted to implement the software volume controls outline in a softvol how-to that I found, but I couldn’t get it to work. I did some additional digging, and I found a solution buried within a python script for a Adafruit project. The following works for controlling the volume (in this case, reducing the volume to 80% of maximum):

amixer cset numid=1 -- 80%

Note that you can use this command to change the volume while sound is being played an its effect takes place immediately. Also, I noticed that once the volume has been adjusted, its effect remains even after a reboot.

WAV and MP3 Conversion

[This section is retarded and needs to be re-written. Who wants to convert a file that's already encoded in a lossy format like MP3 anway? A walking fruitcake?]

Th 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

Bottom Line

While you can get ALSA working on the Raspberry Pi, it appears only partly supported, maybe buggy, and poorly documented. If you just want to simply get sound out of the device (like I do), you may be fine, or you may find yourself in a room with doors to twisty mazes branching out in all directions. If you have some desire to do some sound processing with ALSA, your likely to be very frustrated.

Other Audio Systems

Liquidsoap

Liquidsoap is an odd bird. It's an Ocaml based tool for streaming audio, and now video as well. Until recently (as of 5/2013), the distros that include liquidsoap (Ubuntu and Debian) provided the 0.92 version. With version 1.x.x came incompatible language changes, more features, and many bugs fixed. Ubuntu and Debian are now distributing 1.x.x version(s). Liquidsoap apparently runs well on most versions of Linux, but for other flavours, such as Fedora/RedHat/CentOS, you'll have to build it yourself. This is a signpost: if you're messing with audio, you probably want to be running Ubuntu/Debian (unless you have religious or other issues about distros). Liquidsoap is a whole other topic, and I'll address that in another Wiki page soon: importantly, what does and what doesn't work (as documented).

Jack

- is an audio router system that can coexist with ALSA. I'll be writing more about this soon! Jack involves a yet another server daemon. It appears Jack may be the best way to deal with audio on a VPS (Virtual Server) where audio/motherboard hardware is not available.

Pulse Audio

- this is the audio system that's included with Ubuntu. It coexists with ALSA well, but if you're not aware of both being present, it can get confusing.

See Also

Video: Sound configuration on Raspberry Pi with ALSA

http://wa2iac.com/wiki/index.php?title=Liquidsoap