A sound box is like a music box, except it can play one or more audio files. I previously wrote tutorials on an easier sound box to build using a “DF Player” or using an “Arduino” + a “DF Player”, but this time we’re going to build a more “computer-like” stereo sound box, which means it can also be more “intelligent”… As always, if you have any questions, feel free to contact me!
Overview
Whether it’s a Zero, 1, 2, 3, or 4, the Raspberry Pi is an inexpensive microcomputer that can also do great things with audio. In this guide, we’ll see how to turn it into a stereo audio file player using the “Python 3” programming language. It doesn’t require any special skills.
Except for the “Zero” version, the Raspberry Pi has an onboard sound card with a jack output for stereo headphones/line-out. However, regardless of the version, you can plug in any commercial USB external sound card to improve quality or do multitrack audio.
One advantage of the Raspberry Pi is that it has many digital inputs and outputs thanks to what’s called the GPIO. It’s a large 40-pin connector you can hook up to all kinds of things (LEDs, sensors, actuators, etc.).
Step by step
1. Preparation
The first step is to install Raspberry Pi OS. As a reminder, you need to plug your microSD card into a computer and use “Raspberry Pi Imager” to install the OS. This app is available for Windows, Mac, and Linux.
You could use the “Lite” OS, but then you’d have to configure everything via the Terminal, meaning command line only, without a mouse. If you need this tutorial, you’re probably a beginner. So install the “Full” OS.
Once the OS is on the microSD card, insert it into the Raspberry Pi, plug in a keyboard, a mouse, and a screen, then boot it up.
2. Setup
Now answer all the Raspberry’s questions: country, language, keyboard, etc. Most importantly, it will ask you to create a “login” and a “password,” as well as your Wi-Fi credentials. Then, when prompted, update the OS.
3. Organization
Click the “globe” icon at the top left to open the “Web Browser.” Open LaSonotheque.org and download any MP3 or WAV file.
A bar appears at the bottom of the “Web Browser” showing download progress. Once it’s finished, click the up arrow, then “Show in folder.”
The file manager opens directly in the “Téléchargements” or “Downloads” folder (remember that folder name).
Right-click the file and choose “Rename” to rename it to “test”. Keep the .mp3 or .wav extension at the end.
You can also go back to LaSonotheque.org and open the guide you’re reading right now directly on the Raspberry Pi. That will make it easier to copy/paste the code snippets I’m going to give you.
4. Programming
Since we’re going to use the “VLC” player, you need to install it. Click the “black screen” icon at the top left to open the “Terminal.”
Type:
pip install python-vlc
Press Enter. “VLC” will install. Once the Terminal shows it’s ready for new commands (in green and blue), close the “Terminal.”
Click the raspberry icon at the top left, then “Programming” > “Thonny”. It’s the programming software I prefer.
Copy and paste this full code:
import time, vlc
player = vlc.MediaPlayer()
player.audio_set_volume(100)
def lecture():
player = vlc.MediaPlayer("/home/LOGIN/Téléchargements/test.wav") # Replace "LOGIN" (with your Raspberry username) and "Téléchargements" (if the folder containing the sound file is different)!
player.play()
time.sleep(5)
player.pause()
time.sleep(1)
player.play()
time.sleep(5)
player.stop()
while True:
lecture()
Don’t forget to replace “LOGIN” (with your Raspberry username) and “Téléchargements” (if the folder containing the sound file is different)!
Click “Run” to see that it works!
You can click “Save” to save the program. To make the rest simpler, name it “test.py” and save it in the “Téléchargements” folder.
A simple explanation of this code:
- We import the “time” library (to keep track of time) and “vlc” (to play the audio file).
- We name the audio player “player”.
- We set the player volume to 100%.
- We define the “lecture()” function.
- We point to the “test” file we downloaded.
- We start playback of that file.
- We wait 5 seconds.
- We pause playback.
- We wait 1 second.
- We resume playback.
- We wait 5 seconds.
- We stop playback.
- We create an infinite loop with “while True”.
- Inside it, we run the “lecture()” function.
5. Autostart (optional)
For now, the program starts manually. So we’re going to automate it so it runs in a loop as soon as the Raspberry boots.
Open the “Terminal” (top left, the black icon). Then type this command, which will make your script executable. Make sure the folder names (“LOGIN” and “Téléchargements”) are correct. Then press Enter:
sudo chmod +x /home/LOGIN/téléchargements/test.py
Now you’re going to open the file that controls what launches automatically at startup. Then press Enter:
sudo nano /etc/xdg/lxsession/LXDE-pi/autostart
A few lines are already there. Scroll to the very bottom and add this line, again adjusting the folder names as needed:
@lxterminal --command="/home/LOGIN/téléchargements/test.py"
To save, follow the instructions shown at the bottom: exit with “Ctrl+X”, then confirm with the letter “O” and Enter, then press Enter again to validate the filename.
You’ll be back in the terminal. You can restart the Raspberry by clicking the raspberry icon > “Logout” > “Reboot.”
You’ll see that the script starts by itself when the Raspberry boots.
6. Even more (still optional)
VLC can also play videos, make playlists, and many other commands and instances are available (besides “play”, “pause”, and “stop”).
One of them is “player.is_playing()”, which tells you whether a sound is currently playing.
So you can perform an action IF audio is playing or not (“if”). For example, you could start (or restart) the same file if no file is currently playing (“== 0”) by modifying the “while True:” loop like this:
while True:
if player.is_playing() == 0 :
lecture()
With that same check, you could turn on an LED connected to the Raspberry’s GPIO while sound is playing (“== 1”), like this:
if player.is_playing() == 1 :
# Code to turn on an LED
Here’s a (nearly) complete code example combining those two ideas that you can copy and paste:
import time, vlc
player = vlc.MediaPlayer()
player.audio_set_volume(100)
def lecture():
player = vlc.MediaPlayer("/home/LOGIN/Téléchargements/test.wav") # Replace "LOGIN" (with your Raspberry username) and "Téléchargements" (if the folder containing the sound file is different)!
player.play()
while True:
if player.is_playing() == 0 :
lecture()
else :
# Code to turn on an LED ?
time.sleep(0.5)
You’ll notice I added a half-second “time.sleep()”. It’s not mandatory, but it’s always good to prevent the microcontroller from spinning for no reason...
7. And much more (even more optional)
Now that you’re up and running with this, know that you can do pretty much anything with it. You could:
- Use the Raspberry’s GPIO to trigger different files when a visitor presses buttons in an exhibit.
- Play sounds when someone picks up a telephone handset.
- Connect a motion sensor to play a welcome message when someone enters your store.
- Etc.
- Etc.
- Etc.
Conclusion
I can’t say it enough: you can build some really crazy things. The Raspberry is inexpensive and capable of a lot. At first, you might not see every opportunity, but they’ll appear over time. Personally, depending on the project, I use a simple DF Player, sometimes paired with an Arduino, and sometimes a Raspberry. It’s a treat!
Either way, if you have any questions, don’t hesitate to contact me!