Playing with the Sound on Raspberry Pi

Playing with the Sound on Raspberry Pi

Raspberry Pi Speaker

Raspberry Pi Speaker

Because I had been using a display without speakers, until now I had been playing with the Raspberry Pi without any sound. I found a small unused speaker in my house, so I connected it right away! Finally, I can try out playing with sound, which is what wanted to do!

Because I never touch anything on the audio input, we will start with the default setting. First, I would like to simply start by playing some sounds!

What We Will Use

Speaker and earphones
Picture 1

This time, the start is the speaker!

This speaker is rechargeable and the size of my palm. So it’s size and ease-of-use are perfect for the Raspberry Pi. To make sure it does not run out of power in the middle, I have made sure it is fully charged before playing with it.
I also brought the earphones that came with my iPhone 5.  I wish I had realized sooner… But because you just need to connect to an audio terminal, it is possible to substitute with some ordinary 3.5 mm earphones. I recommend using earphones for when you want to test something quietly.

How to Play Sound?

After preparing the speaker, let’s actually try playing a sound! Speaking of sound, there were some items related to “sound” in the tutorial in Scracth, the programming software programming we played around with before. So, I would like to first try using Scratch.

Raspberry Pi Scratch
Figure 1

I have assembled it like this!
This is a minimal, simple process.  When clicking on the cat icon (sprite 1), we should hear a cat’s “meow”. If you click on the cat right away, a “meow”…huh? It doesn’t make any sounds!?

I looked into it thinking that maybe the volume was set at zero. But I discovered a way to set it from the command line.

Raspberry Pi Alsa Mixer
Figure 2

It is possible to adjust the volume with the up and down keys. Let set it up to an appropriate value. When you are done, return to the previous screen by pressing the [Escape] key.

And let’s re-run Scratch! …… No sound yet!
After some more investigations, it seems that if you’re using the HDMI, it may be selected as the audio output destination. In that case, no sound will be heard from the speaker. You nned ti set either the HDMI or the audio terminal to be used for the audio output. Similarly to the “alsamixer” command, we will use the “amixer” command to set the audio output to the analog output.

Let’s start again and run Scratch again!
This time, the cat meowed!!

After executing the script we created, the “meow” would not stop. So, it seems that we need to add a “stop all sound” command. To stop the sound from being interrupted, let’s also add a sleep process of 1-2 seconds. It worked perfectly when executed, as in Figure 3.

Raspberry Pi Scratch Script
Figure 3

Now that we’re able to play sounds, next I’d like to be able to listen to music.

Playing  MP3! Starting by Using Some Tools

By looking for any pre-installed music player software, I found a tool called “VLC Media Player” in the “Sound and Video” in the Start menu! Let’s see if I can play music with that.
Windows, Mac and Linux versions are also available for download on the official site.

Raspberry Pi VLC
Figure 4

This is the initial screen (Figure 4).
By the way, VLC media player would not launch without being a pi user (speaking of which, the same thing happened when using “ScratchGPIO5“).

Raspberry Pi VLC Open Folder
Figure 5

To play a music file, first choose the playback method from “media” in the menu at the top. Besides local files, you can also specify a file on a disk drive or the from the Internet. If you select “Open Folder,” the files in the specified folder will automatically play in succession.

Because there are some music files used by the packages in the Raspberry Pi, let’s test playing music using these files!

Let’s Play MP3 Using Commands!

I will first introduce the incorrect example.
Similar to when we tested playing the .wav file, I thought we could also play the mp3 with the “aplay” command. Here is the executed command.

As soon it runs, there’s a terrible noise!
You cannot use the “aplay” command to play MP3, as it seems to need a separate package. To stop playback, press [CTRL] + [C]! This is command is really useful. It can be used to force quit when there is an operation mistake or when in an infinite loop, etc. (This is like [Ctrl] + [Alt] + [Delete] for Windows!).

Let’s start again and install the package!
This time, I use the “mpg321” package, an mp3 player that uses command lines.

After installing it, I was able to use it from the command line immediately.
Just specify the mp3 file after “mpg321” for the playback command and we’re set!

Raspberry Pi
Figure 6

It played without problems!
It will display the file information, such as the title, and play time on the console. By using the “-q” option, it is possible to have it play without displaying anything.
Other option values can be displayed in a list if run with the “-help” option. By the way, even if you make a mistake in typing the option value, this list and a message will be displayed.

Raspberry Pi
Figure 7

I tried capturing the actual results of playing the MP3 on this video!

Can you hear the song playing?
I ran it with a “-K” option which allow me to adjust the volume with the keyboard during playback. It will start with the volume at its maximum (100%). This is the same volume level as the default state without options. As described in Figure 8, pressing the “/” key to decrease the volume or the “*” key to increase it, you can confirm on the console that the percentage changes by three points.

For the MP3, I borrowed a license-free sound file!

Video Playback

While trying to play music, I also attempted to play a video!
As I didn’t have any video file available in the Raspberry Pi to test, I used a video I just shot. This is a video I shot with my iPhone, so the file format is .mov. Let’s put the file “test.mov” in the appropriate folder in the Raspberry Pi.

For the package for playing videos, there is something called “omxplayer” pre-installed on Raspbian. Simply specify the file name and the video will play.

When you run it, the video will appear and fill the entire display width. It will look like Figure 8!

Raspberry Pi Raspbian OMX Player
Figure 8

If there is no sound, use the “-o” option and specify the audio output destination. For audio terminal, use “local”; for output via the display, specify “HDMI.”

To confirm the other option values, let’s execute the command with the “-h” option.

Raspberry Pi
Figure 9

Omxplayer can be controlled with the keyboard while playing. You can confirm key inputs with “-keys.”

Raspberry Pi
Figure 10

Summary

We were able to successfully play both music and video!
At first, I struggled to the Raspberry Pi to play sounds.  But I was able to achieve my goal (it is difficult to communicate this in words, but actually I spent a considerable amount of time).
There are many option values for both “mpg321” and “omxplayer,” so it seems like it would be interesting to try playing around with changing various things. In the future, I am looking forward to playing with BGM on the Raspberry Pi!

The next article, will be my long-awaited Internet radio receiver!