Skip to content

Instantly share code, notes, and snippets.

@melodyloveless melodyloveless/01_.md
Last active Dec 15, 2018

Embed
What would you like to do?
Live Code NYC x Live Code PHLY - Workshop - 12/15/18

Sonic Pi - Workshop - Live Code NYC x Live Code PHLY 2018

Authors

Melody Loveless
Website: melodyloveless.com
Instagram: @melodycodes

Materials

  • Computer
  • Sonic Pi
  • Speakers/Headphones

Vocabulary

  • Algorithm - list of instructions meant to finish a task
  • Commands - an instruction for the computer. Many commands put together make up algorithms and computer programs.
  • Algorave - a performance/event where music is generated by code. Typically, the musicians are paired with a visual artist, the code is displayed for the public, and the music is created for dancing
  • Loop - the action of doing something over and over again
  • Parameter - an extra piece of information that customizes a command and/or function
  • Syntax - the structure or rules of a language
  • Synthesizer - an electronic musical instrument that generates audio signals that may be converted to sound
  • Sample - a sound file that is used a piece of music

Listening Examples

The following are examples of music to inspire you as you code. Some of the songs/examples technically belong into multiple categories (ex. some process pieces could be danced too) - and that's okay! Your music can be fluid in categories too.

Traditional/Dance Music

Process Music

Ambient Music

Other Algorithmic Music

Post Session

The following are places where you can find more resources/materials

Resources/Inspiration for Music Making

References

Sonic Pi - http://sonic-pi.net/

Examples

Notes / Synthesizers

Remember:

  • We can declare any number between 0-127 as the pitch with zero (0) being the lowest pitch and 127 as the highest

How to play a note

  1. Type the command "play"
  2. Hit space
  3. Type a number between 0-127
  4. Add sleep ___ below the command to finish the algorithm
play 60 
sleep 1 

How to play a chord

Everything before "sleep 1" will play at the same time. This makes a chord.

play 60 
play 64
play 67
play 72
sleep 1

How to change a synthesizer

You can change a synthesizer by using the command "use_synth" and declaring a synthesizer afterwards

Ex. use_synth :saw, use_synth :sine

In the example below, we are changing synthesizers between the first and second note.

use_synth :saw
play 60 
sleep 1
use_synth :sine
play 64
sleep 1

Samples

Sonic Pi has built in samples for us to play. These instructions are for how to play them.

How to play a sample

  1. Type the command "sample"
  2. Hit space
  3. Scroll down the list and pick a built-in sample to use
  4. Add sleep ___ below the command to finish the algorithm
sample :bd_boom
sleep 1 

Iterations / Loops / Live_Loops

In Sonic Pi, we can play blocks using "_.times do", "loop", and "live_loop"

How to use a _.times do

  1. Decide how many times you want your command to repeat
  2. Write that number down
  3. Add ".times do" after the number
  4. Insert code that you want to repeat
  5. End the algorithm by adding "end" at the bottom of the block

In this example, we are saying to play the sample four times

4.times do
  sample :elec_blip
  sleep 0.5
end

How to use a live_loop

  1. Type "live_loop"
  2. Hit space and type a semi-colon (:)
  3. Add a name after the semi-colon (ex. live_loop :one)
  4. Insert code into block
  5. End the algorithm by adding "end" at the bottom of the block

Remember:

  • Loops are meant to repeat forever
  • Using multiple live_loops means you'll have multiple blocks of code running at the same time
  • Each live_loop has to have a different name

For more information on code structure and syntax, visit https://sonic-pi.net/tutorial.html#section-5

In the example below, the bass drum is being played in a "live_loop :one".

live_loop :one do
sample :bd_808
sleep 1
end

Parameters / Effects

In Sonic Pi, we can change the way a sound is played by adjusting paramaters. These include: amp, pan, attack, rate, release, sustain.

Amp

Amp is short for amplitude. Amplitude controls the volume. We can change the amplitude for notes and samples.

In this example, we are saying to play the note half at "0.5"

play 60, amp: 0.5
sleep 1

Pan

We can say which speaker we want our sound to play out of using pan. We can adjust the panning for notes and samples. When the pan is -1, we are saying to play only out of the left speaker. When the pan is 1, we are saying to play only out of the right speaker.

In this example, we are saying to play the first note out of the left speaker and then the second sample out of the right twice.

2.times do
play 60, pan: -1
sleep 1
sample :bd_fat, pan: 1
sleep 1
end

Attack

We can control the beginning of a note or sample by adjusting the attack.

