@thi.ng/dsp 4.6.16 → 4.6.18

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (66) hide show
  1. package/CHANGELOG.md +1 -1
  2. package/README.md +1 -1
  3. package/add.js +24 -38
  4. package/addg.js +4 -19
  5. package/adsr.js +148 -156
  6. package/agen.js +17 -21
  7. package/allpass.js +37 -38
  8. package/alt.js +26 -22
  9. package/anti-alias.js +8 -37
  10. package/api.js +0 -1
  11. package/aproc.js +24 -30
  12. package/biquad.js +188 -184
  13. package/bounce.js +15 -16
  14. package/complex.js +4 -1
  15. package/const.js +12 -13
  16. package/convert.js +18 -64
  17. package/cosine.js +43 -48
  18. package/curve.js +12 -41
  19. package/dcblock.js +9 -10
  20. package/delay.js +100 -111
  21. package/feedback-delay.js +23 -30
  22. package/fft.js +188 -334
  23. package/filter-delay.js +23 -27
  24. package/filter-response.js +22 -33
  25. package/foldback.js +26 -35
  26. package/impulse-train.js +35 -37
  27. package/impulse.js +27 -42
  28. package/integrator.js +27 -32
  29. package/internal/ensure.js +4 -1
  30. package/internal/take.js +8 -5
  31. package/iterable.js +32 -43
  32. package/line.js +10 -25
  33. package/madd.js +25 -40
  34. package/mapg.js +77 -69
  35. package/merge.js +18 -20
  36. package/mix.js +20 -16
  37. package/mul.js +24 -38
  38. package/multiplex.js +15 -11
  39. package/onepole.js +41 -42
  40. package/osc-additive.js +25 -53
  41. package/osc-cos.js +4 -1
  42. package/osc-dsf.js +15 -52
  43. package/osc-mix.js +6 -20
  44. package/osc-parabolic.js +4 -13
  45. package/osc-rect.js +6 -8
  46. package/osc-saw.js +4 -1
  47. package/osc-sin.js +4 -1
  48. package/osc-tri.js +4 -1
  49. package/osc-wavetable.js +12 -9
  50. package/osc.js +40 -74
  51. package/package.json +13 -10
  52. package/pan.js +23 -29
  53. package/pink-noise.js +35 -57
  54. package/pipe.js +6 -4
  55. package/power.js +40 -96
  56. package/product.js +5 -4
  57. package/reciprocal.js +20 -22
  58. package/ref.js +24 -20
  59. package/serial.js +59 -60
  60. package/sincos.js +45 -61
  61. package/sum.js +5 -4
  62. package/svf.js +74 -78
  63. package/sweep.js +4 -27
  64. package/waveshaper.js +41 -60
  65. package/white-noise.js +17 -23
  66. package/window.js +67 -52
package/foldback.js CHANGED
@@ -1,38 +1,29 @@
1
1
  import { foldback as _foldback } from "@thi.ng/math/interval";
2
2
  import { AProc } from "./aproc.js";
