diff --git a/docs/stochastic-methods.md b/docs/stochastic-methods.md index 47a0d1ff..68136457 100644 --- a/docs/stochastic-methods.md +++ b/docs/stochastic-methods.md @@ -20,6 +20,7 @@ const Rand = require('total-serialism').Stochastic; - [shuffle](#shuffle) - [choose](#choose) - [pick](#pick) +- [clave](#clave) - [expand](#expand) - [MarkovChain](#markovchain) @@ -229,6 +230,33 @@ Rand.pick(5, ['c', 'e', ['g', 'd']]); +## clave + +Generate random clave patterns. The output is a binary list that represents a rhythm, where 1's represent onsets and 0's rests. First argument sets the list length output, second argument sets the maximum gap between onsets, third argument the minimum gap. + +**arguments** +- {Int+} -> output length of rhythm (default=8) +- {Int+} -> maximum gap between onsets (default=3) +- {Int+} -> minimum gap between onsets (default=2) + +```js +Rand.clave(); +//=> [ 1, 0, 1, 0, 0, 1, 0, 1 ] +//=> █ █ █ █ + +Rand.clave(8); +//=> [ 1, 0, 0, 1, 0, 1, 0, 1 ] +//=> █ █ █ █ + +Rand.clave(16, 4); +//=> [ 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 1 ] +//=> █ █ █ █ █ █ + +Rand.clave(16, 3, 1); +//=> [ 1, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0, 1 ] +//=> █ █ ██ █ █ █ +``` + ## expand Expand an array based upon the pattern within an array. The pattern is derived from the rate in change between values by calculating the differences between every consecutive value. The newly generated values are selected randomly from the list of possible changes, but in such a way that every change occurs once in the sequence of total changes before reshuffling and selecting the next one (see the `pick` method for explanation). The resulting output starts with the input array. diff --git a/src/gen-stochastic.js b/src/gen-stochastic.js index 2cf8faae..f0baf5f9 100644 --- a/src/gen-stochastic.js +++ b/src/gen-stochastic.js @@ -158,28 +158,35 @@ exports.dice = dice; // Generate random clave patterns. Outputs a binary list as rhythm, // where 1's represent onsets and 0's represent rests. // -// @param {Int} -> output length of rhythm -// @param {Int} -> rhythmic gaps to choose from (1, 2, 3 etc) -// @param {Float} -> density distribution between -1 and 1. +// @param {Int} -> output length of rhythm (default=8) +// @param {Int} -> maximum gap between onsets (default=3) +// @param {Int} -> minimum gap between onsets (default=2) // -function clave(len=1, max=3, min=2, dist=0){ +function clave(len=8, max=3, min=2){ let arr = []; + // limit list length len = Math.max(1, len); // swap if lo > hi if (min > max){ var t=min, min=max; max=t; } + // limit lower ranges min = Math.max(1, min); - max = Math.max(min, max); - - console.log(len, min, max); + max = Math.max(min, max) + 1; let sum = 0; let rtm = []; + // randomly generate list of gap intervals while (sum < len){ - let r = Math.floor(rng() * (max + 1 - min)) + min; + r = Math.floor(rng() * (max - min)) + min; rtm.push(r); sum += r; } - console.log(rtm, Util.sum(rtm)); + // convert rhythmic "gaps" to binary pattern + rtm.forEach((g) => { + for (let i=0; i