Skip to content

Commit

Permalink
added padding() method
Browse files Browse the repository at this point in the history
  • Loading branch information
tmhglnd committed Nov 13, 2021
1 parent 825c98f commit d532bc7
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 25 deletions.
18 changes: 18 additions & 0 deletions docs/transform-methods.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,24 @@ Mod.copy(['c', 'f', 'g'], 3);

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

## padding

Pad an array with zeroes (or any other value) up to the length specified. The padding value can optionally be changed and the shift argument rotates the list n-steps left or right (negative). This method is similar to `every()` except arguments are not specified in musical bars/divisions.

**arguments**
- {NumberArrray} -> Array to use every n-bars
- {Int} -> output length of array (optional, default=16)
- {Value} -> padding value for the added items (optional, default=0)
- {Number} -> shift in steps (optional, default=0)

```js
Mod.pad([3, 7, 11, 12], 9);
//=> [ 3, 7, 11, 12, 0, 0, 0, 0, 0 ]

Mod.pad(['c', 'f', 'g'], 11, '-', 4);
//=> [ '-', '-', '-', '-', 'c', 'f', 'g', '-', '-', '-', '-' ]
```

## every

Add zeroes to an array with a number sequence. The division determines the amount of values per bar. The total length equals the bars times division. This method is very useful for rhythms that must occur once in a while, but can also be use for melodic phrases. Also works with strings.
Expand Down
52 changes: 34 additions & 18 deletions src/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,12 @@ const Util = require('./utility');
function clone(a=[0], ...c){
// flatten array if multi-dimensional
if (!c.length) {
// c = [0];
return a;
} else {
c = flatten(c);
}
var arr = [];
for (var i=0; i<c.length; i++){
let arr = [];
for (let i=0; i<c.length; i++){
arr = arr.concat(a.map(v => Util.add(v, c[i])));
}
return arr;
Expand All @@ -55,8 +54,8 @@ exports.clone = clone;
//
function combine(...args){
if (!args.length){ return [0]; }
var arr = [];
for (var i=0; i<args.length; i++){
let arr = [];
for (let i=0; i<args.length; i++){
arr = arr.concat(args[i]);
}
return arr;
Expand All @@ -71,8 +70,8 @@ exports.join = combine;
// @return {Array}
//
function duplicate(a=[0], d=2){
var arr = [];
for (var i=0; i<Math.max(1,d); i++){
let arr = [];
for (let i=0; i<Math.max(1,d); i++){
arr = arr.concat(a);
}
return arr;
Expand All @@ -81,28 +80,45 @@ exports.duplicate = duplicate;
exports.copy = duplicate;
exports.dup = duplicate;

// add zeroes to an array with a rhythmic sequence
// the division determins the amount of values per bar
// pad an array with zeroes (or other values)
// the division determines the amount of values per bar
// total length = bars * div
//
// param {Array} -> Array to use every n-bars
// param {Int} -> amount of bars
// param {Int} -> amount of values per bar
// param {Int} -> amount of bars (optional, default=1)
// param {Int} -> amount of values per bar (optional, default=16)
// param {Value} -> padding argument (optional, default=0)
// param {Number} -> shift the output by n-divs (optional, default=0)
// return {Array}
//
function every(a=[0], bars=4, div=16, pad=0, shift=0){
function every(a=[0], bars=1, div=16, pad=0, shift=0){
let len = Math.floor(bars * div) - a.length;
if (len < 1 ) {
return a;
} else {
let arr = new Array(len).fill(pad);
let sft = Math.floor(shift * div);
return padding(a, len, pad, sft);
}
exports.every = every;

return rotate(a.concat(arr), Math.floor(shift*div));
// similar to every(), but instead of specifying bars/devisions
// this method allows you to specify the exact length of the array
// and the shift is not a ratio but in whole integer steps
//
// param {Array} -> Array to use every n-bars
// param {Int} -> Array length output
// param {Number} -> shift the output by n-divs (optional, default=0)
// param {Value} -> padding argument (optional, default=0)
// return {Array}
//
function padding(a=[0], length=16, pad=0, shift=0){
a = Array.isArray(a)? a : [a];
let len = length - a.length;
if (len < 1) {
return a;
}
let arr = new Array(len).fill(pad);
return rotate(a.concat(arr), shift);
}
exports.every = every;
exports.padding = padding;
exports.pad = padding;

// flatten a multidimensional array. Optionally set the depth
// for the flattening
Expand Down
17 changes: 10 additions & 7 deletions test/serialism.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -309,25 +309,28 @@ function testMod(){
test('Mod.clone([0, 5, 7], [0, 12, -12])');
test("Mod.clone(['kick', 'snare', 'hat'], ['_808', '_909'])");
test("Mod.clone(['c', ['e', 'g']], ['4', '5', '#3'])");

test("Mod.flat([1, [2, 3, [4], 5], 6])");
/*

test("Mod.combine()");
test("Mod.combine([0, 5], [[12, 19], 7])");
test("Mod.combine([0, 5], [[12, [19, 24]], 7])");
test("Mod.combine([0, 5], 12, [7, 3])");
test("Mod.combine(['c4', 'e4'], ['g4', 'f4'])");
test("Mod.combine([['c4', 'e4']], ['g4', 'f4'])");

test("Mod.duplicate()");
test("Mod.duplicate([0, 7, 12], 3)");
test("Mod.duplicate([0, [3, 7], 12], 2)");
test("Mod.duplicate(['c', 'f', 'g'], 4)");
test("Mod.every([1, 0, 1, 1, 1], 2, 8)");
test("Mod.every([1, 1, 0, 1], 4, 5, 0, -1)");
test("Mod.every([3, 0, 7, 9, 11], 2, 8, 12)");
test("Mod.every([3, [0, 7, 9], 11], 1, 12)");
test("Mod.every(['c4', 'eb4', 'g4', 'f4', 'eb4'], 2, 8, 'r')");

test("Mod.pad([3, 7, 11, 12], 9)");
test("Mod.pad(['c', 'f', 'g'], 11, '-', 4)");
/*
test("Mod.flat([1, [2, 3, [4], 5], 6])");
test("Mod.filter([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [3, 8, 10])");
test("Mod.filter([0, 1.618, 2, 3.14, 4], 3.14)");
test("Mod.filter([0, 1, 'foo', 'bar', 2, 3], ['1', 'foo'])");
Expand Down

0 comments on commit d532bc7

Please sign in to comment.