@thi.ng/dsp 3.0.31 → 4.0.0
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.
- package/CHANGELOG.md +82 -69
- package/README.md +66 -57
- package/{gen/add.d.ts → add.d.ts} +4 -2
- package/{gen/add.js → add.js} +7 -2
- package/{comp/addg.d.ts → addg.d.ts} +1 -1
- package/{comp/addg.js → addg.js} +0 -0
- package/{gen/adsr.d.ts → adsr.d.ts} +1 -1
- package/{gen/adsr.js → adsr.js} +22 -2
- package/{gen/agen.d.ts → agen.d.ts} +1 -1
- package/{gen/agen.js → agen.js} +3 -4
- package/{proc/allpass.d.ts → allpass.d.ts} +0 -0
- package/{proc/allpass.js → allpass.js} +5 -1
- package/{gen/alt.d.ts → alt.d.ts} +0 -0
- package/{gen/alt.js → alt.js} +3 -1
- package/{util/anti-alias.d.ts → anti-alias.d.ts} +0 -0
- package/{util/anti-alias.js → anti-alias.js} +1 -1
- package/{proc/aproc.d.ts → aproc.d.ts} +6 -4
- package/{proc/aproc.js → aproc.js} +8 -4
- package/{proc/biquad.d.ts → biquad.d.ts} +1 -1
- package/{proc/biquad.js → biquad.js} +15 -3
- package/bounce.d.ts +13 -0
- package/bounce.js +18 -0
- package/{util/complex.d.ts → complex.d.ts} +1 -1
- package/{util/complex.js → complex.js} +1 -1
- package/{gen/const.d.ts → const.d.ts} +0 -0
- package/{gen/const.js → const.js} +0 -0
- package/{util/convert.d.ts → convert.d.ts} +0 -0
- package/{util/convert.js → convert.js} +1 -1
- package/{gen/cosine.d.ts → cosine.d.ts} +0 -0
- package/{gen/cosine.js → cosine.js} +5 -1
- package/{gen/curve.d.ts → curve.d.ts} +0 -0
- package/{gen/curve.js → curve.js} +0 -0
- package/{proc/dcblock.d.ts → dcblock.d.ts} +0 -0
- package/{proc/dcblock.js → dcblock.js} +0 -0
- package/{proc/delay.d.ts → delay.d.ts} +0 -0
- package/{proc/delay.js → delay.js} +6 -2
- package/{proc/feedback-delay.d.ts → feedback-delay.d.ts} +0 -0
- package/{proc/feedback-delay.js → feedback-delay.js} +2 -1
- package/{fft/fft.d.ts → fft.d.ts} +1 -1
- package/{fft/fft.js → fft.js} +2 -2
- package/{util/filter-response.d.ts → filter-response.d.ts} +1 -1
- package/{util/filter-response.js → filter-response.js} +3 -2
- package/{proc/foldback.d.ts → foldback.d.ts} +0 -0
- package/{proc/foldback.js → foldback.js} +3 -1
- package/{gen/impulse-train.d.ts → impulse-train.d.ts} +0 -0
- package/{gen/impulse-train.js → impulse-train.js} +5 -0
- package/{gen/impulse.d.ts → impulse.d.ts} +0 -0
- package/{gen/impulse.js → impulse.js} +2 -0
- package/index.d.ts +56 -55
- package/index.js +56 -55
- package/{proc/integrator.d.ts → integrator.d.ts} +0 -0
- package/{proc/integrator.js → integrator.js} +2 -0
- package/internal/take.d.ts +3 -0
- package/internal/take.js +6 -0
- package/{gen/iterable.d.ts → iterable.d.ts} +1 -1
- package/{gen/iterable.js → iterable.js} +4 -4
- package/{gen/line.d.ts → line.d.ts} +0 -0
- package/{gen/line.js → line.js} +0 -0
- package/{gen/madd.d.ts → madd.d.ts} +0 -0
- package/{gen/madd.js → madd.js} +4 -0
- package/{comp/mapg.d.ts → mapg.d.ts} +2 -2
- package/{comp/mapg.js → mapg.js} +16 -2
- package/{proc/mix.d.ts → mix.d.ts} +0 -0
- package/{proc/mix.js → mix.js} +2 -1
- package/{gen/mul.d.ts → mul.d.ts} +0 -0
- package/{gen/mul.js → mul.js} +3 -0
- package/multiplex.d.ts +26 -0
- package/{comp/multiplex.js → multiplex.js} +2 -1
- package/{proc/onepole.d.ts → onepole.d.ts} +1 -1
- package/{proc/onepole.js → onepole.js} +6 -1
- package/{osc/additive.d.ts → osc-additive.d.ts} +16 -2
- package/{osc/additive.js → osc-additive.js} +16 -2
- package/osc-cos.d.ts +3 -0
- package/{osc/cos.js → osc-cos.js} +1 -1
- package/{osc/dsf.d.ts → osc-dsf.d.ts} +2 -2
- package/{osc/dsf.js → osc-dsf.js} +1 -1
- package/{osc/mix.d.ts → osc-mix.d.ts} +2 -2
- package/{osc/mix.js → osc-mix.js} +1 -1
- package/{osc/parabolic.d.ts → osc-parabolic.d.ts} +2 -2
- package/{osc/parabolic.js → osc-parabolic.js} +1 -1
- package/{osc/rect.d.ts → osc-rect.d.ts} +2 -2
- package/{osc/rect.js → osc-rect.js} +1 -1
- package/osc-saw.d.ts +3 -0
- package/{osc/saw.js → osc-saw.js} +1 -1
- package/osc-sin.d.ts +3 -0
- package/{osc/sin.js → osc-sin.js} +1 -1
- package/osc-tri.d.ts +3 -0
- package/{osc/tri.js → osc-tri.js} +1 -1
- package/{osc/wavetable.d.ts → osc-wavetable.d.ts} +2 -2
- package/{osc/wavetable.js → osc-wavetable.js} +2 -1
- package/{gen/osc.d.ts → osc.d.ts} +1 -1
- package/{gen/osc.js → osc.js} +6 -2
- package/package.json +201 -33
- package/{gen/pink-noise.d.ts → pink-noise.d.ts} +1 -1
- package/{gen/pink-noise.js → pink-noise.js} +6 -1
- package/{comp/pipe.d.ts → pipe.d.ts} +8 -1
- package/{comp/pipe.js → pipe.js} +0 -0
- package/{fft/power.d.ts → power.d.ts} +1 -1
- package/{fft/power.js → power.js} +2 -2
- package/{comp/product.d.ts → product.d.ts} +1 -1
- package/{comp/product.js → product.js} +0 -0
- package/{gen/reciprocal.d.ts → reciprocal.d.ts} +0 -0
- package/{gen/reciprocal.js → reciprocal.js} +2 -0
- package/serial.d.ts +44 -0
- package/{comp/compp.js → serial.js} +20 -10
- package/{gen/sincos.d.ts → sincos.d.ts} +0 -0
- package/{gen/sincos.js → sincos.js} +6 -1
- package/{comp/sum.d.ts → sum.d.ts} +1 -1
- package/{comp/sum.js → sum.js} +0 -0
- package/{proc/svf.d.ts → svf.d.ts} +1 -1
- package/{proc/svf.js → svf.js} +11 -1
- package/{gen/sweep.d.ts → sweep.d.ts} +1 -1
- package/{gen/sweep.js → sweep.js} +1 -1
- package/{proc/waveshaper.d.ts → waveshaper.d.ts} +0 -0
- package/{proc/waveshaper.js → waveshaper.js} +5 -1
- package/{gen/white-noise.d.ts → white-noise.d.ts} +1 -1
- package/{gen/white-noise.js → white-noise.js} +3 -1
- package/{fft/window.d.ts → window.d.ts} +1 -1
- package/{fft/window.js → window.js} +2 -2
- package/comp/compp.d.ts +0 -33
- package/comp/multiplex.d.ts +0 -11
- package/lib/index.js +0 -1703
- package/lib/index.js.map +0 -1
- package/lib/index.umd.js +0 -1
- package/lib/index.umd.js.map +0 -1
- package/osc/cos.d.ts +0 -3
- package/osc/saw.d.ts +0 -3
- package/osc/sin.d.ts +0 -3
- package/osc/tri.d.ts +0 -3
package/lib/index.js
DELETED
|
@@ -1,1703 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
-
|
|
5
|
-
var errors = require('@thi.ng/errors');
|
|
6
|
-
var transducers = require('@thi.ng/transducers');
|
|
7
|
-
var math = require('@thi.ng/math');
|
|
8
|
-
var checks = require('@thi.ng/checks');
|
|
9
|
-
var random = require('@thi.ng/random');
|
|
10
|
-
|
|
11
|
-
class AGen {
|
|
12
|
-
constructor(_val) {
|
|
13
|
-
this._val = _val;
|
|
14
|
-
}
|
|
15
|
-
deref() {
|
|
16
|
-
return this._val;
|
|
17
|
-
}
|
|
18
|
-
*[Symbol.iterator]() {
|
|
19
|
-
while (true)
|
|
20
|
-
yield this.next();
|
|
21
|
-
}
|
|
22
|
-
take(num, out = [], idx = 0) {
|
|
23
|
-
for (; --num >= 0;) {
|
|
24
|
-
out[idx++] = this.next();
|
|
25
|
-
}
|
|
26
|
-
return out;
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
function mapG(op, ...args) {
|
|
31
|
-
switch (args.length) {
|
|
32
|
-
case 2:
|
|
33
|
-
return new MapG1(op, args[0], args[1]);
|
|
34
|
-
case 3:
|
|
35
|
-
return new MapG2(op, args[0], args[1], args[2]);
|
|
36
|
-
case 4:
|
|
37
|
-
return new MapG3(op, args[0], args[1], args[2], args[3]);
|
|
38
|
-
case 5:
|
|
39
|
-
return new MapG4(op, args[0], args[1], args[2], args[3], args[4]);
|
|
40
|
-
default:
|
|
41
|
-
errors.illegalArity(args.length);
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
class MapG1 extends AGen {
|
|
45
|
-
constructor(_op, _a, init) {
|
|
46
|
-
super(init);
|
|
47
|
-
this._op = _op;
|
|
48
|
-
this._a = _a;
|
|
49
|
-
}
|
|
50
|
-
next() {
|
|
51
|
-
return (this._val = this._op(this._a.next(), this._val));
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
class MapG2 extends AGen {
|
|
55
|
-
constructor(_op, _a, _b, init) {
|
|
56
|
-
super(init);
|
|
57
|
-
this._op = _op;
|
|
58
|
-
this._a = _a;
|
|
59
|
-
this._b = _b;
|
|
60
|
-
}
|
|
61
|
-
next() {
|
|
62
|
-
return (this._val = this._op(this._a.next(), this._b.next(), this._val));
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
class MapG3 extends AGen {
|
|
66
|
-
constructor(_op, _a, _b, _c, init) {
|
|
67
|
-
super(init);
|
|
68
|
-
this._op = _op;
|
|
69
|
-
this._a = _a;
|
|
70
|
-
this._b = _b;
|
|
71
|
-
this._c = _c;
|
|
72
|
-
}
|
|
73
|
-
next() {
|
|
74
|
-
return (this._val = this._op(this._a.next(), this._b.next(), this._c.next(), this._val));
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
class MapG4 extends AGen {
|
|
78
|
-
constructor(_op, _a, _b, _c, _d, init) {
|
|
79
|
-
super(init);
|
|
80
|
-
this._op = _op;
|
|
81
|
-
this._a = _a;
|
|
82
|
-
this._b = _b;
|
|
83
|
-
this._c = _c;
|
|
84
|
-
this._d = _d;
|
|
85
|
-
}
|
|
86
|
-
next() {
|
|
87
|
-
return (this._val = this._op(this._a.next(), this._b.next(), this._c.next(), this._d.next(), this._val));
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
const addG = (step, start = 0) => new MapG1((a, b) => a + b, step, start - step.deref());
|
|
92
|
-
|
|
93
|
-
class AProc {
|
|
94
|
-
constructor(_val) {
|
|
95
|
-
this._val = _val;
|
|
96
|
-
}
|
|
97
|
-
deref() {
|
|
98
|
-
return this._val;
|
|
99
|
-
}
|
|
100
|
-
xform() {
|
|
101
|
-
return transducers.map((x) => this.next(x));
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
class AProc2 {
|
|
105
|
-
constructor(_val) {
|
|
106
|
-
this._val = _val;
|
|
107
|
-
}
|
|
108
|
-
deref() {
|
|
109
|
-
return this._val;
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
function compP(...procs) {
|
|
114
|
-
const [a, b, c, d] = procs;
|
|
115
|
-
switch (procs.length) {
|
|
116
|
-
case 2:
|
|
117
|
-
return new CompP2(a, b);
|
|
118
|
-
case 3:
|
|
119
|
-
return new CompP3(a, b, c);
|
|
120
|
-
case 4:
|
|
121
|
-
return new CompP4(a, b, c, d);
|
|
122
|
-
default:
|
|
123
|
-
return new CompP(procs);
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
class CompP2 extends AProc {
|
|
127
|
-
constructor(_a, _b) {
|
|
128
|
-
super(null);
|
|
129
|
-
this._a = _a;
|
|
130
|
-
this._b = _b;
|
|
131
|
-
}
|
|
132
|
-
next(x) {
|
|
133
|
-
return (this._val = this._b.next(this._a.next(x)));
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
class CompP3 extends AProc {
|
|
137
|
-
constructor(_a, _b, _c) {
|
|
138
|
-
super(null);
|
|
139
|
-
this._a = _a;
|
|
140
|
-
this._b = _b;
|
|
141
|
-
this._c = _c;
|
|
142
|
-
}
|
|
143
|
-
next(x) {
|
|
144
|
-
return (this._val = this._c.next(this._b.next(this._a.next(x))));
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
class CompP4 extends AProc {
|
|
148
|
-
constructor(_a, _b, _c, _d) {
|
|
149
|
-
super(null);
|
|
150
|
-
this._a = _a;
|
|
151
|
-
this._b = _b;
|
|
152
|
-
this._c = _c;
|
|
153
|
-
this._d = _d;
|
|
154
|
-
}
|
|
155
|
-
next(x) {
|
|
156
|
-
return (this._val = this._d.next(this._c.next(this._b.next(this._a.next(x)))));
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
class CompP extends AProc {
|
|
160
|
-
constructor(_procs) {
|
|
161
|
-
super(null);
|
|
162
|
-
this._procs = _procs;
|
|
163
|
-
}
|
|
164
|
-
next(x) {
|
|
165
|
-
return (this._val = this._procs.reduce((x, p) => p.next(x), x));
|
|
166
|
-
}
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
function multiplex(...procs) {
|
|
170
|
-
return new Multiplex(procs);
|
|
171
|
-
}
|
|
172
|
-
class Multiplex extends AProc {
|
|
173
|
-
constructor(procs) {
|
|
174
|
-
super(procs.map((p) => p.deref()));
|
|
175
|
-
this._procs = procs;
|
|
176
|
-
}
|
|
177
|
-
next(x) {
|
|
178
|
-
return (this._val = this._procs.map((p) => p.next(x)));
|
|
179
|
-
}
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
function pipe(src, ...procs) {
|
|
183
|
-
let fn;
|
|
184
|
-
const [a, b, c, d] = procs;
|
|
185
|
-
switch (procs.length) {
|
|
186
|
-
case 1:
|
|
187
|
-
fn = (x) => a.next(x);
|
|
188
|
-
break;
|
|
189
|
-
case 2:
|
|
190
|
-
fn = (x) => b.next(a.next(x));
|
|
191
|
-
break;
|
|
192
|
-
case 3:
|
|
193
|
-
fn = (x) => c.next(b.next(a.next(x)));
|
|
194
|
-
break;
|
|
195
|
-
case 4:
|
|
196
|
-
fn = (x) => d.next(c.next(b.next(a.next(x))));
|
|
197
|
-
break;
|
|
198
|
-
default:
|
|
199
|
-
fn = (x) => procs.reduce((x, p) => p.next(x), x);
|
|
200
|
-
}
|
|
201
|
-
return new MapG1(fn, src, null);
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
function product(a, b, c) {
|
|
205
|
-
return c
|
|
206
|
-
? new MapG3((a, b, c) => a * b * c, a, b, c, 0)
|
|
207
|
-
: new MapG2((a, b) => a * b, a, b, 0);
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
function sum(a, b, c) {
|
|
211
|
-
return c
|
|
212
|
-
? new MapG3((a, b, c) => a + b + c, a, b, c, 0)
|
|
213
|
-
: new MapG2((a, b) => a + b, a, b, 0);
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
const add = (step, start, clamp) => new Add(step, start, clamp);
|
|
217
|
-
class Add extends AGen {
|
|
218
|
-
constructor(_step = 1, _start = 0, _clamp) {
|
|
219
|
-
super(0);
|
|
220
|
-
this._step = _step;
|
|
221
|
-
this._start = _start;
|
|
222
|
-
this._clamp = _clamp;
|
|
223
|
-
this.reset();
|
|
224
|
-
}
|
|
225
|
-
reset() {
|
|
226
|
-
this._val = this._start - this._step;
|
|
227
|
-
return this;
|
|
228
|
-
}
|
|
229
|
-
next() {
|
|
230
|
-
let v = this._val + this._step;
|
|
231
|
-
return (this._val =
|
|
232
|
-
this._clamp !== undefined
|
|
233
|
-
? this._start < this._clamp
|
|
234
|
-
? Math.min(v, this._clamp)
|
|
235
|
-
: Math.max(v, this._clamp)
|
|
236
|
-
: v);
|
|
237
|
-
}
|
|
238
|
-
}
|
|
239
|
-
|
|
240
|
-
const madd = (factor, start, offset, clamp) => new MAdd(factor, start, offset, clamp);
|
|
241
|
-
class MAdd extends AGen {
|
|
242
|
-
constructor(_factor = 1, _start = 1, _offset = 0, _clamp) {
|
|
243
|
-
super(0);
|
|
244
|
-
this._factor = _factor;
|
|
245
|
-
this._start = _start;
|
|
246
|
-
this._offset = _offset;
|
|
247
|
-
this._clamp = _clamp;
|
|
248
|
-
this.reset();
|
|
249
|
-
}
|
|
250
|
-
reset() {
|
|
251
|
-
this._val = (this._start - this._offset) / this._factor;
|
|
252
|
-
return this;
|
|
253
|
-
}
|
|
254
|
-
next() {
|
|
255
|
-
let v = this._val * this._factor + this._offset;
|
|
256
|
-
return (this._val =
|
|
257
|
-
this._clamp !== undefined
|
|
258
|
-
? this._start < this._clamp
|
|
259
|
-
? Math.min(v, this._clamp)
|
|
260
|
-
: Math.max(v, this._clamp)
|
|
261
|
-
: v);
|
|
262
|
-
}
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
const curve = (start, end, num, rate = 0.1, skipFirst = false, clampEnd = false) => {
|
|
266
|
-
const c = Math.exp(-Math.log((Math.abs(end - start) + rate) / rate) / num);
|
|
267
|
-
const offset = (start < end ? end + rate : end - rate) * (1 - c);
|
|
268
|
-
return new MAdd(c, skipFirst ? offset + start * c : start, offset, clampEnd ? end : undefined);
|
|
269
|
-
};
|
|
270
|
-
|
|
271
|
-
var EnvPhase;
|
|
272
|
-
(function (EnvPhase) {
|
|
273
|
-
EnvPhase[EnvPhase["ATTACK"] = 0] = "ATTACK";
|
|
274
|
-
EnvPhase[EnvPhase["DECAY"] = 1] = "DECAY";
|
|
275
|
-
EnvPhase[EnvPhase["SUSTAIN"] = 2] = "SUSTAIN";
|
|
276
|
-
EnvPhase[EnvPhase["RELEASE"] = 3] = "RELEASE";
|
|
277
|
-
EnvPhase[EnvPhase["IDLE"] = 4] = "IDLE";
|
|
278
|
-
})(EnvPhase || (EnvPhase = {}));
|
|
279
|
-
const adsr = (opts) => new ADSR(opts);
|
|
280
|
-
class ADSR extends AGen {
|
|
281
|
-
constructor(opts) {
|
|
282
|
-
super(0);
|
|
283
|
-
opts = Object.assign({ a: 0, d: 0, s: 1, r: 0, acurve: 0.1, dcurve: 0.001, slen: Infinity, gain: 1 }, opts);
|
|
284
|
-
this.setAttack(opts.a);
|
|
285
|
-
this.setDecay(opts.d);
|
|
286
|
-
this.setRelease(opts.r);
|
|
287
|
-
this.setSustain(opts.s, opts.slen);
|
|
288
|
-
this.setCurveA(opts.acurve);
|
|
289
|
-
this.setCurveD(opts.dcurve);
|
|
290
|
-
this.setGain(opts.gain);
|
|
291
|
-
this.reset();
|
|
292
|
-
}
|
|
293
|
-
reset() {
|
|
294
|
-
this._phase = 0 ;
|
|
295
|
-
this._curve = curve(0, 1, this._atime + 1, this._acurve, true);
|
|
296
|
-
this._val = 0;
|
|
297
|
-
return this;
|
|
298
|
-
}
|
|
299
|
-
release() {
|
|
300
|
-
if (this._phase < 3 ) {
|
|
301
|
-
this._phase = 3 ;
|
|
302
|
-
this._curve = curve(this._sustain, 0, this._rtime + 1, this._dcurve, true);
|
|
303
|
-
}
|
|
304
|
-
}
|
|
305
|
-
isSustained() {
|
|
306
|
-
return this._phase === 2 ;
|
|
307
|
-
}
|
|
308
|
-
isDone() {
|
|
309
|
-
return this._phase === 4 ;
|
|
310
|
-
}
|
|
311
|
-
next() {
|
|
312
|
-
let v;
|
|
313
|
-
switch (this._phase) {
|
|
314
|
-
case 4 :
|
|
315
|
-
return 0;
|
|
316
|
-
case 0 :
|
|
317
|
-
v = this._curve.next();
|
|
318
|
-
if (v >= 1) {
|
|
319
|
-
v = 1;
|
|
320
|
-
this._phase = 1 ;
|
|
321
|
-
this._curve = curve(1, this._sustain, this._dtime + 1, this._dcurve, true);
|
|
322
|
-
}
|
|
323
|
-
break;
|
|
324
|
-
case 1 :
|
|
325
|
-
v = this._curve.next();
|
|
326
|
-
if (v <= this._sustain) {
|
|
327
|
-
v = this._sustain;
|
|
328
|
-
this._phase = 2 ;
|
|
329
|
-
this._curve = add(1, 1);
|
|
330
|
-
}
|
|
331
|
-
break;
|
|
332
|
-
case 2 :
|
|
333
|
-
if (this._curve.next() >= this._speriod) {
|
|
334
|
-
this.release();
|
|
335
|
-
}
|
|
336
|
-
return this._val;
|
|
337
|
-
case 3 :
|
|
338
|
-
v = this._curve.next();
|
|
339
|
-
if (v < 0) {
|
|
340
|
-
v = 0;
|
|
341
|
-
this._phase = 4 ;
|
|
342
|
-
}
|
|
343
|
-
}
|
|
344
|
-
return (this._val = v * this._gain);
|
|
345
|
-
}
|
|
346
|
-
setAttack(steps) {
|
|
347
|
-
this._atime = Math.max(steps, 0);
|
|
348
|
-
}
|
|
349
|
-
setDecay(steps) {
|
|
350
|
-
this._dtime = Math.max(steps, 0);
|
|
351
|
-
}
|
|
352
|
-
setRelease(steps) {
|
|
353
|
-
this._rtime = Math.max(steps, 0);
|
|
354
|
-
}
|
|
355
|
-
setSustain(level, duration) {
|
|
356
|
-
this._sustain = math.clamp01(level);
|
|
357
|
-
duration !== undefined && (this._speriod = duration);
|
|
358
|
-
}
|
|
359
|
-
setCurveA(ratio) {
|
|
360
|
-
this._acurve = Math.max(ratio, 1e-9);
|
|
361
|
-
}
|
|
362
|
-
setCurveD(ratio) {
|
|
363
|
-
this._dcurve = Math.max(ratio, 1e-9);
|
|
364
|
-
}
|
|
365
|
-
setGain(gain) {
|
|
366
|
-
this._gain = gain;
|
|
367
|
-
}
|
|
368
|
-
}
|
|
369
|
-
|
|
370
|
-
const alt = (n = 1) => new Alt(n, -n);
|
|
371
|
-
const altT = (a, b) => new Alt(a, b);
|
|
372
|
-
const altB = (x = true) => new Alt(x, !x);
|
|
373
|
-
class Alt extends AGen {
|
|
374
|
-
constructor(_a, _b) {
|
|
375
|
-
super(_b);
|
|
376
|
-
this._a = _a;
|
|
377
|
-
this._b = _b;
|
|
378
|
-
this._flip = true;
|
|
379
|
-
}
|
|
380
|
-
reset() {
|
|
381
|
-
this._flip = true;
|
|
382
|
-
return this;
|
|
383
|
-
}
|
|
384
|
-
next() {
|
|
385
|
-
return (this._val = (this._flip = !this._flip) ? this._b : this._a);
|
|
386
|
-
}
|
|
387
|
-
}
|
|
388
|
-
|
|
389
|
-
const constant = (x) => new Const(x);
|
|
390
|
-
class Const extends AGen {
|
|
391
|
-
next() {
|
|
392
|
-
return this._val;
|
|
393
|
-
}
|
|
394
|
-
}
|
|
395
|
-
|
|
396
|
-
const cosine = (freq, amp) => new Cosine(freq, amp);
|
|
397
|
-
class Cosine extends AGen {
|
|
398
|
-
constructor(_freq, _amp = 1) {
|
|
399
|
-
super(0);
|
|
400
|
-
this._freq = _freq;
|
|
401
|
-
this._amp = _amp;
|
|
402
|
-
this.calcCoeffs();
|
|
403
|
-
}
|
|
404
|
-
reset() {
|
|
405
|
-
this.calcCoeffs();
|
|
406
|
-
return this;
|
|
407
|
-
}
|
|
408
|
-
next() {
|
|
409
|
-
const t = this._nxt * this._cos - this._val;
|
|
410
|
-
this._val = this._nxt;
|
|
411
|
-
this._nxt = t;
|
|
412
|
-
return this._val;
|
|
413
|
-
}
|
|
414
|
-
freq() {
|
|
415
|
-
return this._freq;
|
|
416
|
-
}
|
|
417
|
-
setFreq(freq) {
|
|
418
|
-
this._freq = freq;
|
|
419
|
-
this.calcCoeffs();
|
|
420
|
-
}
|
|
421
|
-
amp() {
|
|
422
|
-
return this._amp;
|
|
423
|
-
}
|
|
424
|
-
setAmp(amp) {
|
|
425
|
-
this._amp = amp;
|
|
426
|
-
this.calcCoeffs();
|
|
427
|
-
}
|
|
428
|
-
calcCoeffs() {
|
|
429
|
-
this._nxt = this._amp;
|
|
430
|
-
this._cos = Math.cos(this._freq * math.TAU) * 2;
|
|
431
|
-
this._val = this._cos * this._amp * 0.5;
|
|
432
|
-
}
|
|
433
|
-
}
|
|
434
|
-
|
|
435
|
-
const impulse = (on = 1) => new Impulse(on, 0);
|
|
436
|
-
const impulseT = (on, off) => new Impulse(on, off);
|
|
437
|
-
const impulseB = (start = true) => new Impulse(start, !start);
|
|
438
|
-
class Impulse extends AGen {
|
|
439
|
-
constructor(_on, _off) {
|
|
440
|
-
super(_on);
|
|
441
|
-
this._on = _on;
|
|
442
|
-
this._off = _off;
|
|
443
|
-
}
|
|
444
|
-
reset() {
|
|
445
|
-
this._val = this._on;
|
|
446
|
-
return this;
|
|
447
|
-
}
|
|
448
|
-
next() {
|
|
449
|
-
const x = this._val;
|
|
450
|
-
this._val = this._off;
|
|
451
|
-
return x;
|
|
452
|
-
}
|
|
453
|
-
}
|
|
454
|
-
|
|
455
|
-
const impulseTrain = (period, start) => new ImpulseTrain(1, 0, period, start);
|
|
456
|
-
const impulseTrainT = (on, off, period, start) => new ImpulseTrain(on, off, period, start);
|
|
457
|
-
const impulseTrainB = (period, start) => new ImpulseTrain(true, false, period, start);
|
|
458
|
-
class ImpulseTrain extends AGen {
|
|
459
|
-
constructor(_on, _off, _period, _pos = 0) {
|
|
460
|
-
super(_off);
|
|
461
|
-
this._on = _on;
|
|
462
|
-
this._off = _off;
|
|
463
|
-
this._period = _period;
|
|
464
|
-
this._pos = _pos;
|
|
465
|
-
this._startpos = --this._pos;
|
|
466
|
-
}
|
|
467
|
-
reset() {
|
|
468
|
-
this._val = this._off;
|
|
469
|
-
this._pos = this._startpos;
|
|
470
|
-
return this;
|
|
471
|
-
}
|
|
472
|
-
next() {
|
|
473
|
-
return (this._val =
|
|
474
|
-
++this._pos >= this._period
|
|
475
|
-
? ((this._pos = 0), this._on)
|
|
476
|
-
: this._off);
|
|
477
|
-
}
|
|
478
|
-
}
|
|
479
|
-
|
|
480
|
-
const iterable = (src, initial) => new $Iterable(src, initial);
|
|
481
|
-
class $Iterable {
|
|
482
|
-
constructor(src, initial) {
|
|
483
|
-
this._iter = src[Symbol.iterator]();
|
|
484
|
-
this._val = initial;
|
|
485
|
-
}
|
|
486
|
-
deref() {
|
|
487
|
-
return this._val;
|
|
488
|
-
}
|
|
489
|
-
*[Symbol.iterator]() {
|
|
490
|
-
while (true)
|
|
491
|
-
yield this.next();
|
|
492
|
-
}
|
|
493
|
-
next() {
|
|
494
|
-
if (this._iter) {
|
|
495
|
-
const res = this._iter.next();
|
|
496
|
-
if (!res.done) {
|
|
497
|
-
this._val = res.value;
|
|
498
|
-
}
|
|
499
|
-
else {
|
|
500
|
-
this._iter = null;
|
|
501
|
-
}
|
|
502
|
-
}
|
|
503
|
-
return this._val;
|
|
504
|
-
}
|
|
505
|
-
take(num, out = [], idx = 0) {
|
|
506
|
-
for (; --num >= 0;) {
|
|
507
|
-
out[idx++] = this.next();
|
|
508
|
-
}
|
|
509
|
-
return out;
|
|
510
|
-
}
|
|
511
|
-
}
|
|
512
|
-
|
|
513
|
-
const line = (start = 0, end = 1, num = 10, skipFirst = false, clampEnd = false) => {
|
|
514
|
-
const dt = (end - start) / num;
|
|
515
|
-
return new Add(dt, skipFirst ? start + dt : start, clampEnd ? end : undefined);
|
|
516
|
-
};
|
|
517
|
-
|
|
518
|
-
const mul = (factor, start, clamp) => new Mul(factor, start, clamp);
|
|
519
|
-
class Mul extends AGen {
|
|
520
|
-
constructor(_factor = 1, _start = 1, _clamp) {
|
|
521
|
-
super(0);
|
|
522
|
-
this._factor = _factor;
|
|
523
|
-
this._start = _start;
|
|
524
|
-
this._clamp = _clamp;
|
|
525
|
-
this.reset();
|
|
526
|
-
}
|
|
527
|
-
reset() {
|
|
528
|
-
this._val = this._start / this._factor;
|
|
529
|
-
return this;
|
|
530
|
-
}
|
|
531
|
-
next() {
|
|
532
|
-
let v = this._val * this._factor;
|
|
533
|
-
return (this._val =
|
|
534
|
-
this._clamp !== undefined
|
|
535
|
-
? this._start < this._clamp
|
|
536
|
-
? Math.min(v, this._clamp)
|
|
537
|
-
: Math.max(v, this._clamp)
|
|
538
|
-
: v);
|
|
539
|
-
}
|
|
540
|
-
}
|
|
541
|
-
|
|
542
|
-
const osc = (osc, freq, amp, dc) => new Osc(osc, freq, amp, dc);
|
|
543
|
-
const modOsc = (osc, freq, fmod, amod = 1) => new Osc(osc, sum(fmod, checks.isNumber(freq) ? add(freq) : freq), amod);
|
|
544
|
-
class Osc extends AGen {
|
|
545
|
-
constructor(_osc, freq, amp = 1, _dc = 0) {
|
|
546
|
-
super(0);
|
|
547
|
-
this._osc = _osc;
|
|
548
|
-
this._dc = _dc;
|
|
549
|
-
this.setFreq(freq);
|
|
550
|
-
this.setAmp(amp);
|
|
551
|
-
}
|
|
552
|
-
next() {
|
|
553
|
-
return (this._val = this._osc(this._phase.next(), 1, this._amp.next(), this._dc));
|
|
554
|
-
}
|
|
555
|
-
setFreq(freq, phase = 0) {
|
|
556
|
-
this._phase = checks.isNumber(freq) ? new Add(freq, phase) : freq;
|
|
557
|
-
}
|
|
558
|
-
setAmp(amp) {
|
|
559
|
-
this._amp = checks.isNumber(amp) ? new Const(amp) : amp;
|
|
560
|
-
}
|
|
561
|
-
}
|
|
562
|
-
|
|
563
|
-
const AMP = [3.8024, 2.9694, 2.597, 3.087, 3.4006];
|
|
564
|
-
const PROB = [0.00198, 0.0128, 0.049, 0.17, 0.682];
|
|
565
|
-
const pinkNoise = (gain, rnd, amp, prob) => new PinkNoise(gain, rnd, amp, prob);
|
|
566
|
-
class PinkNoise extends AGen {
|
|
567
|
-
constructor(_gain = 1, _rnd = random.SYSTEM, _amp = AMP, prob = PROB) {
|
|
568
|
-
super(0);
|
|
569
|
-
this._gain = _gain;
|
|
570
|
-
this._rnd = _rnd;
|
|
571
|
-
this._amp = _amp;
|
|
572
|
-
this._gain /= _amp.reduce((acc, x) => acc + x, 0);
|
|
573
|
-
this._psum = prob.reduce((acc, x, i) => (acc.push(i > 0 ? acc[i - 1] + x : x), acc), []);
|
|
574
|
-
this._bins = [0, 0, 0, 0, 0];
|
|
575
|
-
}
|
|
576
|
-
reset() {
|
|
577
|
-
this._bins.fill(0);
|
|
578
|
-
return this;
|
|
579
|
-
}
|
|
580
|
-
next() {
|
|
581
|
-
const { _bins, _rnd, _amp, _psum } = this;
|
|
582
|
-
const bin = _rnd.float();
|
|
583
|
-
for (let i = 0; i < 5; i++) {
|
|
584
|
-
if (bin <= _psum[i]) {
|
|
585
|
-
_bins[i] = _rnd.norm(_amp[i]);
|
|
586
|
-
break;
|
|
587
|
-
}
|
|
588
|
-
}
|
|
589
|
-
return (this._val =
|
|
590
|
-
this._gain *
|
|
591
|
-
(_bins[0] + _bins[1] + _bins[2] + _bins[3] + _bins[4]));
|
|
592
|
-
}
|
|
593
|
-
}
|
|
594
|
-
|
|
595
|
-
const reciprocal = (step) => new Reciprocal(step);
|
|
596
|
-
class Reciprocal extends AGen {
|
|
597
|
-
constructor(_step = 1) {
|
|
598
|
-
super(1);
|
|
599
|
-
this._step = _step;
|
|
600
|
-
this.reset();
|
|
601
|
-
}
|
|
602
|
-
reset() {
|
|
603
|
-
this._n = 1 - this._step;
|
|
604
|
-
return this;
|
|
605
|
-
}
|
|
606
|
-
next() {
|
|
607
|
-
this._n += this._step;
|
|
608
|
-
return (this._val = 1 / this._n);
|
|
609
|
-
}
|
|
610
|
-
}
|
|
611
|
-
|
|
612
|
-
class SinCos extends AGen {
|
|
613
|
-
constructor(_freq, _amp = 1) {
|
|
614
|
-
super([0, _amp]);
|
|
615
|
-
this._freq = _freq;
|
|
616
|
-
this._amp = _amp;
|
|
617
|
-
this.calcCoeffs();
|
|
618
|
-
}
|
|
619
|
-
reset() {
|
|
620
|
-
this.calcCoeffs();
|
|
621
|
-
return this;
|
|
622
|
-
}
|
|
623
|
-
next() {
|
|
624
|
-
this._val = [this._s, this._c];
|
|
625
|
-
this._s += this._f * this._c;
|
|
626
|
-
this._c -= this._f * this._s;
|
|
627
|
-
return this._val;
|
|
628
|
-
}
|
|
629
|
-
freq() {
|
|
630
|
-
return this._freq;
|
|
631
|
-
}
|
|
632
|
-
setFreq(freq) {
|
|
633
|
-
this._freq = freq;
|
|
634
|
-
this.calcCoeffs();
|
|
635
|
-
}
|
|
636
|
-
amp() {
|
|
637
|
-
return this._amp;
|
|
638
|
-
}
|
|
639
|
-
setAmp(amp) {
|
|
640
|
-
this._amp = amp;
|
|
641
|
-
this.calcCoeffs();
|
|
642
|
-
}
|
|
643
|
-
calcCoeffs() {
|
|
644
|
-
this._f = math.TAU * this._freq;
|
|
645
|
-
this._s = 0;
|
|
646
|
-
this._c = this._amp;
|
|
647
|
-
}
|
|
648
|
-
}
|
|
649
|
-
|
|
650
|
-
const sweep = (start, end, steps, rate, clamp = true) => addG(curve(start, end, steps, rate, false, clamp));
|
|
651
|
-
|
|
652
|
-
const whiteNoise = (gain, rnd) => new WhiteNoise(gain, rnd);
|
|
653
|
-
class WhiteNoise extends AGen {
|
|
654
|
-
constructor(_gain = 1, _rnd = random.SYSTEM) {
|
|
655
|
-
super(0);
|
|
656
|
-
this._gain = _gain;
|
|
657
|
-
this._rnd = _rnd;
|
|
658
|
-
}
|
|
659
|
-
reset() {
|
|
660
|
-
return this;
|
|
661
|
-
}
|
|
662
|
-
next() {
|
|
663
|
-
return (this._val = this._rnd.norm(this._gain));
|
|
664
|
-
}
|
|
665
|
-
}
|
|
666
|
-
|
|
667
|
-
const allpass = (freq) => new AllPass1(freq);
|
|
668
|
-
class AllPass1 extends AProc {
|
|
669
|
-
constructor(freq) {
|
|
670
|
-
super(0);
|
|
671
|
-
this.setFreq(freq);
|
|
672
|
-
this.reset();
|
|
673
|
-
}
|
|
674
|
-
reset() {
|
|
675
|
-
this._z1 = 0;
|
|
676
|
-
return this;
|
|
677
|
-
}
|
|
678
|
-
next(x) {
|
|
679
|
-
const { _coeff, _z1 } = this;
|
|
680
|
-
x -= _z1 * _coeff;
|
|
681
|
-
this._z1 = x;
|
|
682
|
-
return x * _coeff + _z1;
|
|
683
|
-
}
|
|
684
|
-
low(x) {
|
|
685
|
-
return (x + this.next(x)) * 0.5;
|
|
686
|
-
}
|
|
687
|
-
high(x) {
|
|
688
|
-
return (x - this.next(x)) * 0.5;
|
|
689
|
-
}
|
|
690
|
-
freq() {
|
|
691
|
-
return this._freq;
|
|
692
|
-
}
|
|
693
|
-
setFreq(freq) {
|
|
694
|
-
this._freq = math.clamp05(freq);
|
|
695
|
-
this._coeff = Math.tan(freq * math.PI - math.QUARTER_PI);
|
|
696
|
-
}
|
|
697
|
-
}
|
|
698
|
-
|
|
699
|
-
const normFreq = (f, fs) => f / fs;
|
|
700
|
-
const freqRad = (f, fs) => (f / fs) * math.TAU;
|
|
701
|
-
const freqMs = (f) => 1000 / f;
|
|
702
|
-
const radFreq = (rad, fs) => (rad / math.TAU) * fs;
|
|
703
|
-
const msFrames = (t, fs) => t * 0.001 * fs;
|
|
704
|
-
const framesMs = (frames, fs) => (frames / fs) * 1000;
|
|
705
|
-
const magDb = (x) => (20 * Math.log(x)) / Math.LN10;
|
|
706
|
-
const dbMag = (x) => 10 ** (x / 20);
|
|
707
|
-
|
|
708
|
-
const biquad = (type, fc, q, gain) => new Biquad(type, fc, q, gain);
|
|
709
|
-
const biquadLP = (fc, q) => new Biquad("lp", fc, q);
|
|
710
|
-
const biquadHP = (fc, q) => new Biquad("hp", fc, q);
|
|
711
|
-
const biquadBP = (fc, q) => new Biquad("bp", fc, q);
|
|
712
|
-
const biquadNotch = (fc, q) => new Biquad("notch", fc, q);
|
|
713
|
-
const biquadPeak = (fc, q, gain = 6) => new Biquad("peak", fc, q, gain);
|
|
714
|
-
const biquadLoShelf = (fc, gain = -6) => new Biquad("loshelf", fc, undefined, gain);
|
|
715
|
-
const biquadHiShelf = (fc, gain = -6) => new Biquad("hishelf", fc, undefined, gain);
|
|
716
|
-
class Biquad extends AProc {
|
|
717
|
-
constructor(_type, _freq, _q = math.SQRT2_2, _gain = 0) {
|
|
718
|
-
super(0);
|
|
719
|
-
this._type = _type;
|
|
720
|
-
this._freq = _freq;
|
|
721
|
-
this._q = _q;
|
|
722
|
-
this._gain = _gain;
|
|
723
|
-
this.reset();
|
|
724
|
-
this.calcCoeffs();
|
|
725
|
-
}
|
|
726
|
-
reset() {
|
|
727
|
-
this._z1 = this._z2 = this._val = 0;
|
|
728
|
-
return this;
|
|
729
|
-
}
|
|
730
|
-
next(x) {
|
|
731
|
-
const out = x * this._a0 + this._z1;
|
|
732
|
-
this._z1 = x * this._a1 + this._z2 - this._b1 * out;
|
|
733
|
-
this._z2 = x * this._a2 - this._b2 * out;
|
|
734
|
-
return (this._val = out);
|
|
735
|
-
}
|
|
736
|
-
freq() {
|
|
737
|
-
return this._freq;
|
|
738
|
-
}
|
|
739
|
-
q() {
|
|
740
|
-
return this._q;
|
|
741
|
-
}
|
|
742
|
-
gain() {
|
|
743
|
-
return this._gain;
|
|
744
|
-
}
|
|
745
|
-
set(fc, q, gain) {
|
|
746
|
-
this._freq = math.clamp05(fc);
|
|
747
|
-
this._q = q;
|
|
748
|
-
this._gain = gain;
|
|
749
|
-
this.calcCoeffs();
|
|
750
|
-
}
|
|
751
|
-
setFreq(fc) {
|
|
752
|
-
this._freq = math.clamp05(fc);
|
|
753
|
-
this.calcCoeffs();
|
|
754
|
-
}
|
|
755
|
-
setQ(q) {
|
|
756
|
-
this._q = q;
|
|
757
|
-
this.calcCoeffs();
|
|
758
|
-
}
|
|
759
|
-
setGain(g) {
|
|
760
|
-
this._gain = g;
|
|
761
|
-
this.calcCoeffs();
|
|
762
|
-
}
|
|
763
|
-
filterCoeffs() {
|
|
764
|
-
return {
|
|
765
|
-
zeroes: [this._a0, this._a1, this._a2],
|
|
766
|
-
poles: [1, this._b1, this._b2],
|
|
767
|
-
};
|
|
768
|
-
}
|
|
769
|
-
calcCoeffs() {
|
|
770
|
-
const k = Math.tan(math.PI * this._freq);
|
|
771
|
-
const k2 = k * k;
|
|
772
|
-
const k22 = 2 * (k2 - 1);
|
|
773
|
-
const kq = k / this._q;
|
|
774
|
-
const k2kqp1 = 1 + kq + k2;
|
|
775
|
-
const k2kqm1 = 1 - kq + k2;
|
|
776
|
-
const ksqrt2 = k * math.SQRT2;
|
|
777
|
-
const v = dbMag(Math.abs(this._gain));
|
|
778
|
-
const kvq = k * (v / this._q);
|
|
779
|
-
const ksqrt2v = k * Math.sqrt(2 * v);
|
|
780
|
-
let norm = 1 / k2kqp1;
|
|
781
|
-
switch (this._type) {
|
|
782
|
-
case "lp":
|
|
783
|
-
this._a0 = k2 * norm;
|
|
784
|
-
this._a1 = 2 * this._a0;
|
|
785
|
-
this._a2 = this._a0;
|
|
786
|
-
this._b1 = k22 * norm;
|
|
787
|
-
this._b2 = k2kqm1 * norm;
|
|
788
|
-
break;
|
|
789
|
-
case "hp":
|
|
790
|
-
this._a0 = norm;
|
|
791
|
-
this._a1 = -2 * this._a0;
|
|
792
|
-
this._a2 = this._a0;
|
|
793
|
-
this._b1 = k22 * norm;
|
|
794
|
-
this._b2 = k2kqm1 * norm;
|
|
795
|
-
break;
|
|
796
|
-
case "bp":
|
|
797
|
-
this._a0 = kq * norm;
|
|
798
|
-
this._a1 = 0;
|
|
799
|
-
this._a2 = -this._a0;
|
|
800
|
-
this._b1 = k22 * norm;
|
|
801
|
-
this._b2 = k2kqm1 * norm;
|
|
802
|
-
break;
|
|
803
|
-
case "notch":
|
|
804
|
-
this._a0 = (1 + k2) * norm;
|
|
805
|
-
this._a1 = k22 * norm;
|
|
806
|
-
this._a2 = this._a0;
|
|
807
|
-
this._b1 = this._a1;
|
|
808
|
-
this._b2 = k2kqm1 * norm;
|
|
809
|
-
break;
|
|
810
|
-
case "peak": {
|
|
811
|
-
const z1 = 1 + kvq + k2;
|
|
812
|
-
const z2 = 1 - kvq + k2;
|
|
813
|
-
if (this._gain >= 0) {
|
|
814
|
-
this._a0 = z1 * norm;
|
|
815
|
-
this._a1 = k22 * norm;
|
|
816
|
-
this._a2 = z2 * norm;
|
|
817
|
-
this._b1 = this._a1;
|
|
818
|
-
this._b2 = k2kqm1 * norm;
|
|
819
|
-
}
|
|
820
|
-
else {
|
|
821
|
-
norm = 1 / z1;
|
|
822
|
-
this._a0 = k2kqp1 * norm;
|
|
823
|
-
this._a1 = k22 * norm;
|
|
824
|
-
this._a2 = k2kqm1 * norm;
|
|
825
|
-
this._b1 = this._a1;
|
|
826
|
-
this._b2 = z2 * norm;
|
|
827
|
-
}
|
|
828
|
-
break;
|
|
829
|
-
}
|
|
830
|
-
case "loshelf": {
|
|
831
|
-
const z1 = 1 + ksqrt2 + k2;
|
|
832
|
-
const z2 = 1 - ksqrt2 + k2;
|
|
833
|
-
const vk2 = v * k2;
|
|
834
|
-
const y1 = 1 + ksqrt2v + vk2;
|
|
835
|
-
const y2 = 1 - ksqrt2v + vk2;
|
|
836
|
-
const vk22 = 2 * (vk2 - 1);
|
|
837
|
-
if (this._gain >= 0) {
|
|
838
|
-
norm = 1 / z1;
|
|
839
|
-
this._a0 = y1 * norm;
|
|
840
|
-
this._a1 = vk22 * norm;
|
|
841
|
-
this._a2 = y2 * norm;
|
|
842
|
-
this._b1 = k22 * norm;
|
|
843
|
-
this._b2 = z2 * norm;
|
|
844
|
-
}
|
|
845
|
-
else {
|
|
846
|
-
norm = 1 / y1;
|
|
847
|
-
this._a0 = z1 * norm;
|
|
848
|
-
this._a1 = k22 * norm;
|
|
849
|
-
this._a2 = z2 * norm;
|
|
850
|
-
this._b1 = vk22 * norm;
|
|
851
|
-
this._b2 = y2 * norm;
|
|
852
|
-
}
|
|
853
|
-
break;
|
|
854
|
-
}
|
|
855
|
-
case "hishelf": {
|
|
856
|
-
const z1 = 1 + ksqrt2 + k2;
|
|
857
|
-
const z2 = 1 - ksqrt2 + k2;
|
|
858
|
-
const y1 = v + ksqrt2v + k2;
|
|
859
|
-
const y2 = v - ksqrt2v + k2;
|
|
860
|
-
const vk2 = 2 * (k2 - v);
|
|
861
|
-
if (this._gain >= 0) {
|
|
862
|
-
norm = 1 / z1;
|
|
863
|
-
this._a0 = y1 * norm;
|
|
864
|
-
this._a1 = vk2 * norm;
|
|
865
|
-
this._a2 = y2 * norm;
|
|
866
|
-
this._b1 = k22 * norm;
|
|
867
|
-
this._b2 = z2 * norm;
|
|
868
|
-
}
|
|
869
|
-
else {
|
|
870
|
-
norm = 1 / y1;
|
|
871
|
-
this._a0 = z1 * norm;
|
|
872
|
-
this._a1 = k22 * norm;
|
|
873
|
-
this._a2 = z2 * norm;
|
|
874
|
-
this._b1 = vk2 * norm;
|
|
875
|
-
this._b2 = y2 * norm;
|
|
876
|
-
}
|
|
877
|
-
break;
|
|
878
|
-
}
|
|
879
|
-
default:
|
|
880
|
-
errors.unsupported(`invalid filter type: ${this._type}`);
|
|
881
|
-
}
|
|
882
|
-
}
|
|
883
|
-
}
|
|
884
|
-
|
|
885
|
-
const onepoleLP = (fc) => new OnePole("lp", fc);
|
|
886
|
-
const onepoleHP = (fc) => new OnePole("hp", fc);
|
|
887
|
-
class OnePole extends AProc {
|
|
888
|
-
constructor(_type, _freq) {
|
|
889
|
-
super(0);
|
|
890
|
-
this._type = _type;
|
|
891
|
-
this._freq = _freq;
|
|
892
|
-
this.setFreq(_freq);
|
|
893
|
-
}
|
|
894
|
-
clear() {
|
|
895
|
-
this._val = 0;
|
|
896
|
-
}
|
|
897
|
-
reset() {
|
|
898
|
-
this.clear();
|
|
899
|
-
return this;
|
|
900
|
-
}
|
|
901
|
-
next(x) {
|
|
902
|
-
return (this._val = x * this._a0 + this._val * this._b1);
|
|
903
|
-
}
|
|
904
|
-
setFreq(fc) {
|
|
905
|
-
this._freq = fc = math.clamp05(fc);
|
|
906
|
-
if (this._type === "lp") {
|
|
907
|
-
this._b1 = Math.exp(-math.TAU * fc);
|
|
908
|
-
this._a0 = 1 - this._b1;
|
|
909
|
-
}
|
|
910
|
-
else {
|
|
911
|
-
this._b1 = -Math.exp(-math.TAU * (0.5 - fc));
|
|
912
|
-
this._a0 = 1 + this._b1;
|
|
913
|
-
}
|
|
914
|
-
}
|
|
915
|
-
filterCoeffs() {
|
|
916
|
-
return {
|
|
917
|
-
zeroes: [this._a0],
|
|
918
|
-
poles: [1, this._type === "lp" ? this._b1 : -this._b1],
|
|
919
|
-
};
|
|
920
|
-
}
|
|
921
|
-
}
|
|
922
|
-
|
|
923
|
-
const dcBlock = (freq) => new DCBlock("lp", freq);
|
|
924
|
-
class DCBlock extends OnePole {
|
|
925
|
-
next(x) {
|
|
926
|
-
return x - super.next(x);
|
|
927
|
-
}
|
|
928
|
-
}
|
|
929
|
-
|
|
930
|
-
const delay = (n) => new Delay(n, 0);
|
|
931
|
-
const delayT = (n, off) => new Delay(n, off);
|
|
932
|
-
class Delay extends AProc {
|
|
933
|
-
constructor(n, _empty) {
|
|
934
|
-
super(checks.isFunction(_empty) ? _empty() : _empty);
|
|
935
|
-
this._empty = _empty;
|
|
936
|
-
this._wpos = n - 1;
|
|
937
|
-
this._rpos = 0;
|
|
938
|
-
this._buf = new Array(n);
|
|
939
|
-
this.clear();
|
|
940
|
-
}
|
|
941
|
-
get length() {
|
|
942
|
-
return this._buf.length;
|
|
943
|
-
}
|
|
944
|
-
clear() {
|
|
945
|
-
const { _buf, _empty } = this;
|
|
946
|
-
if (checks.isFunction(_empty)) {
|
|
947
|
-
for (let i = _buf.length; --i >= 0;) {
|
|
948
|
-
this._buf[i] = _empty();
|
|
949
|
-
}
|
|
950
|
-
}
|
|
951
|
-
else {
|
|
952
|
-
this._buf.fill(_empty);
|
|
953
|
-
}
|
|
954
|
-
}
|
|
955
|
-
reset() {
|
|
956
|
-
this.clear();
|
|
957
|
-
return this;
|
|
958
|
-
}
|
|
959
|
-
deref() {
|
|
960
|
-
return this._buf[this._rpos];
|
|
961
|
-
}
|
|
962
|
-
tap(t) {
|
|
963
|
-
return this._buf[math.wrap((t | 0) + this._wpos, 0, this._buf.length - 1)];
|
|
964
|
-
}
|
|
965
|
-
multiTap(t, out = []) {
|
|
966
|
-
for (let i = t.length; --i >= 0;) {
|
|
967
|
-
out[i] = this.tap(t[i]);
|
|
968
|
-
}
|
|
969
|
-
return out;
|
|
970
|
-
}
|
|
971
|
-
next(x) {
|
|
972
|
-
this.step();
|
|
973
|
-
this._buf[this._wpos] = x;
|
|
974
|
-
return x;
|
|
975
|
-
}
|
|
976
|
-
step() {
|
|
977
|
-
const n = this._buf.length;
|
|
978
|
-
++this._wpos >= n && (this._wpos -= n);
|
|
979
|
-
++this._rpos >= n && (this._rpos -= n);
|
|
980
|
-
}
|
|
981
|
-
}
|
|
982
|
-
|
|
983
|
-
const feedbackDelay = (n, feedback) => new FeedbackDelay(n, feedback);
|
|
984
|
-
class FeedbackDelay extends Delay {
|
|
985
|
-
constructor(n, _feedback = 0.5) {
|
|
986
|
-
super(n, 0);
|
|
987
|
-
this._feedback = _feedback;
|
|
988
|
-
this.setFeedback(_feedback);
|
|
989
|
-
}
|
|
990
|
-
next(x) {
|
|
991
|
-
return super.next(x + this._buf[this._rpos] * this._feedback);
|
|
992
|
-
}
|
|
993
|
-
feedback() {
|
|
994
|
-
return this._feedback;
|
|
995
|
-
}
|
|
996
|
-
setFeedback(feedback) {
|
|
997
|
-
this._feedback = math.clamp01(feedback);
|
|
998
|
-
}
|
|
999
|
-
}
|
|
1000
|
-
|
|
1001
|
-
const foldback = (thresh, amp) => new Foldback(thresh, amp);
|
|
1002
|
-
class Foldback extends AProc {
|
|
1003
|
-
constructor(_thresh = 1, _amp = 1 / _thresh) {
|
|
1004
|
-
super(0);
|
|
1005
|
-
this._thresh = _thresh;
|
|
1006
|
-
this._amp = _amp;
|
|
1007
|
-
}
|
|
1008
|
-
next(x) {
|
|
1009
|
-
return (this._val = math.foldback(this._thresh, x) * this._amp);
|
|
1010
|
-
}
|
|
1011
|
-
threshold() {
|
|
1012
|
-
return this._thresh;
|
|
1013
|
-
}
|
|
1014
|
-
setThreshold(t) {
|
|
1015
|
-
this._thresh = t;
|
|
1016
|
-
}
|
|
1017
|
-
amp() {
|
|
1018
|
-
return this._amp;
|
|
1019
|
-
}
|
|
1020
|
-
setAmp(a) {
|
|
1021
|
-
this._amp = a;
|
|
1022
|
-
}
|
|
1023
|
-
}
|
|
1024
|
-
|
|
1025
|
-
const integrator = (coeff, start) => new Integrator(coeff, start);
|
|
1026
|
-
class Integrator extends AProc {
|
|
1027
|
-
constructor(_coeff = 1, _start = 0) {
|
|
1028
|
-
super(_start);
|
|
1029
|
-
this._coeff = _coeff;
|
|
1030
|
-
this._start = _start;
|
|
1031
|
-
}
|
|
1032
|
-
reset() {
|
|
1033
|
-
this._val = this._start;
|
|
1034
|
-
return this;
|
|
1035
|
-
}
|
|
1036
|
-
next(x) {
|
|
1037
|
-
return (this._val = this._val * this._coeff + x);
|
|
1038
|
-
}
|
|
1039
|
-
coeff() {
|
|
1040
|
-
return this._coeff;
|
|
1041
|
-
}
|
|
1042
|
-
setCoeff(c) {
|
|
1043
|
-
this._coeff = c;
|
|
1044
|
-
}
|
|
1045
|
-
}
|
|
1046
|
-
|
|
1047
|
-
class Mix extends AProc2 {
|
|
1048
|
-
constructor(_t) {
|
|
1049
|
-
super(0);
|
|
1050
|
-
this._t = _t;
|
|
1051
|
-
}
|
|
1052
|
-
get mix() {
|
|
1053
|
-
return this._t;
|
|
1054
|
-
}
|
|
1055
|
-
set mix(x) {
|
|
1056
|
-
this._t = math.clamp01(x);
|
|
1057
|
-
}
|
|
1058
|
-
next(a, b) {
|
|
1059
|
-
return (this._val = a + (b - a) * this._t);
|
|
1060
|
-
}
|
|
1061
|
-
}
|
|
1062
|
-
|
|
1063
|
-
const svfLP = (fc, q) => new SVF("lp", fc, q);
|
|
1064
|
-
const svfHP = (fc, q) => new SVF("hp", fc, q);
|
|
1065
|
-
const svfBP = (fc, q) => new SVF("bp", fc, q);
|
|
1066
|
-
const svfNotch = (fc, q) => new SVF("notch", fc, q);
|
|
1067
|
-
const svfPeak = (fc, q) => new SVF("peak", fc, q);
|
|
1068
|
-
const svfAllpass = (fc, q) => new SVF("all", fc, q);
|
|
1069
|
-
class SVF extends AProc {
|
|
1070
|
-
constructor(_type, _freq, _q = 0.5) {
|
|
1071
|
-
super(0);
|
|
1072
|
-
this._type = _type;
|
|
1073
|
-
this._freq = _freq;
|
|
1074
|
-
this._q = _q;
|
|
1075
|
-
this.reset();
|
|
1076
|
-
this.computeCoeffs();
|
|
1077
|
-
}
|
|
1078
|
-
reset() {
|
|
1079
|
-
this._c1 = this._c2 = this._val = 0;
|
|
1080
|
-
return this;
|
|
1081
|
-
}
|
|
1082
|
-
next(x) {
|
|
1083
|
-
const { _c1, _c2 } = this;
|
|
1084
|
-
const x1 = this._a1 * _c1 + this._a2 * (x - _c2);
|
|
1085
|
-
const x2 = _c2 + this._g * x1;
|
|
1086
|
-
this._c1 = 2 * x1 - _c1;
|
|
1087
|
-
this._c2 = 2 * x2 - _c2;
|
|
1088
|
-
switch (this._type) {
|
|
1089
|
-
case "lp":
|
|
1090
|
-
return (this._val = x2);
|
|
1091
|
-
case "hp":
|
|
1092
|
-
return (this._val = x - this._k * x1 - x2);
|
|
1093
|
-
case "bp":
|
|
1094
|
-
return (this._val = x1);
|
|
1095
|
-
case "notch":
|
|
1096
|
-
return (this._val = x - this._k * x1);
|
|
1097
|
-
case "peak":
|
|
1098
|
-
return (this._val = 2 * x2 - x + this._k * x1);
|
|
1099
|
-
case "all":
|
|
1100
|
-
return (this._val = x - 2 * this._k * x1);
|
|
1101
|
-
}
|
|
1102
|
-
}
|
|
1103
|
-
set(fc, q) {
|
|
1104
|
-
this._freq = fc;
|
|
1105
|
-
this._q = q;
|
|
1106
|
-
this.computeCoeffs();
|
|
1107
|
-
}
|
|
1108
|
-
setFreq(fc) {
|
|
1109
|
-
this._freq = fc;
|
|
1110
|
-
this.computeCoeffs();
|
|
1111
|
-
}
|
|
1112
|
-
setQ(q) {
|
|
1113
|
-
this._q = q;
|
|
1114
|
-
this.computeCoeffs();
|
|
1115
|
-
}
|
|
1116
|
-
computeCoeffs() {
|
|
1117
|
-
this._freq = math.clamp05(this._freq);
|
|
1118
|
-
const g = (this._g = Math.tan(math.PI * this._freq));
|
|
1119
|
-
this._k = 2 - 2 * this._q;
|
|
1120
|
-
this._a1 = 1 / (1 + g * (g + this._k));
|
|
1121
|
-
this._a2 = g * this._a1;
|
|
1122
|
-
}
|
|
1123
|
-
}
|
|
1124
|
-
|
|
1125
|
-
const waveShaper = (thresh, amp, map) => new WaveShaper(thresh, amp, map);
|
|
1126
|
-
class WaveShaper extends AProc {
|
|
1127
|
-
constructor(_coeff = 3, amp = true, _map = waveshapeSigmoid) {
|
|
1128
|
-
super(0);
|
|
1129
|
-
this._coeff = _coeff;
|
|
1130
|
-
this._map = _map;
|
|
1131
|
-
amp === true ? this.setAutoGain() : this.setAmp(amp);
|
|
1132
|
-
}
|
|
1133
|
-
next(x) {
|
|
1134
|
-
return (this._val = this._amp * this._map(x, this._coeff));
|
|
1135
|
-
}
|
|
1136
|
-
coeff() {
|
|
1137
|
-
return this._coeff;
|
|
1138
|
-
}
|
|
1139
|
-
setCoeff(t) {
|
|
1140
|
-
this._coeff = Math.max(t, 0);
|
|
1141
|
-
this._autoGain && this.setAutoGain();
|
|
1142
|
-
}
|
|
1143
|
-
amp() {
|
|
1144
|
-
return this._amp;
|
|
1145
|
-
}
|
|
1146
|
-
setAmp(a) {
|
|
1147
|
-
this._amp = a;
|
|
1148
|
-
this._autoGain = false;
|
|
1149
|
-
}
|
|
1150
|
-
setAutoGain() {
|
|
1151
|
-
this._amp = 1 / this._map(1, this._coeff);
|
|
1152
|
-
this._autoGain = true;
|
|
1153
|
-
}
|
|
1154
|
-
}
|
|
1155
|
-
const waveshapeTan = (x, k) => Math.atan(k * x) / k;
|
|
1156
|
-
const waveshapeSigmoid = (x, k) => 2 / (1 + Math.exp(-k * x)) - 1;
|
|
1157
|
-
const waveshapeSin = (x, k) => Math.sin((math.PI / k) * x);
|
|
1158
|
-
|
|
1159
|
-
const gibbs = (n, i) => Math.cos(((i - 1) * math.HALF_PI) / n) ** 2;
|
|
1160
|
-
const fejer = (k, n) => (n - k) / n;
|
|
1161
|
-
const polyBLEP = (dt, t) => t < dt
|
|
1162
|
-
? ((t /= dt), t + t - t * t - 1)
|
|
1163
|
-
: t > 1 - dt
|
|
1164
|
-
? ((t = (t - 1) / dt), t * t + t + t + 1)
|
|
1165
|
-
: 0;
|
|
1166
|
-
|
|
1167
|
-
const sin$1 = (phase, freq, amp = 1, dc = 0) => dc + amp * Math.sin(phase * freq * math.TAU);
|
|
1168
|
-
|
|
1169
|
-
const additive = (osc, freqFn, ampFn, n) => {
|
|
1170
|
-
const fcache = [];
|
|
1171
|
-
const acache = [];
|
|
1172
|
-
for (let i = 1; i <= n; i++) {
|
|
1173
|
-
fcache.push(freqFn(i));
|
|
1174
|
-
acache.push(ampFn(i));
|
|
1175
|
-
}
|
|
1176
|
-
return (phase, freq, amp = 1, dc = 0) => {
|
|
1177
|
-
let y = 0;
|
|
1178
|
-
for (let i = 0; i < n; i++) {
|
|
1179
|
-
y += osc(phase, freq * fcache[i], acache[i]);
|
|
1180
|
-
}
|
|
1181
|
-
return dc + amp * y;
|
|
1182
|
-
};
|
|
1183
|
-
};
|
|
1184
|
-
const squareAdditive = (n = 8) => additive(sin$1, (i) => 2 * (i - 1) + 1, (i) => (1 / (2 * (i - 1) + 1)) * gibbs(n, i), n);
|
|
1185
|
-
const sawAdditive = (n = 8) => additive(sin$1, (i) => i, (i) => (1 / i) * gibbs(n, i), n);
|
|
1186
|
-
|
|
1187
|
-
const cos$1 = (phase, freq, amp = 1, dc = 0) => dc + amp * Math.cos(phase * freq * math.TAU);
|
|
1188
|
-
|
|
1189
|
-
const dsf = (phase, freq, amp = 1, dc = 0, alpha = 0.5, beta = 1) => {
|
|
1190
|
-
const aa = alpha * alpha;
|
|
1191
|
-
const a2 = 2 * alpha;
|
|
1192
|
-
phase *= math.TAU * freq;
|
|
1193
|
-
return (amp *
|
|
1194
|
-
(((1 - aa) * Math.sin(phase)) /
|
|
1195
|
-
(1 + aa - a2 * Math.cos(beta * phase))) +
|
|
1196
|
-
dc);
|
|
1197
|
-
};
|
|
1198
|
-
const dsfHOF = (alpha = 0.5, beta = 1) => {
|
|
1199
|
-
const aa = alpha * alpha;
|
|
1200
|
-
const a2 = 2 * alpha;
|
|
1201
|
-
return (phase, freq, amp = 1, dc = 0) => {
|
|
1202
|
-
phase *= math.TAU * freq;
|
|
1203
|
-
return (amp *
|
|
1204
|
-
(((1 - aa) * Math.sin(phase)) /
|
|
1205
|
-
(1 + aa - a2 * Math.cos(beta * phase))) +
|
|
1206
|
-
dc);
|
|
1207
|
-
};
|
|
1208
|
-
};
|
|
1209
|
-
|
|
1210
|
-
const mixOsc = (osc1, osc2) => (phase, freq, amp = 1, dc = 0, t = 0.5) => math.mix(osc1(phase, freq, amp, dc), osc2(phase, freq, amp, dc), t);
|
|
1211
|
-
const mixOscHOF = (osc1, osc2, mix = 0.5) => (phase, freq, amp = 1, dc = 0) => math.mix(osc1(phase, freq, amp, dc), osc2(phase, freq, amp, dc), mix);
|
|
1212
|
-
|
|
1213
|
-
const parabolic = (phase, freq, amp = 1, dc = 0) => dc + amp * (8 * (math.fract(phase * freq) - 0.5) ** 2 - 1);
|
|
1214
|
-
|
|
1215
|
-
const rect = (phase, freq, amp = 1, dc = 0, duty = 0.5) => dc + amp * (math.fract(phase * freq) < duty ? 1 : -1);
|
|
1216
|
-
const rectHOF = (duty = 0.5) => (phase, freq, amp, dc) => rect(phase, freq, amp, dc, duty);
|
|
1217
|
-
|
|
1218
|
-
const saw = (phase, freq, amp = 1, dc = 0) => dc + amp * (1 - 2 * math.fract(phase * freq));
|
|
1219
|
-
|
|
1220
|
-
const tri = (phase, freq, amp = 1, dc = 0) => dc + amp * (Math.abs(math.fract(phase * freq) * 4 - 2) - 1);
|
|
1221
|
-
|
|
1222
|
-
const wavetable = (table, interpolate = math.mix) => {
|
|
1223
|
-
const n = table.length;
|
|
1224
|
-
const n1 = n - 1;
|
|
1225
|
-
return (phase, freq, amp = 1, dc = 0) => {
|
|
1226
|
-
phase = math.fract(phase * freq) * n;
|
|
1227
|
-
let i = phase | 0;
|
|
1228
|
-
let j = i < n1 ? i + 1 : 0;
|
|
1229
|
-
return dc + amp * interpolate(table[i], table[j], phase - i);
|
|
1230
|
-
};
|
|
1231
|
-
};
|
|
1232
|
-
|
|
1233
|
-
const isComplex = (buf) => !checks.isNumber(buf[0]);
|
|
1234
|
-
|
|
1235
|
-
const integralT = (window) => {
|
|
1236
|
-
let sum = 0;
|
|
1237
|
-
for (let i = window.length; --i >= 0;) {
|
|
1238
|
-
sum += window[i];
|
|
1239
|
-
}
|
|
1240
|
-
return sum;
|
|
1241
|
-
};
|
|
1242
|
-
const integralTSquared = (window) => {
|
|
1243
|
-
let sum = 0;
|
|
1244
|
-
for (let i = window.length; --i >= 0;) {
|
|
1245
|
-
sum += window[i] ** 2;
|
|
1246
|
-
}
|
|
1247
|
-
return sum;
|
|
1248
|
-
};
|
|
1249
|
-
const integralF = ([real, img]) => {
|
|
1250
|
-
let sum = 0;
|
|
1251
|
-
for (let i = real.length; --i >= 0;) {
|
|
1252
|
-
sum += Math.hypot(real[i], img[i]);
|
|
1253
|
-
}
|
|
1254
|
-
return sum;
|
|
1255
|
-
};
|
|
1256
|
-
const integralFSquared = ([real, img]) => {
|
|
1257
|
-
let sum = 0;
|
|
1258
|
-
for (let i = real.length; --i >= 0;) {
|
|
1259
|
-
sum += real[i] ** 2 + img[i] ** 2;
|
|
1260
|
-
}
|
|
1261
|
-
return sum;
|
|
1262
|
-
};
|
|
1263
|
-
const powerScale = (scale, base = 1) => checks.isNumber(scale) ? scale : base / integralT(scale);
|
|
1264
|
-
const invPowerScale = (scale, base = 1) => checks.isNumber(scale) ? scale : integralT(scale) / base;
|
|
1265
|
-
const powerSumSquared = (window) => isComplex(window)
|
|
1266
|
-
? integralFSquared(window) / window[0].length
|
|
1267
|
-
: integralTSquared(window);
|
|
1268
|
-
const powerMeanSquared = (window) => powerSumSquared(window) /
|
|
1269
|
-
(isComplex(window) ? window[0].length : window.length);
|
|
1270
|
-
const powerTimeIntegral = (window, fs) => (isComplex(window) ? integralFSquared(window) : integralTSquared(window)) /
|
|
1271
|
-
fs;
|
|
1272
|
-
|
|
1273
|
-
const PI4 = 4 * math.PI;
|
|
1274
|
-
const PI6 = 6 * math.PI;
|
|
1275
|
-
const sin = Math.sin;
|
|
1276
|
-
const cos = Math.cos;
|
|
1277
|
-
const window = (fn, lenOfBuf) => {
|
|
1278
|
-
const buf = checks.isNumber(lenOfBuf) ? new Float64Array(lenOfBuf) : lenOfBuf;
|
|
1279
|
-
const n = buf.length - 1;
|
|
1280
|
-
for (let i = 0; i <= n; i++) {
|
|
1281
|
-
buf[i] = fn(i, n);
|
|
1282
|
-
}
|
|
1283
|
-
return buf;
|
|
1284
|
-
};
|
|
1285
|
-
const applyWindow = (signal, window, out = signal) => {
|
|
1286
|
-
for (let i = signal.length; --i >= 0;) {
|
|
1287
|
-
out[i] = signal[i] * window[i];
|
|
1288
|
-
}
|
|
1289
|
-
return out;
|
|
1290
|
-
};
|
|
1291
|
-
const windowRect = () => 1;
|
|
1292
|
-
const windowBartlett = (i, n) => 1 - Math.abs((i - n / 2) / (n / 2));
|
|
1293
|
-
const windowWelch = (i, n) => 1 - ((i - n / 2) / (n / 2)) ** 2;
|
|
1294
|
-
const windowSin = (i, n) => sin((math.PI * i) / n);
|
|
1295
|
-
const windowSinPow = (k) => (i, n) => sin((math.PI * i) / n) ** k;
|
|
1296
|
-
const windowLanczos = (i, n) => {
|
|
1297
|
-
i = math.PI * ((2 * i) / n - 1);
|
|
1298
|
-
return sin(i) / i;
|
|
1299
|
-
};
|
|
1300
|
-
const windowCosSum = (k) => {
|
|
1301
|
-
let ik = 1 - k;
|
|
1302
|
-
return (i, n) => k - ik * cos((math.TAU * i) / n);
|
|
1303
|
-
};
|
|
1304
|
-
const windowCosSum3 = (k1, k2, k3) => (i, n) => {
|
|
1305
|
-
i /= n;
|
|
1306
|
-
return k1 + k2 * cos(math.TAU * i) + k3 * cos(PI4 * i);
|
|
1307
|
-
};
|
|
1308
|
-
const windowCosSum4 = (k1, k2, k3, k4) => (i, n) => {
|
|
1309
|
-
i /= n;
|
|
1310
|
-
return k1 + k2 * cos(math.TAU * i) + k3 * cos(PI4 * i) + k4 * cos(PI6 * i);
|
|
1311
|
-
};
|
|
1312
|
-
const windowHann = windowCosSum(0.5);
|
|
1313
|
-
const windowHamming = windowCosSum(0.53836);
|
|
1314
|
-
const windowBlackman = windowCosSum3(0.42, -0.5, 0.08);
|
|
1315
|
-
const windowBlackmanHarris = windowCosSum4(0.35875, -0.48829, 0.14128, 0.01168);
|
|
1316
|
-
const windowNuttall = windowCosSum4(0.355768, -0.487396, 0.144232, -0.012604);
|
|
1317
|
-
const windowBlackmanNuttall = windowCosSum4(0.3635819, -0.4891775, 0.1365995, -0.0106411);
|
|
1318
|
-
const windowGauss = (a = 0.4) => (i, n) => {
|
|
1319
|
-
n /= 2;
|
|
1320
|
-
return Math.exp(-0.5 * ((i - n) / (a * n)) ** 2);
|
|
1321
|
-
};
|
|
1322
|
-
|
|
1323
|
-
const complexArray = (n) => [
|
|
1324
|
-
new Float64Array(n),
|
|
1325
|
-
new Float64Array(n),
|
|
1326
|
-
];
|
|
1327
|
-
const copyComplex = (complex) => [
|
|
1328
|
-
complex[0].slice(),
|
|
1329
|
-
complex[1].slice(),
|
|
1330
|
-
];
|
|
1331
|
-
function conjugate(src, isImg = true) {
|
|
1332
|
-
if (isComplex(src)) {
|
|
1333
|
-
const n = src[0].length;
|
|
1334
|
-
const res = complexArray(n * 2);
|
|
1335
|
-
const [sreal, simg] = src;
|
|
1336
|
-
const [dreal, dimg] = res;
|
|
1337
|
-
dreal.set(sreal);
|
|
1338
|
-
dimg.set(simg);
|
|
1339
|
-
for (let i = 1, j = n * 2 - 1; i < n; i++, j--) {
|
|
1340
|
-
dreal[j] = sreal[i];
|
|
1341
|
-
dimg[j] = -simg[i];
|
|
1342
|
-
}
|
|
1343
|
-
return res;
|
|
1344
|
-
}
|
|
1345
|
-
else {
|
|
1346
|
-
const n = src.length;
|
|
1347
|
-
const dest = new Float64Array(n * 2);
|
|
1348
|
-
dest.set(src);
|
|
1349
|
-
for (let i = 1, j = n * 2 - 1; i < n; i++, j--) {
|
|
1350
|
-
dest[j] = isImg ? -src[i] : src[i];
|
|
1351
|
-
}
|
|
1352
|
-
return dest;
|
|
1353
|
-
}
|
|
1354
|
-
}
|
|
1355
|
-
const swapR = (real, n) => {
|
|
1356
|
-
const n2 = n >> 1;
|
|
1357
|
-
let ii;
|
|
1358
|
-
let jj;
|
|
1359
|
-
let k;
|
|
1360
|
-
let t;
|
|
1361
|
-
for (let i = 1, j = 1; i < n; i++) {
|
|
1362
|
-
if (i < j) {
|
|
1363
|
-
ii = i - 1;
|
|
1364
|
-
jj = j - 1;
|
|
1365
|
-
t = real[jj];
|
|
1366
|
-
real[jj] = real[ii];
|
|
1367
|
-
real[ii] = t;
|
|
1368
|
-
}
|
|
1369
|
-
k = n2;
|
|
1370
|
-
while (k < j) {
|
|
1371
|
-
j -= k;
|
|
1372
|
-
k >>= 1;
|
|
1373
|
-
}
|
|
1374
|
-
j += k;
|
|
1375
|
-
}
|
|
1376
|
-
};
|
|
1377
|
-
const swapRI = (real, img, n) => {
|
|
1378
|
-
const n2 = n >> 1;
|
|
1379
|
-
let ii;
|
|
1380
|
-
let jj;
|
|
1381
|
-
let k;
|
|
1382
|
-
let t;
|
|
1383
|
-
for (let i = 1, j = 1; i < n; i++) {
|
|
1384
|
-
if (i < j) {
|
|
1385
|
-
ii = i - 1;
|
|
1386
|
-
jj = j - 1;
|
|
1387
|
-
t = real[jj];
|
|
1388
|
-
real[jj] = real[ii];
|
|
1389
|
-
real[ii] = t;
|
|
1390
|
-
t = img[jj];
|
|
1391
|
-
img[jj] = img[ii];
|
|
1392
|
-
img[ii] = t;
|
|
1393
|
-
}
|
|
1394
|
-
k = n2;
|
|
1395
|
-
while (k < j) {
|
|
1396
|
-
j -= k;
|
|
1397
|
-
k >>= 1;
|
|
1398
|
-
}
|
|
1399
|
-
j += k;
|
|
1400
|
-
}
|
|
1401
|
-
};
|
|
1402
|
-
const transform = (real, img, n) => {
|
|
1403
|
-
let step = 1;
|
|
1404
|
-
let prevStep;
|
|
1405
|
-
let i, j, ii, ip;
|
|
1406
|
-
let tr, ti;
|
|
1407
|
-
let ur, ui;
|
|
1408
|
-
let wr, wi;
|
|
1409
|
-
let t;
|
|
1410
|
-
for (let b = Math.ceil(Math.log2(n)); --b >= 0;) {
|
|
1411
|
-
prevStep = step;
|
|
1412
|
-
step <<= 1;
|
|
1413
|
-
ur = 1;
|
|
1414
|
-
ui = 0;
|
|
1415
|
-
t = Math.PI / prevStep;
|
|
1416
|
-
wr = Math.cos(t);
|
|
1417
|
-
wi = -Math.sin(t);
|
|
1418
|
-
for (j = 1; j <= prevStep; j++) {
|
|
1419
|
-
for (i = j; i <= n; i += step) {
|
|
1420
|
-
ip = i + prevStep - 1;
|
|
1421
|
-
ii = i - 1;
|
|
1422
|
-
tr = real[ip] * ur - img[ip] * ui;
|
|
1423
|
-
ti = real[ip] * ui + img[ip] * ur;
|
|
1424
|
-
real[ip] = real[ii] - tr;
|
|
1425
|
-
img[ip] = img[ii] - ti;
|
|
1426
|
-
real[ii] += tr;
|
|
1427
|
-
img[ii] += ti;
|
|
1428
|
-
}
|
|
1429
|
-
t = ur;
|
|
1430
|
-
ur = t * wr - ui * wi;
|
|
1431
|
-
ui = t * wi + ui * wr;
|
|
1432
|
-
}
|
|
1433
|
-
}
|
|
1434
|
-
};
|
|
1435
|
-
const fft = (complex, window) => {
|
|
1436
|
-
let real, img;
|
|
1437
|
-
if (isComplex(complex)) {
|
|
1438
|
-
real = complex[0];
|
|
1439
|
-
img = complex[1];
|
|
1440
|
-
}
|
|
1441
|
-
else {
|
|
1442
|
-
real = complex;
|
|
1443
|
-
}
|
|
1444
|
-
if (window) {
|
|
1445
|
-
applyWindow(real, window);
|
|
1446
|
-
}
|
|
1447
|
-
const n = real.length;
|
|
1448
|
-
if (img) {
|
|
1449
|
-
swapRI(real, img, n);
|
|
1450
|
-
}
|
|
1451
|
-
else {
|
|
1452
|
-
swapR(real, n);
|
|
1453
|
-
img = new Float64Array(n);
|
|
1454
|
-
}
|
|
1455
|
-
transform(real, img, n);
|
|
1456
|
-
return [real, img];
|
|
1457
|
-
};
|
|
1458
|
-
const ifft = (src) => {
|
|
1459
|
-
let complex = isComplex(src)
|
|
1460
|
-
? src
|
|
1461
|
-
: [new Float64Array(src.length), src];
|
|
1462
|
-
fft([complex[1], complex[0]]);
|
|
1463
|
-
return scaleFFT(complex, 1 / complex[0].length);
|
|
1464
|
-
};
|
|
1465
|
-
const scaleFFT = (complex, scale) => {
|
|
1466
|
-
const [real, img] = complex;
|
|
1467
|
-
const n = real.length;
|
|
1468
|
-
for (let i = 0; i < n; i++) {
|
|
1469
|
-
real[i] *= scale;
|
|
1470
|
-
img[i] *= scale;
|
|
1471
|
-
}
|
|
1472
|
-
return [real, img];
|
|
1473
|
-
};
|
|
1474
|
-
const normalizeFFT = (complex, window = 2 / complex[0].length) => scaleFFT(complex, powerScale(window, 2));
|
|
1475
|
-
const denormalizeFFT = (complex, window = complex[0].length / 2) => scaleFFT(complex, invPowerScale(window, 2));
|
|
1476
|
-
const thresholdFFT = (complex, eps = 1e-12) => {
|
|
1477
|
-
const [real, img] = complex;
|
|
1478
|
-
for (let i = 0, n = real.length; i < n; i++) {
|
|
1479
|
-
if (Math.hypot(real[i], img[i]) < eps) {
|
|
1480
|
-
real[i] = img[i] = 0;
|
|
1481
|
-
}
|
|
1482
|
-
}
|
|
1483
|
-
return complex;
|
|
1484
|
-
};
|
|
1485
|
-
const spectrumMag = (complex, n = complex[0].length / 2, out = []) => {
|
|
1486
|
-
const [real, img] = complex;
|
|
1487
|
-
for (let i = 0; i < n; i++) {
|
|
1488
|
-
out[i] = Math.hypot(real[i], img[i]);
|
|
1489
|
-
}
|
|
1490
|
-
return out;
|
|
1491
|
-
};
|
|
1492
|
-
const spectrumPow = (complex, db = false, window = 2 / complex[0].length, n = complex[0].length / 2, out = []) => {
|
|
1493
|
-
const [real, img] = complex;
|
|
1494
|
-
const scale = powerScale(window, 2);
|
|
1495
|
-
for (let i = 0; i < n; i++) {
|
|
1496
|
-
const p = real[i] ** 2 + img[i] ** 2;
|
|
1497
|
-
out[i] = db ? magDb(Math.sqrt(p) * scale) : p * scale;
|
|
1498
|
-
}
|
|
1499
|
-
return out;
|
|
1500
|
-
};
|
|
1501
|
-
const spectrumPhase = (complex, n = complex[0].length / 2, out = []) => {
|
|
1502
|
-
const [real, img] = complex;
|
|
1503
|
-
for (let i = 0; i < n; i++) {
|
|
1504
|
-
out[i] = Math.atan2(img[i], real[i]);
|
|
1505
|
-
}
|
|
1506
|
-
return out;
|
|
1507
|
-
};
|
|
1508
|
-
const freqBin = (f, fs, n) => ((f * n) / fs) | 0;
|
|
1509
|
-
const binFreq = (bin, fs, n) => (bin * fs) / n;
|
|
1510
|
-
const fftFreq = (n, fs, m = n / 2) => {
|
|
1511
|
-
const res = new Float64Array(m);
|
|
1512
|
-
for (let i = 0; i <= m; i++) {
|
|
1513
|
-
res[i] = binFreq(i, fs, n);
|
|
1514
|
-
}
|
|
1515
|
-
return res;
|
|
1516
|
-
};
|
|
1517
|
-
|
|
1518
|
-
const filterResponseRaw = (zeroes, poles, freq, db = true) => {
|
|
1519
|
-
const w0 = math.TAU * freq;
|
|
1520
|
-
const [cp, sp] = convolve(poles, w0);
|
|
1521
|
-
const [cz, sz] = convolve(zeroes, w0);
|
|
1522
|
-
const mag = Math.sqrt((cz * cz + sz * sz) / (cp * cp + sp * sp));
|
|
1523
|
-
const phase = Math.atan2(sp, cp) - Math.atan2(sz, cz);
|
|
1524
|
-
return { freq, phase, mag: db ? magDb(mag) : mag };
|
|
1525
|
-
};
|
|
1526
|
-
const filterResponse = (coeffs, freq, db) => filterResponseRaw(coeffs.zeroes, coeffs.poles, freq, db);
|
|
1527
|
-
const freqRange = (fstart, fend, num) => line(fstart, fend, num - 1).take(num);
|
|
1528
|
-
const convolve = (coeffs, w0) => {
|
|
1529
|
-
let c = 0;
|
|
1530
|
-
let s = 0;
|
|
1531
|
-
for (let i = coeffs.length; --i >= 0;) {
|
|
1532
|
-
const k = math.cossin(w0 * i, coeffs[i]);
|
|
1533
|
-
c += k[0];
|
|
1534
|
-
s += k[1];
|
|
1535
|
-
}
|
|
1536
|
-
return [c, s];
|
|
1537
|
-
};
|
|
1538
|
-
|
|
1539
|
-
exports.$Iterable = $Iterable;
|
|
1540
|
-
exports.ADSR = ADSR;
|
|
1541
|
-
exports.AGen = AGen;
|
|
1542
|
-
exports.AProc = AProc;
|
|
1543
|
-
exports.AProc2 = AProc2;
|
|
1544
|
-
exports.Add = Add;
|
|
1545
|
-
exports.AllPass1 = AllPass1;
|
|
1546
|
-
exports.Alt = Alt;
|
|
1547
|
-
exports.Biquad = Biquad;
|
|
1548
|
-
exports.CompP = CompP;
|
|
1549
|
-
exports.CompP2 = CompP2;
|
|
1550
|
-
exports.CompP3 = CompP3;
|
|
1551
|
-
exports.CompP4 = CompP4;
|
|
1552
|
-
exports.Const = Const;
|
|
1553
|
-
exports.Cosine = Cosine;
|
|
1554
|
-
exports.DCBlock = DCBlock;
|
|
1555
|
-
exports.Delay = Delay;
|
|
1556
|
-
exports.FeedbackDelay = FeedbackDelay;
|
|
1557
|
-
exports.Foldback = Foldback;
|
|
1558
|
-
exports.Impulse = Impulse;
|
|
1559
|
-
exports.ImpulseTrain = ImpulseTrain;
|
|
1560
|
-
exports.Integrator = Integrator;
|
|
1561
|
-
exports.MAdd = MAdd;
|
|
1562
|
-
exports.MapG1 = MapG1;
|
|
1563
|
-
exports.MapG2 = MapG2;
|
|
1564
|
-
exports.MapG3 = MapG3;
|
|
1565
|
-
exports.MapG4 = MapG4;
|
|
1566
|
-
exports.Mix = Mix;
|
|
1567
|
-
exports.Mul = Mul;
|
|
1568
|
-
exports.Multiplex = Multiplex;
|
|
1569
|
-
exports.OnePole = OnePole;
|
|
1570
|
-
exports.Osc = Osc;
|
|
1571
|
-
exports.PinkNoise = PinkNoise;
|
|
1572
|
-
exports.Reciprocal = Reciprocal;
|
|
1573
|
-
exports.SVF = SVF;
|
|
1574
|
-
exports.SinCos = SinCos;
|
|
1575
|
-
exports.WaveShaper = WaveShaper;
|
|
1576
|
-
exports.WhiteNoise = WhiteNoise;
|
|
1577
|
-
exports.add = add;
|
|
1578
|
-
exports.addG = addG;
|
|
1579
|
-
exports.additive = additive;
|
|
1580
|
-
exports.adsr = adsr;
|
|
1581
|
-
exports.allpass = allpass;
|
|
1582
|
-
exports.alt = alt;
|
|
1583
|
-
exports.altB = altB;
|
|
1584
|
-
exports.altT = altT;
|
|
1585
|
-
exports.applyWindow = applyWindow;
|
|
1586
|
-
exports.binFreq = binFreq;
|
|
1587
|
-
exports.biquad = biquad;
|
|
1588
|
-
exports.biquadBP = biquadBP;
|
|
1589
|
-
exports.biquadHP = biquadHP;
|
|
1590
|
-
exports.biquadHiShelf = biquadHiShelf;
|
|
1591
|
-
exports.biquadLP = biquadLP;
|
|
1592
|
-
exports.biquadLoShelf = biquadLoShelf;
|
|
1593
|
-
exports.biquadNotch = biquadNotch;
|
|
1594
|
-
exports.biquadPeak = biquadPeak;
|
|
1595
|
-
exports.compP = compP;
|
|
1596
|
-
exports.complexArray = complexArray;
|
|
1597
|
-
exports.conjugate = conjugate;
|
|
1598
|
-
exports.constant = constant;
|
|
1599
|
-
exports.copyComplex = copyComplex;
|
|
1600
|
-
exports.cos = cos$1;
|
|
1601
|
-
exports.cosine = cosine;
|
|
1602
|
-
exports.curve = curve;
|
|
1603
|
-
exports.dbMag = dbMag;
|
|
1604
|
-
exports.dcBlock = dcBlock;
|
|
1605
|
-
exports.delay = delay;
|
|
1606
|
-
exports.delayT = delayT;
|
|
1607
|
-
exports.denormalizeFFT = denormalizeFFT;
|
|
1608
|
-
exports.dsf = dsf;
|
|
1609
|
-
exports.dsfHOF = dsfHOF;
|
|
1610
|
-
exports.feedbackDelay = feedbackDelay;
|
|
1611
|
-
exports.fejer = fejer;
|
|
1612
|
-
exports.fft = fft;
|
|
1613
|
-
exports.fftFreq = fftFreq;
|
|
1614
|
-
exports.filterResponse = filterResponse;
|
|
1615
|
-
exports.filterResponseRaw = filterResponseRaw;
|
|
1616
|
-
exports.foldback = foldback;
|
|
1617
|
-
exports.framesMs = framesMs;
|
|
1618
|
-
exports.freqBin = freqBin;
|
|
1619
|
-
exports.freqMs = freqMs;
|
|
1620
|
-
exports.freqRad = freqRad;
|
|
1621
|
-
exports.freqRange = freqRange;
|
|
1622
|
-
exports.gibbs = gibbs;
|
|
1623
|
-
exports.ifft = ifft;
|
|
1624
|
-
exports.impulse = impulse;
|
|
1625
|
-
exports.impulseB = impulseB;
|
|
1626
|
-
exports.impulseT = impulseT;
|
|
1627
|
-
exports.impulseTrain = impulseTrain;
|
|
1628
|
-
exports.impulseTrainB = impulseTrainB;
|
|
1629
|
-
exports.impulseTrainT = impulseTrainT;
|
|
1630
|
-
exports.integralF = integralF;
|
|
1631
|
-
exports.integralFSquared = integralFSquared;
|
|
1632
|
-
exports.integralT = integralT;
|
|
1633
|
-
exports.integralTSquared = integralTSquared;
|
|
1634
|
-
exports.integrator = integrator;
|
|
1635
|
-
exports.invPowerScale = invPowerScale;
|
|
1636
|
-
exports.isComplex = isComplex;
|
|
1637
|
-
exports.iterable = iterable;
|
|
1638
|
-
exports.line = line;
|
|
1639
|
-
exports.madd = madd;
|
|
1640
|
-
exports.magDb = magDb;
|
|
1641
|
-
exports.mapG = mapG;
|
|
1642
|
-
exports.mixOsc = mixOsc;
|
|
1643
|
-
exports.mixOscHOF = mixOscHOF;
|
|
1644
|
-
exports.modOsc = modOsc;
|
|
1645
|
-
exports.msFrames = msFrames;
|
|
1646
|
-
exports.mul = mul;
|
|
1647
|
-
exports.multiplex = multiplex;
|
|
1648
|
-
exports.normFreq = normFreq;
|
|
1649
|
-
exports.normalizeFFT = normalizeFFT;
|
|
1650
|
-
exports.onepoleHP = onepoleHP;
|
|
1651
|
-
exports.onepoleLP = onepoleLP;
|
|
1652
|
-
exports.osc = osc;
|
|
1653
|
-
exports.parabolic = parabolic;
|
|
1654
|
-
exports.pinkNoise = pinkNoise;
|
|
1655
|
-
exports.pipe = pipe;
|
|
1656
|
-
exports.polyBLEP = polyBLEP;
|
|
1657
|
-
exports.powerMeanSquared = powerMeanSquared;
|
|
1658
|
-
exports.powerScale = powerScale;
|
|
1659
|
-
exports.powerSumSquared = powerSumSquared;
|
|
1660
|
-
exports.powerTimeIntegral = powerTimeIntegral;
|
|
1661
|
-
exports.product = product;
|
|
1662
|
-
exports.radFreq = radFreq;
|
|
1663
|
-
exports.reciprocal = reciprocal;
|
|
1664
|
-
exports.rect = rect;
|
|
1665
|
-
exports.rectHOF = rectHOF;
|
|
1666
|
-
exports.saw = saw;
|
|
1667
|
-
exports.sawAdditive = sawAdditive;
|
|
1668
|
-
exports.scaleFFT = scaleFFT;
|
|
1669
|
-
exports.sin = sin$1;
|
|
1670
|
-
exports.spectrumMag = spectrumMag;
|
|
1671
|
-
exports.spectrumPhase = spectrumPhase;
|
|
1672
|
-
exports.spectrumPow = spectrumPow;
|
|
1673
|
-
exports.squareAdditive = squareAdditive;
|
|
1674
|
-
exports.sum = sum;
|
|
1675
|
-
exports.svfAllpass = svfAllpass;
|
|
1676
|
-
exports.svfBP = svfBP;
|
|
1677
|
-
exports.svfHP = svfHP;
|
|
1678
|
-
exports.svfLP = svfLP;
|
|
1679
|
-
exports.svfNotch = svfNotch;
|
|
1680
|
-
exports.svfPeak = svfPeak;
|
|
1681
|
-
exports.sweep = sweep;
|
|
1682
|
-
exports.thresholdFFT = thresholdFFT;
|
|
1683
|
-
exports.tri = tri;
|
|
1684
|
-
exports.waveShaper = waveShaper;
|
|
1685
|
-
exports.waveshapeSigmoid = waveshapeSigmoid;
|
|
1686
|
-
exports.waveshapeSin = waveshapeSin;
|
|
1687
|
-
exports.waveshapeTan = waveshapeTan;
|
|
1688
|
-
exports.wavetable = wavetable;
|
|
1689
|
-
exports.whiteNoise = whiteNoise;
|
|
1690
|
-
exports.window = window;
|
|
1691
|
-
exports.windowBartlett = windowBartlett;
|
|
1692
|
-
exports.windowBlackman = windowBlackman;
|
|
1693
|
-
exports.windowBlackmanHarris = windowBlackmanHarris;
|
|
1694
|
-
exports.windowBlackmanNuttall = windowBlackmanNuttall;
|
|
1695
|
-
exports.windowGauss = windowGauss;
|
|
1696
|
-
exports.windowHamming = windowHamming;
|
|
1697
|
-
exports.windowHann = windowHann;
|
|
1698
|
-
exports.windowLanczos = windowLanczos;
|
|
1699
|
-
exports.windowNuttall = windowNuttall;
|
|
1700
|
-
exports.windowRect = windowRect;
|
|
1701
|
-
exports.windowSin = windowSin;
|
|
1702
|
-
exports.windowSinPow = windowSinPow;
|
|
1703
|
-
exports.windowWelch = windowWelch;
|