Rhythm Generation With an Euclidian Algorithm

Posted: October 26th, 2008 | Filed under: Music, Graphics, Video, Personal | Tags: , , , | 18 Comments »

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

18 Comments

  1. 1 Jeremy Voorhis said at 22:23 on November 17th, 2008:

    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. 2 Aleksey Gureiev said at 22:39 on November 17th, 2008:

    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. 3 Jeremy Voorhis said at 23:39 on November 17th, 2008:

    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. 4 Aleksey Gureiev said at 00:55 on November 18th, 2008:

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

  5. 5 wesen said at 15:12 on December 18th, 2008:

    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. 6 Aleksey Gureiev said at 15:23 on December 18th, 2008:

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

  7. 7 wesen said at 15:31 on December 18th, 2008:

    ok this is broken, gimme a few minutes :)

  8. 8 wesen said at 15:46 on December 18th, 2008:

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

  9. 9 intellijel said at 07:24 on December 30th, 2008:

    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. 10 Aleksey Gureiev said at 11:12 on December 30th, 2008:

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

  11. 11 Benny said at 01:36 on February 25th, 2009:

    I have read the pdf about Bjorklund’s algorithm. I understand the basics of this algotith, but I do not fully understand your implementation in C (I’m not a programmer, but I have some Java knowledge). I think that << is a bitshift operator, but what does the do ? Can you explain the C implementation (I want to make a java version of it because I am experimating with the java MIDI API and those eucledean rhytms sounds cewl).

  12. 12 Aleksey Gureiev said at 09:42 on February 25th, 2009:

    Benny, it’s the wrong place to post your question. It’s not my PDF and not my C-implementation. You’d better ask wesen.

  13. 13 Robin Price said at 14:38 on March 26th, 2009:

    Nice work. I found the error, your implementation of the algorithm didn’t work where there where more pulses than pauses, I did a js version that fixes this.

    function euclid(steps, pulses) {
    var r = new Array();
    if (pulses > steps) { //test for input for sanity
    post(”Error: too many pulses for steps, truncating to step number\n”);
    for (i = 0; i = pulses) { //first case more pauses than pulses
    per_pulse = Math.floor(pauses / pulses);
    remainder = pauses % pulses;
    for (i = 0; i < pulses; i++) {
    r.push(1);
    for (j = 0; j < per_pulse; j++) {
    r.push(0);
    }
    if (i < remainder) {
    r.push(0);
    }
    }
    } else { //second case more pulses than pauses
    per_pause = Math.floor( (pulses – pauses) / pauses);
    remainder = (pulses – pauses) % pauses;
    for (i = 0; i < pauses; i++) {
    r.push(1);
    r.push(0);
    for (j = 0; j < per_pause; j++) {
    r.push(1);
    }
    if (i < remainder) {
    r.push(1);
    }
    }
    }
    }
    return r;
    }

  14. 14 registeringdomainnamesismorefunthandoingrealwork.com » Euclidean algorithmic beat generator said at 03:54 on April 4th, 2009:

    [...] on the subject and checking out some other similar creations across the internets, including a vst, ruby and lisp implementation I decided this sounded interesting enough to devote some time to and [...]

  15. 15 catfingers said at 08:28 on May 5th, 2009:

    my 2cents. Yeah I thought about it more as a problem ofdistributing numbers rather than working on lists (I first saw this on Wesen’s site) – although you are given a list at the end… Here’s a literal copy of Alexey’s code in javascript:

    var steps = 16;
    var attacks = 3;
    var rests = steps – attacks;
    var per_attack =parseInt(rests/attacks);
    var rem = rests % attacks;

    //check args – attacks must steps) {
    attacks = steps;
    }

    var rhythm = new Array();
    var i = 0;
    var j = 0;

    while(i<attacks) {
    document.writeln(”attk “+i);
    rhythm[j] = 1;
    j++;
    for(var k=0;k<per_attack;k++) {
    rhythm[j]=0;
    j++;
    }
    if(i<rem) {
    rhythm[j]=0;
    j++;
    }
    i++;
    }

    I checked it a bit I think it’s ok. I’m using something like this in a Max js object so I can enter values from rotary controllers.

    Ash

  16. 16 catfingers said at 03:11 on May 6th, 2009:

    Whoops, sorry, my 2cents was worth considerably less than even that small sum. Yeah Robin’s contribution works. Mine doesn’t!!! Better testing in future.

  17. 17 Yury said at 23:05 on May 9th, 2009:

    Have you seen Giles Bowkett’s MIDI generator? http://wiki.github.com/gilesbowkett/archaeopteryx

  18. 18 Aleksey Gureiev said at 09:20 on May 10th, 2009:

    Yeah. I’m not into generative music myself, but it’s great to give the machine a chance to do something artistic every now and then. :)


Leave a Reply