Raspberry Pi For Dummies, 4th Edition
Book image
Explore Book Buy On Amazon
Your Raspberry Pi can perform all the tricks when it comes to music. One of the best features of Sonic Pi is the live loop, which enables you to change your music while it repeats.

The following program modifies the previous example by turning it into a live loop and adding tempo (BPM is short for beats per minute), synth, and option choices. Changing the synth is like changing the instrument the notes are played on. Options (or opts) can be added after the instruction to play a note and affect how the note is played on that instrument. The attack option changes how long a note takes to reach its full volume, for example.

The new lines are in bold in the following code:

note_pitches = [:c4, :d4, :e4, :f4, :g4, :a4, :c5]

live_loop :endless_notes do

use_bpm 120

use_synth :dull_bell

play note_pitches.choose(), attack: 0.01

sleep 0.5


Take care with where you put spaces around colons. In Sonic Pi, colons are used immediately before the name of something (such as a live loop, sample, synth, or chord). The program won’t work if you put a space between the colon and the synth name. Colons are also used to separate a parameter (or something you can change) from its value. In this example, attack is a parameter. The colon must go immediately after the parameter name.

Note that you need to give a live loop a name. This program uses :endless_notes, but you could choose something else if you prefer.

When you run the program, you'll hear the new synth sound (:dull_bell), and notice the new tempo.

The clever bit is that you can make changes to the program and have them incorporated seamlessly into the music. For example, try changing the synth to :chipbass and clicking the Run button. The tune changes instrument, but everything remains in time. Try changing the BPM to 60 and clicking the Run button to hear the music slow down. Change the attack value to 1, and you'll hear the sharp percussive sound of the instrument change to a slow drone, almost like a church organ. You can add more options to the same play instruction, as long as you separate them with a comma.

To tell Sonic Pi that you've finished making changes and to hear them in your music, you must click Run again.

The Help pane includes a list of synths you can choose from. Show the Help pane and then click Synths at the bottom. For each synth, there is information about the available options, and useful number values to try.

For more fluid experimentation, the autocomplete feature can be used while you're writing your code. When you want to change synth, for example, delete the name of the current synth, including the space before it, and then tap space after use_synth to see the list of available synths in the autocomplete menu. Click one in the menu, or highlight it using the cursor keys and select it by pressing Tab or Enter. This is a quick way to enter your code, but also a great way to explore the synths and options available.

choosing a synth with autocomplete on raspberry pi Choosing a synth using the autocomplete feature.

If you press Run multiple times in a program that doesn't use a live loop, you will hear multiple instances of the music at the same time, with no synchronization between them.

About This Article

This article is from the book:

About the book authors:

Sean McManus is an expert technology and business author. His previous books include Mission Python, Coder Academy, and Cool Scratch Projects in Easy Steps. Mike Cook is a lifelong electronics buff, a former lecturer in physics at Manchester Metropolitan University, and the author of more than 300 articles on computing and electronics. You'll often find him monitoring technology forums under the moniker Grumpy Mike.

This article can be found in the category: