Skip to content

Instantly share code, notes, and snippets.

@berndverst
Last active January 11, 2021 02:41
Show Gist options
  • Save berndverst/c1b47d052716327a3694358557e9c3c6 to your computer and use it in GitHub Desktop.
Save berndverst/c1b47d052716327a3694358557e9c3c6 to your computer and use it in GitHub Desktop.
Workshop: Creating Music through Live Coding

Creating Music through Live Coding

Author: Bernd Verst

In this workshop we will use the Sonic Pi live coding synthesizer to create part of an iconic song, live. We'll be creating the repeating four bar / measure phrase of the opening song to Hamilton, Alexander Hamilton.

Participation is strongly encouraged. A note on accessibility: Unfortunately this workshop relies on the participant / learner to receive immediate feedback via audio. Hearing impaired participation is possible and correctness of a piece of music can be achieved, but will be less enjoyable.

Prerequisites

You need to install Sonic Pi from here: https://sonic-pi.net/

The structure of the "Alexander Hamilton" song

This songs consists of two elements:

  • snaps on alternating beats, the off-beats
  • a piano bass line

Setting up the project

The Alexander Hamilton song's tempo is 68 beats (quarter notes) per minute. We will refer to one quarter note as one beat or one unit of time. In Sonic Pi we add the following instruction at the top:

use_bpm 68

By default Sonic Pi uses a synthesizer sound that does not suit our song. We can instead change this to something that resembles a piano with the following instruction:

use_synth :piano

But what about the snaps we need? Sonic Pi comes with several built in samples (short recordings) that can be played. We need to implement our desired sample called :perc_snap (see the Samples documentation) with the following command:

load_sample :perc_snap

Live coding with endless loops

When live coding music we want to be producing music output at all times. To do this we can create loops that repeat endlessly, reflecting the changes we make the next time the loop starts.

live_loop :some_loop_name_here do
   # instructions here
end

Timing and Rhythm

In Sonic Pi all instructions are executed simultaneously. Consecutive instructions don't wait for the former instruction to complete (unless it is a special timed instruction).

Therefore, after an instruction you should wait (sleep) the desired number of beats for your next instruction to occur at the correct time.

Example:

# Do something on beat 1
sleep 2
# Do something 2 beats later, on beat 3

Making Music (or any sound)

To play the clap sound we imported (loaded) use the command:

sample :perc_snap

To play a piano key / note / pitch use the command:

play :B2

or alternatively

play 47

Your turn: Creating the Snap

Using the following code, create the repeating snap pattern. While we are working on a total 4 beat / measure phrase, recall that the snap occurs on every second beat.

use_bpm 68
use_synth :piano

load_sample :perc_snap

live_loop :hamilton_drums do
  # replace with your instructions to create the snap pattern
end

Your turn: The bass line

Now add the piano bass line in a separate live loop below your existing one:

live_loop :hamilton_piano do
  # replace with your instructions to create the piano bass line
end

If you are familiar with the song (or want to compare to Spotify etc), try to determine the notes / pitches of the bass line yourself, otherwise read here:

B F# G D A#

To ensure we aren't using pitches too high or too low, we'll use:

B2 F#2 G2 D2 A#2

In Sonic Pi these pitches are refered to as:

:B2 :Fs2 :G2 :D2 :As2

If you are unfamiliar with music notation, you can alternatively refer to these pitches as the following key numbers (each number refers to a half step difference in pitch or a single key on a piano when considering both black and white keys)

47 42 43 38 46

Recall you can play a key with play :B2 or play 47. Use sleep to time these pitches correctly.

Your project structure should look like this:

use_bpm 68
use_synth :piano

load_sample :perc_snap

live_loop :hamilton_drums do
  # replace with your instructions to create the snap pattern
end

live_loop :hamilton_piano do
  # replace with your instructions to create the piano bass line
end

Completed example

Click here to see the completed example.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment