Skip to content

Instantly share code, notes, and snippets.

@hzulla
Last active May 9, 2023 09:25
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save hzulla/cf9165ba15342e5df9b3 to your computer and use it in GitHub Desktop.
Save hzulla/cf9165ba15342e5df9b3 to your computer and use it in GitHub Desktop.
Crash into Sonic Pi: Learn to code music in less than 30 minutes
# -------------------------------------------
# CRASH INTO SONIC PI!
# Learn to code music in less than 30 minutes
# -------------------------------------------
# - download Sonic Pi from sonic-pi.net
# - copy and paste these code snippets
# - change and experiment with the snippets
# - go!
# -------------------------------------------
# These snippets were made for a workshop to
# show coders the very basic commands of
# Sonic Pi.
#
# If you're not in a hurry and wish to learn
# a whole lot more tricks, you should read
# through the excellent tutorial, it can be
# found within the Sonic Pi help system that
# comes with the software.
# -------------------------------------------
# Hanno Zulla, November 2015
# -------------------------------------------
# -------------------------------------------
# play midi note number 60 and wait one beat
play 60
sleep 1
# -------------------------------------------
# that midi code 60, it's the same as :c4
play :c4
sleep 1
# -------------------------------------------
# here's how you can play a chord
play :c4
play :e4
play :g4
sleep 1
# -------------------------------------------
# you can also play the same chord like this
play_chord [:c4, :e4, :g4]
sleep 1
# -------------------------------------------
# or like this
play_chord chord(:c4, :major)
sleep 1
# -------------------------------------------
# play notes from a scale, sleep 1 inbetween
play_pattern scale(:c4, :minor)
# -------------------------------------------
# change the sleep value between the notes
play_pattern_timed scale(:c4, :major_pentatonic), 0.125
# -------------------------------------------
# we can also use more than one sleep value
play_pattern_timed scale(:c4, :major_pentatonic), [0.125, 0.25]
# -------------------------------------------
# everything sounds better with reverb!
with_fx :reverb do
play_chord chord(:c4, :major)
end
# -------------------------------------------
# manipulate the synth envelope
use_synth :tb303
play :c4, attack: 2, sustain: 1, release: 4
# -------------------------------------------
# a melody generated from random numbers
live_loop :doodle do
play scale(:c4, :minor_pentatonic).choose
sleep [0.125, 0.25, 0.5].choose
end
# -------------------------------------------
# ...you can spice it up with sound fx
live_loop :doodle do
with_fx :slicer do
play scale(:c4, :minor_pentatonic).choose
sleep [0.125, 0.25, 0.5].choose
end
end
# -------------------------------------------
# here are some of the synths of Sonic Pi
use_synth :piano
play_chord chord(:e4, :major)
sleep 1
[:saw, :piano, :tb303, :prophet].each do |n|
use_synth n
play_chord chord(:e4, :major)
sleep 1
end
# -------------------------------------------
# some samples are included, e.g. drumloops
sample :loop_amen
# ...and you can manipulate them
sample :loop_amen, rate: 0.5
sample :loop_amen, rate: 0.5, pitch: 12
sample :loop_amen, rate: -1.0
# -------------------------------------------
# let's make that an endless drumloop
live_loop :beats do
sample :loop_amen
sleep sample_duration(:loop_amen)
end
# -------------------------------------------
# here's a drumloop with a random melody
use_bpm 75
live_loop :beats do
sample :loop_amen, beat_stretch: 2
sleep 2
end
live_loop :doodle do
use_random_seed 1234
2.times do
sync :beats
with_fx :slicer do
4.times do
play scale(:c4, :minor_pentatonic).choose
sleep [0.125, 0.25, 0.5].choose
end
end
end
end
# -------------------------------------------
# four live loops play a classic canon
use_bpm 140
melody = (
# Frère Jacques!
[:f4, 1, :g4, 1, :a4, 1, :f4, 1] * 2 +
# Dormez vous?
[:a4, 1, :bb4, 1, :c5, 2] * 2 +
# Sonnez les matines!
[:c5, 0.5, :d5, 0.5, :c5, 0.5, :bb4, 0.5, :a4, 1, :f4, 1] * 2 +
# Ding, ding dong!
[:f4, 1, :c4, 1, :f4, 2] * 2
).ring
4.times do |c|
sleep 8 * c
live_loop c.to_s.to_sym do
play melody.tick
sleep melody.tick
end
end
# -------------------------------------------
# a trippy drum-computer beat
use_bpm 120
live_loop :boom do
sync :drums
sample :bd_boom
sleep 4
end
live_loop :drums do
with_fx :wobble do
with_fx :ixi_techno do
sample :drum_cymbal_pedal
sleep 0.25
sample :drum_cymbal_pedal
sleep 0.5
sample :drum_cymbal_pedal
sleep 0.25
sample :bd_haus
sample :drum_cymbal_hard
sleep 1
sample :drum_cymbal_closed
sleep 0.5
sample :drum_cymbal_closed
sleep 0.5
sample :bd_haus
sample :drum_cymbal_closed
sleep 1
end
end
end
# -------------------------------------------
# four chords plus bass and drums
m = [
chord(:A3, :minor),
chord(:F3, :major),
chord(:C3, :major),
chord(:G3, :major)
].ring
live_loop :chords do
use_synth :beep
c = m.tick
play_pattern_timed c, 0.125, release: 1
sleep 1-3*0.125
end
live_loop :base do
sync :chords
use_synth :prophet
use_transpose -12
c = m.tick
play c[0], release: 1, amp: 0.5
sleep 1
end
live_loop :beats do
sync :chords
sample :loop_amen_full, beat_stretch: 8, amp: 0.5
sleep 8
end
live_loop :boom do
sync :chords
sample :bd_boom
sleep 4
end
@hopbit
Copy link

hopbit commented Apr 2, 2016

Hi there,

Very nice intro :) I'm just curious about one thing, line 213:

sleep 1-3*0.125

You could just use value 0.625 in this case. Is this just to show new users to SPi, that one can use math within his compositions?

Luke.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment