@logue/reverb 1.1.0 → 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 +1 -1
- package/dist/Reverb.d.ts +2 -2
- package/dist/Reverb.d.ts.map +1 -1
- package/dist/Reverb.es.js +410 -181
- package/dist/Reverb.iife.js +498 -2
- package/dist/Reverb.umd.js +499 -2
- package/dist/interfaces/OptionInterface.d.ts.map +1 -1
- package/package.json +13 -13
package/dist/Reverb.es.js
CHANGED
|
@@ -5,11 +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.1.
|
|
8
|
+
* @version 1.1.2
|
|
9
9
|
* @see {@link https://github.com/logue/Reverb.js}
|
|
10
10
|
*/
|
|
11
11
|
|
|
12
|
-
const
|
|
12
|
+
const Noise = {
|
|
13
13
|
BLUE: "blue",
|
|
14
14
|
GREEN: "green",
|
|
15
15
|
PINK: "pink",
|
|
@@ -17,243 +17,471 @@ const a = {
|
|
|
17
17
|
VIOLET: "violet",
|
|
18
18
|
WHITE: "white",
|
|
19
19
|
BROWN: "red"
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
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
|
+
}
|
|
34
38
|
}
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
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
|
+
}
|
|
46
55
|
}
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
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,
|
|
50
65
|
peaks: 2,
|
|
51
|
-
randomAlgorithm:
|
|
66
|
+
randomAlgorithm: SYSTEM,
|
|
52
67
|
decay: 2,
|
|
53
68
|
delay: 0,
|
|
54
|
-
reverse:
|
|
69
|
+
reverse: false,
|
|
55
70
|
time: 2,
|
|
56
|
-
filterType: "
|
|
71
|
+
filterType: "allpass",
|
|
57
72
|
filterFreq: 2200,
|
|
58
73
|
filterQ: 1,
|
|
59
74
|
mix: 0.5,
|
|
60
|
-
once:
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
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;
|
|
98
|
+
}
|
|
78
99
|
}
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
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
|
+
}
|
|
86
119
|
}
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
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;
|
|
90
146
|
};
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
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
|
+
}
|
|
98
174
|
}
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
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
|
+
}
|
|
104
192
|
}
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
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
|
+
}
|
|
109
214
|
}
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
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
|
+
}
|
|
118
229
|
}
|
|
119
|
-
const
|
|
120
|
-
|
|
121
|
-
|
|
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));
|
|
122
249
|
}
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
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
|
+
}
|
|
130
271
|
}
|
|
131
|
-
|
|
132
|
-
}
|
|
133
|
-
yield* N(o([]));
|
|
272
|
+
yield* unreduced(complete([]));
|
|
134
273
|
}
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
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
|
+
};
|
|
142
313
|
}
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
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);
|
|
146
330
|
}
|
|
147
|
-
connect(
|
|
148
|
-
|
|
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;
|
|
149
343
|
}
|
|
150
|
-
disconnect(
|
|
151
|
-
|
|
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;
|
|
152
351
|
}
|
|
153
|
-
mix(
|
|
154
|
-
if (!this.inRange(
|
|
352
|
+
mix(mix) {
|
|
353
|
+
if (!this.inRange(mix, 0, 1)) {
|
|
155
354
|
throw new RangeError("[Reverb.js] Dry/Wet ratio must be between 0 to 1.");
|
|
156
|
-
|
|
355
|
+
}
|
|
356
|
+
this.options.mix = mix;
|
|
357
|
+
this.dryGainNode.gain.value = 1 - this.options.mix;
|
|
358
|
+
this.wetGainNode.gain.value = this.options.mix;
|
|
157
359
|
}
|
|
158
|
-
time(
|
|
159
|
-
if (!this.inRange(
|
|
360
|
+
time(value) {
|
|
361
|
+
if (!this.inRange(value, 1, 50)) {
|
|
160
362
|
throw new RangeError(
|
|
161
363
|
"[Reverb.js] Time length of inpulse response must be less than 50sec."
|
|
162
364
|
);
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
);
|
|
365
|
+
}
|
|
366
|
+
this.options.time = value;
|
|
367
|
+
this.buildImpulse();
|
|
166
368
|
}
|
|
167
|
-
decay(
|
|
168
|
-
if (!this.inRange(
|
|
369
|
+
decay(value) {
|
|
370
|
+
if (!this.inRange(value, 0, 100)) {
|
|
169
371
|
throw new RangeError(
|
|
170
372
|
"[Reverb.js] Inpulse Response decay level must be less than 100."
|
|
171
373
|
);
|
|
172
|
-
|
|
374
|
+
}
|
|
375
|
+
this.options.decay = value;
|
|
376
|
+
this.buildImpulse();
|
|
173
377
|
}
|
|
174
|
-
delay(
|
|
175
|
-
if (!this.inRange(
|
|
378
|
+
delay(value) {
|
|
379
|
+
if (!this.inRange(value, 0, 100)) {
|
|
176
380
|
throw new RangeError(
|
|
177
381
|
"[Reverb.js] Inpulse Response delay time must be less than 100."
|
|
178
382
|
);
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
);
|
|
383
|
+
}
|
|
384
|
+
this.options.delay = value;
|
|
385
|
+
this.buildImpulse();
|
|
182
386
|
}
|
|
183
|
-
reverse(
|
|
184
|
-
this.options.reverse =
|
|
185
|
-
|
|
186
|
-
);
|
|
387
|
+
reverse(reverse) {
|
|
388
|
+
this.options.reverse = reverse;
|
|
389
|
+
this.buildImpulse();
|
|
187
390
|
}
|
|
188
|
-
filterType(
|
|
189
|
-
this.filterNode.type = this.options.filterType =
|
|
391
|
+
filterType(type = "allpass") {
|
|
392
|
+
this.filterNode.type = this.options.filterType = type;
|
|
190
393
|
}
|
|
191
|
-
filterFreq(
|
|
192
|
-
if (!this.inRange(
|
|
394
|
+
filterFreq(freq) {
|
|
395
|
+
if (!this.inRange(freq, 20, 2e4)) {
|
|
193
396
|
throw new RangeError(
|
|
194
397
|
"[Reverb.js] Filter frequrncy must be between 20 and 20000."
|
|
195
398
|
);
|
|
196
|
-
|
|
399
|
+
}
|
|
400
|
+
this.options.filterFreq = freq;
|
|
401
|
+
this.filterNode.frequency.value = this.options.filterFreq;
|
|
197
402
|
}
|
|
198
|
-
filterQ(
|
|
199
|
-
if (!this.inRange(
|
|
403
|
+
filterQ(q) {
|
|
404
|
+
if (!this.inRange(q, 0, 10)) {
|
|
200
405
|
throw new RangeError(
|
|
201
406
|
"[Reverb.js] Filter quality value must be between 0 and 10."
|
|
202
407
|
);
|
|
203
|
-
|
|
408
|
+
}
|
|
409
|
+
this.options.filterQ = q;
|
|
410
|
+
this.filterNode.Q.value = this.options.filterQ;
|
|
204
411
|
}
|
|
205
|
-
peaks(
|
|
206
|
-
this.options.peaks =
|
|
412
|
+
peaks(p) {
|
|
413
|
+
this.options.peaks = p;
|
|
414
|
+
this.buildImpulse();
|
|
207
415
|
}
|
|
208
|
-
scale(
|
|
209
|
-
this.options.scale =
|
|
416
|
+
scale(s) {
|
|
417
|
+
this.options.scale = s;
|
|
418
|
+
this.buildImpulse();
|
|
210
419
|
}
|
|
211
|
-
randomAlgorithm(
|
|
212
|
-
this.options.randomAlgorithm =
|
|
420
|
+
randomAlgorithm(a) {
|
|
421
|
+
this.options.randomAlgorithm = a;
|
|
422
|
+
this.buildImpulse();
|
|
213
423
|
}
|
|
214
|
-
setNoise(
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
424
|
+
setNoise(type) {
|
|
425
|
+
this.options.noise = type;
|
|
426
|
+
switch (type) {
|
|
427
|
+
case Noise.BLUE:
|
|
428
|
+
this.noise = blue;
|
|
218
429
|
break;
|
|
219
|
-
case
|
|
220
|
-
this.noise =
|
|
430
|
+
case Noise.GREEN:
|
|
431
|
+
this.noise = green;
|
|
221
432
|
break;
|
|
222
|
-
case
|
|
223
|
-
this.noise =
|
|
433
|
+
case Noise.PINK:
|
|
434
|
+
this.noise = pink;
|
|
224
435
|
break;
|
|
225
|
-
case
|
|
226
|
-
case
|
|
227
|
-
this.noise =
|
|
436
|
+
case Noise.RED:
|
|
437
|
+
case Noise.BROWN:
|
|
438
|
+
this.noise = red;
|
|
228
439
|
break;
|
|
229
|
-
case
|
|
230
|
-
this.noise =
|
|
440
|
+
case Noise.VIOLET:
|
|
441
|
+
this.noise = violet;
|
|
231
442
|
break;
|
|
232
443
|
default:
|
|
233
|
-
this.noise =
|
|
234
|
-
break;
|
|
444
|
+
this.noise = white;
|
|
235
445
|
}
|
|
236
|
-
this.buildImpulse()
|
|
446
|
+
this.buildImpulse();
|
|
237
447
|
}
|
|
238
|
-
setRandomAlgorythm(
|
|
239
|
-
this.options.randomAlgorithm =
|
|
448
|
+
setRandomAlgorythm(algorithm) {
|
|
449
|
+
this.options.randomAlgorithm = algorithm;
|
|
450
|
+
this.buildImpulse();
|
|
240
451
|
}
|
|
241
|
-
inRange(
|
|
242
|
-
return (
|
|
452
|
+
inRange(x, min, max) {
|
|
453
|
+
return (x - min) * (x - max) <= 0;
|
|
243
454
|
}
|
|
244
455
|
buildImpulse() {
|
|
245
|
-
const
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
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;
|
|
249
475
|
}
|
|
250
|
-
|
|
476
|
+
impulse.getChannelData(0).set(impulseL);
|
|
477
|
+
impulse.getChannelData(1).set(impulseR);
|
|
478
|
+
this.convolverNode.buffer = impulse;
|
|
251
479
|
}
|
|
252
|
-
getNoise(
|
|
480
|
+
getNoise(duration) {
|
|
253
481
|
return [
|
|
254
|
-
...
|
|
255
|
-
|
|
256
|
-
this.
|
|
482
|
+
...take(
|
|
483
|
+
duration,
|
|
484
|
+
this.noise === white ? white(this.options.scale, this.options.randomAlgorithm) : this.noise(
|
|
257
485
|
this.options.peaks,
|
|
258
486
|
this.options.scale,
|
|
259
487
|
this.options.randomAlgorithm
|
|
@@ -262,9 +490,10 @@ class v {
|
|
|
262
490
|
];
|
|
263
491
|
}
|
|
264
492
|
}
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
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 };
|