3
- /**
4
- * Recursively folds input into `[-thresh .. +thresh]` interval and
5
- * amplifies it with `amp` (default: 1/thresh).
6
- *
7
- * @remarks
8
- * Reference:
9
- * - https://www.desmos.com/calculator/lkyf2ag3ta
10
- *
11
- * @param thresh - fold threshold
12
- * @param amp - post amplifier
13
- */
14
- export const foldback = (thresh, amp) => new Foldback(thresh, amp);
15
- export class Foldback extends AProc {
16
- _thresh;
17
- _amp;
18
- constructor(_thresh = 1, _amp = 1 / _thresh) {
19
- super(0);
20
- this._thresh = _thresh;
21
- this._amp = _amp;
22
- }
23
- next(x) {
24
- return (this._val = _foldback(this._thresh, x) * this._amp);
25
- }
26
- threshold() {
27
- return this._thresh;
28
- }
29
- setThreshold(t) {
30
- this._thresh = t;
31
- }
32
- amp() {
33
- return this._amp;
34
- }
35
- setAmp(a) {
36
- this._amp = a;
37
- }
3
+ const foldback = (thresh, amp) => new Foldback(thresh, amp);
4
+ class Foldback extends AProc {
5
+ constructor(_thresh = 1, _amp = 1 / _thresh) {
6
+ super(0);
7
+ this._thresh = _thresh;
8
+ this._amp = _amp;
9
+ }
10
+ next(x) {
11
+ return this._val = _foldback(this._thresh, x) * this._amp;
12
+ }
13
+ threshold() {
14
+ return this._thresh;
15
+ }
16
+ setThreshold(t) {
17
+ this._thresh = t;
18
+ }
19
+ amp() {
20
+ return this._amp;
21
+ }
22
+ setAmp(a) {
23
+ this._amp = a;
24
+ }
38
25
  }
26
+ export {
27
+ Foldback,
28
+ foldback
29
+ };
package/impulse-train.js CHANGED
@@ -1,39 +1,37 @@
1
1
  import { AGen } from "./agen.js";
2
- /**
3
- * https://en.wikipedia.org/wiki/Dirac_comb
4
- *
5
- * @param period -
6
- * @param start -
7
- */
8
- export const impulseTrain = (period, start) => new ImpulseTrain(1, 0, period, start);
9
- export const impulseTrainT = (on, off, period, start) => new ImpulseTrain(on, off, period, start);
10
- export const impulseTrainB = (period, start) => new ImpulseTrain(true, false, period, start);
11
- export class ImpulseTrain extends AGen {
12
- _on;
13
- _off;
14
- _period;
15
- _pos;
16
- _startpos;
17
- constructor(_on, _off, _period, _pos = 0) {
18
- super(_off);
19
- this._on = _on;
20
- this._off = _off;
21
- this._period = _period;
22
- this._pos = _pos;
23
- this._startpos = --this._pos;
24
- }
25
- copy() {
26
- return new ImpulseTrain(this._on, this._off, this._period, this._startpos + 1);
27
- }
28
- reset() {
29
- this._val = this._off;
30
- this._pos = this._startpos;
31
- return this;
32
- }
33
- next() {
34
- return (this._val =
35
- ++this._pos >= this._period
36
- ? ((this._pos = 0), this._on)
37
- : this._off);
38
- }
2
+ const impulseTrain = (period, start) => new ImpulseTrain(1, 0, period, start);
3
+ const impulseTrainT = (on, off, period, start) => new ImpulseTrain(on, off, period, start);
4
+ const impulseTrainB = (period, start) => new ImpulseTrain(true, false, period, start);
5
+ class ImpulseTrain extends AGen {
6
+ constructor(_on, _off, _period, _pos = 0) {
7
+ super(_off);
8
+ this._on = _on;
9
+ this._off = _off;
10
+ this._period = _period;
11
+ this._pos = _pos;
12
+ this._startpos = --this._pos;
13
+ }
14
+ _startpos;
15
+ copy() {
16
+ return new ImpulseTrain(
17
+ this._on,
18
+ this._off,
19
+ this._period,
20
+ this._startpos + 1
21
+ );
22
+ }
23
+ reset() {
24
+ this._val = this._off;
25
+ this._pos = this._startpos;
26
+ return this;
27
+ }
28
+ next() {
29
+ return this._val = ++this._pos >= this._period ? (this._pos = 0, this._on) : this._off;
30
+ }
39
31
  }
32
+ export {
33
+ ImpulseTrain,
34
+ impulseTrain,
35
+ impulseTrainB,
36
+ impulseTrainT
37
+ };
package/impulse.js CHANGED
@@ -1,44 +1,29 @@
1
1
  import { AGen } from "./agen.js";
2
- /**
3
- * Numeric version of {@link impulseT}, using given `on` (default: 1) as
4
- * initial value and zero for the remaining values.
5
- *
6
- * @param on -
7
- */
8
- export const impulse = (on = 1) => new Impulse(on, 0);
9
- /**
10
- * Creates a new impulse gen, producing a single `on` for the first
11
- * invocation of {@link IGen.next}, then only `off` thereafter.
12
- *
13
- * @param on - impulse value
14
- * @param off -
15
- */
16
- export const impulseT = (on, off) => new Impulse(on, off);
17
- /**
18
- * Boolean version of {@link impulseT}, using given `start` (default:
19
- * true) as initial value and its inverse for the remaining values.
20
- *
21
- * @param start -
22
- */
23
- export const impulseB = (start = true) => new Impulse(start, !start);
24
- export class Impulse extends AGen {
25
- _on;
26
- _off;
27
- constructor(_on, _off) {
28
- super(_on);
29
- this._on = _on;
30
- this._off = _off;
31
- }
32
- copy() {
33
- return new Impulse(this._on, this._off);
34
- }
35
- reset() {
36
- this._val = this._on;
37
- return this;
38
- }
39
- next() {
40
- const x = this._val;
41
- this._val = this._off;
42
- return x;
43
- }
2
+ const impulse = (on = 1) => new Impulse(on, 0);
3
+ const impulseT = (on, off) => new Impulse(on, off);
4
+ const impulseB = (start = true) => new Impulse(start, !start);
5
+ class Impulse extends AGen {
6
+ constructor(_on, _off) {
7
+ super(_on);
8
+ this._on = _on;
9
+ this._off = _off;
10
+ }
11
+ copy() {
12
+ return new Impulse(this._on, this._off);
13
+ }
14
+ reset() {
15
+ this._val = this._on;
16
+ return this;
17
+ }
18
+ next() {
19
+ const x = this._val;
20
+ this._val = this._off;
21
+ return x;
22
+ }
44
23
  }
24
+ export {
25
+ Impulse,
26
+ impulse,
27
+ impulseB,
28
+ impulseT
29
+ };
package/integrator.js CHANGED
@@ -1,34 +1,29 @@
1
1
  import { AProc } from "./aproc.js";
2
- /**
3
- * Leaky integrator.
4
- *
5
- * https://en.wikipedia.org/wiki/Leaky_integrator
6
- *
7
- * @param coeff - leak (default: 1)
8
- */
9
- export const integrator = (coeff, start) => new Integrator(coeff, start);
10
- export class Integrator extends AProc {
11
- _coeff;
12
- _start;
13
- constructor(_coeff = 1, _start = 0) {
14
- super(_start);
15
- this._coeff = _coeff;
16
- this._start = _start;
17
- }
18
- copy() {
19
- return new Integrator(this._coeff, this._start);
20
- }
21
- reset() {
22
- this._val = this._start;
23
- return this;
24
- }
25
- next(x) {
26
- return (this._val = this._val * this._coeff + x);
27
- }
28
- coeff() {
29
- return this._coeff;
30
- }
31
- setCoeff(c) {
32
- this._coeff = c;
33
- }
2
+ const integrator = (coeff, start) => new Integrator(coeff, start);
3
+ class Integrator extends AProc {
4
+ constructor(_coeff = 1, _start = 0) {
5
+ super(_start);
6
+ this._coeff = _coeff;
7
+ this._start = _start;
8
+ }
9
+ copy() {
10
+ return new Integrator(this._coeff, this._start);
11
+ }
12
+ reset() {
13
+ this._val = this._start;
14
+ return this;
15
+ }
16
+ next(x) {
17
+ return this._val = this._val * this._coeff + x;
18
+ }
19
+ coeff() {
20
+ return this._coeff;
21
+ }
22
+ setCoeff(c) {
23
+ this._coeff = c;
24
+ }
34
25
  }
26
+ export {
27
+ Integrator,
28
+ integrator
29
+ };
@@ -1,3 +1,6 @@
1
1
  import { isNumber } from "@thi.ng/checks/is-number";
2
2
  import { Const } from "../const.js";
3
- export const __ensureGenN = (x, clamp) => isNumber(x) ? new Const(clamp(x)) : x;
3
+ const __ensureGenN = (x, clamp) => isNumber(x) ? new Const(clamp(x)) : x;
4
+ export {
5
+ __ensureGenN
6
+ };
package/internal/take.js CHANGED
@@ -1,6 +1,9 @@
1
- export const __take = (src, num, out = [], idx = 0) => {
2
- for (; num-- > 0;) {
3
- out[idx++] = src.next();
4
- }
5
- return out;
1
+ const __take = (src, num, out = [], idx = 0) => {
2
+ for (; num-- > 0; ) {
3
+ out[idx++] = src.next();
4
+ }
5
+ return out;
6
+ };
7
+ export {
8
+ __take
6
9
  };
package/iterable.js CHANGED
@@ -1,46 +1,35 @@
1
1
  import { __take } from "./internal/take.js";
2
- /**
3
- * Wraps given ES6 iterable to provide full {@link IGen} implementation.
4
- *
5
- * @remarks
6
- * Calling `.next()` on this wrapped instance will always succeed, even if the
7
- * original iterable already is exhausted (in which case the last valid value
8
- * will be repeated ad infinitum).
9
- *
10
- * The `initial` value is required to satisfy `.deref()` and the case where the
11
- * iterable doesn't provide a single value.
12
- *
13
- * @param src -
14
- * @param initial -
15
- */
16
- export const iterable = (src, initial) => new $Iterable(src, initial);
17
- export class $Iterable {
18
- _iter;
19
- _val;
20
- constructor(src, initial) {
21
- this._iter = src[Symbol.iterator]();
22
- this._val = initial;
23
- }
24
- deref() {
25
- return this._val;
26
- }
27
- *[Symbol.iterator]() {
28
- while (true)
29
- yield this.next();
30
- }
31
- next() {
32
- if (this._iter) {
33
- const res = this._iter.next();
34
- if (!res.done) {
35
- this._val = res.value;
36
- }
37
- else {
38
- this._iter = null;
39
- }
40
- }
41
- return this._val;
42
- }
43
- take(num, out = [], idx = 0) {
44
- return __take(this, num, out, idx);
2
+ const iterable = (src, initial) => new $Iterable(src, initial);
3
+ class $Iterable {
4
+ _iter;
5
+ _val;
6
+ constructor(src, initial) {
7
+ this._iter = src[Symbol.iterator]();
8
+ this._val = initial;
9
+ }
10
+ deref() {
11
+ return this._val;
12
+ }
13
+ *[Symbol.iterator]() {
14
+ while (true)
15
+ yield this.next();
16
+ }
17
+ next() {
18
+ if (this._iter) {
19
+ const res = this._iter.next();
20
+ if (!res.done) {
21
+ this._val = res.value;
22
+ } else {
23
+ this._iter = null;
24
+ }
45
25
  }
26
+ return this._val;
27
+ }
28
+ take(num, out = [], idx = 0) {
29
+ return __take(this, num, out, idx);
30
+ }
46
31
  }
32
+ export {
33
+ $Iterable,
34
+ iterable
35
+ };
package/line.js CHANGED
@@ -1,27 +1,12 @@
1
1
  import { Add } from "./add.js";
2
- /**
3
- * Timebased version of {@link add}. Creates a new `Add` gen based on
4
- * given `start` (default: 0) and `end` (default: 1) positions and
5
- * tracing a line over `num` steps.
6
- *
7
- * @remarks
8
- * Unless `skipFirst` is true, the `end` value is only reached at `num +
9
- * 1` steps. The line will NOT stop at `end` but continue indefinitely
10
- * if more than `n + 1` values are requested from the generator.
11
- *
12
- * @example
13
- * ```ts
14
- * line(0, 1, 5).take(7)
15
- * // [ 0, 0.2, 0.4, 0.6, 0.8, 1, 1.2 ]
16
- * ```
17
- *
18
- * @param start - start value
19
- * @param end - end value
20
- * @param num - num steps (default: 10)
21
- * @param skipFirst - true to skip start value (default: false)
22
- * @param clampEnd - true to clamp curve at end value (default: false)
23
- */
24
- export const line = (start = 0, end = 1, num = 10, skipFirst = false, clampEnd = false) => {
25
- const dt = (end - start) / num;
26
- return new Add(dt, skipFirst ? start + dt : start, clampEnd ? end : undefined);
2
+ const line = (start = 0, end = 1, num = 10, skipFirst = false, clampEnd = false) => {
3
+ const dt = (end - start) / num;
4
+ return new Add(
5
+ dt,
6
+ skipFirst ? start + dt : start,
7
+ clampEnd ? end : void 0
8
+ );
9
+ };
10
+ export {
11
+ line
27
12
  };
package/madd.js CHANGED
@@ -1,42 +1,27 @@
1
1
  import { AGen } from "./agen.js";
2
- /**
3
- * Returns new multiply-add gen producing `y(t) = factor * y(t-1) +
4
- * offset`. If `clamp` is given, the curve will be clamped at that
5
- * value.
6
- *
7
- * @param factor - default 1
8
- * @param start - default 1
9
- * @param offset - default 0
10
- * @param clamp - optional final value
11
- */
12
- export const madd = (factor, start, offset, clamp) => new MAdd(factor, start, offset, clamp);
13
- export class MAdd extends AGen {
14
- _factor;
15
- _start;
16
- _offset;
17
- _clamp;
18
- constructor(_factor = 1, _start = 1, _offset = 0, _clamp) {
19
- super(0);
20
- this._factor = _factor;
21
- this._start = _start;
22
- this._offset = _offset;
23
- this._clamp = _clamp;
24
- this.reset();
25
- }
26
- copy() {
27
- return new MAdd(this._factor, this._start, this._offset, this._clamp);
28
- }
29
- reset() {
30
- this._val = (this._start - this._offset) / this._factor;
31
- return this;
32
- }
33
- next() {
34
- let v = this._val * this._factor + this._offset;
35
- return (this._val =
36
- this._clamp !== undefined
37
- ? this._start < this._clamp
38
- ? Math.min(v, this._clamp)
39
- : Math.max(v, this._clamp)
40
- : v);
41
- }
2
+ const madd = (factor, start, offset, clamp) => new MAdd(factor, start, offset, clamp);
3
+ class MAdd extends AGen {
4
+ constructor(_factor = 1, _start = 1, _offset = 0, _clamp) {
5
+ super(0);
6
+ this._factor = _factor;
7
+ this._start = _start;
8
+ this._offset = _offset;
9
+ this._clamp = _clamp;
10
+ this.reset();
11
+ }
12
+ copy() {
13
+ return new MAdd(this._factor, this._start, this._offset, this._clamp);
14
+ }
15
+ reset() {
16
+ this._val = (this._start - this._offset) / this._factor;
17
+ return this;
18
+ }
19
+ next() {
20
+ let v = this._val * this._factor + this._offset;
21
+ return this._val = this._clamp !== void 0 ? this._start < this._clamp ? Math.min(v, this._clamp) : Math.max(v, this._clamp) : v;
22
+ }
42
23
  }
24
+ export {
25
+ MAdd,
26
+ madd
27
+ };
package/mapg.js CHANGED
@@ -1,76 +1,84 @@
1
1
  import { illegalArity } from "@thi.ng/errors/illegal-arity";
2
2
  import { AGen } from "./agen.js";
3
- export function mapG(op, ...args) {
4
- switch (args.length) {
5
- case 2:
6
- return new MapG1(op, args[0], args[1]);
7
- case 3:
8
- return new MapG2(op, args[0], args[1], args[2]);
9
- case 4:
10
- return new MapG3(op, args[0], args[1], args[2], args[3]);
11
- case 5:
12
- return new MapG4(op, args[0], args[1], args[2], args[3], args[4]);
13
- default:
14
- illegalArity(args.length);
15
- }
3
+ function mapG(op, ...args) {
4
+ switch (args.length) {
5
+ case 2:
6
+ return new MapG1(op, args[0], args[1]);
7
+ case 3:
8
+ return new MapG2(op, args[0], args[1], args[2]);
9
+ case 4:
10
+ return new MapG3(op, args[0], args[1], args[2], args[3]);
11
+ case 5:
12
+ return new MapG4(op, args[0], args[1], args[2], args[3], args[4]);
13
+ default:
14
+ illegalArity(args.length);
15
+ }
16
16
  }
17
- export class MapG1 extends AGen {
18
- _op;
19
- _a;
20
- constructor(_op, _a, init) {
21
- super(init);
22
- this._op = _op;
23
- this._a = _a;
24
- }
25
- next() {
26
- return (this._val = this._op(this._a.next(), this._val));
27
- }
17
+ class MapG1 extends AGen {
18
+ constructor(_op, _a, init) {
19
+ super(init);
20
+ this._op = _op;
21
+ this._a = _a;
22
+ }
23
+ next() {
24
+ return this._val = this._op(this._a.next(), this._val);
25
+ }
28
26
  }
29
- export class MapG2 extends AGen {
30
- _op;
31
- _a;
32
- _b;
33
- constructor(_op, _a, _b, init) {
34
- super(init);
35
- this._op = _op;
36
- this._a = _a;
37
- this._b = _b;
38
- }
39
- next() {
40
- return (this._val = this._op(this._a.next(), this._b.next(), this._val));
41
- }
27
+ class MapG2 extends AGen {
28
+ constructor(_op, _a, _b, init) {
29
+ super(init);
30
+ this._op = _op;
31
+ this._a = _a;
32
+ this._b = _b;
33
+ }
34
+ next() {
35
+ return this._val = this._op(
36
+ this._a.next(),
37
+ this._b.next(),
38
+ this._val
39
+ );
40
+ }
42
41
  }
43
- export class MapG3 extends AGen {
44
- _op;
45
- _a;
46
- _b;
47
- _c;
48
- constructor(_op, _a, _b, _c, init) {
49
- super(init);
50
- this._op = _op;
51
- this._a = _a;
52
- this._b = _b;
53
- this._c = _c;
54
- }
55
- next() {
56
- return (this._val = this._op(this._a.next(), this._b.next(), this._c.next(), this._val));
57
- }
42
+ class MapG3 extends AGen {
43
+ constructor(_op, _a, _b, _c, init) {
44
+ super(init);
45
+ this._op = _op;
46
+ this._a = _a;
47
+ this._b = _b;
48
+ this._c = _c;
49
+ }
50
+ next() {
51
+ return this._val = this._op(
52
+ this._a.next(),
53
+ this._b.next(),
54
+ this._c.next(),
55
+ this._val
56
+ );
57
+ }
58
58
  }
59
- export class MapG4 extends AGen {
60
- _op;
61
- _a;
62
- _b;
63
- _c;
64
- _d;
65
- constructor(_op, _a, _b, _c, _d, init) {
66
- super(init);
67
- this._op = _op;
68
- this._a = _a;
69
- this._b = _b;
70
- this._c = _c;
71
- this._d = _d;
72
- }
73
- next() {
74
- return (this._val = this._op(this._a.next(), this._b.next(), this._c.next(), this._d.next(), this._val));
75
- }
59
+ class MapG4 extends AGen {
60
+ constructor(_op, _a, _b, _c, _d, init) {
61
+ super(init);
62
+ this._op = _op;
63
+ this._a = _a;
64
+ this._b = _b;
65
+ this._c = _c;
66
+ this._d = _d;
67
+ }
68
+ next() {
69
+ return this._val = this._op(
70
+ this._a.next(),
71
+ this._b.next(),
72
+ this._c.next(),
73
+ this._d.next(),
74
+ this._val
75
+ );
76
+ }
76
77
  }
78
+ export {
79
+ MapG1,
80
+ MapG2,
81
+ MapG3,
82
+ MapG4,
83
+ mapG
84
+ };