How to Create Christmas Music With a Raspberry Pi

Although gift giving can be good for the soul, it’s not the only magical feeling you can get this holiday season. Creating music programmatically is a great way to challenge yourself creatively and prove that you can do something that you may not felt you were capable of at the beginning of the year.

Let’s dive in to this Christmas coding project that will not only earn you a fun way to create music, but will also make Santa’s elves jealous, too.

Getting Started

Before you can dive in to your code, be sure to confirm you have the these items ready:

  • Raspberry Pi OS installed on your Raspberry Pi (can be any model). If you’re not sure how to do so, we make the installation process easy for you.
  • Sonic Pi, a tool to help you create music using code. You can download Sonic Pi, for the Raspberry Pi, at the official Sonic website. There is a 32-bit and 64-bit ARM DEB file for you to download. Which file you pick will depend on if you have 32-bit or 64-bit Raspberry Pi OS installed.

The Christmas classic, the 12 Days of Christmas, is a great way to understand how simple the Sonic Pi music application really can be. With straightforward code elements involved, almost anyone can create their own music without touching an instrument.

First, grab the completed code from the Raspberry Pi GitHub repository. You can either download the days-of-christmas-finished.txt or copy the text out of the GitHub code window instead.

Next, let’s install Sonic Pi. Open your downloads folder or wherever you saved the DEB file. Right-click on the file and choose Software Installer. From there, you should be prompted to install Sonic Pi.

How to Code a Carol

When you Open the Sonic Pi application, you’ll see a blank screen with a #Welcome to Sonic Pi comment. At this point, you can either open the 12 Days of Christmas text file or paste the code bits from the Raspberry Pi GitHub page directly.

Some basic music knowledge will come in handy. Yet, if you’re not too familiar with music theory that’s okay too.

If you think of the 12 Days of Christmas song, you’ll hear a lot of song pieces that can be repeated. For example, “On the first day of Christmas, my true love sent to me.” This introduction repeats using the same notes. We’re not worried about song lyrics here. We’re only concerned with the melody.

There are a few other song sections that repeat. In the verse, you’ll notice that the tune is the same for “Two turtledoves” and “Three French hens.” Same idea goes for the section “(And) a partridge in a pear tree.”

You’ll need to define the song sections including what notes to play in your melody, and how fast to play each note.

 notes = [:c5, :d5, :bb4, :a4, :f4, :g4, :f4]
durations = [1, 0.5, 0.5, 0.5, 0.5, 1, 1]
play_pattern_timed notes, durations

In music, there are 8 octaves. Sonic looks for a note letter and an octave number. This will tell Sonic the note and tone to play. With respect to duration, this will define how long the tone will play (or ring) for. The functions on the third row of the code tells Sonic what to do with the notes and durations.

You’ll add a bpm function which is known as beats per minute. How many times are you snapping your finger in a minute? That’s the same thing as what bpm is. For this melody, that’s why you’ll notice that bpm is set to 120.

creating music using sonic pi

From there, all the definitions can be put into each respective section. To move through this Christmas tune, you’ll want to loop through the 12 days.

Tell Sonic to start at the first day of Christmas using days = 1. In order to carry out this function, simply use the 12.times do loop.

Some song sections will be a little different, so you’ll want to account for that with an if statement.

creating a music loop with sonic pi

That’s the nuts and bolts of creating music programmatically. If you would like a detailed tutorial, the Raspberry Pi Foundation has put together a wonderful reference to help you code this carol too.

With a good foundation under your belt, why not build some musical instruments using your Raspberry Pi? May we suggest naming your band “The Four Cores?”

Enjoy Listening to Your Masterpiece

While you’re exploring your creative limitations, there are so many examples of creators making music with Sonic Pi. Give the internet a good search to find more musical inspiration and enjoy playing with the notes, timings, and song sections. Maybe you’ll soon be hired as the next Raspberry Pi DJ?

All the best, Maestro! May your keyboard and creative mind become one as you set out to program your next one hit wonder!

Source link

Leave a Reply

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