How to make music on the Raspberry Pi with Sonic Pi

Sean McManus, co-author of Raspberry Pi For Dummies, shares the 'extended remix' of the example Sonic Pi song in the book

Sonic Pi is a programming language, based on Ruby, that you can use to write music for the Raspberry Pi. It includes a library of samples and special effects, and a number of different synthesiser voices. You can combine them to write music, using "threads" to play different parts of the music at the same time, and synchronisation commands to coordinate between them.

In the second edition of Raspberry Pi For Dummies, I explain how to write programs using Sonic Pi, including a program that plays a jingle based on your name, and a program that synchronises a drum beat with synths and guitar samples.

The final example in that chapter is a piece of music called 'Showroom Dummies', and I also created an extended version of it, which you can hear below:

The Sonic Pi screen looks like this:

screengrab of Sonic Pi

The Sonic Pi screen layout

The main parts of the Sonic Pi screen are:

  • The workspaces: The largest box here, in the top left, is where you type in your programs. Use the tabs under the workspaces to switch between them.
  • The Output pane: This tells you what Sonic Pi is playing and when, which is useful for debugging.
  • The Documentation pane: At the bottom of the screen, the Documentation pane gives you help on the synths, examples, special effects and samples included. Use the tabs on the left to switch between those parts of the documentation.

Code for the extended Sonic Pi music

The code below reproduces the music you heard above. However, it gets a bit glitchy after about 90 seconds because the Raspberry Pi gets overwhelmed (the green box in the bottom right fills to the brim!). Hopefully, as the Raspberry Pi's software is optimised, this will become less of an issue. I wrote another version of this program (using less elegant code) which synchronises everything against the :loop_start cue, and uses loops that wait for the loop to start a certain number of times before playing. That code ran with fewer difficulties, so I wonder whether Sonic Pi struggles with having too many different synchronisation commands?

To use this code, copy and paste it into one of the workspaces (see above), and then click the Run button at the top of the screen.

# Showroom Dummies (extended version)
# Music example from Raspberry Pi For Dummies, 2nd Edition

in_thread do
  drum_rate = sample_duration :loop_industrial
  drum_rate = drum_rate * 2
  loop_number = 0
  loop do
    loop_number = loop_number + 1
    puts "Loop number " + loop_number.to_s
    cue :guit_e_begin if loop_number > 4 and loop_number % 2 == 1
    cue :melody_begin if loop_number > 8 and (loop_number - 9) % 16 == 0
    cue :slide_begin if loop_number > 18 and (loop_number - 19) % 4 == 0
    cue :bass_begin if loop_number == 25
    cue :loop_start
    sample :loop_industrial, rate: 0.5
    sleep drum_rate

in_thread do
  loop do
    sync :guit_e_begin
    with_fx :distortion do
      sample :guit_e_fifths

in_thread do
  loop do
    sync :melody_begin
    note_pitches = [67, 62, 64, 64, 67, 62, 64, 67, 62]
    note_pitches.each do |note|
      use_synth :saw
      with_fx :wobble do
        sync :loop_start
        play note
        play note - 12

in_thread do
  loop do
    sync :slide_begin
    with_fx :echo do
      sample :guit_e_slide

in_thread do
  sync :bass_begin
  drums = 1
  loop do
    bass_pitches = [55, 55, 47, 47, 50, 50, 52, 50]
    drums = -1 * drums
    bass_pitches.each do |bassnote|
      sync :loop_start
      use_synth :tb303
      with_fx :slicer do
        play bassnote
      if drums == 1
        sample :loop_amen

Learn how to write your own music in Sonic Pi

To discover how you can compose your own music in Sonic Pi, see the second edition of Raspberry Pi For Dummies. I've created a reference chart showing the Sonic Pi note names and numbers. You can also hear some of my synth and sample-based music here (which was mostly not written with the Raspberry Pi).


© Sean McManus. All rights reserved.

Visit for free chapters from Sean's coding books (including Mission Python, Scratch Programming in Easy Steps and Coder Academy) and more!

Discover my latest books

Mission Python book

Mission Python

Code a space adventure game in this Python programming book published by No Starch Press.

Coder Academy book

Coder Academy

Learn to make games and other programs in Scratch, and make a web page in HTML. Highly interactive book for 7-10 year olds.

Cool Scratch Projects in Easy Steps book

Cool Scratch Projects in Easy Steps

Discover how to make 3D games, create mazes, build a drum machine, make a game with cartoon animals and more!

Walking astronaut from Mission Python book Top | Search | Help | Privacy | Access Keys | Contact me
Home | Blog | Copywriting Services | Books | Free book chapters | Articles | Photos | Games | Shop | About