Home » Electronics » Getting Audio Out Working on the Raspberry Pi

Getting Audio Out Working on the Raspberry Pi

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

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

Sound Check

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"

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

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:

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

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

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’ll be fine.  But if you have some desire to do some sound processing with ALSA, your likely to be very frustrated.

Epilogue

This specific post has gotten about 25% of all the viewings of my blog. I’m not sure why this is the case but I speculate that there are many people tying to make RPi into a Media Player and looking for answers to their technical problems.

At this point in time, others have done some additional postings and they are more instructive than my post. You should check out:

About these ads

34 Comments

  1. Seamus Import says:

    Hi Jeff, Many thanks for the “Getting Sound to work…” info.
    I have discovered that until I edit the “/boot/config.txt” by un-commenting “hdmi_drive=2″, my sound does not work through my HDMI socket. Unfortunately this edit causes a thin blue line appearing across the bottom of the screen even when I start the GUI! Any suggestions would be appreciated.
    All the best and thanks again!
    Seamus

    • Seamus
      I didn’t touch the /boot/config.txt file and I didn’t attempt to get sound out of HDMI. Since I’m using ALSA, I select analog audio out using the command “amixer cset numid=3 1″. If your using ALSA, try this command “amixer cset numid=3 2″ for HDMI.

      Hope this helps …. I know your pain. It appears that sound wasn’t high on the RPi feature list.

      Good luck … Jeff

      • Seamus Import says:

        Hi Jeff, Thanks for the reply.
        I have set the amixer to numid=3 2 and followed all your other instructions to the letter. Sound does work if I force hdmi_drive=2 but I get this blue line at the bottom of the display as soon as the pi has read that line of /boot/config.txt.
        My queery realy is why? As the display works fine either way!

    • Jeff Pavlov says:

      thanks!!!! this worked for me.

  2. Seamus -
    Judging from your description (i.e. blue line at the bottom of the display), I suspect your running X Window. Might be some strange interaction going on inside the RPi with sound, HDMI, X, etc. What if you don’t run X when you boot up? What if you change the X Window Manager? If X is playing a role here, tweaking your X Window environment may clear the problem.

    I can’t reproduce your problem on my system and maybe is because I’m using X Windows differently than you. My configuration looks like this – http://jeffskinnerbox.wordpress.com/2012/10/04/establishing-an-x-window-system-environment-for-my-pc-and-raspberry-pi/

    Sorry, I don’t have an answer for you but experimenting with X might give you more insight.

  3. Nigel says:

    Hi, I am new to all of this and I am sure that the description you have given is correct, but to a complete novice I am at a loss where to start. Can you give me some simple instructions. My pi is playing iplayer, but i get no sound whatsoever……

    • Nigel –
      I have been doing a lot of re-configuring my RPi but I happen to have a 2nd one that I have hardly touched. So I went to this almost virgin RPi and did the follow:

      1) I ran the command “speaker-test -t sine -f 440 -c 2 -s 1″ and I got no sound …. so this is good because it appear to be the same problem your having (maybe).
      2) Next, enter the following command to unmute the speaker “amixer set PCM unmute”.
      3) Now make sure your have some reasonable speaker volume level with “amixer cset numid=1 — 80%”
      4) Now run the test again “speaker-test -t sine -f 440 -c 2 -s 1″. Hopefully now you hear sound!!

      All this is very cryptic, I know. But such is the state of RPi at this time. You should be set now. You will not have to enter these commands again unless you want to mute or change volume levels.

      I hope this helps! Let me know.

      • Nigel says:

        Thanks for the reply. I am pretty sure I have made it work now. I tried it on a different TV and it worked. I need to check it on the tv I was working with originally to be 100% sure. It may be something to do with the fact I had to use an adapter on the older tv to coonnect the HD cable.
        Thanks again for the reply.

        Nigel

  4. Bill says:

    I started doing the things in your instructions but when I did sudo apt-get install alsa-utils and sudo apt-get install lame it gave me a load of shit about things failing and I don’t deal with python very often so could you help me PLEASE!

  5. rocketero says:

    I don’t have any problem getting sound from the RPI when using apps like omxplayer (through the command line). But the problem is with apps like Audacious, or Guayadeque, when I open these apps and try to play an mp3 or a radio station (Guayadeque connects to Jamendo, Shoutcast and others music stations) there is not sound coming out. I have try to set all kind of changes in ‘preferences’ in each app but still I don’t get sound. Any suggestion on what’s is going on?

    • rocketero says:

      out of nowhere 2 apps now get audio output: Guayadeque and lxmusic. Both were not getting any audio output before. I didn’t do anything different, just reboot it and got them working. But still have a couple that don’t get audio: Audicious and VLC. I wish I could get audio from VLC as it’s a very nice app I use in Android and in Windows. As a note I have a LCD TV and the audio is HDMI.

      The latest RPI Wheeze, from 12/16/2012 doesn’t install lxmusic, so I did installed it afterwards and it’s playing fine wav and mp3 files.

      • Rocketero –
        I have been loading software via the Linux apt-get utility (I’m guessing you have not). I did fined VLC (but not Audicious) via apt-get. You need to make sure the apt-get database is up to date. First thing to do is to update apt-get’s local database with server’s pkglist’s files. Then checks for outdated packages in the system and automatically upgrades them. You can do this via:

        sudo apt-get update
        sudo apt-get upgrade

        This will get you the latest and greatest RPI Wheeze. Your going to get a great deal of output and it will take several minutes. Also it will likely promote you to enter “yes”.

        Now to check if VLC is within the database, run the following: apt-cache search VLC

        The output will list multiple VLC related packages. Now run: sudo apt-get install VLC

        You should be good to go at this point ….. assuming they did a good job porting VLC to the Raspberry Pi.

  6. Mark says:

    I have a similar problem. I cannot get the sound to work with the mini-RCA jack plugged into my speaker-less HDMI monitor RCA jack. I tried uncommenting the hdmi_drive=2 line and rebooting and it still didn’t produce sound. However I did get sound production when I used the amixer cset numid=3 1 command and plugged the audio jack into the pi. I tried rebooting after adding this command in the config.txt file and rebooting. But, no luck. I have to keep entering the amixer cset numid=3 1 command after booting up. Is there a way around this?

  7. James Boag says:

    Hi All, I’ve been working on my Raspberry pi powered door bell, It tweets, pushes a message to my mobile phone and should play a sample, my issue is that although by running sudo amixer cset numid=3 1 from the terminal I get pefect sound out via the 3.5 mm jack (don’t all door bells have a sub woofer) it fails to configure the sound to 3.5mm jack at boot, Ive tried alsactl store and all sort but i have to run the cset manually every time i boot, any suguestions, as i feel my door bell is bound to fail if user intervention of this sort is required every time i reboot my pi, Many thanks

  8. Hall 2001 says:

    Jeff,

    Thanks for the useful information. My experiences with LXDE up to this point are mostly favorable, especially from a hardware resource perspective. The documentation for the modules within it have been somewhat sparse. Thanks for sharing you observations!

  9. Best Raspberry Pi tutorial I have seen – because you explain why you do each stage, you cover all possibilities (of things not working) and best of all, it works!

  10. Michael says:

    I don’t think this line forces to Pi to use the 3.5mm jack for audio output.

    amixer cset numid=3 1

    Given these two commands:
    $ amixer -h
    cset cID P set control contents for one control

    $ amixer controls
    numid=4,iface=MIXER,name=’Master Playback Switch’
    numid=3,iface=MIXER,name=’Master Playback Volume’
    numid=2,iface=MIXER,name=’Capture Switch’
    numid=1,iface=MIXER,name=’Capture Volume’

    I think the line I initially referenced sets the Master Playback Volume to nearly zero. The line does not seem to control whether the Pi outputs audio to the 3.5mm jack or the HDMI port.

  11. Goran says:

    Thanks for this post. I managed to get MPD and Client175 runing and your post with the alsa commands halped me a lot. I hope you dont mind that I posted a link to here on my blog. Just something where I keep usefull posts in case I delete my bookmarks by mistake.

    Thanks again!

  12. magnificent put up, very informative. I’m wondering why the other experts of this sector don’t notice this.
    You should proceed your writing. I am sure, you have a huge readers’ base already!

  13. heldny says:

    Hey! I have sound on my music box with the raspberry but a problem with the autoboot. If the raspberry boot i will hear an sound. If the system is up i don’t have problems with the sound… The problem is that i use an python script for autoboot. The box don’t play the sound.. how i can change the boot options?

  14. Carolyn Krueger says:

    Hello – after trying almost everything I found a post here: http://www.raspberrypi.org/phpBB3/viewtopic.php?f=63&t=26936: “I just discovered that when I added the volume app to the taskbar, the slider was all the way down, which must be it’s default volume setting. I can now hear sound, after sliding it up! So easy once found.”

    In my case this worked immediately. It seems to be obvious, but I think this should stand at the very beginning for all raspberry beginners with sound problems! On the other side you learn a lot about raspi when trying all the other tricks…

  15. If you would like to improve your experience just keep visiting this site and be updated with the newest gossip posted here.

  16. Cliff Evans says:

    Hi Jeff – I followed your first command: sudo apt-get install alsa-utils > enter
    followed by : sudo apt-get install mpg321 > enter
    followed by : sudo apt-get install lame > enter
    Then Rebooted.
    But when it came to Enabling the Sound Module command: 1smod > enter

    The message I then got was : ” bash: 1smod: command not found”

    Jeff, Why is the 1smod command not found? Any help would be much appreciated!

    Cliff E

    P.S
    The difference between my Version of RPi and yours, is pi@raspberrypi:~
    my version is pi:raspberrypi:~
    I fear that I am clutching at straws here!

  17. […] the mpg321 drivers for the audio, a detailed summary of the drivers and options can be found on Jeff Skinners Box.  For this project you will simply need to run the following commands in the command promptbash […]

  18. craiginwales says:

    amixer cset numid=1 — 80% works perfectly for me (thank you for sharing), but after each boot I need to type this command again. Anyway to make this persistent?

  19. Ross says:

    I have a simple python3 script which can play audio using mpg321 made buttons for start, stop (kill) buttons and buttons to switch between audio jack and HDMI audio on PI (kind of cool for debugging)
    having trouble with volume control- built a slider which allows volume input from 0-100. variable vol_set

    Issue is with python3 code on Pi

    os.system(‘amixer cset numid=1 — {vol_set}%’) #does not work

    how do I pass vol_set which is my variable for new volume into amixer??
    amixer cset numid=1 80% works from command line can change % from 0- 100%

  20. […] Fuentes: diverteka, hackingthesystem4fun.blogspot.com.es,  jeffskinnerbox.wordpress.com, v1.corenominal.org […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 42 other followers

%d bloggers like this: