Creative programming workshop with Sonic Pi
I found a hands-on tutorial for Sonic pi.
Here is how it goes. We already covered some of t he poonts by sequentially going through the documentation.
Looping
We can loop infinitely by:
loop do sleep 0.5 play :d4 sleep 0.5 play :e4 sleep 0.5 play :c4 sleep 0.5 end
Or we can specify the number of repitions
4.times do sleep 0.5 play :d4 sleep 0.5 play :e4 sleep 0.5 play :c4 sleep 0.5 end
Adding different tracks
Here a live_loop comes into play. basically multiple sounds play at the same time.
use_bpm 100
live_loop :drums do
sample :drum_heavy_kick
sleep 1
sample :drum_snare_hard
sleep 1
sample :drum_heavy_kick
sleep 1
sample :drum_snare_hard
sleep 1
end
live_loop :hihat do
sample :drum_cymbal_closed
sleep 0.25
sample :drum_cymbal_pedal
sleep 1
end
live_loop :bass do
use_synth :fm
play :c2, attack: 0, release: 0.25
sleep 0.25
play :c2, attack: 0, release: 0.3
sleep 2
play :e2
sleep 0.75
play :f2
sleep 1
end
Now that we have the bass and drums, let’s start to worry about the main melody.
Lead track
Instead of typing play, sleep for every single note, we can use the play_pattern_timed method.
live_loop :melody do play_pattern_timed [:c4, :e4, :f4, :g4, :f4, :e4, :f4, :g4, :f4, :e4, :f4], [0.25, 0.25, 0.25, 1.5, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25] end
play_pattern_timed [notes],[lengths]
(pseudo-)Randomness
We can play random notes, lengths and anything that can be controlled by numbers really.
- to choose randomly from a list of notes:
play [40,45,50].choose
- To randomly choose an octave
use_octave [0, 1].choose
- To randomly choose a note from a chord:
play (chord :c, :major).choose
You get the point.
We already covered ops, let’s look at effects;
Effects
we change the effects by encapsulating the code with with_fx
live_loop :effects do with_fx :wobble do with_fx :slicer do with_fx :reverb do play rrand(20,120) sleep 0.5 sample :elec_plip sleep 0.5 play rrand(20,120) end end end end
This is what this piece of code sounds like:
Ticks and rings
Ticks go through the notes of a progression sequentially in a list. A finite list is bound to end. So we make it a circular list. A ring. Then we are ready to pass it.
Variables
Variables are used to reference a certain object and do operations over it. In the context [of music, we can change the pitch played.
This is a useful tutorial plus it comes with a little cheat sheet:
https://sonic-pi.mehackit.org/exercises/en/10-cheatsheet/01-cheatsheet.html