In this example, we are telling our synthesizer to take two beats to fade in.

2.times do
    play 60,
      attack: 2 #how long it takes to fades in
    sleep 4
  end

Release

We can control the end of a note by adjusting the release.

In this example, we are telling our synthesizer to take two beats to fade out.

2.times do
  use_synth :beep
  play 60,
    release: 2 #how long it takes to fades out
  sleep 2
end

Sustain

We can control how long a note or sample is played at full amplitude by using sustain. Sustain can be difficult to work with and is best paired with a different paramater like attack and/or release.

In this example, we are telling our synthesizer to sustain the pitch at full volume for 0.5 a beat, release for 1 beat.

2.times do
  use_synth :beep
  play 80, sustain: 0.5, release: 1 
  sleep 2
end

In this example, we are telling our synthesizer to fade in the attack for one beat, sustain the pitch at full volume for 2 beats, release the sound for 1 beat.

2.times do
  use_synth :blade
  play 40, attack: 1, sustain: 2, release: 1
  sleep 4
end

Sonic Pi Recipes

Instructions: Explore each prompt by coding with musical goals in mind while using all of the required ingredients. Use a combination of ingredients to create your piece. If you need more help making decisions, try listening to an example and break down the events happening within it.

Required Ingredients:

  • At least four (4) commands
  • At least four (4) parameters
  • At least one (1) effect (with_fx)

1. Traditional/Dance Music

Instructions: Code music that emulates traditional/dance music.

Useful Ingredients:

  • cue and sync
  • sleep

Tips:

  • Use “cue” and “sync” to line up your loops (live_loop)
  • See Tutorial 9.3 to read more about syncing live loops
  • If you don't know where to start, listen to a song you like and copy the process that you hear. -- Drum beat happens 4 times, then the synth comes in to play chords, etc.

2. Ambient/Soundscape

Instructions: Create a piece that is considered loose in time or meant to emulate a space.

If you need a starting point/inspiration, try creating a piece based off of:

  • The Future
  • Outer Space
  • Ocean
  • Haunted House

Useful Ingredients:

  • rrand
  • rrand_i
  • rate
  • choose

3. Process Music

Instructions: Create a song/piece based on a process.

Examples of Processes:

  • Slow to Fast and/or Fast to Slow
  • High to Low and/or Low to High
  • Happy to Angry and/or Sad to Angry

Useful Ingredients:

  • ring / tick
  • sleep
  • rate

Tips: Slow to Fast:

  • Change the value of sleep from larger to smaller numbers
  • Change the rate of the sample from a smaller number to a larger number
