Wednesday, 9 November 2016

Computer Programmed Music for Kids: Scratch, Pencil Code, Trinket, Sonic Pi

My nearly 10 year old son got interested in finding ways to program music so we've tried out Scratch, Pencil Code Gym Jam, Trinket and Sonic Pi.

He came home from school wanting to learn to play In the Hall of the Mountain King by Edvard Greig which he had heard at school. He has liked the piece for a long time but hadn't been at the point where he could play it.

He plays the violin and reads music so we found some sheet music and he took it to his music lesson for some help in getting started. He's working on playing the piece on the violin but we thought it might be interesting to try and program it too.

We tried Scratch, Sonic Pi, Pencil Code Gym Jam and Trinket, all tools that can be used for free, and except for Sonic Pi they can all be used in a web browser without downloading or installing anything.

You might also like:

Programming Music with Scratch

Scratch uses midi notes which can be selected from an on screen piano keyboard. The first challenge was to turn the musical notation into letters / midi numbers. This was a great exercise in logical thinking and applying a cipher!
Then he started coding in Scratch. Scratch has really simple clear commands for musical programming. First he just programmed the notes with the correct lengths. He used custom blocks for repeated patterns within the notes. He found some errors in his transcription by listening to the music and was able to fix them with a bit of help from his dad.

Scratch doesn't have a violin instrument but it does have a cello so my son was happy to use that.

But the music shows that the notes should be played Staccato which means that the notes are played quickly with a small gap in between. There is a Pizzicato instrument which was quite good, but my son wanted to fine tune the effect. It was easy to achieve a Staccato effect in Scratch by adding a custom staccato block which takes a note and the beats it should be played for and plays the note followed by a small rest. This sounded much better.

Using Scratch to play music turned out to be a great introduction to custom blocks and to input parameters - there was a real reason to use them to create the staccato effect. He's still working on the project but here's a snapshot of Hall of the Mountain King in Scratch.

Scratch projects can easily be shared online and can also be embedded in web pages.

There's also the Scratch Tone Synth Extension for ScratchX which allows you to play around with electronically generated synthesizer sounds and concepts in Scratch.

Programming Music with Sonic Pi

I did show my son Sonic Pi before he started coding in Scratch but it was clear that we would have to spend lots of time reading and looking things up rather than just getting on with the coding as he could in Scratch. This surprised me a bit as it's a language specifically for writing music.

We could play basic midi notes easily enough. But putting a more complex piece together was more work than in Scratch. It seems that you can either add sleep commands within notes or add a list of timings after the list of notes. The former is long-winded while the latter has an unnatural separation between notes and their timing. This might make sense for a very repetitive rhythm but it seemed awkward for the piece we were working with.

Scratch was much simpler with its 'play sound note for beats' and 'rest' commands. Patterns can easily be programmed in where there's repetition.

There didn't seem to be an easy way to get a staccato effect without lots of reading and experimenting though I'm sure it's possible. There are lots of ways to create effects.

Update: Sam Aaron (creator of Sonic Pi) reached out to us and helped. Thanks Sam! From the Sonic Pi documenation, 'release' is "is the time it takes for the synth’s sound to fade out."

Here's our attempt at staccato in Sonic Pi:

If we had been able to quickly find a violin sound to use my son might have persevered but he was put off by the mostly electronic sounds and the need to go and read documentation before being able to do anything.

Sonic Pi isn't available as an online editor and must be installed, it comes pre-installed on the Raspberry Pi.

Sonic Pi would be better for older coders who want to make the most of its more advanced features and who like electro-synth style music.

Trinket Interactive Music Notation

Trinket is an online coding tool which supports Python and HTML coding. It also has an interactive music notation editor.

Trinket is fairly basic but has the advantage of showing musical notation. You can also embed music in web pages, click the play button to try it.

We couldn't see a way to achieve a Staccato effect without adding lots of complexity by using short rests which would spoil the readability of the music.

Trinket is fab for composing and sharing simple pieces of music.

Pencil Code Gym Jam

We also looked at Pencil Code Gym Jam. Pencil Code allows you to easily switch between blocks and CoffeeScript text-based coding. You can create art, games and importantly for this project music. 

It was really quick to write the tune and the timing in Pencil Code Gym Jam and making the notes Stacatto just involved adding a dot before them (the musical notation is a dot above or below the note.)

       play '.B/2 .C/2 .D/2 .E/2 .F/2 .D/2 . F'

Wow. That's easy. And you can easily get the notes to play on an on-screen piano. Pencil Code can also be embedded in web pages and projects can easily be shared with a url.

Click to play:
You've got access to loads of blocks for creating a multimedia project or game that includes music.

You don't have a choice of instruments in Pencil Code (though it's something they would like to add). You can create your own sounds using some included waveforms and parameters (in the same style as Sonic Pi, but easier to get started with.)

The lack of a stringed instrument wasn't ideal for our project and my son already knows Scratch well so he stuck to Scratch this time but Pencil Code looks fab. 

Programming Music on the 3D Printer

While all this was going on, my partner used MIDI-to-CNC to play the Hall of the Mountain Kings on the 3D printer, as you do. This takes frequencies and turns them into movements on the 3D printer. Gives a new meaning to electronic music ...


Scratch is really quick and easy for programming with music, it has just the right set of concepts and programming constructs. It also has the added bonus of having the ability to create full multimedia projects that combine sound, animation and interactivity. You do have to translate from musical notes to midi tones, there's a built in piano to help with this, but you lose the connection. Lots of kids will already be familiar with Scratch which helps.

Pencil Code Gym Jam is also excellent and makes it really easy to keep the relationship with the musical notes. It also has the option to experiment with electronic synth sounds. Pencil Code also allows kids to easily transition to text when they're ready.

Trinket is very useful for children who also read sheet music or want to learn. The focus is on the music rather than coding. It's great for learning music theory and composing music. The Open Music Theory online textbook uses embedded trinkets.

Sonic Pi looks good for teenagers wanting to work with electronic music. It might be a good way to get a music enthusiast to learn coding concepts. We'd probably start with Pencil Code Gym Jam first though as it's an easier environment to learn the concepts of programmed, generated and interactive synth music.

Our focus was on coding an existing piece of music, of course kids can also explore composing their own music through coding which includes the possibility of 'live coding' and computer generated music. I definitely recommend trying to program music as a great way to improve children's coding ability and their understanding of music.

More from Tech Age Kids:


Post a Comment