Official website for Linux User & Developer
FOLLOW US ON:
May
28

Sonic Pi tutorial – learn to code with Sonic Pi

by Rob Zwetsloot

Take the next step in programming and create your own melodies with Sonic Pi, the musical programming language

With Scratch we’ve learned how to operate under the logic of programming. The next step is to then use that within a programming language – the problem is that many of the available languages can look a little intimidating. This is where Sonic Pi comes in, offering a very simple language style that can ease you in to the basics of working with code.

It’s quite straightforward to use as – Sonic Pi allows you to choose from a small selection of instruments and select a tone to play with it. These can be turned into complex melodies using loops and threads and even some form of user input.

Sonic Pi setup
Sonic Pi setup

Step 01 Install Raspbian

If you’ve installed the latest version of Raspbian, Sonic
Pi will be included by default. If you’re still using a slightly older version, then you’ll need to install it via the repos. Do this with:

$ sudo apt-get install sonic-pi

Step 02 Get started with Sonic Pi

Sonic Pi is located in the Education category in the menus. Open it up and you’ll be presented with something that looks like an IDE. The pane on the left allows you to enter code, and then you can save and preview it as well. Any errors are displayed separately from the output.

Step 03 Your first note

Our first thing to try out with Sonic Pi is simply being able to play a note. Sonic Pi has a few defaults already pre-set, so we can get started with:

play 50

Press run and the output window should show you exactly what is happening.

Step 04 Set the beat

For any piece of music, you’ll probably want to set the beat. We can start by putting:

with_tempo 200

At the start of our code. We can then test this out by creating a string of midi notes using play_pattern.

Step 05 Advance your melody

We can start making some complex melodies by using more of Sonic Pi’s functions. You can change the note type by using with_synth, reverse a pattern, and even create a finite loop with the x.times function. ‘Do’ and ‘end’ signify the start and end of the loop.

Step 06 Play a concert

Using the in_thread function, we can create another thread for the Sonic Pi instance and have several lines of musical code play at once instead of in sequence. Here we’ve made it create a series of notes in a random sequence.

Full code listing

with_tempo 200

play_pattern [40,25,45,25,25,50,50]

2.times do
   with_synth “beep”
   play_pattern [40,25,45,25,25,50,50]
   play_pattern [40,25,45,25,25,50,50].reverse
end

play_pad “saws”, 3

in_thread do
   with_synth “fm”
   6.times do
      if rand < 0.5
         play 30
      else
         play 50
      end
      sleep 2
   end
end

2.times do
   play_synth “pretty_bell”
   play_pattern [40,25,45,25,25,50,50]
   play_pattern [40,25,45,25,25,50,50].reverse
end

  • Tell a Friend
  • Follow our Twitter to find out about all the latest Linux news, reviews, previews, interviews, features and a whole more.
    • Eddie Walsh

      Hi Just started on Sonic Pi and all I get is a lot of errors after running Play 50, so then put with_tempo 200 ,run, still list of errors, so what am I doing wrong, Regards DM

    • Eddie – upgrade to v2 and follow the built in tutorial. Syntax has slightly changed since v1 but the app is way more powerful and fun now. If your’e still having issues, let us know on the mailing list: https://groups.google.com/forum/#!forum/sonic-pi

    • Richard Dickinson

      Error: undefined method `play_pad’ for #