Rhythm Generation With an Euclidian Algorithm
Posted: October 26th, 2008 | Filed under: Music, Graphics, Video, Personal | Tags: euclidian, generation, music, rhythm | 18 Comments »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
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

The Ruby on Rails addict, industrial photographer and amateur electronic music composer. In the mean time I build great web applications, contribute to OSS and help AVAAZ to save Great Barrier Reef.
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
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.
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.
I’m not very much into ruby+midi, but I know who is. Thanks for the pointer!
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;
}
Hey, wesen. Where was I wrong with the algorithm?
ok this is broken, gimme a few minutes
try it with 5,8 and compare to 1 0 11 0 11 0
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.
Right, I second this — velocity is second in the row of the most important factors. Wesen, have you tried it all in Numerology 2?
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).
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.
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;
}
[...] 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 [...]
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
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.
Have you seen Giles Bowkett’s MIDI generator? http://wiki.github.com/gilesbowkett/archaeopteryx
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.