43

Sonic Pi for Creative Coders

KATE LOMAX / COFOUNDER, ARTEFACTO

Type of Library Best Suited for: Any

Cost Estimate: $0

Makerspace Necessary? No

PROJECT DESCRIPTION

Many learn-to-code tools forget to bring the creativity and so they can be a little dry for beginners. Creative computing tools offer a way for people to learn new coding and computer science skills while also creating new and interesting things.

Sonic Pi is a great new resource for learning to code and for composing music. Think of it as a new kind of musical instrument. You can write code to create your own music, including using samples and different instruments.

OVERVIEW

Sonic Pi is a live coding environment that was developed by Sam Aaron at the University of Cambridge Computer Laboratory. It was created specifically to support both music and computing lessons in schools. Sonic Pi was created to work with the Raspberry Pi computer and comes preinstalled on the Raspbian operating system, but you can install and use Sonic Pi on PC and Mac computers as well.

With Sonic Pi, you can compose music by writing text-based code. It’s based on the Ruby programming language and helps users learn computer science concepts such as debugging, loops, conditionals, data structures, and more. Plus, you can code music along the way. This is a great, engaging tool for learn-to-code programs as well as for music lessons. It’s suitable for a wide range of ages.

In this workshop we introduce learners to the Sonic Pi software, learn about the main features, and start composing music by writing code. You can start with simple melodies, but you can also use Sonic Pi with more advanced learners to work on soundtracking or Foley projects. This workshop can be adapted to suit the age (and music preferences) of the learners.

MATERIALS LIST

It’s a good idea to have some simple song notations printed out for workshop attendees to use when they’re first getting started. You can find music scores from sites like Jellynote at https://www.jellynote.com.

NECESSARY EQUIPMENT

STEP-BY-STEP INSTRUCTIONS

Start with simple melodies to pick up the syntax and then gradually introduce different instruments, beats, and code structures.

1. Introducing the Sonic Pi Interface

Give an overview of the user interface. Sonic Pi comes with a comprehensive tutorial that you can let users work through at their own pace, or you can demonstrate the main areas of the interface (as below) and then jump right into writing notes and basic melodies.

2. Introducing the Syntax

Once users are familiar with the different parts of the application, you can start introducing the syntax of the Sonic Pi coding environment. The main concepts to introduce at this early stage are the different ways you can write notes, how to set rests between notes using the sleep command, and how to combine notes to form chords.

FIGURE 43.1

The Sonic Pi interface

Demonstrate the two different ways to write notes in Sonic Pi—using the name of the note or its MIDI note number. A simple Google search for “MIDI note numbers” will return many charts as results.

It can help to describe the way the numbers align with a MIDI or piano keyboard—the lower the number, the lower the note that is played. Higher notes on the keyboard will play higher-pitched sounds.

But you can also use the name of the note preceded by a semicolon and indicate whether notes are sharp or flat using s or b after the note respectively.

Chords are when you play more than one note at a time without a rest between them.

3. Playing Simple Melodies

Once everyone is comfortable with the basic syntax, you can start creating simple melodies. You can pick a well-known nursery rhyme or theme song or let the users suggest a starter song.

Demonstrate how to code a melody by combining the keys or notes using the “sleep” command with a number parameter to designate rests between the notes (in seconds).

You can pick a simple melody from songs that the students might recognize. If you can’t think of any, you can’t really go wrong with “Frère Jacques” or “The Imperial March” (Darth Vader’s theme).

4. Debugging and Refactoring

A key principle of coding and developing software is Don’t Repeat Yourself, or DRY. So next, we want the learners to DRY their code up by rewriting it in a cleaner way that removes unnecessary repetition.

You can run a task a specific number of times using the times method (e.g., 2.times do . . . end will run the code in-between twice). Give everyone time to rewrite any code that repeats using a loop and the times method. Also, show students how to add comments to their code and why commenting your code can be useful.

You can also store values that are going to be used more than once (like sleep statements, for example) using variables.

Once everyone has had a chance to refactor their code, ask them to rerun it and check that everything is okay.

If anyone gets an error when they run their code (such as missing a semicolon from before a note or forgetting to include an end after a do statement), show them the error log—it will spit out a whole bunch of stuff, but somewhere among all of that will be some useful information about the error, including the line number.

5. Changing the Sounds

Now that everyone has a handle on writing clean code to create melodies, they can start experimenting with different instruments and samples.

Sonic Pi also comes with a plethora of synthesizers you can use—everything from a piano synth to “blade” (Blade Runner-style strings). Thankfully, it also has autocomplete, so you can try out some random synths to find out one that works best.

Demonstrate how to change from the default synth by adding a use_synth :name of synth statement at the top of the code. You can also show how to specify a sample by typing sample :name of sample.

Give students a chance to try a few of the included synths and samples using either the autocomplete or by checking the list of synths and samples available in the help panel. There are also some additional parameters that can be introduced at this point, including how to alter the duration and amplitude of samples.

6. Composition Playtime

Once everyone has had a chance to play with different synths and samples, it’s a good time to introduce code blocks that let you combine beats, sounds, and melodies into a more complex tune. Next, demonstrate how to combine different sounds, beats, and melodies at the same time using loops and threads.

You can then give everyone a chance to play and to compose something of their own by trying out different aspects of Sonic Pi coding. One way to do this is to create a theme or a challenge to prompt users to create their own music. For example, you could ask everyone to write a summery tune, or to write a theme song for an imaginary TV spy show. These can be individual or group projects.

LEARNING OUTCOMES

Through this project, participants learn . . .

RECOMMENDED NEXT PROJECTS

There are different ways you can enable and support learners in exploring the more advanced coding potential of Sonic Pi, including:

A follow-up program might be to teach participants to use Sonic Pi on Raspberry Pi computers.

Notes

1. NASA sounds on Soundcloud, https://​soundcloud.com/​nasa.

2. British Library Sounds, http://sounds.bl.uk/.