Skip to content

Instantly share code, notes, and snippets.

@rowena-s
Last active September 10, 2019 12:06
Show Gist options
  • Save rowena-s/f32cc837db205f8e8b270f90b1dba402 to your computer and use it in GitHub Desktop.
Save rowena-s/f32cc837db205f8e8b270f90b1dba402 to your computer and use it in GitHub Desktop.
Code Snippets and links for rorosyd SonicPi talk

Official sites

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

Installation

https://sonic-pi.net/ . For Windows, Mac, Raspberry Pi
https://github.com/samaaron/sonic-pi/blob/master/INSTALL-LINUX.md For Linux distros (build from source)

Forums

https://in-thread.sonic-pi.net/ (official help 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 YoutTube channel https://www.youtube.com/channel/UCtUhMj_RnVKy5Jiyr-fWZwg
https://www.youtube.com/watch?v=xpaO8HTrQ1M - YOW! Perth talk
https://codeclubau.org/projects - Sonic Pi lesson packs, a great way for anyone to get started!

https://gist.github.com/xavriley/87ef7548039d1ee301bb - Mario NES
https://www.earthtoabigail.com/blog/audio-representation-bubble-sort-with-ruby-sonicpi
https://gist.github.com/danreedy/a0f0aa1ec2eb275c55a2 - Let It Go

Links and code used in talk

System Architecture

https://in-thread.sonic-pi.net/t/sonic-pi-architecture/329/3
https://github.com/samaaron/sonic-pi/wiki/Sonic-Pi-Internals

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 50 Plays midi note 50 - higher number -> higher pitch, lower number -> lower pitch

play 55.2341234 You can use decimals...

play 50 + Math::PI ... and do maths too

and even use musical notation!

play :c2 Plays C in the second octave

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

play :fs6 Plays F sharp in the 6th octave (in music, a hash is used for sharp but that's the ruby comment symbol)

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 (all at once)

play chord(55, :major)

or

play_chord([60,62,67])

You can play each note individually too!

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

See Four_Chord_Song.rb

Timed notes (one at a time)

play_pattern_timed <array of notes>, <array of beat numbers>

e.g.

play_pattern_timed [70, 65, 72, 61, 54], [1, 0.5]

or taking a shortcut and randomising notes from a scale

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

Changing the lengths of the notes

With ADSR envelopes

Plays note 65 held for 4 beats

play 65, sustain: 4

Plays note 65 with a short fade out for a short cut-off effect or staccato

play 65, release: 0.5

You can combine args and make the sustain longer than the sleep for a slurred or legato effect

play 65, sustain: 1.1, release: 0.5
sleep 1
play 68

Playing Samples

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

sample :perc_till

changing the speed

sample :loop_amen, rate: 2

or sample <path_to_file_on_your_laptop_as_a_string>

See Addams_Family_Intro.rb

Using Synths

Game Of Thrones theme synth tester

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 execute blocks of code concurrently. in_thread will execute the block once whereas live_loop will create an infinite loop. Both will take a delay: n param which will delay execution by n beats(see Frere_Jaques.rb)

converting from time(seconds) to beats seconds/60\*bpm

Sample OSC Connection

using https://play.google.com/store/apps/details?id=com.ffsmultimedia.osccontroller

live_loop :slider_tune do
  use_real_time
  note = sync "/osc/test/slider1" #change this to match your OSC interface
  puts "playing note #{note[0]} "
  play (note[0])
  
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