Skip to content

Instantly share code, notes, and snippets.

@omardelarosa
Last active June 11, 2018 19:00
Show Gist options
  • Save omardelarosa/930cf264a7f80f744b05a38605bb8055 to your computer and use it in GitHub Desktop.
Save omardelarosa/930cf264a7f80f744b05a38605bb8055 to your computer and use it in GitHub Desktop.
A Study in "A Study in Keith"
# Welcome to Sonic Pi v3.1
use_bpm 70
T = 4.0
# State machine utility functions
define :markov do |a, h| h[a].sample; end # Chooses the next state at random from hash
define :g do |k| get[k]; end # simplified root note in scale getter
define :s do |k, n| set k, n; end # simplified root note setter
define :mnote do |key,chain| s key, (markov (g key), chain); g key; end
set :k, 1 # Init state
# Plays a note - this is a function in order to seamlessly use MIDI instead
define :pplay do |n, d = 0.1|
use_synth :fm # Using FM for portability
play n, release: d, sustain: d # This can also be MIDI
end
# Scale
sc_root = :D2
sc_type = :major
sc = scale(sc_root, sc_type)
# Chords in scale
chords = (1..7).map {|n| chord_degree n, sc_root, sc_type }.ring
# Markov chain
K = {
1 => [7],
7 => [1, 5],
5 => [1, 6],
6 => [2],
2 => [5]
}
define :pchord do |chr, d = T|
with_fx :level, amp: 0.5 do
pplay chr[0], d
pplay chr[1] + 12, d
pplay chr[2] + 12, d
end
end
# Main loop
live_loop :ppiano do
chr = chords[mnote :k, K] # get next note state from markov chain
pchord chr # Chord (left hand)
12.times do
pplay chr[rrand(-2,2).to_i] + 24 # Melody (right hand)
sleep T/16
end
chr2 = chords[mnote :k, K] # get next note state from markov chain
pchord chr2, T/4 # Chord (left hand)
4.times do
pplay chr[rrand(-2,2).to_i] + 24 # Melody (right hand)
sleep T/16
end
end
live_loop :kick do
p = (bools 1,0,0,1, 0,0,1,0, 0,0,0,0, 1,0,0,0)
16.times do
sample :bd_fat, amp: 2.5 if p.tick
sleep T/16
end
end
live_loop :snare do
p = (bools 0,0,0,0, 1,0,0,0, 0,0,0,0, 1,0,0,0)
16.times do
sample :sn_dolf, amp: 1.0 if p.tick
sleep T/16
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment