I decided to attach some speakers to my Raspberry Pi. I didn’t want to spend much so I picked up a pair of stereo speakers from my local Poundworld. They cost £1 exactly. They aren’t exactly the sort of speakers you would hook up to your HiFi system but for experimenting you can’t beat the value. They have a 3.5mm jack which plugs into the Pi’s analog audio connector.
In time I want to play sound files from my Python programs to provide audio notifications.
Install the Sound Drivers
Update : The newer Debian “Wheezy” image enables the sound driver by default so this section can be ignored.
In the Debian “Squeeze” image the ALSA sound drivers are considered experimental and have been disabled by default. To enable sound you need to type the commands below into the command line :
sudo apt-get install alsa-utils
That will install the required sound driver files and tools. Type “Y” when prompted. The following command will load the sound driver :
sudo modprobe snd_bcm2835
By default the output is set to automatically select the interface (HDMI if available otherwise analog). You can force it to use a specific interface using :
sudo 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 :
sudo amixer cset numid=3 1
You will need to run the modprobe command again when your Pi is rebooted/restarted.
Playing A WAV File
In order to play a WAV file we need a file to play. Type the following command to download a test WAV file :
sudo wget http://www.freespecialeffects.co.uk/soundfx/sirens/police_s.wav
Now we can play it :
sudo aplay police_s.wav
If you are lucky the sound will play through your speakers or headphones.
I tried a number of WAV files but only some of them played. Hopefully this will be resolved in future releases of the sound drivers.