Sonic Pi Workshop

Last week, I gave a workshop at the University of Iowa College of Engineering, introducing students to the Sonic Pi language and environment. This music-oriented programming language is designed for live coding. It can also be used to introduce text-based coding to people with no programming experience.

The students were very curious and active: we had the time to go through all of the examples and to introduce what live coding is in the electronic music community. Find below the code excerpts I used for this one-hour workshop.

Play a note

play 70
Cmd-R to run the code

Chord

play 72
play 75
play 79

Melody

play 72
sleep 1
play 75
sleep 1
play 79

Play with durations & pitch

Duration is in “beats”. Default BPM is 60, i.e. 1 beat is 1 second.
Pitch is the MIDI note number. Try 60.25 if you like eighth tones.

First option: amp
play 50, amp: 0.1
sleep 0.25
play 55, amp: 0.2
sleep 0.25
play 57, amp: 0.4
sleep 0.25
play 62, amp: 1

Using synthesizers

use_synth :saw
play 38
sleep 0.25
play 50
sleep 0.25
use_synth :tb303
play 38
use_synth :dsaw
play 50
use_synth :prophet
play 57

Envelopes

See https://sonic-pi.net/tutorial.html#section-2-1

Samples

sample :ambi_lunar_land
sample :ambi_drone, pan: -1

Choice of samples: see auto-completion on ambi_, guit_, drum_

Playback rate

sample :guit_e_slide, rate: 1
sample :guit_e_slide, rate: 0.5
sample :guit_e_slide, rate: 2

Randomisation

play rrand(60, 72)

Cmd-R several times. See what’s printed as information.
random vs. pseudo-random.

Let’s try a loop now:
loop do
play rrand(60, 72)
sleep 0.5
end

loop do
sample :perc_bell, rate: rrand(0.125, 1.5)
sleep rrand(0.2, 2)
end

Cutoff frequency for a low-pass filter

use_synth :tb303
loop do
play 50, release: 0.1, cutoff: rrand(60, 120)
sleep 0.125
end

Choose a random element in a list

loop do
play choose([60, 65, 72])
sleep 1
end

Random value between 0 and 1

loop do
play 60, amp: rand
sleep 0.25
end

Loops

3.times do
play 50
sleep 0.5
sample :elec_blup
sleep 0.5
play 62
sleep 0.25
end

if then else with a “flip a coin” condition
loop do
if one_in(2)
sample :drum_heavy_kick
else
sample :drum_cymbal_closed
end
sleep 0.5
end

Intro to Live Coding

live_loop :foo do
play 60
sleep 1
end

live_loop :foo do
play rrand_i(60, 63)
sleep 0.5
end

live_loop :foo do
play choose([60, 65, 72])
sleep 0.5
end

live_loop :foo do
sample :loop_garzul, amp: 0.1
use_synth :prophet
play 2*rrand_i(30, 33)
sleep 0.5
end

Several loops must have different names
live_loop :foo do
sample :loop_garzul, amp: 0.1
sleep 1
end

live_loop :bar do
sample :bd_haus
sleep 0.4
end

Synchronize loops
live_loop :foo do
sample :loop_garzul, amp: 0.1
sleep 0.3
end

live_loop :bar do
sync :foo
sample :bd_haus
sleep 0.3
end

Tick through a ring

Local iteration over a ring in a live_loop
(ring 60, 62, 64) is a ring
[60, 62, 64].ring is a ring
Then, you apply the tick method:
[60, 62, 64].ring.tick

live_loop :foo do
play (ring 60, 62, 64, 65, 67).tick
sleep 0.7
end

Comments

Popular posts from this blog

Max for Live: Extreme Time Stretching with Spectral Stretch

GroundSwell 2020 Emerging Composers Competition

A Free Tutorial on Spectral Sound Processing Using Max/MSP and Jitter