@logue/reverb 1.3.5 → 1.3.7
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.es.js +206 -425
- package/dist/Reverb.iife.js +2 -633
- package/dist/Reverb.umd.js +2 -636
- package/package.json +21 -23
package/dist/Reverb.iife.js
CHANGED
|
@@ -5,639 +5,8 @@
|
|
|
5
5
|
* @author Logue <logue@hotmail.co.jp>
|
|
6
6
|
* @copyright 2019-2023 By Masashi Yoshikawa All rights reserved.
|
|
7
7
|
* @license MIT
|
|
8
|
-
* @version 1.3.
|
|
8
|
+
* @version 1.3.7
|
|
9
9
|
* @see {@link https://github.com/logue/Reverb.js}
|
|
10
10
|
*/
|
|
11
11
|
|
|
12
|
-
var Reverb = (function () {
|
|
13
|
-
'use strict';
|
|
14
|
-
|
|
15
|
-
const INV_MAX = 1 / 2 ** 32;
|
|
16
|
-
class ARandom {
|
|
17
|
-
float(norm = 1) {
|
|
18
|
-
return this.int() * INV_MAX * norm;
|
|
19
|
-
}
|
|
20
|
-
probability(p) {
|
|
21
|
-
return this.float() < p;
|
|
22
|
-
}
|
|
23
|
-
norm(norm = 1) {
|
|
24
|
-
return (this.int() * INV_MAX - 0.5) * 2 * norm;
|
|
25
|
-
}
|
|
26
|
-
normMinMax(min, max) {
|
|
27
|
-
const x = this.minmax(min, max);
|
|
28
|
-
return this.float() < 0.5 ? x : -x;
|
|
29
|
-
}
|
|
30
|
-
minmax(min, max) {
|
|
31
|
-
return this.float() * (max - min) + min;
|
|
32
|
-
}
|
|
33
|
-
minmaxInt(min, max) {
|
|
34
|
-
min |= 0;
|
|
35
|
-
const range = (max | 0) - min;
|
|
36
|
-
return range ? min + (this.int() % range) : min;
|
|
37
|
-
}
|
|
38
|
-
minmaxUint(min, max) {
|
|
39
|
-
min >>>= 0;
|
|
40
|
-
const range = (max >>> 0) - min;
|
|
41
|
-
return range ? min + (this.int() % range) : min;
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
const random = Math.random;
|
|
46
|
-
/**
|
|
47
|
-
* A `Math.random()` based {@link IRandom} implementation. Also @see
|
|
48
|
-
* {@link SYSTEM}.
|
|
49
|
-
*/
|
|
50
|
-
class SystemRandom extends ARandom {
|
|
51
|
-
int() {
|
|
52
|
-
return (random() * 4294967296) /* 2**32 */ >>> 0;
|
|
53
|
-
}
|
|
54
|
-
float(norm = 1) {
|
|
55
|
-
return random() * norm;
|
|
56
|
-
}
|
|
57
|
-
norm(norm = 1) {
|
|
58
|
-
return (random() - 0.5) * 2 * norm;
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
/**
|
|
62
|
-
* Used as default PRNG throughout most other thi.ng projects, though usually is
|
|
63
|
-
* configurable.
|
|
64
|
-
*/
|
|
65
|
-
const SYSTEM = new SystemRandom();
|
|
66
|
-
|
|
67
|
-
const defaults = {
|
|
68
|
-
noise: "white",
|
|
69
|
-
scale: 1,
|
|
70
|
-
peaks: 2,
|
|
71
|
-
randomAlgorithm: SYSTEM,
|
|
72
|
-
decay: 2,
|
|
73
|
-
delay: 0,
|
|
74
|
-
reverse: false,
|
|
75
|
-
time: 2,
|
|
76
|
-
filterType: "allpass",
|
|
77
|
-
filterFreq: 2200,
|
|
78
|
-
filterQ: 1,
|
|
79
|
-
mix: 0.5,
|
|
80
|
-
once: false
|
|
81
|
-
};
|
|
82
|
-
|
|
83
|
-
const Meta = {
|
|
84
|
-
version: "1.3.5",
|
|
85
|
-
date: "2023-11-20T03:01:00.334Z"
|
|
86
|
-
};
|
|
87
|
-
|
|
88
|
-
const Noise = {
|
|
89
|
-
/** Blue noise */
|
|
90
|
-
blue: "blue",
|
|
91
|
-
/** Brown noise (same as red noise) */
|
|
92
|
-
brown: "red",
|
|
93
|
-
/** Green noise */
|
|
94
|
-
green: "green",
|
|
95
|
-
/** Pink noise */
|
|
96
|
-
pink: "pink",
|
|
97
|
-
/** Red noise */
|
|
98
|
-
red: "red",
|
|
99
|
-
/** Violet noise */
|
|
100
|
-
violet: "violet",
|
|
101
|
-
/** White noise */
|
|
102
|
-
white: "white"
|
|
103
|
-
};
|
|
104
|
-
|
|
105
|
-
const DEFAULT_OPTS = {
|
|
106
|
-
bins: 2,
|
|
107
|
-
scale: 1,
|
|
108
|
-
rnd: SYSTEM,
|
|
109
|
-
};
|
|
110
|
-
|
|
111
|
-
const preseed = (n, scale, rnd) => {
|
|
112
|
-
const state = new Array(n);
|
|
113
|
-
for (let i = 0; i < n; i++) {
|
|
114
|
-
state[i] = rnd.norm(scale);
|
|
115
|
-
}
|
|
116
|
-
return state;
|
|
117
|
-
};
|
|
118
|
-
const sum = (src) => src.reduce((sum, x) => sum + x, 0);
|
|
119
|
-
function* interleave(a, b) {
|
|
120
|
-
const src = [a[Symbol.iterator](), b[Symbol.iterator]()];
|
|
121
|
-
for (let i = 0; true; i ^= 1) {
|
|
122
|
-
const next = src[i].next();
|
|
123
|
-
if (next.done)
|
|
124
|
-
return;
|
|
125
|
-
yield next.value;
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
/**
|
|
130
|
-
* High-pass filtered noise. Opposite of {@link red}.
|
|
131
|
-
*
|
|
132
|
-
* @param opts -
|
|
133
|
-
*/
|
|
134
|
-
function* blue(opts) {
|
|
135
|
-
const { bins, scale, rnd } = {
|
|
136
|
-
...DEFAULT_OPTS,
|
|
137
|
-
...opts,
|
|
138
|
-
};
|
|
139
|
-
const state = preseed(bins, scale, rnd);
|
|
140
|
-
state.forEach((x, i) => (state[i] = i & 1 ? x : -x));
|
|
141
|
-
const invN = 1 / bins;
|
|
142
|
-
let acc = sum(state);
|
|
143
|
-
for (let i = 0, sign = -1; true; ++i >= bins && (i = 0)) {
|
|
144
|
-
acc -= state[i];
|
|
145
|
-
acc += state[i] = sign * rnd.norm(scale);
|
|
146
|
-
sign ^= 0xfffffffe;
|
|
147
|
-
yield sign * acc * invN;
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
/**
|
|
152
|
-
* Band-pass filtered noise (interleaved blue noise). Opposite of
|
|
153
|
-
* {@link violet}.
|
|
154
|
-
*
|
|
155
|
-
* @param opts -
|
|
156
|
-
*/
|
|
157
|
-
const green = (opts) => interleave(blue(opts), blue(opts));
|
|
158
|
-
|
|
159
|
-
/**
|
|
160
|
-
* Returns number of 1 bits in `x`.
|
|
161
|
-
*
|
|
162
|
-
* @param x -
|
|
163
|
-
*/
|
|
164
|
-
const ctz32 = (x) => {
|
|
165
|
-
let c = 32;
|
|
166
|
-
x &= -x;
|
|
167
|
-
x && c--;
|
|
168
|
-
x & 0x0000ffff && (c -= 16);
|
|
169
|
-
x & 0x00ff00ff && (c -= 8);
|
|
170
|
-
x & 0x0f0f0f0f && (c -= 4);
|
|
171
|
-
x & 0x33333333 && (c -= 2);
|
|
172
|
-
x & 0x55555555 && (c -= 1);
|
|
173
|
-
return c;
|
|
174
|
-
};
|
|
175
|
-
|
|
176
|
-
/**
|
|
177
|
-
* Exponential decay (1/f) noise, based on Voss-McCarthy algorithm.
|
|
178
|
-
*
|
|
179
|
-
* @remarks
|
|
180
|
-
* The number of internal states should be in the [4..32] range (default: 8).
|
|
181
|
-
* Due to JS integer limitations, `n` > 32 are meaningless.
|
|
182
|
-
*
|
|
183
|
-
* References:
|
|
184
|
-
*
|
|
185
|
-
* - https://www.dsprelated.com/showarticle/908.php
|
|
186
|
-
* - https://www.firstpr.com.au/dsp/pink-noise/#Voss-McCartney
|
|
187
|
-
*
|
|
188
|
-
* @param opts -
|
|
189
|
-
*/
|
|
190
|
-
function* pink(opts) {
|
|
191
|
-
const { bins, scale, rnd } = {
|
|
192
|
-
...DEFAULT_OPTS,
|
|
193
|
-
bins: 8,
|
|
194
|
-
...opts,
|
|
195
|
-
};
|
|
196
|
-
const state = preseed(bins, scale, rnd);
|
|
197
|
-
const invN = 1 / bins;
|
|
198
|
-
let acc = sum(state);
|
|
199
|
-
for (let i = 0; true; i = (i + 1) >>> 0) {
|
|
200
|
-
const id = ctz32(i) % bins;
|
|
201
|
-
acc -= state[id];
|
|
202
|
-
acc += state[id] = rnd.norm(scale);
|
|
203
|
-
yield acc * invN;
|
|
204
|
-
}
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
/**
|
|
208
|
-
* Low-pass filtered noise (same as brown noise). Opposite of {@link blue}.
|
|
209
|
-
*
|
|
210
|
-
* @param opts -
|
|
211
|
-
*/
|
|
212
|
-
function* red(opts) {
|
|
213
|
-
const { bins, scale, rnd } = {
|
|
214
|
-
...DEFAULT_OPTS,
|
|
215
|
-
...opts,
|
|
216
|
-
};
|
|
217
|
-
const state = preseed(bins, scale, rnd);
|
|
218
|
-
const invN = 1 / bins;
|
|
219
|
-
let acc = sum(state);
|
|
220
|
-
for (let i = 0; true; ++i >= bins && (i = 0)) {
|
|
221
|
-
acc -= state[i];
|
|
222
|
-
acc += state[i] = rnd.norm(scale);
|
|
223
|
-
yield acc * invN;
|
|
224
|
-
}
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
/**
|
|
228
|
-
* Band-stop filtered noise (interleaved red noise). Opposite of {@link green}.
|
|
229
|
-
*
|
|
230
|
-
* @param opts -
|
|
231
|
-
*/
|
|
232
|
-
const violet = (opts) => interleave(red(opts), red(opts));
|
|
233
|
-
|
|
234
|
-
/**
|
|
235
|
-
* Unfiltered noise w/ uniform distribution. Merely yields samples from
|
|
236
|
-
* given PRNG.
|
|
237
|
-
*
|
|
238
|
-
* @param opts -
|
|
239
|
-
*/
|
|
240
|
-
function* white(opts) {
|
|
241
|
-
const { scale, rnd } = { ...DEFAULT_OPTS, ...opts };
|
|
242
|
-
while (true) {
|
|
243
|
-
yield rnd.norm(scale);
|
|
244
|
-
}
|
|
245
|
-
}
|
|
246
|
-
|
|
247
|
-
const implementsFunction = (x, fn) => x != null && typeof x[fn] === "function";
|
|
248
|
-
|
|
249
|
-
const ensureTransducer = (x) => implementsFunction(x, "xform") ? x.xform() : x;
|
|
250
|
-
|
|
251
|
-
const isIterable = (x) => x != null && typeof x[Symbol.iterator] === "function";
|
|
252
|
-
|
|
253
|
-
class Reduced {
|
|
254
|
-
value;
|
|
255
|
-
constructor(val) {
|
|
256
|
-
this.value = val;
|
|
257
|
-
}
|
|
258
|
-
deref() {
|
|
259
|
-
return this.value;
|
|
260
|
-
}
|
|
261
|
-
}
|
|
262
|
-
const reduced = (x) => new Reduced(x);
|
|
263
|
-
const isReduced = (x) => x instanceof Reduced;
|
|
264
|
-
const ensureReduced = (x) => x instanceof Reduced ? x : new Reduced(x);
|
|
265
|
-
const unreduced = (x) => (x instanceof Reduced ? x.deref() : x);
|
|
266
|
-
|
|
267
|
-
/**
|
|
268
|
-
* Convenience helper for building a full {@link Reducer} using the identity
|
|
269
|
-
* function (i.e. `(x) => x`) as completion step (true for 90% of all
|
|
270
|
-
* bundled transducers).
|
|
271
|
-
*
|
|
272
|
-
* @param init - init step of reducer
|
|
273
|
-
* @param rfn - reduction step of reducer
|
|
274
|
-
*/
|
|
275
|
-
const reducer = (init, rfn) => [init, (acc) => acc, rfn];
|
|
276
|
-
|
|
277
|
-
function push(xs) {
|
|
278
|
-
return xs
|
|
279
|
-
? [...xs]
|
|
280
|
-
: reducer(() => [], (acc, x) => (acc.push(x), acc));
|
|
281
|
-
}
|
|
282
|
-
|
|
283
|
-
/**
|
|
284
|
-
* Takes a transducer and input iterable. Returns iterator of
|
|
285
|
-
* transformed results.
|
|
286
|
-
*
|
|
287
|
-
* @param xform -
|
|
288
|
-
* @param xs -
|
|
289
|
-
*/
|
|
290
|
-
function* iterator(xform, xs) {
|
|
291
|
-
const rfn = ensureTransducer(xform)(push());
|
|
292
|
-
const complete = rfn[1];
|
|
293
|
-
const reduce = rfn[2];
|
|
294
|
-
for (let x of xs) {
|
|
295
|
-
const y = reduce([], x);
|
|
296
|
-
if (isReduced(y)) {
|
|
297
|
-
yield* unreduced(complete(y.deref()));
|
|
298
|
-
return;
|
|
299
|
-
}
|
|
300
|
-
if (y.length) {
|
|
301
|
-
yield* y;
|
|
302
|
-
}
|
|
303
|
-
}
|
|
304
|
-
yield* unreduced(complete([]));
|
|
305
|
-
}
|
|
306
|
-
|
|
307
|
-
/**
|
|
308
|
-
* Reducer composition helper, internally used by various transducers
|
|
309
|
-
* during initialization. Takes existing reducer `rfn` (a 3-tuple) and a
|
|
310
|
-
* reducing function `fn`. Returns a new reducer tuple.
|
|
311
|
-
*
|
|
312
|
-
* @remarks
|
|
313
|
-
* `rfn[2]` reduces values of type `B` into an accumulator of type `A`.
|
|
314
|
-
* `fn` accepts values of type `C` and produces interim results of type
|
|
315
|
-
* `B`, which are then (possibly) passed to the "inner" `rfn[2]`
|
|
316
|
-
* function. Therefore the resulting reducer takes inputs of `C` and an
|
|
317
|
-
* accumulator of type `A`.
|
|
318
|
-
*
|
|
319
|
-
* It is assumed that `fn` internally calls `rfn[2]` to pass its own
|
|
320
|
-
* results for further processing by the nested reducer `rfn`.
|
|
321
|
-
*
|
|
322
|
-
* @example
|
|
323
|
-
* ```ts
|
|
324
|
-
* compR(rfn, fn)
|
|
325
|
-
* // [rfn[0], rfn[1], fn]
|
|
326
|
-
* ```
|
|
327
|
-
*
|
|
328
|
-
* @param rfn -
|
|
329
|
-
* @param fn -
|
|
330
|
-
*/
|
|
331
|
-
const compR = (rfn, fn) => [rfn[0], rfn[1], fn];
|
|
332
|
-
|
|
333
|
-
function take(n, src) {
|
|
334
|
-
return isIterable(src)
|
|
335
|
-
? iterator(take(n), src)
|
|
336
|
-
: (rfn) => {
|
|
337
|
-
const r = rfn[2];
|
|
338
|
-
let m = n;
|
|
339
|
-
return compR(rfn, (acc, x) => --m > 0
|
|
340
|
-
? r(acc, x)
|
|
341
|
-
: m === 0
|
|
342
|
-
? ensureReduced(r(acc, x))
|
|
343
|
-
: reduced(acc));
|
|
344
|
-
};
|
|
345
|
-
}
|
|
346
|
-
|
|
347
|
-
class Reverb {
|
|
348
|
-
/** Version strings */
|
|
349
|
-
static version = Meta.version;
|
|
350
|
-
/** Build date */
|
|
351
|
-
static build = Meta.date;
|
|
352
|
-
/** AudioContext */
|
|
353
|
-
ctx;
|
|
354
|
-
/** Wet Level (Reverberated node) */
|
|
355
|
-
wetGainNode;
|
|
356
|
-
/** Dry Level (Original sound node) */
|
|
357
|
-
dryGainNode;
|
|
358
|
-
/** Impulse response filter */
|
|
359
|
-
filterNode;
|
|
360
|
-
/** Convolution node for applying impulse response */
|
|
361
|
-
convolverNode;
|
|
362
|
-
/** Output gain node */
|
|
363
|
-
outputNode;
|
|
364
|
-
/** Option */
|
|
365
|
-
options;
|
|
366
|
-
/** Connected flag */
|
|
367
|
-
isConnected;
|
|
368
|
-
/** Noise Generator */
|
|
369
|
-
noise = white;
|
|
370
|
-
/**
|
|
371
|
-
* Constructor
|
|
372
|
-
*
|
|
373
|
-
* @param ctx - Root AudioContext
|
|
374
|
-
* @param options - Configure
|
|
375
|
-
*/
|
|
376
|
-
constructor(ctx, options) {
|
|
377
|
-
this.ctx = ctx;
|
|
378
|
-
this.options = Object.assign(defaults, options);
|
|
379
|
-
this.wetGainNode = this.ctx.createGain();
|
|
380
|
-
this.dryGainNode = this.ctx.createGain();
|
|
381
|
-
this.filterNode = this.ctx.createBiquadFilter();
|
|
382
|
-
this.convolverNode = this.ctx.createConvolver();
|
|
383
|
-
this.outputNode = this.ctx.createGain();
|
|
384
|
-
this.isConnected = false;
|
|
385
|
-
this.filterType(this.options.filterType);
|
|
386
|
-
this.setNoise(this.options.noise);
|
|
387
|
-
this.buildImpulse();
|
|
388
|
-
this.mix(this.options.mix);
|
|
389
|
-
}
|
|
390
|
-
/**
|
|
391
|
-
* Connect the node for the reverb effect to the original sound node.
|
|
392
|
-
*
|
|
393
|
-
* @param sourceNode - Input source node
|
|
394
|
-
*/
|
|
395
|
-
connect(sourceNode) {
|
|
396
|
-
if (this.isConnected && this.options.once) {
|
|
397
|
-
this.isConnected = false;
|
|
398
|
-
return this.outputNode;
|
|
399
|
-
}
|
|
400
|
-
this.convolverNode.connect(this.filterNode);
|
|
401
|
-
this.filterNode.connect(this.wetGainNode);
|
|
402
|
-
sourceNode.connect(this.convolverNode);
|
|
403
|
-
sourceNode.connect(this.dryGainNode).connect(this.outputNode);
|
|
404
|
-
sourceNode.connect(this.wetGainNode).connect(this.outputNode);
|
|
405
|
-
this.isConnected = true;
|
|
406
|
-
return this.outputNode;
|
|
407
|
-
}
|
|
408
|
-
/**
|
|
409
|
-
* Disconnect the reverb node
|
|
410
|
-
*
|
|
411
|
-
* @param sourceNode - Input source node
|
|
412
|
-
*/
|
|
413
|
-
disconnect(sourceNode) {
|
|
414
|
-
if (this.isConnected) {
|
|
415
|
-
this.convolverNode.disconnect(this.filterNode);
|
|
416
|
-
this.filterNode.disconnect(this.wetGainNode);
|
|
417
|
-
}
|
|
418
|
-
this.isConnected = false;
|
|
419
|
-
return sourceNode;
|
|
420
|
-
}
|
|
421
|
-
/**
|
|
422
|
-
* Dry/Wet ratio
|
|
423
|
-
*
|
|
424
|
-
* @param mix - Ratio (0~1)
|
|
425
|
-
*/
|
|
426
|
-
mix(mix) {
|
|
427
|
-
if (!Reverb.inRange(mix, 0, 1)) {
|
|
428
|
-
throw new RangeError("[Reverb.js] Dry/Wet ratio must be between 0 to 1.");
|
|
429
|
-
}
|
|
430
|
-
this.options.mix = mix;
|
|
431
|
-
this.dryGainNode.gain.value = 1 - this.options.mix;
|
|
432
|
-
this.wetGainNode.gain.value = this.options.mix;
|
|
433
|
-
}
|
|
434
|
-
/**
|
|
435
|
-
* Set Impulse Response time length (second)
|
|
436
|
-
*
|
|
437
|
-
* @param value - IR length
|
|
438
|
-
*/
|
|
439
|
-
time(value) {
|
|
440
|
-
if (!Reverb.inRange(value, 1, 50)) {
|
|
441
|
-
throw new RangeError(
|
|
442
|
-
"[Reverb.js] Time length of inpulse response must be less than 50sec."
|
|
443
|
-
);
|
|
444
|
-
}
|
|
445
|
-
this.options.time = value;
|
|
446
|
-
this.buildImpulse();
|
|
447
|
-
}
|
|
448
|
-
/**
|
|
449
|
-
* Impulse response decay rate.
|
|
450
|
-
*
|
|
451
|
-
* @param value - Decay value
|
|
452
|
-
*/
|
|
453
|
-
decay(value) {
|
|
454
|
-
if (!Reverb.inRange(value, 0, 100)) {
|
|
455
|
-
throw new RangeError(
|
|
456
|
-
"[Reverb.js] Inpulse Response decay level must be less than 100."
|
|
457
|
-
);
|
|
458
|
-
}
|
|
459
|
-
this.options.decay = value;
|
|
460
|
-
this.buildImpulse();
|
|
461
|
-
}
|
|
462
|
-
/**
|
|
463
|
-
* Delay before reverberation starts
|
|
464
|
-
*
|
|
465
|
-
* @param value - Time[ms]
|
|
466
|
-
*/
|
|
467
|
-
delay(value) {
|
|
468
|
-
if (!Reverb.inRange(value, 0, 100)) {
|
|
469
|
-
throw new RangeError(
|
|
470
|
-
"[Reverb.js] Inpulse Response delay time must be less than 100."
|
|
471
|
-
);
|
|
472
|
-
}
|
|
473
|
-
this.options.delay = value;
|
|
474
|
-
this.buildImpulse();
|
|
475
|
-
}
|
|
476
|
-
/**
|
|
477
|
-
* Reverse the impulse response.
|
|
478
|
-
*
|
|
479
|
-
* @param reverse - Reverse IR
|
|
480
|
-
*/
|
|
481
|
-
reverse(reverse) {
|
|
482
|
-
this.options.reverse = reverse;
|
|
483
|
-
this.buildImpulse();
|
|
484
|
-
}
|
|
485
|
-
/**
|
|
486
|
-
* Filter for impulse response
|
|
487
|
-
*
|
|
488
|
-
* @param type - Filiter Type
|
|
489
|
-
*/
|
|
490
|
-
filterType(type = "allpass") {
|
|
491
|
-
this.filterNode.type = this.options.filterType = type;
|
|
492
|
-
}
|
|
493
|
-
/**
|
|
494
|
-
* Filter frequency applied to impulse response
|
|
495
|
-
*
|
|
496
|
-
* @param freq - Frequency
|
|
497
|
-
*/
|
|
498
|
-
filterFreq(freq) {
|
|
499
|
-
if (!Reverb.inRange(freq, 20, 2e4)) {
|
|
500
|
-
throw new RangeError(
|
|
501
|
-
"[Reverb.js] Filter frequrncy must be between 20 and 20000."
|
|
502
|
-
);
|
|
503
|
-
}
|
|
504
|
-
this.options.filterFreq = freq;
|
|
505
|
-
this.filterNode.frequency.value = this.options.filterFreq;
|
|
506
|
-
}
|
|
507
|
-
/**
|
|
508
|
-
* Filter quality.
|
|
509
|
-
*
|
|
510
|
-
* @param q - Quality
|
|
511
|
-
*/
|
|
512
|
-
filterQ(q) {
|
|
513
|
-
if (!Reverb.inRange(q, 0, 10)) {
|
|
514
|
-
throw new RangeError(
|
|
515
|
-
"[Reverb.js] Filter quality value must be between 0 and 10."
|
|
516
|
-
);
|
|
517
|
-
}
|
|
518
|
-
this.options.filterQ = q;
|
|
519
|
-
this.filterNode.Q.value = this.options.filterQ;
|
|
520
|
-
}
|
|
521
|
-
/**
|
|
522
|
-
* set IR source noise peaks
|
|
523
|
-
*
|
|
524
|
-
* @param p - Peaks
|
|
525
|
-
*/
|
|
526
|
-
peaks(p) {
|
|
527
|
-
this.options.peaks = p;
|
|
528
|
-
this.buildImpulse();
|
|
529
|
-
}
|
|
530
|
-
/**
|
|
531
|
-
* set IR source noise scale.
|
|
532
|
-
*
|
|
533
|
-
* @param s - Scale
|
|
534
|
-
*/
|
|
535
|
-
scale(s) {
|
|
536
|
-
this.options.scale = s;
|
|
537
|
-
this.buildImpulse();
|
|
538
|
-
}
|
|
539
|
-
/**
|
|
540
|
-
* set IR source noise generator.
|
|
541
|
-
*
|
|
542
|
-
* @param a - Algorithm
|
|
543
|
-
*/
|
|
544
|
-
randomAlgorithm(a) {
|
|
545
|
-
this.options.randomAlgorithm = a;
|
|
546
|
-
this.buildImpulse();
|
|
547
|
-
}
|
|
548
|
-
/**
|
|
549
|
-
* Inpulse Response Noise algorithm.
|
|
550
|
-
*
|
|
551
|
-
* @param type - IR noise algorithm type.
|
|
552
|
-
*/
|
|
553
|
-
setNoise(type) {
|
|
554
|
-
this.options.noise = type;
|
|
555
|
-
switch (type) {
|
|
556
|
-
case Noise.blue:
|
|
557
|
-
this.noise = blue;
|
|
558
|
-
break;
|
|
559
|
-
case Noise.green:
|
|
560
|
-
this.noise = green;
|
|
561
|
-
break;
|
|
562
|
-
case Noise.pink:
|
|
563
|
-
this.noise = pink;
|
|
564
|
-
break;
|
|
565
|
-
case Noise.red:
|
|
566
|
-
case Noise.brown:
|
|
567
|
-
this.noise = red;
|
|
568
|
-
break;
|
|
569
|
-
case Noise.violet:
|
|
570
|
-
this.noise = violet;
|
|
571
|
-
break;
|
|
572
|
-
default:
|
|
573
|
-
this.noise = white;
|
|
574
|
-
}
|
|
575
|
-
this.buildImpulse();
|
|
576
|
-
}
|
|
577
|
-
/**
|
|
578
|
-
* Set Random Algorythm
|
|
579
|
-
*
|
|
580
|
-
* @param algorithm - Algorythm
|
|
581
|
-
*/
|
|
582
|
-
setRandomAlgorythm(algorithm) {
|
|
583
|
-
this.options.randomAlgorithm = algorithm;
|
|
584
|
-
this.buildImpulse();
|
|
585
|
-
}
|
|
586
|
-
/**
|
|
587
|
-
* Return true if in range, otherwise false
|
|
588
|
-
*
|
|
589
|
-
* @param x - Target value
|
|
590
|
-
* @param min - Minimum value
|
|
591
|
-
* @param max - Maximum value
|
|
592
|
-
*/
|
|
593
|
-
static inRange(x, min, max) {
|
|
594
|
-
return (x - min) * (x - max) <= 0;
|
|
595
|
-
}
|
|
596
|
-
/** Utility function for building an impulse response from the module parameters. */
|
|
597
|
-
buildImpulse() {
|
|
598
|
-
const rate = this.ctx.sampleRate;
|
|
599
|
-
const duration = Math.max(rate * this.options.time, 1);
|
|
600
|
-
const delayDuration = rate * this.options.delay;
|
|
601
|
-
const impulse = this.ctx.createBuffer(2, duration, rate);
|
|
602
|
-
const impulseL = new Float32Array(duration);
|
|
603
|
-
const impulseR = new Float32Array(duration);
|
|
604
|
-
const noiseL = this.getNoise(duration);
|
|
605
|
-
const noiseR = this.getNoise(duration);
|
|
606
|
-
for (let i = 0; i < duration; i++) {
|
|
607
|
-
let n = 0;
|
|
608
|
-
if (i < delayDuration) {
|
|
609
|
-
impulseL[i] = 0;
|
|
610
|
-
impulseR[i] = 0;
|
|
611
|
-
n = this.options.reverse ?? false ? duration - (i - delayDuration) : i - delayDuration;
|
|
612
|
-
} else {
|
|
613
|
-
n = this.options.reverse ?? false ? duration - i : i;
|
|
614
|
-
}
|
|
615
|
-
impulseL[i] = (noiseL[i] ?? 0) * (1 - n / duration) ** this.options.decay;
|
|
616
|
-
impulseR[i] = (noiseR[i] ?? 0) * (1 - n / duration) ** this.options.decay;
|
|
617
|
-
}
|
|
618
|
-
impulse.getChannelData(0).set(impulseL);
|
|
619
|
-
impulse.getChannelData(1).set(impulseR);
|
|
620
|
-
this.convolverNode.buffer = impulse;
|
|
621
|
-
}
|
|
622
|
-
/**
|
|
623
|
-
* Noise source
|
|
624
|
-
*
|
|
625
|
-
* @param duration - length of IR.
|
|
626
|
-
*/
|
|
627
|
-
getNoise(duration) {
|
|
628
|
-
return [
|
|
629
|
-
...take(
|
|
630
|
-
duration,
|
|
631
|
-
this.noise({
|
|
632
|
-
bins: this.options.peaks,
|
|
633
|
-
scale: this.options.scale,
|
|
634
|
-
rnd: this.options.randomAlgorithm
|
|
635
|
-
})
|
|
636
|
-
)
|
|
637
|
-
];
|
|
638
|
-
}
|
|
639
|
-
}
|
|
640
|
-
|
|
641
|
-
return Reverb;
|
|
642
|
-
|
|
643
|
-
})();
|
|
12
|
+
var Reverb=function(){"use strict";const g=23283064365386963e-26;class F{float(e=1){return this.int()*g*e}probability(e){return this.float()<e}norm(e=1){return(this.int()*g-.5)*2*e}normMinMax(e,s){const i=this.minmax(e,s);return this.float()<.5?i:-i}minmax(e,s){return this.float()*(s-e)+e}minmaxInt(e,s){e|=0;const i=(s|0)-e;return i?e+this.int()%i:e}minmaxUint(e,s){e>>>=0;const i=(s>>>0)-e;return i?e+this.int()%i:e}}const m=Math.random;class T extends F{int(){return m()*4294967296>>>0}float(e=1){return m()*e}norm(e=1){return(m()-.5)*2*e}}const w=new T,C={noise:"white",scale:1,peaks:2,randomAlgorithm:w,decay:2,delay:0,reverse:!1,time:2,filterType:"allpass",filterFreq:2200,filterQ:1,mix:.5,once:!1},R={version:"1.3.7",date:"2024-01-29T10:30:54.595Z"},u={blue:"blue",brown:"red",green:"green",pink:"pink",red:"red",violet:"violet",white:"white"},f={bins:2,scale:1,rnd:w},b=(t,e,s)=>{const i=new Array(t);for(let n=0;n<t;n++)i[n]=s.norm(e);return i},y=t=>t.reduce((e,s)=>e+s,0);function*I(t,e){const s=[t[Symbol.iterator](),e[Symbol.iterator]()];for(let i=0;;i^=1){const n=s[i].next();if(n.done)return;yield n.value}}function*v(t){const{bins:e,scale:s,rnd:i}={...f,...t},n=b(e,s,i);n.forEach((r,l)=>n[l]=l&1?r:-r);const c=1/e;let o=y(n);for(let r=0,l=-1;;++r>=e&&(r=0))o-=n[r],o+=n[r]=l*i.norm(s),l^=4294967294,yield l*o*c}const x=t=>I(v(t),v(t)),E=t=>{let e=32;return t&=-t,t&&e--,t&65535&&(e-=16),t&16711935&&(e-=8),t&252645135&&(e-=4),t&858993459&&(e-=2),t&1431655765&&(e-=1),e};function*j(t){const{bins:e,scale:s,rnd:i}={...f,bins:8,...t},n=b(e,s,i),c=1/e;let o=y(n);for(let r=0;;r=r+1>>>0){const l=E(r)%e;o-=n[l],o+=n[l]=i.norm(s),yield o*c}}function*N(t){const{bins:e,scale:s,rnd:i}={...f,...t},n=b(e,s,i),c=1/e;let o=y(n);for(let r=0;;++r>=e&&(r=0))o-=n[r],o+=n[r]=i.norm(s),yield o*c}const M=t=>I(N(t),N(t));function*k(t){const{scale:e,rnd:s}={...f,...t};for(;;)yield s.norm(e)}const S=(t,e)=>t!=null&&typeof t[e]=="function",Q=t=>S(t,"xform")?t.xform():t,q=t=>t!=null&&typeof t[Symbol.iterator]=="function";class d{value;constructor(e){this.value=e}deref(){return this.value}}const D=t=>new d(t),L=t=>t instanceof d,B=t=>t instanceof d?t:new d(t),G=t=>t instanceof d?t.deref():t,O=(t,e)=>[t,s=>s,e];function U(t){return t?[...t]:O(()=>[],(e,s)=>(e.push(s),e))}function*z(t,e){const s=Q(t)(U()),i=s[1],n=s[2];for(let c of e){const o=n([],c);if(L(o)){yield*G(i(o.deref()));return}o.length&&(yield*o)}yield*G(i([]))}const P=(t,e)=>[t[0],t[1],e];function A(t,e){return q(e)?z(A(t),e):s=>{const i=s[2];let n=t;return P(s,(c,o)=>--n>0?i(c,o):n===0?B(i(c,o)):D(c))}}class h{static version=R.version;static build=R.date;ctx;wetGainNode;dryGainNode;filterNode;convolverNode;outputNode;options;isConnected;noise=k;constructor(e,s){this.ctx=e,this.options=Object.assign(C,s),this.wetGainNode=this.ctx.createGain(),this.dryGainNode=this.ctx.createGain(),this.filterNode=this.ctx.createBiquadFilter(),this.convolverNode=this.ctx.createConvolver(),this.outputNode=this.ctx.createGain(),this.isConnected=!1,this.filterType(this.options.filterType),this.setNoise(this.options.noise),this.buildImpulse(),this.mix(this.options.mix)}connect(e){return this.isConnected&&this.options.once?(this.isConnected=!1,this.outputNode):(this.convolverNode.connect(this.filterNode),this.filterNode.connect(this.wetGainNode),e.connect(this.convolverNode),e.connect(this.dryGainNode).connect(this.outputNode),e.connect(this.wetGainNode).connect(this.outputNode),this.isConnected=!0,this.outputNode)}disconnect(e){return this.isConnected&&(this.convolverNode.disconnect(this.filterNode),this.filterNode.disconnect(this.wetGainNode)),this.isConnected=!1,e}mix(e){if(!h.inRange(e,0,1))throw new RangeError("[Reverb.js] Dry/Wet ratio must be between 0 to 1.");this.options.mix=e,this.dryGainNode.gain.value=1-this.options.mix,this.wetGainNode.gain.value=this.options.mix}time(e){if(!h.inRange(e,1,50))throw new RangeError("[Reverb.js] Time length of inpulse response must be less than 50sec.");this.options.time=e,this.buildImpulse()}decay(e){if(!h.inRange(e,0,100))throw new RangeError("[Reverb.js] Inpulse Response decay level must be less than 100.");this.options.decay=e,this.buildImpulse()}delay(e){if(!h.inRange(e,0,100))throw new RangeError("[Reverb.js] Inpulse Response delay time must be less than 100.");this.options.delay=e,this.buildImpulse()}reverse(e){this.options.reverse=e,this.buildImpulse()}filterType(e="allpass"){this.filterNode.type=this.options.filterType=e}filterFreq(e){if(!h.inRange(e,20,2e4))throw new RangeError("[Reverb.js] Filter frequrncy must be between 20 and 20000.");this.options.filterFreq=e,this.filterNode.frequency.value=this.options.filterFreq}filterQ(e){if(!h.inRange(e,0,10))throw new RangeError("[Reverb.js] Filter Q value must be between 0 and 10.");this.options.filterQ=e,this.filterNode.Q.value=this.options.filterQ}peaks(e){this.options.peaks=e,this.buildImpulse()}scale(e){this.options.scale=e,this.buildImpulse()}randomAlgorithm(e){this.options.randomAlgorithm=e,this.buildImpulse()}setNoise(e){switch(this.options.noise=e,e){case u.blue:this.noise=v;break;case u.green:this.noise=x;break;case u.pink:this.noise=j;break;case u.red:case u.brown:this.noise=N;break;case u.violet:this.noise=M;break;default:this.noise=k}this.buildImpulse()}setRandomAlgorythm(e){this.options.randomAlgorithm=e,this.buildImpulse()}static inRange(e,s,i){return(e-s)*(e-i)<=0}buildImpulse(){const e=this.ctx.sampleRate,s=Math.max(e*this.options.time,1),i=e*this.options.delay,n=this.ctx.createBuffer(2,s,e),c=new Float32Array(s),o=new Float32Array(s),r=this.getNoise(s),l=this.getNoise(s);for(let a=0;a<s;a++){let p=0;a<i?(c[a]=0,o[a]=0,p=this.options.reverse??!1?s-(a-i):a-i):p=this.options.reverse??!1?s-a:a,c[a]=(r[a]??0)*(1-p/s)**this.options.decay,o[a]=(l[a]??0)*(1-p/s)**this.options.decay}n.getChannelData(0).set(c),n.getChannelData(1).set(o),this.convolverNode.buffer=n}getNoise(e){return[...A(e,this.noise({bins:this.options.peaks,scale:this.options.scale,rnd:this.options.randomAlgorithm}))]}}return h}();
|