Code Your First Song with Sonic Pi

Sonic Pi is a live coding music synthesiser that allows you to write music with just a few lines of code.

I’ve always been a lover of music. In fact, when I was 10 I used to try composing songs with Music Maker on my family PC and writing nonsense lyrics to instrumental songs (none of which will ever see the light of day, I promise you). Now as an adult I work as a software engineer, I play guitar almost daily and I listen to music of all kinds so naturally, when I first came across Sonic Pi, I was excited to try it out. I mean, it combines code with music: what’s not to love?

This step-by-step guide will help you get started and create your first composition with Sonic Pi.

1. Download and Install Sonic Pi

The first step is to download and install Sonic Pi on your computer. You can download the latest version of Sonic Pi from the official website here. The software is available for Windows, MacOS, and Linux.

2. Open Sonic Pi and Create a New Buffer

Once Sonic Pi is installed, open it up and create a new buffer by clicking on the “+” button in the top left corner. This will open a new window where you can write your code.

3. Basic Music Syntax

Sonic Pi uses a simple syntax for music composition. The basic building blocks are notes and chords, which are played by the synth sounds. For example, the following code will play a simple melody:

play 60 # Play a note at pitch 60 (C)
sleep 0.5 # Sleep for half a beat
play 62 # Play a note at pitch 62 (D)
sleep 0.5 # Sleep for half a beat
play 64 # Play a note at pitch 64 (E)
sleep 0.5 # Sleep for half a beat

You can probably tell from the code block above that it’s quite intuitive to understand once you know the basic key words. The hashtag (#) denotes the start of a comment, and can be used to write notes for yourself.

play is to play a sound, and the number following that refers to a pitch. For example, 60 is a middle C (if you know the piano), and adding 1 will add a half a tone (so 61 would be C#, 61 would be D, etc… And going lower would take us lower down the scale accordingly).

The key word sleep adds a soundless space, and the number following will represent how many beats (1 being one beat).

4. Adding Chords

To add chords, you can use the play_chord function, that allows us to play a specified chord. The function takes two arguments: the first argument is the chord root, and the second argument is the chord type. For example, the following code will play a C major chord:

play_chord [60, 64, 67], sustain: 0.5 # Play a C major chord

5. Adding Drum Beats

Sonic Pi also includes a variety of drum samples that you can use to add rhythm to your compositions. To play a drum sample, use the “sample” function. For example, the following code will play a kick drum:

sample :drum_bass_hard # Play a kick drum

6. Looping

To repeat a section of code, you can use the live_loop function. This function takes a block of code and repeats it in a loop. For example, the following code will repeat the drum beat:

live_loop :drum_beat do
    sample :drum_bass_hard
    sleep 0.5
    sample :drum_snare_hard
    sleep 0.5
end

7. Adding Variation

To add variation to your loops, you can use the use_synth and use_sample functions. For example, the following code will switch between two different synths in the loop:

live_loop :synth_variation do
    use_synth :saw
    play 60
    sleep 0.5
    use_synth :square
    play 62
    sleep 0.5
end

8. Putting It All Together

Now that you have an understanding of the basic building blocks of Sonic Pi, you can start putting everything together!

Combine your chords, drum beats, and loops to create a complete composition, or just mess around, and see how it sounds. For more information, you can check out the tutorial here on the Sonic Pi website.

As you can see, Sonic Pi is a powerful and flexible tool for creating music. With a little practice, you can create complex and engaging compositions at your finger tips.

Happy composing! Have you used Sonic Pi before?

Feature image photo by Veronika Tarakanova on Unsplash, photos in blog by Mad Fish Digital and Sincerely Media
Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s