How to Code a Song Using Sheet Music in Sonic Pi

by AnnaGraceWelch in Circuits > Art

6462 Views, 5 Favorites, 0 Comments

How to Code a Song Using Sheet Music in Sonic Pi

Screen Shot 2019-05-06 at 9.12.17 PM.png

This instructable is going to outline some basic steps and pieces of code to use when coding a song in Sonic Pi using sheet music! There is a million other pieces of code to try to add flavor to your finished piece so make sure to also play around yourself and see what you can come up with!

The sheet music I used was a simple piano arrangement of "I Want to Break Free" by Queen. If you want to download this same sheet music, you can do so here: https://www.musicnotes.com/sheetmusic/mtd.asp?ppn=...

Materials

Screen Shot 2019-05-06 at 7.43.48 PM.png
Screen Shot 2019-05-06 at 8.51.19 PM.png

For this project you will need:

1. A computer compatible with Sonic Pi

2. Sonic Pi software

3. Sheet music

4. Basic knowledge of how to read music and of scientific pitch notation

Find Out the BPM of Your Song and Code That

Screen Shot 2019-05-06 at 8.48.31 PM.png

In my case, the BPM is printed on the sheet music. However, that is often not the case. For help finding the BPM of your song, you can use this website: https://tunebat.com/

The BPM of my song was 109 beats per minute. Once you know the BPM of your song, go to the first blank line in your buffer and type "use_bpm 109" using your BPM. There should be a space between the words and the numbers and the value you put it for BPM should turn blue, indicating it is a number.

Going ahead and coding the BPM in my experience has helped tremendously with figuring out timing while coding.

Follow the Sheet Music and Code Notes and Chords

Screen Shot 2019-05-06 at 8.53.26 PM.png
Screen Shot 2019-05-06 at 8.53.50 PM.png
Screen Shot 2019-05-06 at 8.53.13 PM.png
Screen Shot 2019-05-06 at 8.56.27 PM.png

Now, it is time to simply translate the notes on the staffs on your sheet music to lines of code. You can use the simple "play" and "sleep" functions in order to do this, but I have found it actually makes it a little easier to use more complicated lines of code. Be sure to pay attention to the key signature in order to make sure you are putting sharps and flats where they need to be if there are any.

In my case, my first note was an a4 on the staff in scientific pitch notation. To code this, I typed "play:a4" in a free line in the buffer, making sure to include the colon so the software knows it is a note. The colon and note should turn pink if you have inputted it correctly. Since this note was an eighth note and the time signature is 4/4 time, I coded "sleep 0.5" after it.

After this I had a lot of notes in a series that would be much easier coded in one line of code than several. To do this, I used "play_pattern_timed [:d5, :e5, :e5], [1,1,1]" The first set of brackets should contain your notes on the staff in order, separated by commas and the second should contain the duration of each note in order (1 for quarter note, 2 for half note, 0.5 for eighth note, etc.)

If you want a chord to play at the same time like I did, type "play_chord [:d, :fs, :a]" without coding a sleep in between. Within the brackets, you should code the notes that make up the chord you are trying to play. If that is all the notes you want to play at one time, code sleep after this with the number after sleep being the duration of your shortest note in the series you just coded.

If you have a sequence of repetition in your code, you can loop one set of code using "4.times do" at the beginning of the code you wish to loop and "end" at the end of the section you wish to loop. The number before ".times do" signifies how many times you want the section of code to repeat. If you have inputted it correctly, both "do" and "end" will turn orange.

If you have a single note you wish to play for a certain duration other than 1, you can code it like this: "play:e5, sustain: 0.5, release: 0.1" with the number after sustain being the duration of the note. If you have typed it correctly, sustain and release will turn pink and the numbers will turn blue.

Using these basic pieces of code, you should be able to code your song by translating the sheet music to code. If you play it and something doesn't sound right, use trial and error until you have the sound you desire! Sometimes, you have to change to a new piece of code or add a "sleep" somewhere.

Choose Your Synth

Screen Shot 2019-05-06 at 8.53.00 PM.png

Now that you have coded your whole song using sheet music and you like how it sounds, it is time to choose your synth. You can either keep it as the default or explore the many options available within the software.

To find the options, click "Help" at the top right of the window, opening the bottom tutorial screen. Click on synths at the bottom and experiment. Go to the top of your buffer and write under the line where we previously coded the BPM, type: "use_synth:dtri" with the word(s) after the colon being the synth chosen for your song. Hit play and see if you like the sound. If not, keep exploring until you find one you do!

Done!

Screen Shot 2019-05-06 at 9.09.18 PM.png

Your coded song from sheet music in Sonic Pi should now be complete. Hit the "Run" button, sit back, relax, and listen to your masterpiece!