use_bpm 120
#BPM - Beats Per Minute
4.times do
sample :glitch_perc1,
amp: 1, #volume
pan: -1 #pan - left speaker
sleep 2
sample :glitch_perc2,
amp: 1,
pan: 1 #pan - right speaker
sleep 2
end
2.times do
sample :ambi_drone,
rate: 1 #rate of 1 is normal
sleep 4
sample :ambi_drone,
rate: 2 #rate of 2 is twice as fast
sleep 4
end
2.times do
sample :misc_burp,
rate: 1 #rate of 1 is normal
sleep 4
sample :misc_burp,
rate: 0.5 #rate of 0.5 is half as slow
sleep 4
end
#Different waves forms
#Run the following code with the oscilloscope (waveform viewer) on to see
#the physical properties of the waves
#For more about attack, release, and playing with durations, go to Tutorial 2.4
2.times do
use_synth :sine
play 72,
amp: 2,
attack: 2, #take 2 beats
release: 2
sleep 4
end
2.times do
use_synth :square
play 72,
amp: 2,
attack: 2,
release: 2
sleep 4
end
2.times do
use_synth :tri
play 72,
amp: 2,
attack: 2,
release: 2
sleep 4
end
2.times do
use_synth :saw
play 72,
amp: 2,
attack: 2,
release: 2
sleep 4
end
use_bpm 120
#BPM - Beats Per Minute
4.times do
sample :glitch_perc1,
amp: 1, #volume
pan: -1 #pan - left speaker
sleep 2
sample :glitch_perc2,
amp: 1,
pan: 1 #pan - right speaker
sleep 2
end
2.times do
sample :ambi_drone,
rate: 1 #rate of 1 is normal
sleep 4
sample :ambi_drone,
rate: 2 #rate of 2 is twice as fast
sleep 4
end
2.times do
sample :misc_burp,
rate: 1 #rate of 1 is normal
sleep 4
sample :misc_burp,
rate: 0.5 #rate of 0.5 is half as slow
sleep 4
end
use_bpm 120
#BPM - Beats Per Minute
#Can anyone read this for me and tell me what's going to happen?
1.times do
4.times do
play 60
sleep 1
end
sleep 2
4.times do
play 60,
attack: 2 #how long it takes to fades in
sleep 4
end
sleep 2
4.times do
play 60,
release: 2 #how long it takes to fade out
sleep 4
end
sleep 2
end
use_bpm 120
4.times do
play [72, 84, 62].choose #this chooses randomly between 4 notes
sleep 1
end
4.times do
play 70,
pan: [-1, 1].choose #chooses randomly between left and right speakers
sleep [0.5, 1].choose #this chooses randomnly between 2 durations for sleep
end
8.times do
play 84,
amp: [0.5, 1].choose #chooses randomly between volume
sleep [0.25, 0.5, 0.75].choose #this chooses randomnly between 3 durations for sleep
end
#Ambient Music #1 - rrand, rrand_i, using variables
#For general information about using rrand and rrand_i, go to Tutorial 4 and/or Appendix 9
#For more about sample slicing, go to Appendix 12
use_bpm 70
live_loop :ambient1 do
play [:f5, :e5, :f4, :e4].choose,
attack: 4,
release: 2,
pan: rrand(-1, 1)
sleep [4, 6, 8].choose
end
live_loop :ambient2 do
x = rrand_i(2, 8)
print x #we can use print to see what the variable represents
y = rrand(-1, 1)
print y
sample :ambi_lunar_land,
amp: 0.5,
rate: y,
attack: x/4,
release: x/4
sleep x
end
live_loop :ambient3 do
x = rrand_i(2, 8)
y = rrand(-1, 1)
z = rrand(0, 0.5)
w = z + 0.25
sample :loop_amen,
start: z,
finish: w,
amp: [0.5, 0.25].choose,
rate: y,
attack: x/4,
release: x/4
sleep x
end
live_loop :ambient4 do
x = rrand_i(4, 16)
y = rrand(-1, 1)
z = rrand(0, 0.5)
w = z + 0.33
with_fx [:bitcrusher, :wobble].choose do #chooses between effects
sample [:ambi_glass_rub, :ambi_glass_hum].choose, #chooses between samples
start: z,
finish: w,
amp: [0.5, 0.25].choose,
rate: y,
attack: x/4,
release: x/4
sleep x
end
end
#Process Music Example #1 - Slow Fast Slow
use_bpm 140
live_loop :somegroovething do
sample :bd_fat,
amp: 4
sleep 1
sample :sn_zome,
amp: 1
sleep 1
end
#Slow Fast Slow Using Sleeps
#4, 3, 2, 1, 2, 3, 4
live_loop :process1 do
4.times do
sample :elec_pop
sleep 1
end
4.times do
sample :elec_pop
sleep 0.75
end
4.times do
sample :elec_pop
sleep 0.5
end
8.times do
sample :elec_pop
sleep 0.25
end
4.times do
sample :elec_pop
sleep 0.5
end
4.times do
sample :elec_pop
sleep 0.75
end
end
#Process Music Example #2 - ring
use_bpm 120
#Pattern of 4 part 1
live_loop :process2a do
with_synth :tri do
play (ring 48, 50, 57, 50).tick(:line1), #Look at 9.4 in the Tutorial for more information - Naming Ticks
attack: 2,
release: 2,
pan: -1
sleep 4
end
end
#Pattern of 3 part 1
live_loop :process2b do
with_synth :square do
sleep 2
play (ring 48 + 12, 50 + 12, 57 + 12).tick(:line2),
attack: 2,
release: 2,
pan: 1
sleep 2
end
end
#2 Against 3
live_loop :process2c do
sample (ring :elec_blip, :elec_twang).tick(:poly1)
sleep 0.5
end
#3
live_loop :process2d do
sample :elec_plip,
pan: (ring -1, 1).tick(:moveit)
sleep 0.75
end
#Process Music #3 - high to low, low to high, rate
notez = [45, 49, 52, 56, 57, 59, 61, 64, 68, 69, 71, 73, 76] # 0-12
#low to high
live_loop :process3a do
use_synth :pluck
with_fx :bitcrusher do
4.times do
play notez.choose
sleep [0.25, 0.5].choose
end
4.times do
play notez.choose + 12
sleep [0.25, 0.5].choose
end
4.times do
play notez.choose + 24
sleep [0.25, 0.5].choose
end
end
end
#high to low
live_loop :process3b do
use_synth :piano
with_fx :distortion do
4.times do
play notez.choose
sleep [0.25, 0.5, 0.75].choose
end
4.times do
play notez.choose - 12
sleep [0.25, 0.5, 0.75].choose
end
4.times do
play notez.choose - 24
sleep [0.25, 0.5, 0.75].choose
end
end
end
#low to high to low
live_loop :process3c do
4.times do
sample :ambi_haunted_hum,
rate: (ring 0.5, 1, 2, 1).tick(:wow),
amp: 0.5,
attack: 1,
release: 1
sleep [2, 4, 8].choose
end
end
#Traditional/Dance Music Example #1 - live_loop, cue/sync, ring, choose
use_bpm 90
#Drums in 4
live_loop :dance1 do
cue :maindance
with_fx :reverb do
sample :bd_pure,
amp: 4
sleep 1
sample :drum_snare_soft,
amp: 1
sleep 1
end
end
#Bass line
live_loop :dance2 do
sync :maindance
use_synth :blade
8.times do
play (ring :c2, :e2, :d2, :g2).tick(:bass) #Look at 9.4 in the Tutorial for more information - Naming Ticks
sleep [0.25, 0.5].choose
end
end
#High pitched rythmic
live_loop :dance3 do
sync :maindance
use_synth :pretty_bell
8.times do
play choose(chord(:C7, :major)), #See Tutorial 8.3 for more about using scales
amp: [0.5, 0.25].choose
sleep [0.25, 0.5, 0.75].choose
end
end
#Flowy Lines
live_loop :dance4 do
sync :maindance
use_synth :pretty_bell
2.times do
play (ring :g5, :f5, :e5, :d5).tick(:flow1), #Look at 9.4 in the Tutorial for more information - Naming Ticks
attack: 2,
release: 2
sleep 4
end
4.times do
play [:e4, :b4, :c4, :c5].choose,
attack: 2,
release: 2
sleep 4
end
end
#Traditional/Dance Music Example #2 - live_loop, cue/sync, choose, rrand
#This is a row! If you want to use the same numbers over and over again, try putting them in a row!
notez = [59, 60, 62, 64, 65, 67, 71, 72, 74, 76, 84, 86]
#BPM = Beats Per Minute
use_bpm 120
#Drums in 4
live_loop :dance1 do
cue :maindance
sample :bd_haus,
amp: 2 #amp
sleep 1
sample :drum_snare_hard,
amp: 0.5
sleep 1
end
#Bass line
live_loop :dance2 do
sync :maindance
use_synth :pretty_bell
8.times do
play (notez.choose - 24), # (- 24) puts the notes two octaves down
attack: 2,
release: 2
sleep 4
end
end
#Dynamic Rythmic Stuff
live_loop :dance3 do
sync :maindance
use_synth [:tri, :square].choose #you can have Sonic Pi choose between synths
8.times do
play (ring notez[2] + 12, notez[3] + 12, notez[6] + 12, notez.choose + 12).tick(:maybe), #read section 8.4 for more on rings
release: 2, #effects how the note fades out
amp: [0.5, 0.25].choose,
pan: rrand(-1, 1) #picks a number randomnly between -1 and 1
sleep [0.25, 0.5].choose
end
end
#Random If Statements
live_loop :dance4 do
sync :maindance
16.times do
with_fx :bitcrusher do #this effect makes these drums crunchy
if one_in(2)
sample [:drum_tom_hi_soft, :drum_tom_low_soft].choose,
amp: [0.5, 0.75, 1].choose
sleep [0.25, 0.5, 0.75].choose
else
sample [:drum_cymbal_closed, :drum_tom_hi_hard].choose,
amp: [0.5, 0.75].choose
sleep 0.25
end
end
end
end
notez = [45, 49, 52, 56, 57, 59, 61, 64, 68, 69, 71, 73, 76] # 0-12
live_loop :randomchords do
use_synth :tri
play (ring notez[0], notez[2], notez[3], notez[4], notez[5], notez[6]).shuffle.drop(2).take(3) #this creates a chord everytime. Read Tutorial 8.5 on ring chains for more
sleep 0.5
end
live_loop :prettyslowchords do
use_synth :blade
play (ring notez[7] + 12, notez[4] + 12, notez[5] + 12, notez[3] + 12, notez[9] + 12, notez[8] + 12).shuffle.drop(2).take(3),
attack: 2,
release: 2
sleep 4
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.