RPi-05: Making Sound with Raspberry Pi

Creating a tone

Raspbian comes with the speaker-test program that allows us to test our speakers by creating tones.

How about you make a tone on your Raspberry Pi with the following command:

speaker-test -t sine -f 600 > /dev/null

Press ctrl+c (that’s the control key and the c key at the same time) to make the damn noise stop!

Not hearing anything? Apart from the obvious check of ensuring the headphones are plugged in all the way, there are a couple of things you can check:

  1. You can tell your Pi whether it should use the headphone or the HDMI port for audio. We can do that by using

sudo raspi-config

again. Choose Advanced Settings and this time choose A6 Audio. Then select option 1 – ‘Force 3.5mm (‘headphone’) jack, and exit raspi-config as usual.

  1. In certain situations, the kernel module required to support audio may not be enabled. We can do that with:

sudo modprobe snd_bcm2835
Now try the speaker test command again.

Text to Speech

Our Raspberry Pi is more than just a tone generator. We can do things like text-to-speech and playing mp3 files. In fact this is what differentiates it from other platforms (such as the Arduino).

To give your Raspberry Pi a voice we must first install some more software called eSpeak.

Install espeak by using the following command:
sudo apt-get install espeak

We’re now ready to give our Raspberry Pi the gift of speech! This is done by running:

espeak “Hello World!” > /dev/null

Playing a .wav file

Before we play some audio, we first need some audio to play with. Use the command line downloader wget to download a sample from the Little Bird Electronics website.

wget https://ia800207.us.archive.org/29/items/MLKDream/MLKDream_64kb.mp3

The Raspberry Pi comes with a piece of software called aplaythat lets you play .wav files.

To play a .wav file run the following command.

aplay ~/sample.wav

We can also play handy calibration sounds; for example:

aplay /usr/share/sounds/alsa/Front_Center.wav

Use your directory traversal skills to have a look around the /usr/share/sounds/alsa directory for some other calibrations sounds you can play.

Ok, you’ve made enough noise! Let’s learn how to hook up some electronics. But first we need to learn how to safely switch off our Raspberry Pi!

Leave a Reply

Your email address will not be published. Required fields are marked *