It's Sean!

UK freelance journalist, author
and writer Sean McManus

Printed from www.sean.co.uk. © Sean McManus.

NEW: Sign up for my newsletter!

Home > Books > Raspberry Pi For Dummies > Sonic Pi

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:

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
  end
end

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

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
      end
    end
  end
end

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

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
      end
      if drums == 1
        sample :loop_amen
      end
    end
  end
end

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).

Books by Sean McManus

Scratch Programming in Easy Steps

Scratch Programming in Easy Steps

Raspberry Pi For Dummies

Raspberry Pi For Dummies

Learn to program with the Scratch programming language, widely used in schools and colleges.

Set up your Pi, master Linux, learn Scratch and Python, and create your own electronics projects.

Coder Academy

Coder Academy

Cool Scratch Projects in Easy Steps

Cool Scratch Projects in Easy Steps

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

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

More books

Recommended articles

Stylised coloured photo of a synth

Discover my music and home recording articles

Interested in starting home recording? Follow me as I start recording, using instruments such as the Novation MiniNova and Korg Kaossilator, and share my synth and sample-based music online.

Scratch cat and rainbow circle

10-block demos for Scratch programming

Lots of people of all ages are learning to program with Scratch. Discover some useful tricks and techniques in my 10-block Scratch demos, including special effects you can drop into your games.

Paper Raspberry Pi case

Download free book chapters and more!

Download a free case for your Raspberry Pi, and free chapters from my books about the Raspberry Pi, Scratch, web design and more!

©Sean McManus. www.sean.co.uk.