Programming Electronic Music with Sonic Pi and Ruby
Classroom Tools
- RoomHelper 3000 (works in class only)
- Google Classroom
Resources
- Sonic Pi Web Site
- Sam Aaron Talk and Sonic Pi Tutorial
- Interview with creator Dr. Sam Aaron
- Tutorial from “mehackit”
- Music Educator Jared O’Leary’s Sonic Pi Resources
Getting Help
You can press F1
or Ctrl+i with the keyboard cursor on
something you want to look up, such as play
.
You’ll see the documentation for the feature
in the help window at the bottom.
Playing a Timed Pattern of Notes
Here’s a simple example. Recognize it? Can you hear it in your head before playing it?
use_bpm 180
use_synth :pretty_bell
play_pattern_timed [:c, :e, :c, :e, :c, :f, :e, :ef], [1, 2, 1, 2, 1, 2, 1, 2]
use_bpm
sets the tempo, in beats per minute.use_synth
selects the synthesizer (sound generator) to use. Which ones do you like? The names appear automatically after you type inuse_synth
, and you can choose them one by one.play_pattern_timed
plays a sequence of notes, holding each for the number of beats specified in the second sequence. The firstc
plays for1
beat, the next note,e
, plays for2
beats, and so on.
Play with one or more of these programs for a while, then I’ll explain in more detail how they work, and perhaps introduce you to some more features.
Here’s another example.
# https://en.wikipedia.org/wiki/Fr%C3%A8re_Jacques
use_bpm 120
2.times do
play chord(:c4, :major)
play_pattern [:c5, :d5, :e5, :c5]
end
2.times do
play chord(:c4, :major)
play_pattern_timed [:e5, :f5, :g5], [1, 1, 2]
end
As you might guess, 2.times do
means do the indented part (up until the end
)
two times.
play chord
plays the type of chord you would like.play_pattern
plays a sequence of notes, holding each for one beat.play_pattern_timed
plays notes, holding each for the specified duration.
Simulating Making a Phone Call
Copy and paste this program into Sonic Pi and run it. Do you recognize those sounds?
Effects
Here’s an example of using the slicer effect.
with_fx(:slicer, phase: 0.1) do
play :c, sustain: 4
end
Reverberation
Sample program
Try increasing the room value gradually from 0 to 1.
with_fx(:reverb, room: 0, mix: 0.6) do
3.times do
sample :bass_hit_c
sleep 1.0 / 4
end
end
Silly Reverb Example
use_synth :zawa
with_fx(:reverb, room: 0.8, mix: 0.6) do
loop do
play rrand_i(10, 40)
sleep rrand_i(0.3, 3)
end
end
Panning
Here’s an example with crows.
pan1 = 1
pan2 = -1
live_loop :one do
if pan1 > 0
sample :misc_crow, pan: pan1
pan1 -= 0.1
end
sleep rrand(1, 3)
end
live_loop :two do
if pan2 < 0
sample :misc_crow, pan: pan2
pan2 += 0.1
end
sleep rrand(0, 2)
end
Four Bars of Boogie Wonderland
Copy the code into Sonic Pi. It is easier to copy if you push Raw
first.
Beats and Octaves
We’ll play with variations of this program to hear sounds interfering with each other, and to understand intervals such as octaves.
use_synth_defaults release: 8
play 72, pan: -1
play 72.1, pan: 1
Remember Bubble Sort?
Do-Re-Mi
Here’s the code, if you want to run it yourself and make changes.
use_synth :piano
use_bpm 300
in_thread do
for notes, times in [
[[:c, :d, :e, :c, :e, :c, :e],
[ 3, 1, 3, 1, 2, 2, 4]], # do
[[:d, :e, :f, :f, :e, :d, :f],
[ 3, 1, 1, 1, 1, 1, 8]], # re
[[:e, :f, :g, :e, :g, :e, :g],
[ 3, 1, 3, 1, 2, 2, 4]], # mi
[[:f, :g, :a, :a, :g, :f, :a],
[ 3, 1, 1, 1, 1, 1, 8]], # fa
[[:g, :c, :d, :e, :f, :g, :a],
[ 3, 1, 1, 1, 1, 1, 8]], # so
[[:a, :d, :e, :fs, :g, :a, :b],
[ 3, 1, 1, 1, 1, 1, 8]], # la
[[:b, :e, :fs, :gs, :a, :b, :c5, :b, :bf],
[ 3, 1, 1, 1, 1, 1, 4, 2, 2]], # ti
[[:a, :f, :b, :g, :c5],
[ 4, 4, 4, 4, 8]]
] do
play_pattern_timed notes, times
end
end
in_thread do
use_synth :dsaw
play_pattern_timed [:c2, :g1, :c2, :f1, :c2, :f2, :fs2, :g2, :gs2, :a2, :f2, :g2, :c2],
[16, 16, 16, 16, 8, 8, 8, 8, 8, 8, 8, 8, 8], attack: 1, sustain: 4, release: 2, amp: 0.3
end
in_thread do
32.times do
sample :drum_cowbell, amp: 0.2 # Needs more cowbell?
sleep 4
end
end
Sonic Pi Quiz
Take ten minutes to research your answers to these questions. Work in pairs or larger groups if you like.
- How do you play a note, panned to
- the left
- center
- right
- halfway between the center and the right
- Explain attack, sustain and release
- Describe at least one way to have multiple players (“band members”) playing at the same time
- Name two synths that you like
- Name two samples that you like