@logue/reverb 1.2.0 → 1.2.2

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/dist/Reverb.es.js CHANGED
@@ -5,316 +5,68 @@
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.2
8
+ * @version 1.2.2
9
9
  * @see {@link https://github.com/logue/Reverb.js}
10
10
  */
11
11
 
12
- const Noise = {
13
- BLUE: "blue",
14
- GREEN: "green",
15
- PINK: "pink",
16
- RED: "red",
17
- VIOLET: "violet",
18
- WHITE: "white",
19
- BROWN: "red"
12
+ import { SYSTEM } from '@thi.ng/random';
13
+ import { white, violet, red, pink, green, blue } from '@thi.ng/colored-noise';
14
+ import { take } from '@thi.ng/transducers';
15
+
16
+ /** Impulse response noise generation algorithm */
17
+ const Noise = {
18
+ /** Blue noise */
19
+ BLUE: 'blue',
20
+ /** Green noise */
21
+ GREEN: 'green',
22
+ /** Pink noise */
23
+ PINK: 'pink',
24
+ /** Red noise */
25
+ RED: 'red',
26
+ /** Violet noise */
27
+ VIOLET: 'violet',
28
+ /** White noise */
29
+ WHITE: 'white',
30
+ /** Brown noise (same as red noise) */
31
+ BROWN: 'red',
20
32
  };
21
33
 
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
- }
38
- }
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
- }
55
- }
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"
34
+ /** デフォルト値 */
35
+ const defaults = {
36
+ noise: Noise.WHITE,
37
+ scale: 1,
38
+ peaks: 2,
39
+ randomAlgorithm: SYSTEM,
40
+ decay: 2,
41
+ delay: 0,
42
+ reverse: false,
43
+ time: 2,
44
+ filterType: 'allpass',
45
+ filterFreq: 2200,
46
+ filterQ: 1,
47
+ mix: 0.5,
48
+ once: false,
81
49
  };
82
50
 
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
- }
99
- }
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
- }
119
- }
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;
51
+ // This file is auto-generated by the build system.
52
+ const meta = {
53
+ version: '1.2.2',
54
+ date: '2022-12-22T02:11:59.486Z',
146
55
  };
147
56
 
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
- }
174
- }
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
- }
192
- }
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
- }
214
- }
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
- }
229
- }
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));
249
- }
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([]));
273
- }
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
- };
313
- }
314
-
315
57
  class Reverb {
58
+ static version = meta.version;
59
+ static build = meta.date;
60
+ ctx;
61
+ wetGainNode;
62
+ dryGainNode;
63
+ filterNode;
64
+ convolverNode;
65
+ outputNode;
66
+ options;
67
+ isConnected;
68
+ noise = white;
316
69
  constructor(ctx, options) {
317
- this.noise = white;
318
70
  this.ctx = ctx;
319
71
  this.options = { ...defaults, ...options };
320
72
  this.wetGainNode = this.ctx.createGain();
@@ -481,7 +233,7 @@ class Reverb {
481
233
  return [
482
234
  ...take(
483
235
  duration,
484
- this.noise === white ? white(this.options.scale, this.options.randomAlgorithm) : this.noise(
236
+ this.noise(
485
237
  this.options.peaks,
486
238
  this.options.scale,
487
239
  this.options.randomAlgorithm
@@ -490,8 +242,6 @@ class Reverb {
490
242
  ];
491
243
  }
492
244
  }
493
- Reverb.version = meta.version;
494
- Reverb.build = meta.date;
495
245
  if (!window.Reverb) {
496
246
  window.Reverb = Reverb;
497
247
  }