Rhythm Generation With an Euclidian Algorithm


Work by El Buen Matador

The original post on this is at wesen’s where you can pick up the idea and hear some samples. In this post I simply show the Ruby implementation of the algorithm. It doesn’t work with lists, but rather does simple math to achieve the same effect.

The idea of distributing pulses is curious. It lacks one important aspect though — shifting. Currently all patterns start with “1″. Thanks wesen, for sharing this. Here comes the Ruby version.

def distribute(pulses, steps)
  pauses    = steps - pulses
  per_pulse = (pauses / pulses).to_i
  remainder = pauses % pulses

  rhythm = []
  pulses.times do |pulse|
    rhythm << 1
    per_pulse.times { rhythm << 0 }
    rhythm << 0 if pulse < remainder
  end

  return rhythm
end

# 1 0 0 1 0 0 1 0
puts distribute(3, 8).inspect

10 Responses to “Rhythm Generation With an Euclidian Algorithm”

  1. Jeremy Voorhis Says:

    Hi,

    After reading your post, I hacked out a more literal port of his lisp code and put it up here: http://github.com/jvoorhis/music.rb/tree/master/examples/euclid.rb. It’s in the examples/ directory of my music.rb library, which is very much a work in progress, but it makes it easy to try out different techniques with the algorithm.

    Best,

    Jeremy

  2. Aleksey Gureiev Says:

    Hi Jeremy!

    Thanks for the comment. I’m glad you found wesen’s work useful. You may also want to check Alphacore’s work in music generation. Sounds pretty interesting.

  3. Jeremy Voorhis Says:

    I actually stumbled upon that via a google search for midilib, but haven’t had time to read yet.

    You might want to check out the google group that Topher Cyll started at http://groups.google.com/group/ruby-midi. It’s full of people doing good things with Ruby and midi.

  4. Aleksey Gureiev Says:

    I’m not very much into ruby+midi, but I know who is. Thanks for the pointer!

  5. wesen Says:

    hey aleksey, just found out there is a slight bug in your algorithm, here is my version (in C):

    int main(int argc, char *argv[]) {
    if (argc != 3)
    return 1;
    int pulses = atoi(argv[1]);
    int steps = atoi(argv[2]);
    uint32_t rhythm = 0;

    int pauses = steps - pulses;
    int per_pulse = pauses / pulses;
    int remainder = pauses % pulses;

    printf(”per_pulse: %d, remainder: %d\n”, per_pulse, remainder);

    int cnt = 0;
    for (int pulse = 0; pulse < pulses; pulse++) {
    SET_BIT(rhythm, 0);
    rhythm <<= 1;
    cnt += 1;
    rhythm <<= (per_pulse);
    cnt += per_pulse;
    if (cnt < remainder) {
    rhythm <>= 1;

    printf(”%x\n”, rhythm);
    printb(rhythm);

    return 0;
    }

  6. Aleksey Gureiev Says:

    Hey, wesen. Where was I wrong with the algorithm?

  7. wesen Says:

    ok this is broken, gimme a few minutes :)

  8. wesen Says:

    try it with 5,8 and compare to 1 0 11 0 11 0

  9. intellijel Says:

    In my experience with polyrhythms (and I am by no means an expert) I found that the accenting was equally or more important than the pulse spacing. I have been looking at these different polyrhythm generators and I haven’t seen any that are also generating velocity levels to correspond to the different accents. For example if in 4/4 time, the 1st, 5th, 9th, and 13th 1/16th note have the hardest accent and on 3rd, 7th, 11th and 15th 1/16th notes there is a weaker accent.

    If you were playing a polyrhythm like 2 over 3 beats then one rhythm would have accents every other note and the other rhythm would have accents every third note.

  10. Aleksey Gureiev Says:

    Right, I second this — velocity is second in the row of the most important factors. Wesen, have you tried it all in Numerology 2?

Leave a Reply