Skip to content

Commit

Permalink
pick method can be applied to durations as well. Added a weight funct…
Browse files Browse the repository at this point in the history
…ion to provide weights for random distribution to picking
  • Loading branch information
charlieroberts committed Apr 11, 2012
1 parent c43ae0a commit b58e982
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
23 changes: 20 additions & 3 deletions js/gibber/sequence.js
Expand Up @@ -202,10 +202,27 @@ Seq.prototype = {
shouldReturn = true;

// only use duration with negative offset
if(this.offset < 0)
nextPhase += (this.durations != null) ? this.durations[this.durationCounter % this.durations.length] : this.speed;
if(this.offset < 0) {
if(this.durations != null) {
if(this.durations.pick != null) {
nextPhase += this.durations.pick();
}else{
nextPhase +=this.durations[this.durationCounter % this.durations.length]
}
}else{
nextPhase += this.speed;
}
}
}else{
nextPhase += (this.durations != null) ? this.durations[this.durationCounter % this.durations.length] : this.speed;
if(this.durations != null) {
if(this.durations.pick != null) {
nextPhase += this.durations.pick();
}else{
nextPhase += this.durations[this.durationCounter % this.durations.length]
}
}else{
nextPhase += this.speed;
}
}
// TODO: should this flip-flop between floor and ceiling instead of rounding?
nextPhase = Math.round(nextPhase);
Expand Down
26 changes: 26 additions & 0 deletions js/gibber/utilities.js
Expand Up @@ -216,6 +216,32 @@ window.rndf = window.randomf = function(min, max) {
return min + rr;
}

/* returns function that randomly picks from an array using the provided weights. Example:
a = [1,2,3,4]
a.pick = weight([.5,.25,.1,.15])
a.pick();
TODO: Figure out an algorithm(s) to automatically create weights with different schemes
*/
window.weight = function(weights) {
var w = weights;
var returnValue = this[0];
function pick() {
var total = 0;
var r = rndf();
for(var i = 0; i < w.length; i++) {
total += w[i];
if(r < total) {
returnValue = this[i];
break;
}
}
return returnValue;
}

return pick;
}

window.getSpeed = function(div) {
return window["_"+div];
};
Expand Down

0 comments on commit b58e982

Please sign in to comment.