Skip to content

Instantly share code, notes, and snippets.

@rowena-s
Last active February 15, 2021 00:33
Show Gist options
  • Save rowena-s/c2fcb29d4832f6435d13d9651fb58416 to your computer and use it in GitHub Desktop.
Save rowena-s/c2fcb29d4832f6435d13d9651fb58416 to your computer and use it in GitHub Desktop.
Material for MUED4002 Tech Week session

Official sites

https://sonic-pi.net/
https://twitter.com/sonic_pi
https://twitter.com/samaaron
https://www.patreon.com/samaaron

Forums

https://in-thread.sonic-pi.net/ (official help and discussion forum)
https://www.reddit.com/r/SonicPi/

Other useful links & resources

https://www.raspberrypi.org/magpi/issues/essentials-sonic-pi-v1/
https://sonic-pi.net/tutorial
http://sonic-pi.mehackit.org/exercises/en/10-cheatsheet/01-cheatsheet.html
https://rbnrpi.wordpress.com and YouTube channel https://www.youtube.com/channel/UCtUhMj_RnVKy5Jiyr-fWZwg
https://www.youtube.com/watch?v=xpaO8HTrQ1M - YOW! Perth talk
https://www.youtube.com/watch?v=gETE5Pe_Fdk - Zendesk talk

Classical Music Examples

https://www.youtube.com/watch?v=qnzlH02SSqU - Bach Suite no 3 BWV 1068
https://www.youtube.com/watch?v=5gRcWfWusN0#t=1m12s - Brandenburg Concerto No 2

Introduction to Ruby courses

https://www.codecademy.com/learn/learn-ruby
https://launchschool.com/books/ruby/read/basics
https://learnrubythehardway.org/book/
https://repl.it/languages/Ruby - Browser based environment for running Ruby code

ADSR envelope

ADSR envelope https://blog.landr.com/wp-content/uploads/2016/10/ASDR-01.jpg
https://www.youtube.com/watch?v=4HIS9Tqukr8#t=16s

Playing Notes

One Note

play 60 Plays midi note 60 - higher number -> higher pitch, lower number -> lower pitch

play 65.2341234 You can use decimals...

play 60 +2 ... and do maths too

play :c2 Plays C in the second octave

play :bb or play bf Plays B flat in the 4th octave (default)

play :fs6 Plays F sharp in the 6th octave

NOTE: # is used as the comment character. This means the computer will ignore everything after it on the same line. It's good for putting in human readable notes about what that bit of code does or keeping code in the file that you don't want to run.

Two notes

This will play two notes at the same time

play 60
play 61

