Skip to content

Commit

Permalink
allow frequency modulation in sine() method
Browse files Browse the repository at this point in the history
  • Loading branch information
tmhglnd committed Mar 19, 2021
1 parent dd38ec3 commit 5e9d8a0
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 16 deletions.
40 changes: 34 additions & 6 deletions docs/generative-methods.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,34 +78,49 @@ Gen.spreadInclusive(5, 3, 12);
Gen.spreadInclusive(5, 12, 3);
//=> [ 12, 9, 7, 5, 3 ]

// generate an array of 9 floats between -1 - 1 (inclusive)
Gen.spreadInclusiveFloat(9, -1, 1);
//=> [ -1, -0.75, -0.5, -0.25, 0, 0.25, 0.5, 0.75, 1 ]
// generate an array of 5 floats (inclusive)
Gen.spreadInclusiveFloat(5);
//=> [ 0, 0.25, 0.5, 0.75, 1 ]
// Alternative: Gen.spreadIncF()

```

## spreadExp

Similar to spread and spreadFloat but with an optional exponent as 4th argument
Similar to spread and spreadFloat but with an optional exponent as 4th argument.

**arguments**
- {Int+} -> Length of array
- {Number} -> Low value (optional, default=0)
- {Number} -> High value (exclusive, optional, default=length)
- {Number} -> Exponent (optional, default=1)

## spreadInclusiveExp
```js
Gen.spreadExp(10, 0, 10, 2);
//=> [
// 0, 0, 0, 0, 1,
// 2, 3, 4, 6, 8
// ]
```

Similar to spreadInclusive and spreadInclusiveFloat but with an optional exponent as 4th argument
## spreadInclusiveExp

Similar to spreadInclusive and spreadInclusiveFloat but with an optional exponent as 4th argument.

**arguments**
- {Int+} -> Length of array
- {Number} -> Low value (optional)
- {Number} -> High value (inclusive, optional)
- {Number} -> Exponent (optional, default=1)

```js
Gen.spreadInclusiveExp(10, 0, 10, 2);
//=> [
// 0, 0, 0, 1, 1,
// 3, 4, 6, 7, 10
// ]
```

## fill

Fill an array with values. Arguments are in pairs. Every pair consists of `<value, amount>` The value is repeated n-amount of times in the array.
Expand Down Expand Up @@ -220,6 +235,19 @@ Gen.cosineFloat(8);
// -0.60 ┤ ╰╮ ╭╯
// -1.00 ┤ ╰────╯

// frequency modulation of the period argument with another array
Gen.sineFloat(40, Gen.sineFloat(40, 4, 1, 5));
//=> 1.00 ┤ ╭╮ ╭──╮ ╭╮ ╭╮ ╭─╮
// 0.80 ┤ │╰╮╭╯ │ ╭╮ ╭╮ ││ ││ ╭╯ │
// 0.60 ┤╭╯ ││ ╰╮││ ││ ││ ││ │ │
// 0.40 ┤│ ╰╯ │││ ││ ││ ││ │ │
// 0.20 ┤│ ││╰╮╭╯│╭╮ ││ ││ │ │╭╮ ╭╮
// 0.00 ┼╯ ││ ││ ││╰╮╭╯│ ╭╮││ │ │││ ╭╯│
// -0.20 ┤ ││ ││ ││ ││ │ ││││ │ │││ │ │
// -0.40 ┤ ││ ││ ││ ││ │ ││││ │ ╰╯╰╮ │ │
// -0.60 ┤ ││ ╰╯ ││ ││ │ ││││ │ │ │ │
// -0.80 ┤ ││ ││ ╰╯ │ │╰╯│ │ ╰─╯ │
// -1.00 ┤ ╰╯ ╰╯ ╰─╯ ╰─╯ ╰
```

<iframe src="https://editor.p5js.org/tmhglnd/embed/CFOwE1yhW" width="100%" height="250px" frameBorder="0" scrolling="no"></iframe>
12 changes: 9 additions & 3 deletions src/gen-basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,10 +181,10 @@ exports.spreadInclusiveExp = spreadInclusiveExp;
// @return {Array}
//
function fill(...args){
// when no arguments return array of 0
if (!args.length){ return [0]; }
// when arguments uneven strip last argument
if (args.length % 2){ args.pop(); }
// when no arguments return array of 0
if (!args.length){ return [0]; }

let len = args.length/2;
let arr = [];
Expand All @@ -211,6 +211,8 @@ exports.fill = fill;
function sineFloat(len=1, periods=1, lo, hi, phase=0){
if (lo === undefined){ lo = -1; hi = 1; }
else if (hi === undefined){ hi = lo, lo = 0; }
// make periods array
periods = Array.isArray(periods)? periods : [periods];
// if no range specified
// if (lo === undefined){ lo = -1; hi = 1; }
// swap if lo > hi
Expand All @@ -220,9 +222,12 @@ function sineFloat(len=1, periods=1, lo, hi, phase=0){
len = Math.max(1, len);
let arr = [];

let a = Math.PI * 2.0 * periods / len;
let twoPI = Math.PI * 2.0;
// let a = Math.PI * 2.0 * periods / len;
let p = Math.PI * phase * 2.0;
for (let i=0; i<len; i++){
// arr[i] = Math.sin(a * i + p);
let a = twoPI * periods[i % periods.length] / len;
arr[i] = Math.sin(a * i + p);
}
return Util.map(arr, -1, 1, lo, hi);
Expand Down Expand Up @@ -265,3 +270,4 @@ function cosine(len=1, periods=1, lo=12, hi, phase=0){
return arr.map(v => Math.trunc(v));
}
exports.cosine = cosine;

12 changes: 5 additions & 7 deletions test/serialism.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,17 +123,15 @@ function testGen(){
test("Gen.spreadInclusiveFloatExp(12, 0, 10, 0.5)");

// test("Gen.fill()");
// test("Gen.fill(10, 2, 15, 3, 20, 4)");
test("Gen.fill(10, 2, 15, 3, 20, 4)");
test("Gen.fill([10, 20], 2, 15, 3, 20, 4)");
test("Gen.fill([10, 20, 2, 15, 3, 20, 4])");

test("Util.plot(Gen.sineFloat(40, Gen.sineFloat(40, 4, 1, 5)), {log: false, height: 10})");

// test("Util.plot(Gen.sineFloat(16), {log: false, height: 5, data: true})");
// test("Gen.sineFloat(10, 1, -1, 1, 0.5)");
// // test("Gen.sin(8)");
// test("Util.plot(Gen.cosineFloat(16), {log: false, height: 5, data: true})");
// // test("Gen.cos(8)");

// test("Util.plot(Gen.sine(10), {log: false})");
// test("Gen.sine(12, 1, -1, 1)");
// test("Util.plot(Gen.sine(11, 4, 0, 7), {log: false})");

// test("Util.plot(Gen.cosine(7, 1.5), {log: false})");
}
Expand Down

0 comments on commit 5e9d8a0

Please sign in to comment.