Things have moved on a bit with the Raspbian image since I wrote my Raspberry Pi Speakers & Analog Sound Test post. So I decided to take another look as I wanted to start adding some audio to my experiments. I’m making slow progress with my remote controlled car project but I figured I might as well shoe-horn some audio in there as well.
One thing that has changed in the latest Raspian image is that the alsa sound drivers have improved and are enabled by default. So I decided to hook up a rechargeable powered speaker I had using a short 3.5mm jack plug lead and see if I could play some WAV and MP3 files.
The first thing to do is run :
lsmod | grep snd_bcm2835
and check snd_bcm2835 is listed. If it isn’t then run the following command :
sudo modprobe snd_bcm2835
If the module isn’t loaded automatically when you boot then you can force it to load by using the following process :
cd /etc sudo nano modules
Then add ‘snd-bcm2835′ so it looks like this :
# /etc/modules: kernel modules to load at boot time. # # This file contains the names of kernel modules that should be # loaded at boot time, one per line. Lines beginning with "#" are # ignored. Parameters can be specified after the module name. snd-bcm2835
By default the output is set to automatically select the default audio interface (HDMI if available otherwise analog). You can force it to use a specific interface using :
amixer cset numid=3 n
Where <n> is the required interface : 0=auto, 1=analog, 2=hdmi. To force the Raspberry Pi to use the analog output :
amixer cset numid=3 1
I usually have to do this if I boot the Pi with an HDMI cable plugged in. Otherwise it defaults to the 3.5mm jack automatically.
Playing A WAV File Using aplay
Everything should be ready to test some sound files. First up WAV. Use the following commands to grab some WAV files from my sound archive :
wget http://www.freespecialeffects.co.uk/soundfx/sirens/police_s.wav wget http://www.freespecialeffects.co.uk/soundfx/computers/bleep_01.wav
To play it use :
If you are lucky the sound will play through your speakers or headphones.
Playing An MP3 Using MPG321
There are numerous ways to play MP3s from the command line but I tend to use MPG321. This can be installed using :
sudo apt-get -y install mpg321
Once installed we can grab an MP3 to experiment with :
The MP3 file can be played using :
The volume can be adjusted using the ‘g’ command line option. In the example below I set the volume to 50% :
mpg321 -g 50 bubbling_water_1.mp3
Playing An MP3 Using Omxplayer
You can also use the excellent Omxplayer to play MP3s. To get a list of options type :
It is installed by default in the latest Raspbian release but if you need to install it manually you can do so using :
sudo apt-get -y install omxplayer
To play a sound file you simply need to type :
The plus (+) and minus (-) keys can be used to adjust the volume of the playback.
This post covers basic audio playing from the command line. If you want to start playing lists of music then you should explore some of the more advanced media software available for the Pi. This includes XBMC and OpenElec.
In a future post I will cover playing audio within Python scripts using Pygame.
Here are some photos showing the rechargeable powered speaker I use for my Raspberry Pi command line audio tests :