Skip to content

Instantly share code, notes, and snippets.

@rtsho
Last active April 7, 2016 01:31
Show Gist options
  • Save rtsho/1c71f8f486c4158e7743dae2b4722ff3 to your computer and use it in GitHub Desktop.
Save rtsho/1c71f8f486c4158e7743dae2b4722ff3 to your computer and use it in GitHub Desktop.
My 1st sonic pi experiment
#sonic pi
#a piece inspired from Ambien Experiment code by Darin Wilson
#a is the array, i is the previous index. returns a new index in a, in probability close to i.
#The distance is abs(i - j)
#never mind...it just help me remember what the function does
def perlin(a, i)
#create array with probabilities as a function of distance
#the indexes of this new array represent distances from i
a2 = Array.new(a.length){|index| Math.exp(a.length - index)}
a2[0] = 0
#assign probabilities in new array based on distances
#the indexes of this array represent the indexes in the original array (a)
a3 = a.each_with_index.map { |e, index| a2[(i - index).abs] }
s = a3.inject(:+)
a3.map! {|e| e.to_f / s}
#cumulative probas
cumsum = 0
a3.map!{|e| cumsum += e}
#generate uniform random number
u = rand(1.0)
a3.index{|e| e >= u}
end
use_random_seed(0)
use_synth :hollow
with_fx :reverb, mix: 0.7 do
i1 = 0
notes1 = [:D2, :E2, :A2, :As2]
s = [4, 6, 8].choose
live_loop :note1 do
play notes1[i1], attack: s/2+1, release: s/2+1
sleep s
i1 = perlin(notes1, i1)
s = [4, 6, 8].choose
end
i2 = 0
notes2 = [:D4, :E4, :A4, :As4, :D5, :E5]
live_loop :note2 do
i2 = perlin(notes2, i2)
play notes2[i2], attack: s/2+1, release: s/2+1
sleep s
end
i3 = 0
notes3 = [:D3, :D3, :E3, :E3, :A3, :A3, :As3]
live_loop :note3 do
i3 = perlin(notes3, i3)
play notes2[i3], attack: 0.5, release: 3.75, cutoff:90, amp:0.5
sleep s
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment