@logue/reverb 1.0.2 → 1.2.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/README.md +14 -5
- package/dist/Reverb.d.ts +26 -7
- package/dist/Reverb.d.ts.map +1 -1
- package/dist/Reverb.es.js +427 -175
- package/dist/Reverb.iife.js +498 -2
- package/dist/Reverb.umd.js +499 -2
- package/dist/interfaces/OptionInterface.d.ts +17 -4
- package/dist/interfaces/OptionInterface.d.ts.map +1 -1
- package/package.json +17 -15
package/dist/Reverb.es.js
CHANGED
|
@@ -5,14 +5,11 @@
|
|
|
5
5
|
* @author Logue <logue@hotmail.co.jp>
|
|
6
6
|
* @copyright 2019-2022 By Masashi Yoshikawa All rights reserved.
|
|
7
7
|
* @license MIT
|
|
8
|
-
* @version 1.
|
|
8
|
+
* @version 1.1.2
|
|
9
9
|
* @see {@link https://github.com/logue/Reverb.js}
|
|
10
10
|
*/
|
|
11
11
|
|
|
12
|
-
const
|
|
13
|
-
version: "1.0.2",
|
|
14
|
-
date: "2022-09-09T10:45:33.869Z"
|
|
15
|
-
}, l = {
|
|
12
|
+
const Noise = {
|
|
16
13
|
BLUE: "blue",
|
|
17
14
|
GREEN: "green",
|
|
18
15
|
PINK: "pink",
|
|
@@ -20,228 +17,483 @@ const g = {
|
|
|
20
17
|
VIOLET: "violet",
|
|
21
18
|
WHITE: "white",
|
|
22
19
|
BROWN: "red"
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
const INV_MAX = 1 / 2 ** 32;
|
|
23
|
+
class ARandom {
|
|
24
|
+
float(norm = 1) {
|
|
25
|
+
return this.int() * INV_MAX * norm;
|
|
26
|
+
}
|
|
27
|
+
norm(norm = 1) {
|
|
28
|
+
return (this.int() * INV_MAX - 0.5) * 2 * norm;
|
|
29
|
+
}
|
|
30
|
+
minmax(min, max) {
|
|
31
|
+
return this.float() * (max - min) + min;
|
|
32
|
+
}
|
|
33
|
+
minmaxInt(min, max) {
|
|
34
|
+
min |= 0;
|
|
35
|
+
max |= 0;
|
|
36
|
+
return min + ((this.float() * (max - min)) | 0);
|
|
37
|
+
}
|
|
31
38
|
}
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
39
|
+
|
|
40
|
+
const random = Math.random;
|
|
41
|
+
/**
|
|
42
|
+
* A `Math.random()` based {@link IRandom} implementation. Also @see
|
|
43
|
+
* {@link SYSTEM}.
|
|
44
|
+
*/
|
|
45
|
+
class SystemRandom extends ARandom {
|
|
46
|
+
int() {
|
|
47
|
+
return (random() * 4294967296) /* 2**32 */ >>> 0;
|
|
48
|
+
}
|
|
49
|
+
float(norm = 1) {
|
|
50
|
+
return random() * norm;
|
|
51
|
+
}
|
|
52
|
+
norm(norm = 1) {
|
|
53
|
+
return (random() - 0.5) * 2 * norm;
|
|
54
|
+
}
|
|
35
55
|
}
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
56
|
+
/**
|
|
57
|
+
* Used as default PRNG throughout most other thi.ng projects, though usually is
|
|
58
|
+
* configurable.
|
|
59
|
+
*/
|
|
60
|
+
const SYSTEM = new SystemRandom();
|
|
61
|
+
|
|
62
|
+
const defaults = {
|
|
63
|
+
noise: Noise.WHITE,
|
|
64
|
+
scale: 1,
|
|
65
|
+
peaks: 2,
|
|
66
|
+
randomAlgorithm: SYSTEM,
|
|
67
|
+
decay: 2,
|
|
68
|
+
delay: 0,
|
|
69
|
+
reverse: false,
|
|
70
|
+
time: 2,
|
|
71
|
+
filterType: "allpass",
|
|
72
|
+
filterFreq: 2200,
|
|
73
|
+
filterQ: 1,
|
|
74
|
+
mix: 0.5,
|
|
75
|
+
once: false
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
const meta = {
|
|
79
|
+
version: "1.1.2",
|
|
80
|
+
date: "2022-11-06T12:24:34.397Z"
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
const preseed = (n, scale, rnd) => {
|
|
84
|
+
const state = new Array(n);
|
|
85
|
+
for (let i = 0; i < n; i++) {
|
|
86
|
+
state[i] = rnd.norm(scale);
|
|
87
|
+
}
|
|
88
|
+
return state;
|
|
89
|
+
};
|
|
90
|
+
const sum = (src) => src.reduce((sum, x) => sum + x, 0);
|
|
91
|
+
function* interleave(a, b) {
|
|
92
|
+
const src = [a[Symbol.iterator](), b[Symbol.iterator]()];
|
|
93
|
+
for (let i = 0; true; i ^= 1) {
|
|
94
|
+
const next = src[i].next();
|
|
95
|
+
if (next.done)
|
|
96
|
+
return;
|
|
97
|
+
yield next.value;
|
|
43
98
|
}
|
|
44
|
-
n.length && (yield* n);
|
|
45
|
-
}
|
|
46
|
-
yield* N(s([]));
|
|
47
99
|
}
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* High-pass filtered noise. Opposite of {@link red}.
|
|
103
|
+
*
|
|
104
|
+
* @param n -
|
|
105
|
+
* @param scale -
|
|
106
|
+
* @param rnd -
|
|
107
|
+
*/
|
|
108
|
+
function* blue(n = 2, scale = 1, rnd = SYSTEM) {
|
|
109
|
+
const state = preseed(n, scale, rnd);
|
|
110
|
+
state.forEach((x, i) => (state[i] = i & 1 ? x : -x));
|
|
111
|
+
const invN = 1 / n;
|
|
112
|
+
let acc = sum(state);
|
|
113
|
+
for (let i = 0, sign = -1; true; ++i >= n && (i = 0)) {
|
|
114
|
+
acc -= state[i];
|
|
115
|
+
acc += state[i] = sign * rnd.norm(scale);
|
|
116
|
+
sign ^= 0xfffffffe;
|
|
117
|
+
yield sign * acc * invN;
|
|
118
|
+
}
|
|
55
119
|
}
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Band-pass filtered noise (interleaved blue noise). Opposite of
|
|
123
|
+
* {@link violet}.
|
|
124
|
+
*
|
|
125
|
+
* @param n -
|
|
126
|
+
* @param scale -
|
|
127
|
+
* @param rnd -
|
|
128
|
+
*/
|
|
129
|
+
const green = (n = 2, scale = 1, rnd = SYSTEM) => interleave(blue(n, scale, rnd), blue(n, scale, rnd));
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Returns number of 1 bits in `x`.
|
|
133
|
+
*
|
|
134
|
+
* @param x -
|
|
135
|
+
*/
|
|
136
|
+
const ctz32 = (x) => {
|
|
137
|
+
let c = 32;
|
|
138
|
+
x &= -x;
|
|
139
|
+
x && c--;
|
|
140
|
+
x & 0x0000ffff && (c -= 16);
|
|
141
|
+
x & 0x00ff00ff && (c -= 8);
|
|
142
|
+
x & 0x0f0f0f0f && (c -= 4);
|
|
143
|
+
x & 0x33333333 && (c -= 2);
|
|
144
|
+
x & 0x55555555 && (c -= 1);
|
|
145
|
+
return c;
|
|
146
|
+
};
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* Exponential decay (1/f) noise, based on Voss-McCarthy algorithm.
|
|
150
|
+
*
|
|
151
|
+
* @remarks
|
|
152
|
+
* The number of internal states should be in the [4..32] range (default: 8).
|
|
153
|
+
* Due to JS integer limitations, `n` > 32 are meaningless.
|
|
154
|
+
*
|
|
155
|
+
* References:
|
|
156
|
+
*
|
|
157
|
+
* - https://www.dsprelated.com/showarticle/908.php
|
|
158
|
+
* - https://www.firstpr.com.au/dsp/pink-noise/#Voss-McCartney
|
|
159
|
+
*
|
|
160
|
+
* @param n -
|
|
161
|
+
* @param scale -
|
|
162
|
+
* @param rnd -
|
|
163
|
+
*/
|
|
164
|
+
function* pink(n = 8, scale = 1, rnd = SYSTEM) {
|
|
165
|
+
const state = preseed(n, scale, rnd);
|
|
166
|
+
const invN = 1 / n;
|
|
167
|
+
let acc = sum(state);
|
|
168
|
+
for (let i = 0; true; i = (i + 1) >>> 0) {
|
|
169
|
+
const id = ctz32(i) % n;
|
|
170
|
+
acc -= state[id];
|
|
171
|
+
acc += state[id] = rnd.norm(scale);
|
|
172
|
+
yield acc * invN;
|
|
173
|
+
}
|
|
70
174
|
}
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* Low-pass filtered noise (same as brown noise). Opposite of {@link blue}.
|
|
178
|
+
*
|
|
179
|
+
* @param n -
|
|
180
|
+
* @param scale -
|
|
181
|
+
* @param rnd -
|
|
182
|
+
*/
|
|
183
|
+
function* red(n = 2, scale = 1, rnd = SYSTEM) {
|
|
184
|
+
const state = preseed(n, scale, rnd);
|
|
185
|
+
const invN = 1 / n;
|
|
186
|
+
let acc = sum(state);
|
|
187
|
+
for (let i = 0; true; ++i >= n && (i = 0)) {
|
|
188
|
+
acc -= state[i];
|
|
189
|
+
acc += state[i] = rnd.norm(scale);
|
|
190
|
+
yield acc * invN;
|
|
191
|
+
}
|
|
82
192
|
}
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* Band-stop filtered noise (interleaved red noise). Opposite of {@link green}.
|
|
196
|
+
*
|
|
197
|
+
* @param n -
|
|
198
|
+
* @param scale -
|
|
199
|
+
* @param rnd -
|
|
200
|
+
*/
|
|
201
|
+
const violet = (n = 2, scale = 1, rnd = SYSTEM) => interleave(red(n, scale, rnd), red(n, scale, rnd));
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* Unfiltered noise w/ uniform distribution. Merely yields samples from
|
|
205
|
+
* given PRNG.
|
|
206
|
+
*
|
|
207
|
+
* @param scale -
|
|
208
|
+
* @param rnd -
|
|
209
|
+
*/
|
|
210
|
+
function* white(scale = 1, rnd = SYSTEM) {
|
|
211
|
+
while (true) {
|
|
212
|
+
yield rnd.norm(scale);
|
|
213
|
+
}
|
|
97
214
|
}
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
215
|
+
|
|
216
|
+
const implementsFunction = (x, fn) => x != null && typeof x[fn] === "function";
|
|
217
|
+
|
|
218
|
+
const ensureTransducer = (x) => implementsFunction(x, "xform") ? x.xform() : x;
|
|
219
|
+
|
|
220
|
+
const isIterable = (x) => x != null && typeof x[Symbol.iterator] === "function";
|
|
221
|
+
|
|
222
|
+
class Reduced {
|
|
223
|
+
constructor(val) {
|
|
224
|
+
this.value = val;
|
|
225
|
+
}
|
|
226
|
+
deref() {
|
|
227
|
+
return this.value;
|
|
228
|
+
}
|
|
105
229
|
}
|
|
106
|
-
const
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
230
|
+
const reduced = (x) => new Reduced(x);
|
|
231
|
+
const isReduced = (x) => x instanceof Reduced;
|
|
232
|
+
const ensureReduced = (x) => x instanceof Reduced ? x : new Reduced(x);
|
|
233
|
+
const unreduced = (x) => (x instanceof Reduced ? x.deref() : x);
|
|
234
|
+
|
|
235
|
+
/**
|
|
236
|
+
* Convenience helper for building a full {@link Reducer} using the identity
|
|
237
|
+
* function (i.e. `(x) => x`) as completion step (true for 90% of all
|
|
238
|
+
* bundled transducers).
|
|
239
|
+
*
|
|
240
|
+
* @param init - init step of reducer
|
|
241
|
+
* @param rfn - reduction step of reducer
|
|
242
|
+
*/
|
|
243
|
+
const reducer = (init, rfn) => [init, (acc) => acc, rfn];
|
|
244
|
+
|
|
245
|
+
function push(xs) {
|
|
246
|
+
return xs
|
|
247
|
+
? [...xs]
|
|
248
|
+
: reducer(() => [], (acc, x) => (acc.push(x), acc));
|
|
117
249
|
}
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
250
|
+
|
|
251
|
+
/**
|
|
252
|
+
* Takes a transducer and input iterable. Returns iterator of
|
|
253
|
+
* transformed results.
|
|
254
|
+
*
|
|
255
|
+
* @param xform -
|
|
256
|
+
* @param xs -
|
|
257
|
+
*/
|
|
258
|
+
function* iterator(xform, xs) {
|
|
259
|
+
const rfn = ensureTransducer(xform)(push());
|
|
260
|
+
const complete = rfn[1];
|
|
261
|
+
const reduce = rfn[2];
|
|
262
|
+
for (let x of xs) {
|
|
263
|
+
const y = reduce([], x);
|
|
264
|
+
if (isReduced(y)) {
|
|
265
|
+
yield* unreduced(complete(y.deref()));
|
|
266
|
+
return;
|
|
267
|
+
}
|
|
268
|
+
if (y.length) {
|
|
269
|
+
yield* y;
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
yield* unreduced(complete([]));
|
|
123
273
|
}
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
274
|
+
|
|
275
|
+
/**
|
|
276
|
+
* Reducer composition helper, internally used by various transducers
|
|
277
|
+
* during initialization. Takes existing reducer `rfn` (a 3-tuple) and a
|
|
278
|
+
* reducing function `fn`. Returns a new reducer tuple.
|
|
279
|
+
*
|
|
280
|
+
* @remarks
|
|
281
|
+
* `rfn[2]` reduces values of type `B` into an accumulator of type `A`.
|
|
282
|
+
* `fn` accepts values of type `C` and produces interim results of type
|
|
283
|
+
* `B`, which are then (possibly) passed to the "inner" `rfn[2]`
|
|
284
|
+
* function. Therefore the resulting reducer takes inputs of `C` and an
|
|
285
|
+
* accumulator of type `A`.
|
|
286
|
+
*
|
|
287
|
+
* It is assumed that `fn` internally calls `rfn[2]` to pass its own
|
|
288
|
+
* results for further processing by the nested reducer `rfn`.
|
|
289
|
+
*
|
|
290
|
+
* @example
|
|
291
|
+
* ```ts
|
|
292
|
+
* compR(rfn, fn)
|
|
293
|
+
* // [rfn[0], rfn[1], fn]
|
|
294
|
+
* ```
|
|
295
|
+
*
|
|
296
|
+
* @param rfn -
|
|
297
|
+
* @param fn -
|
|
298
|
+
*/
|
|
299
|
+
const compR = (rfn, fn) => [rfn[0], rfn[1], fn];
|
|
300
|
+
|
|
301
|
+
function take(n, src) {
|
|
302
|
+
return isIterable(src)
|
|
303
|
+
? iterator(take(n), src)
|
|
304
|
+
: (rfn) => {
|
|
305
|
+
const r = rfn[2];
|
|
306
|
+
let m = n;
|
|
307
|
+
return compR(rfn, (acc, x) => --m > 0
|
|
308
|
+
? r(acc, x)
|
|
309
|
+
: m === 0
|
|
310
|
+
? ensureReduced(r(acc, x))
|
|
311
|
+
: reduced(acc));
|
|
312
|
+
};
|
|
128
313
|
}
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
314
|
+
|
|
315
|
+
class Reverb {
|
|
316
|
+
constructor(ctx, options) {
|
|
317
|
+
this.noise = white;
|
|
318
|
+
this.ctx = ctx;
|
|
319
|
+
this.options = { ...defaults, ...options };
|
|
320
|
+
this.wetGainNode = this.ctx.createGain();
|
|
321
|
+
this.dryGainNode = this.ctx.createGain();
|
|
322
|
+
this.filterNode = this.ctx.createBiquadFilter();
|
|
323
|
+
this.convolverNode = this.ctx.createConvolver();
|
|
324
|
+
this.outputNode = this.ctx.createGain();
|
|
325
|
+
this.isConnected = false;
|
|
326
|
+
this.filterType(this.options.filterType);
|
|
327
|
+
this.setNoise(this.options.noise);
|
|
328
|
+
this.buildImpulse();
|
|
329
|
+
this.mix(this.options.mix);
|
|
132
330
|
}
|
|
133
|
-
connect(
|
|
134
|
-
|
|
331
|
+
connect(sourceNode) {
|
|
332
|
+
if (this.isConnected && this.options.once) {
|
|
333
|
+
this.isConnected = false;
|
|
334
|
+
return this.outputNode;
|
|
335
|
+
}
|
|
336
|
+
this.convolverNode.connect(this.filterNode);
|
|
337
|
+
this.filterNode.connect(this.wetGainNode);
|
|
338
|
+
sourceNode.connect(this.convolverNode);
|
|
339
|
+
sourceNode.connect(this.dryGainNode).connect(this.outputNode);
|
|
340
|
+
sourceNode.connect(this.wetGainNode).connect(this.outputNode);
|
|
341
|
+
this.isConnected = true;
|
|
342
|
+
return this.outputNode;
|
|
135
343
|
}
|
|
136
|
-
disconnect(
|
|
137
|
-
|
|
344
|
+
disconnect(sourceNode) {
|
|
345
|
+
if (this.isConnected) {
|
|
346
|
+
this.convolverNode.disconnect(this.filterNode);
|
|
347
|
+
this.filterNode.disconnect(this.wetGainNode);
|
|
348
|
+
}
|
|
349
|
+
this.isConnected = false;
|
|
350
|
+
return sourceNode;
|
|
138
351
|
}
|
|
139
|
-
mix(
|
|
140
|
-
if (!this.inRange(
|
|
352
|
+
mix(mix) {
|
|
353
|
+
if (!this.inRange(mix, 0, 1)) {
|
|
141
354
|
throw new RangeError("[Reverb.js] Dry/Wet ratio must be between 0 to 1.");
|
|
142
|
-
|
|
355
|
+
}
|
|
356
|
+
this.options.mix = mix;
|
|
357
|
+
this.dryGainNode.gain.value = 1 - this.options.mix;
|
|
358
|
+
this.wetGainNode.gain.value = this.options.mix;
|
|
143
359
|
}
|
|
144
|
-
time(
|
|
145
|
-
if (!this.inRange(
|
|
360
|
+
time(value) {
|
|
361
|
+
if (!this.inRange(value, 1, 50)) {
|
|
146
362
|
throw new RangeError(
|
|
147
363
|
"[Reverb.js] Time length of inpulse response must be less than 50sec."
|
|
148
364
|
);
|
|
149
|
-
|
|
365
|
+
}
|
|
366
|
+
this.options.time = value;
|
|
367
|
+
this.buildImpulse();
|
|
150
368
|
}
|
|
151
|
-
decay(
|
|
152
|
-
if (!this.inRange(
|
|
369
|
+
decay(value) {
|
|
370
|
+
if (!this.inRange(value, 0, 100)) {
|
|
153
371
|
throw new RangeError(
|
|
154
372
|
"[Reverb.js] Inpulse Response decay level must be less than 100."
|
|
155
373
|
);
|
|
156
|
-
|
|
374
|
+
}
|
|
375
|
+
this.options.decay = value;
|
|
376
|
+
this.buildImpulse();
|
|
157
377
|
}
|
|
158
|
-
delay(
|
|
159
|
-
if (!this.inRange(
|
|
378
|
+
delay(value) {
|
|
379
|
+
if (!this.inRange(value, 0, 100)) {
|
|
160
380
|
throw new RangeError(
|
|
161
381
|
"[Reverb.js] Inpulse Response delay time must be less than 100."
|
|
162
382
|
);
|
|
163
|
-
|
|
383
|
+
}
|
|
384
|
+
this.options.delay = value;
|
|
385
|
+
this.buildImpulse();
|
|
164
386
|
}
|
|
165
|
-
reverse(
|
|
166
|
-
this.options.reverse =
|
|
387
|
+
reverse(reverse) {
|
|
388
|
+
this.options.reverse = reverse;
|
|
389
|
+
this.buildImpulse();
|
|
167
390
|
}
|
|
168
|
-
filterType(
|
|
169
|
-
this.filterNode.type = this.options.filterType =
|
|
391
|
+
filterType(type = "allpass") {
|
|
392
|
+
this.filterNode.type = this.options.filterType = type;
|
|
170
393
|
}
|
|
171
|
-
filterFreq(
|
|
172
|
-
if (!this.inRange(
|
|
394
|
+
filterFreq(freq) {
|
|
395
|
+
if (!this.inRange(freq, 20, 2e4)) {
|
|
173
396
|
throw new RangeError(
|
|
174
397
|
"[Reverb.js] Filter frequrncy must be between 20 and 20000."
|
|
175
398
|
);
|
|
176
|
-
|
|
399
|
+
}
|
|
400
|
+
this.options.filterFreq = freq;
|
|
401
|
+
this.filterNode.frequency.value = this.options.filterFreq;
|
|
177
402
|
}
|
|
178
|
-
filterQ(
|
|
179
|
-
if (!this.inRange(
|
|
403
|
+
filterQ(q) {
|
|
404
|
+
if (!this.inRange(q, 0, 10)) {
|
|
180
405
|
throw new RangeError(
|
|
181
406
|
"[Reverb.js] Filter quality value must be between 0 and 10."
|
|
182
407
|
);
|
|
183
|
-
|
|
408
|
+
}
|
|
409
|
+
this.options.filterQ = q;
|
|
410
|
+
this.filterNode.Q.value = this.options.filterQ;
|
|
184
411
|
}
|
|
185
|
-
|
|
186
|
-
this.options.
|
|
412
|
+
peaks(p) {
|
|
413
|
+
this.options.peaks = p;
|
|
414
|
+
this.buildImpulse();
|
|
187
415
|
}
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
416
|
+
scale(s) {
|
|
417
|
+
this.options.scale = s;
|
|
418
|
+
this.buildImpulse();
|
|
419
|
+
}
|
|
420
|
+
randomAlgorithm(a) {
|
|
421
|
+
this.options.randomAlgorithm = a;
|
|
422
|
+
this.buildImpulse();
|
|
423
|
+
}
|
|
424
|
+
setNoise(type) {
|
|
425
|
+
this.options.noise = type;
|
|
426
|
+
switch (type) {
|
|
427
|
+
case Noise.BLUE:
|
|
428
|
+
this.noise = blue;
|
|
192
429
|
break;
|
|
193
|
-
case
|
|
194
|
-
this.noise =
|
|
430
|
+
case Noise.GREEN:
|
|
431
|
+
this.noise = green;
|
|
195
432
|
break;
|
|
196
|
-
case
|
|
197
|
-
this.noise =
|
|
433
|
+
case Noise.PINK:
|
|
434
|
+
this.noise = pink;
|
|
198
435
|
break;
|
|
199
|
-
case
|
|
200
|
-
case
|
|
201
|
-
this.noise =
|
|
436
|
+
case Noise.RED:
|
|
437
|
+
case Noise.BROWN:
|
|
438
|
+
this.noise = red;
|
|
202
439
|
break;
|
|
203
|
-
case
|
|
204
|
-
this.noise =
|
|
440
|
+
case Noise.VIOLET:
|
|
441
|
+
this.noise = violet;
|
|
205
442
|
break;
|
|
206
443
|
default:
|
|
207
|
-
this.noise =
|
|
208
|
-
break;
|
|
444
|
+
this.noise = white;
|
|
209
445
|
}
|
|
210
446
|
this.buildImpulse();
|
|
211
447
|
}
|
|
212
|
-
|
|
213
|
-
|
|
448
|
+
setRandomAlgorythm(algorithm) {
|
|
449
|
+
this.options.randomAlgorithm = algorithm;
|
|
450
|
+
this.buildImpulse();
|
|
451
|
+
}
|
|
452
|
+
inRange(x, min, max) {
|
|
453
|
+
return (x - min) * (x - max) <= 0;
|
|
214
454
|
}
|
|
215
455
|
buildImpulse() {
|
|
216
|
-
const
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
456
|
+
const rate = this.ctx.sampleRate;
|
|
457
|
+
const duration = Math.max(rate * this.options.time, 1);
|
|
458
|
+
const delayDuration = rate * this.options.delay;
|
|
459
|
+
const impulse = this.ctx.createBuffer(2, duration, rate);
|
|
460
|
+
const impulseL = new Float32Array(duration);
|
|
461
|
+
const impulseR = new Float32Array(duration);
|
|
462
|
+
const noiseL = this.getNoise(duration);
|
|
463
|
+
const noiseR = this.getNoise(duration);
|
|
464
|
+
for (let i = 0; i < duration; i++) {
|
|
465
|
+
let n = 0;
|
|
466
|
+
if (i < delayDuration) {
|
|
467
|
+
impulseL[i] = 0;
|
|
468
|
+
impulseR[i] = 0;
|
|
469
|
+
n = this.options.reverse ? duration - (i - delayDuration) : i - delayDuration;
|
|
470
|
+
} else {
|
|
471
|
+
n = this.options.reverse ? duration - i : i;
|
|
472
|
+
}
|
|
473
|
+
impulseL[i] = noiseL[i] * (1 - n / duration) ** this.options.decay;
|
|
474
|
+
impulseR[i] = noiseR[i] * (1 - n / duration) ** this.options.decay;
|
|
220
475
|
}
|
|
221
|
-
|
|
476
|
+
impulse.getChannelData(0).set(impulseL);
|
|
477
|
+
impulse.getChannelData(1).set(impulseR);
|
|
478
|
+
this.convolverNode.buffer = impulse;
|
|
222
479
|
}
|
|
223
|
-
getNoise(
|
|
224
|
-
return [
|
|
225
|
-
(
|
|
226
|
-
|
|
480
|
+
getNoise(duration) {
|
|
481
|
+
return [
|
|
482
|
+
...take(
|
|
483
|
+
duration,
|
|
484
|
+
this.noise === white ? white(this.options.scale, this.options.randomAlgorithm) : this.noise(
|
|
485
|
+
this.options.peaks,
|
|
486
|
+
this.options.scale,
|
|
487
|
+
this.options.randomAlgorithm
|
|
488
|
+
)
|
|
489
|
+
)
|
|
490
|
+
];
|
|
227
491
|
}
|
|
228
492
|
}
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
reverse: !1,
|
|
237
|
-
time: 2,
|
|
238
|
-
filterType: "lowpass",
|
|
239
|
-
filterFreq: 2200,
|
|
240
|
-
filterQ: 1,
|
|
241
|
-
mix: 0.5,
|
|
242
|
-
once: !1
|
|
243
|
-
};
|
|
244
|
-
window.Reverb || (window.Reverb = y);
|
|
245
|
-
export {
|
|
246
|
-
y as default
|
|
247
|
-
};
|
|
493
|
+
Reverb.version = meta.version;
|
|
494
|
+
Reverb.build = meta.date;
|
|
495
|
+
if (!window.Reverb) {
|
|
496
|
+
window.Reverb = Reverb;
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
export { Reverb as default };
|