Coding Compendium
A free 100-page ebook collecting my projects and tutorials for Raspberry Pi, micro:bit, Scratch and Python. Simply join my newsletter to download it.
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:
The main parts of the Sonic Pi screen are:
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
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 www.sean.co.uk for free chapters from Sean's coding books (including Mission Python, Scratch Programming in Easy Steps and Coder Academy) and more!
A free 100-page ebook collecting my projects and tutorials for Raspberry Pi, micro:bit, Scratch and Python. Simply join my newsletter to download it.
Web Design in Easy Steps, now in its 7th Edition, shows you how to make effective websites that work on any device.
Power up your Microsoft Excel skills with this powerful pocket-sized book of tips that will save you time and help you learn more from your spreadsheets.
This book, now fully updated for Scratch 3, will take you from the basics of the Scratch language into the depths of its more advanced features. A great way to start programming.
Code a space adventure game in this Python programming book published by No Starch Press.
Discover how to make 3D games, create mazes, build a drum machine, make a game with cartoon animals and more!