Skip to content

Commit

Permalink
added function Rand.clave()
Browse files Browse the repository at this point in the history
  • Loading branch information
tmhglnd committed Oct 5, 2021
1 parent a1d811e commit 2ad1cfd
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 13 deletions.
28 changes: 28 additions & 0 deletions docs/stochastic-methods.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const Rand = require('total-serialism').Stochastic;
- [shuffle](#shuffle)
- [choose](#choose)
- [pick](#pick)
- [clave](#clave)
- [expand](#expand)
- [MarkovChain](#markovchain)

Expand Down Expand Up @@ -229,6 +230,33 @@ Rand.pick(5, ['c', 'e', ['g', 'd']]);

<iframe src="https://editor.p5js.org/tmhglnd/embed/6QcjbpzNr" width="100%" height="250px" frameBorder="0" scrolling="no"></iframe>

## 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.
Expand Down
25 changes: 16 additions & 9 deletions src/gen-stochastic.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<g; i++){
arr.push(!i ? 1 : 0);
}
});
return arr.slice(0, len);
}
exports.clave = clave;

Expand Down
11 changes: 7 additions & 4 deletions test/serialism.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,7 @@ console.log();
// markov.clear();
// console.log(markov.table);

// fullTest();
testUtil();

// Rand.clave(8);
fullTest();

function fullTest(){
console.time('Total Time');
Expand Down Expand Up @@ -272,6 +269,12 @@ function testRand(){
test("Rand.twelveTone()");
test("Rand.twelveTone()");

test("Rand.seed(7483)");
test("Rand.clave()");
test("Rand.clave(8)");
test("Rand.clave(16, 4)");
test("Rand.clave(16, 3, 1)");

test("Rand.seed(4923)");
test("Rand.twelveTone()");

Expand Down

0 comments on commit 2ad1cfd

Please sign in to comment.