You are on page 1of 7

SCPhillips.

com
Sound conguration(http://scphillips.com)
on Raspberry Pi with ALSA http://blog.scphillips.com/posts/2013/01/sound-...
MENU

Sound conguration on Raspberry Pi withALSA


Blog (http://blog.scphillips.com/) MENU

Posted in Posted Audio (../../../../categories/audio/) RaspberryPi (../../../../categories/raspberry-pi/) on 2013-01-19 14:53

Using a Raspberry Pi with Android phones Previous (../using-a-raspberry-pi-with-android-phones-for-media-streaming/)

Playing music on a Raspberry Pi using Next (../../07/playing-music-on-a-raspberry-pi-using-upnp-and-dlna-revisited/)

While setting up a Raspberry Pi to play streamed music using UPnP (../using-a-raspberry-pi-with-android-phones-for-media-streaming/),


I have had quite a bit of trouble understanding how to configure the sound on my Raspberry Pi. This is partly because I am running it in
headless mode (no graphical desktop) and partly because sound on Linux is fiendishly complicated (http://tuxradar.com/content
/how-it-works-linux-audio-explained).

Anyway, I am making progress in understanding whats going on but I am no expert. Here are my findings on how to control the ALSA
system from the command line. All I am focussed on here is getting control of the sound output by the 3.5mm stereo socket. If you want
to control the output over the HDMI socket or an external USB sound card then youll need to do some of your own investigation.

ALSA is the lowest level of the Linux sound stack. The alsa-utils package comes ready installed on the debian wheezy distribution I am
using ( 2012-12-16-wheezy-raspbian.zip ). It provides some useful commands: amixer , alsamixer , alsactl , aplay and speaker-test .

Testing
Its good to be able to test things as you go a long to see what affect configuration changes have. You can do this with the speaker-test
command for one, which will by default play white (actually pink) noise out of the speakers. Press Ctrl-C to stop it.

Another command to try is aplay . This command will play WAV files for instance as well as lots of other basic sound file types (but not
things with complex compression such as MP3, FLAC, OGG, etc). The Pi comes with some sound files already present, for instance in
/usr/share/scratch/Media/Sounds . Try:

$ aplay /usr/share/scratch/Media/Sounds/Vocals/Singer2.wav

To get more information while the sound is playing, use the -v and -V options, e.g.

$ aplay /usr/share/scratch/Media/Sounds/Vocals/Singer2.wav -v -V mono

That will print a lot of info about the sound file and what device it is being sent to as well as a VU meter as the sound plays.

To see the available devices, use aplay -L . My machine gives this output (since installing PulseAudio):

$ aplay -L
null
Discard all samples (playback) or generate zero samples (capture)
pulse
Playback/recording through the PulseAudio sound server
sysdefault:CARD=ALSA
bcm2835 ALSA, bcm2835 ALSA
Default Audio Device
1 of 7 Monday 27 March 2017 04:00 PM
It showsconguration
Sound that the default device to use is directly
on Raspberry to the
Pi with ALSA device. Interestingly,
ALSA if I try to play via PulseAudio using aplay with
http://blog.scphillips.com/posts/2013/01/sound-...
aplay -D pulse then I get a bit of a pop but no sound

amixer
$ amixer
Simple mixer control 'Master',0
Capabilities: pvolume pswitch pswitch-joined penum
Playback channels: Front Left - Front Right
Limits: Playback 0 - 65536
Mono:
Front Left: Playback 65536 [100%] [on]
Front Right: Playback 65536 [100%] [on]
Simple mixer control 'Capture',0
Capabilities: cvolume cswitch cswitch-joined penum
Capture channels: Front Left - Front Right
Limits: Capture 0 - 65536
Front Left: Capture 0 [0%] [on]
Front Right: Capture 0 [0%] [on]

Looking at the top part of the output, we can see that the playback volume for the left and right channels are both at maximum (65536,
which is 2^16) and are both unmuted (on).

To control these settings we can use the alsamixer command which gives a graphical display (in your terminal) or the amixer command.
Lets look at amixer first:

$ 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'

Thats the list of things you can change (control) through the command. We can look at the playback volume in more detail:

$ amixer cget numid=3


numid=3,iface=MIXER,name='Master Playback Volume'
; type=INTEGER,access=rw------,values=2,min=0,max=65536,step=1
: values=65536,65536

This shows the maximum and minimum values for the playback volume and that it is actually a pair of values (for left and right
channels). For instance, to change the playback volume we can do:

$ amixer cset numid=3 50%


numid=3,iface=MIXER,name='Master Playback Volume'
; type=INTEGER,access=rw------,values=2,min=0,max=65536,step=1
: values=32768,32768

If you wanted to change the left channel to 50% and the right to 100% for some reason then this command is for you:

$ amixer cset numid=3 50%,100%


numid=3,iface=MIXER,name='Master Playback Volume'
; type=INTEGER,access=rw------,values=2,min=0,max=65536,step=1
: values=32768,65536

To mute the sound you can you control 4:

$ amixer cset numid=4 off


numid=4,iface=MIXER,name='Master Playback Switch'
; type=BOOLEAN,access=rw------,values=1
: values=off

2This
of 7also affects the volume values on control 3: Monday 27 March 2017 04:00 PM
$ amixer cget numid=3
Sound conguration on Raspberry Pi with ALSA http://blog.scphillips.com/posts/2013/01/sound-...
numid=3,iface=MIXER,name='Master Playback Volume'
; type=INTEGER,access=rw------,values=2,min=0,max=65536,step=1
: values=0,0

However, the value of the volumes must be stored somewhere because when you unmute it again it restores them.

I have found odd effects when changing the volume while it is muted so best to avoid doing that.

Saving the ALSAstate


The operating system saves the ALSA sound configuration when you turn it off and restores it when you turn it on. This is done with the
/etc/init.d/alsa-utils script. The state is stored and restored using this file: /var/lib/alsa/asound.state

Once you have the sound configuration as you want it you can store it there using the alsactl command which you have to run with root
permissions as it changes a protected file. Lets set the volume to max, unmute it and store it:

100%
umid=3,iface=MIXER,name='Master Playback Volume'
; type=INTEGER,access=rw------,values=2,min=0,max=65536,step=1
: values=65536,65536
on
umid=4,iface=MIXER,name='Master Playback Switch'
; type=BOOLEAN,access=rw------,values=1

For me, this gives this changes /var/lib/alsa/asound.state to be:

3 of 7 Monday 27 March 2017 04:00 PM


state.ALSA {
Sound conguration on Raspberry Pi with ALSA http://blog.scphillips.com/posts/2013/01/sound-...
control.1 {
iface MIXER
name 'PCM Playback Volume'
value 399
comment {
access 'read write'
type INTEGER
count 1
range '-10239 - 400'
dbmin -9999999
dbmax 400
dbvalue.0 399
}
}
control.2 {
iface MIXER
name 'PCM Playback Switch'
value true
comment {
access 'read write'
type BOOLEAN
count 1
}
}
control.3 {
iface MIXER
name 'PCM Playback Route'
value 2
comment {
access 'read write'
type INTEGER
count 1
range '0 - 2'
}
}
}

If we mute it then have another look, line 5 changes to value -10239, line 13 changes to dbvalue.0 -9999999 and line 19 changes
to false.

If we were to unmute and then set the volume to 50% (as described above) then line 5 changes to value -1405 and line 13 to
dbvalue.0 -1405 (with line 19 being now set to true). Not quite sure where these numbers come from, they are probably the decibel
volumes which are logarithmic, but lets not go there

If you want to stop the current state being saved when the machine shuts down then you need to disable the alsa-utils init script from
running when the machine enters runlevel 0 (halt) and 6 (reboot) (http://www.debian-administration.org/articles/212). A slightly
hacky way of doing this is to execute these commands:

$ sudo mv /etc/rc0.d/K01alsa-utils /etc/rc0.d/k01alsa-utils


$ sudo mv /etc/rc0.d/K06alsa-utils /etc/rc6.d/k01alsa-utils

Note that the files have been renamed to have a lowercase k at the start. When the machine enters runlevel 0 for instance (it does this
when shutting down) all the scripts starting with K in the /etc/rc0.d folder are executed. By renaming it to start with a lowercase k
the file is ignored and the ALSA state is not saved.

alsamixer
As well as using amixer on the command line to change the volumes and so on, you can also use a slightly graphical utility called
alsamixer. Just type alsamixer and youll get something like the screenshot shown below:

4 of 7 Monday 27 March 2017 04:00 PM


Sound conguration on Raspberry Pi with ALSA http://blog.scphillips.com/posts/2013/01/sound-...

alsamixer on a Raspberry Pi

In this utility you can move the volume up and down using the up and down cursor keys, mute and unmute using the m key and move
from one device to another (if you have them) using the left and right cursor keys. Press the Esc key to exit. If you play with this and
then type amixer once youve exited then youll see that the changes are reflected in the output of that command as well.

Finalthoughts
I hope thats useful to someone like me who is struggling to understand all this. It is only one part of the stack, so I hope to look into
PulseAudio in more detail another time as I think thats where my problems are at the moment.

One last thing: if you completely mess things up then this command should sort it all out and make things sane again (sets volume to
44% for me):

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

Audio (../../../../categories/audio/) RaspberryPi (../../../../categories/raspberry-pi/)

Using a Raspberry Pi with Android phones Previous (../using-a-raspberry-pi-with-android-phones-for-media-streaming/)

Playing music on a Raspberry Pi using Next (../../07/playing-music-on-a-raspberry-pi-using-upnp-and-dlna-revisited/)

Comments

5 of 7 Monday 27 March 2017 04:00 PM


Sound conguration
26 Comments Stephenon Raspberry
C Phillips' blog Pi with ALSA http://blog.scphillips.com/posts/2013/01/sound-...

1 Login

Sort by Best
Recommend 36 Share

Join the discussion

Rob Robinette 3 years ago


Great info. My pi's USB sound card's volume was very low, even with alsa's volume at 100%. Following your
lead I ran "alsamixer" in a terminal window, selected F6 to choose "USB Audio" and raised the volume from
46% to to 100% using the up arrow--problem solved. Thanks for the help.
3 Reply Share

Keno Rosario a year ago


Hello sir! Anychance of having a audio processing with alsamixer?
Reply Share

Theagarajan Thanikachalam 2 years ago


Great Info. Thanks a lot.. Keep writing :)
Reply Share

Stephen C Phillips Mod > Theagarajan Thanikachalam 2 years ago


Thanks for commenting :-)
Reply Share

Walter1957 2 years ago


Thanks for the blog. Failed with jack Audio ( very faint chainsaw hiss) on a RPi B+ version 1 but the
LXTerminal version works fine.

Do you know if it is possible to open a .wav file directly in a LXTerminal? F4 opens a folder in LXTerminal.
At the moment I Rename & Copy the file name then Paste after 'aplay ' .
Reply Share

Stephen C Phillips Mod > Walter1957 2 years ago


I'm not quite sure what you mean?
Reply Share

Amit Hermony 2 years ago


Any
insight in using the Alsa C api for controlling volume in each channel?

I would like to control the volume in each channel by using the Alsa C api
I reviewed the code example and I got it to work BUT , each time I set one channel the other is set to the
same level (like mono although I hear stero)
I also noticed that when using alsamixer GUI I have only one bar (can control each channel)
I am able to read the min and max volume and to set the overall volume
What could be wrong?
Thanks

The main functions used were

snd_mixer_selem_set_playback_volume(elem,SND_MIXER_SCHN_SIDE_LEFT, outvol0)
snd_mixer_selem_set_playback_volume(elem,SND_MIXER_SCHN_SIDE_RIGHT, outvol1) {
Reply Share

Scooby Doo > Amit Hermony a year ago


Privacy Enhanced Linux for the Pi 2 comes with all the audio stuff pre-configured. Played MP3 files
on my HDMI TV straight out of the box.

tinyurl com/pelinux (no dot in link)


1.7GB gz compressed microSD card image
user "pi" with password "pi" just like Raspbian
Reply Share

6 of 7 Stephen C Phillips Mod > Amit Hermony 2 years ago


Monday 27 March 2017 04:00 PM
Sorry, the entirety of my ALSA knowledge is written in the article above :-D
Hope you can find an answer somewhere else.
Sound(https://www.addtoany.com/share#url=http%3A%2F%2Fblog.scphillips.com%2Fposts%2F2013%2F01%2Fsound-conguration-on-raspberry-pi-with-alsa%2F&
conguration on Raspberry Pi with ALSA http://blog.scphillips.com/posts/2013/01/sound-...
title=Sound%20conguration%20on%20Raspberry%20Pi%20with%20ALSA)
(/#twitter) (/#facebook) (/#google_plus)

I am a father and fostercarer


a principal research engineer at the IT InnovationCentre
a salsateacher
a Woodcraft Folksupporter
and in my spare time I write and maintain this website

Copyright Stephen C. Phillips, 2015

Cookies, Privacy andLicences (../../../../privacy.html)

7 of 7 Monday 27 March 2017 04:00 PM

You might also like