This will play the two notes sequentially with a gap of one beat (NOT seconds

play 60
sleep 1
play 61

Playing lots of notes

Chords

play chord(55, :major)

or

play_chord([60,62,67])

You can play arpeggios too!

loop do
  play chord(:d, :major).tick
  sleep 0.5
end

See Four_Chord_Song.rb

Scales

You need to loop through them otherwise they all play at once

loop do
  play (scale :c, :major).tick
  sleep 1
end

or

play_pattern_timed scale(:c, :major), [1]

format is play_pattern_timed <notes to play>, <sequence of sleep times between each notes>

Now Randomize It!

play_pattern_timed scale(60, :egyptian).shuffle, [1, 0.5]

or

loop do
  play (scale 50, :major, num_octaves: 2).choose
  sleep 1
end

Playing Samples

https://sonicpisamples.100yen.co.uk/ - website with Sonic Pi samples

sample :drum_cymbal_pedal

or sample <location_of_file_on_your_computer> - easiest way is to drag the file from Finder/File Explorer

You can also play them at different speeds

sample :loop_amen, rate: 0.5 

or pick out specific notes

loop do
  sample :loop_amen, onset: 1
  sleep 0.125
end

or even randomise it

loop do
  sample :loop_amen, onset: pick
  sleep 0.125
end

Using Synths

use_synth :pluck
play chord(:c, :maj)

Oh, cool! We have a digital uke 😃

Testing all the synths with GOT theme (growl one sounds pretty cool)

use_bpm 120
synth_names.each do |s|
  use_synth s
  puts "using synth #{s}"
  play_pattern_timed [:g, :c, :eb, :f, :g, :c, :eb, :f, :d ],[1, 1, 0.5, 0.5, 1, 1, 0.5, 0.5, 2]
  sleep 1
  play_pattern_timed [:d, :bb3, :eb, :d, :f, :bb3, :eb, :d, :c],[2, 2, 0.5, 0.5, 1.5, 2, 0.5, 0.5, 2]
end
 

Loops and layering

in_thread and live_loop allow you to run multiple blocks of code at the same time. in_thread will execute the code once whereas live_loop continuously runs the code in a loop until you stop it. Both will take a delay: n param which will delay execution by n beats(see Frere_Jaques.rb)

Sample OSC Connection

OSC is a protocol similar to MIDI used internally by Sonic Pi but you can also use it to communicate with other devices e.g. phone apps, lights, other computers. I use this one https://play.google.com/store/apps/details?id=com.ffsmultimedia.osccontroller which is free. Another commonly used one is https://hexler.net/products/touchosc.

Example with slider

live_loop :slider_tune do
  use_real_time
  note = sync "/osc/demo/slider1" #change this to match your OSC interface
  puts "playing note #{note[0]} "
  play (note[0])
  
end

Example with buttons to control samples

live_loop :grid_toggle do
  use_real_time
  start = sync "/osc/demo/gridToggle1" #change this to match your OSC interface
  if start[0] == 1.0
    sample :drum_cymbal_hard
    sleep 1
  end
end
#4chord loop
use_bpm 90
live_loop :four_chords do
4.times do
play chord(:d, :maj)
play chord(:d5, :maj)
sleep 1
end
4.times do
play chord(:a, :maj)
play chord(:a3, :maj)
sleep 1
end
4.times do
play chord(:b, :min)
play chord(:b3, :min)
sleep 1
end
4.times do
play chord(:g, :maj)
play chord(:g5, :maj)
sleep 1
end
sleep 0.2
end
use_bpm 110
use_synth :chipbass
define :first_tune do
play :f3
sleep 0.3
play :g3
sleep 0.3
play :a3
sleep 0.3
play :bb3
sleep 0.75
end
define :second_tune do
play :g3
sleep 0.25
play :a4
sleep 0.25
play :b4, release: 0.5
sleep 0.25
play :c4
sleep 0.75
end
define :cowbells do
sample :drum_cowbell, amp: 0.5
sleep 1
sample :drum_cowbell, amp: 0.5
sleep 1
end
first_tune
cowbells
second_tune
sample :misc_burp, amp: 2
sleep 1
sample :misc_burp, amp: 2
sleep 1
second_tune
sleep 0.2
second_tune
first_tune
sample :misc_crow
sleep 1
sample :misc_crow
in_thread do
#frere jaques
2.times do
play_pattern_timed [:c, :d, :e, :c], [0.5]
end
#dormez vous
2.times do
play_pattern_timed [:e, :f, :g], [0.5, 0.5, 1]
end
#sonnez les matinez
2.times do
play_pattern_timed [:g, :a, :g, :f], [0.25]
play_pattern_timed [:e, :c], [0.5]
end
#ding ding dong
2.times do
play_pattern_timed [:c, :g3, :c], [0.5, 0.5, 1]
end
end
#transpose up 4
in_thread delay: 4 do
#frere jaques
2.times do
play_pattern_timed [:f, :g, :a, :f], [0.5]
end
#dormez vous
2.times do
play_pattern_timed [:a, :bb, :c5], [0.5, 0.5, 1]
end
#sonnez les matinez
2.times do
play_pattern_timed [:c5, :d5, :c5, :bb], [0.25]
play_pattern_timed [:a, :f], [0.5]
end
#ding ding dong
2.times do
play_pattern_timed [:f, :c, :f], [0.5, 0.5, 1]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment