Top Logo Sp Logo
How to Create Your Own Funky Music Box with Raspberry Pi

How to Create Your Own Funky Music Box with Raspberry Pi

How to Create Your Own Funky Music Box with Raspberry Pi

 

Get ready to make your very own music box! In this project, we’ll learn how to hook up some buttons to the GPIO pins of your Raspberry Pi and then use them with a Python application in order to create your own music player.

Here at Vilros we all love jamming out to music. So, we’re excited about this challenge to create your own project which ultimately can play all sorts of different sounds. Get your disco ball ready! 

1. Parts You Will Need

  • Software
  • Python 3 and the gpiozero Python Module (both of which come pre-installed on Raspbian)
  • libav-tools (this module can be installed – just type “sudo apt install libav-tools” into the terminal
  • Hardware
  • Raspberry Pi
  • 4 different colored tactile switches (to make buttons for your new player)
  • 4 male X male jumper leads
  • 5 male X female jumper leads
  • breadboard

2. Get Set Up

For this project, you’re going to want to use a bunch of sample sounds.  Even though there are many sound files on Raspbian, they can be tricky to play when you’re using Python.

So, the first thing you want to do is convert the sound files to a file format that’s more compatible with Python. Just follow these steps:

  • Cerate a directory called “gpio-music-box” in your home directory to store all your new files for this project.
  • Create a new directory named “samples” in your gpio-music-box directory.
  • Copy all the sample sounds available in “/opt/sonic-pi/etc/samples” into your new samples directory. 

If you’ve done it correctly, you should now see multiple “.flac” files in your sample directory.

3. Convert your .flac files to .wav

Python works better with .wav files, so your next step is to convert all your .flac files to .wav files. Ideally, you should be able to do this in one big batch step. 

In order, to batch process all your files, you’ll want to use the “libav-tools” software.

For each file with a .flac ending, you can convert it using “avconv”. Just keep the original name of the file, but change .flac to .wav. 

4. Write your Python code

The next step of this project is to write the code for Python. Though you can technically use any text editor, we recommend using Python’s Integrated Development Environment (IDLE) – just make sure you use the version for Python 3!

For this music box project, you’re going to want to start by using 4 different sounds. To do this, first you will need to import and initialize the module called “pygame”. Next, you’ll create a “sound” object and connect it with the path to your new .wav files. 

(Note: You can save your Python file to your gpio-music-box directory by clicking “save” under “file”). 

5. Hook up your buttons

Your next step will be to connect your 4 buttons to your Raspberry Pi.

First, you’ll want to place each of the 4 buttons into the breadboard. Then, using the jumper leads, connect each button to a different GPIO pin on your Raspberry Pi.

(Note: so that you don’t connect too many jumper leads to GND pins, try wiring the negative rail on your breadboard to a single GND pin, which should then make it so that all of your buttons will use the same ground rail.)

6. Play your sounds at the touch of a button

Now, you want to use each button to call a function that takes no arguments. You want to create a trigger that calls the function, so that each time you press a button, you get the desired sound effect.

Because you set up functions in the pygame module, you should now be able to use them with the “when_pressed” method. 

7. Next steps

Congratulations, you should now have a working music box that plays sounds you can rock out to with your friends!

If you want to take this project to the next level, consider working on making your code a bit cleaner. And/or, you might want to add more buttons for more sound options, or LED lights to create a fun disco effect!

To find out more about this project, including code and help tools, just check out this step-by-step guide.

Our team here at Vilros hopes you love dancing along with your new music box.  If you liked this project, be sure to check out more project ideas on our Vilros Projects blog!

Leave a comment

* Required fields

Please note: comments must be approved before they are published.