Euclidean Rhythm Generator

Been a while since I made anything in Sonic Pi so I just decided to try making a Euclidean rhythm generator. It’s working pretty well on my end, IMO.

# EUCLIDIAN SEQUENCER v0.1
# inspired by the code of computermusicdesign.com

define :euclid do |steps, pulses|
  storedRhythm = []
  bucket = 0
  steps.times do
    bucket += pulses
    if bucket >= steps
      bucket -= steps
      storedRhythm.push(1)
    else
      storedRhythm.push(0)
    end
  end
  return storedRhythm
end

define :rotateSeq do |seq, rotate|
  l = seq.length()
  output = Array.new(l)
  i = 0
  l.times do
    output[i] = seq[((i+l-rotate)%l).abs()]
    i += 1
  end
  return output
end

define :euclidSeq do |steps, pulses, rotate, seqSample, vol|
  t = 0
  seq = rotateSeq((euclid steps, pulses), rotate)
  steps.times do
    if seq[t] == 1
      sample seqSample, amp: vol
    end
    sleep 1.0/steps
    t += 1
  end
end

steps = 8

in_thread do
  loop do
    euclidSeq(steps, 3, 1, :drum_bass_hard, 1)
  end
end

in_thread do
  loop do
    euclidSeq(steps, 2, 3, :drum_cymbal_pedal, 0.6)
  end
end

in_thread do
  loop do
    euclidSeq(steps, 5, 1, :drum_tom_lo_hard, 0.4)
  end
end

in_thread do
  loop do
    euclidSeq(steps, 5, 4, :drum_tom_hi_hard, 0.3)
  end
end

7 Likes

That’s cool - you might also want to check out the spread function.

Oh shoot! Didn’t know there was already a Euclidean generator in here, though I probably should have figured :stuck_out_tongue:

1 Like

snerk … saying nothing. :slight_smile:

Eli…

1 Like

I don’t regret doing what I did. I learned a lot about the logic of Euclidean sequences and the syntax of Ruby :stuck_out_tongue:

2 Likes

thanks for the awesome information.

1 Like

thanks my issue has been fixed.