Skip to content

Instantly share code, notes, and snippets.

@withakay
Created October 14, 2011 10:10
Show Gist options
  • Star 14 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save withakay/1286731 to your computer and use it in GitHub Desktop.
Save withakay/1286731 to your computer and use it in GitHub Desktop.
Bjorklund algorithm in JavaScript
/*
An implementation of the Bjorklund algorithm in JavaScript
Inspired by the paper 'The Euclidean Algorithm Generates Traditional Musical Rhythms'
by Godfried Toussaint

This is a port of the original algorithm by E. Bjorklund which I
found in the paper 'The Theory of Rep-Rate Pattern Generation in the SNS Timing Systems' by
E. Bjorklund.
Jack Rutherford
*/
function bjorklund(steps, pulses) {
steps = Math.round(steps);
pulses = Math.round(pulses);
if(pulses > steps || pulses == 0 || steps == 0) {
return new Array();
}
pattern = [];
counts = [];
remainders = [];
divisor = steps - pulses;
remainders.push(pulses);
level = 0;
while(true) {
counts.push(Math.floor(divisor / remainders[level]));
remainders.push(divisor % remainders[level]);
divisor = remainders[level];
level += 1;
if (remainders[level] <= 1) {
break;
}
}
counts.push(divisor);
var r = 0;
var build = function(level) {
r++;
if (level > -1) {
for (var i=0; i < counts[level]; i++) {
build(level-1);
}
if (remainders[level] != 0) {
build(level-2);
}
} else if (level == -1) {
pattern.push(0);
} else if (level == -2) {
pattern.push(1);
}
};
build(level);
return pattern.reverse();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment