Skip to content

Instantly share code, notes, and snippets.

@claritee
Last active May 7, 2023 22:18
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save claritee/08f318e48d1fc8498f19907b8c455ae8 to your computer and use it in GitHub Desktop.
Save claritee/08f318e48d1fc8498f19907b8c455ae8 to your computer and use it in GitHub Desktop.

Cheatsheet for Sonic Pi

Just some notes on Sonic Pi

Play a note

play 60 play :C4

Lowest (0) to highest (100)

Middle C is 60

:C4 is a symbol for C in 4th Octave

Play an Array of Notes

play [50, 55, 62]

Randomly choose a note to play:

play choose([50, 55, 62])

Pick a note from the array

play [50, 55, 62][1]

Sleep

sleep 0.5 Sleep for 0.5 sec

Volume

play 60, amp: 0.3
play 60, amp: 0.9
play 60, amp: 10.0

Holding Notes

play :c4, release: 1.0

Hold the note for 1 sec

Loops

loop do
  play 60
  sleep 0.5
  play 62
  sleep 0.5
end
2.times do
  # code
end

Using Synths

Pre-recorded sounds e.g. bells, beeps

use_synth :fm
play 60

Other ones to try:

  • mod_saw
  • blade
  • dtri
  • pretty_bell

Other pre-recorded sounds

sample :loop_amen
sample :perc_bell, rate: (rrand 0.125, 1.5)

Scales

play_pattern_timed scale(:c3, :major), 0.3, release: 0.5

Same as

play_pattern_timed [:c3, :d3, :e3, :f3, :g3, :a3, :b3, :c4], 0.3, release: 0.5

Using Methods

Define the method

define :ring_perc_bell do
  sample :perc_bell, rate: (rrand 0.125, 1.5)
  sleep rrand(0,2)
end

Then use the method:

loop do
  ring_perc_bell
end

Chords

play chord(:E3, :minor)
play_pattern chord(:E3, :m7)
play_pattern_timed chord(:E3, :m7), 0.25 

Multi-threading

# 1st thread - melody/backing track
in_thread do
  loop do
    # code
  end
end 

# 2nd thread - the actual notes
in_thread do
  loop do
    # code
  end
end 

Live Coding

Music can be changed and adapted in real time, not just play pre-written programs.

Option 1: in normal loop

define :my_loop do
  play 50
  sleep 1
end

loop do
  my_loop
end

To change this in real time:

  • Press play, then change the note, press play again. The music changes live

Option 2: in live loop

live_loop :foo do
  play 60
  sleep 1
end

To change this in real time:

  • While playing, change the note and press play
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment