@logue/reverb 1.2.2 → 1.2.6

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.
@@ -3,251 +3,353 @@
3
3
  *
4
4
  * @description JavaScript Reverb effect class
5
5
  * @author Logue <logue@hotmail.co.jp>
6
- * @copyright 2019-2022 By Masashi Yoshikawa All rights reserved.
6
+ * @copyright 2019-2023 By Masashi Yoshikawa All rights reserved.
7
7
  * @license MIT
8
- * @version 1.2.2
8
+ * @version 1.2.6
9
9
  * @see {@link https://github.com/logue/Reverb.js}
10
10
  */
11
11
 
12
12
  (function (global, factory) {
13
- typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('@thi.ng/random'), require('@thi.ng/colored-noise'), require('@thi.ng/transducers')) :
14
- typeof define === 'function' && define.amd ? define(['@thi.ng/random', '@thi.ng/colored-noise', '@thi.ng/transducers'], factory) :
15
- (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.Reverb = factory(global.random, global.colordNoise, global.transducers));
13
+ typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('@thi.ng/random'), require('@thi.ng/colored-noise'), require('@thi.ng/transducers')) :
14
+ typeof define === 'function' && define.amd ? define(['@thi.ng/random', '@thi.ng/colored-noise', '@thi.ng/transducers'], factory) :
15
+ (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.Reverb = factory(global.random, global.colordNoise, global.transducers));
16
16
  })(this, (function (random, coloredNoise, transducers) { 'use strict';
17
17
 
18
- /** Impulse response noise generation algorithm */
19
- const Noise = {
20
- /** Blue noise */
21
- BLUE: 'blue',
22
- /** Green noise */
23
- GREEN: 'green',
24
- /** Pink noise */
25
- PINK: 'pink',
26
- /** Red noise */
27
- RED: 'red',
28
- /** Violet noise */
29
- VIOLET: 'violet',
30
- /** White noise */
31
- WHITE: 'white',
32
- /** Brown noise (same as red noise) */
33
- BROWN: 'red',
34
- };
18
+ const defaults = {
19
+ noise: "white",
20
+ scale: 1,
21
+ peaks: 2,
22
+ randomAlgorithm: random.SYSTEM,
23
+ decay: 2,
24
+ delay: 0,
25
+ reverse: false,
26
+ time: 2,
27
+ filterType: "allpass",
28
+ filterFreq: 2200,
29
+ filterQ: 1,
30
+ mix: 0.5,
31
+ once: false
32
+ };
35
33
 
36
- /** デフォルト値 */
37
- const defaults = {
38
- noise: Noise.WHITE,
39
- scale: 1,
40
- peaks: 2,
41
- randomAlgorithm: random.SYSTEM,
42
- decay: 2,
43
- delay: 0,
44
- reverse: false,
45
- time: 2,
46
- filterType: 'allpass',
47
- filterFreq: 2200,
48
- filterQ: 1,
49
- mix: 0.5,
50
- once: false,
51
- };
34
+ const meta = {
35
+ version: "1.2.6",
36
+ date: "2023-02-15T04:55:52.436Z"
37
+ };
52
38
 
53
- // This file is auto-generated by the build system.
54
- const meta = {
55
- version: '1.2.2',
56
- date: '2022-12-22T02:11:59.486Z',
57
- };
39
+ const Noise = {
40
+ /** Blue noise */
41
+ BLUE: "blue",
42
+ /** Green noise */
43
+ GREEN: "green",
44
+ /** Pink noise */
45
+ PINK: "pink",
46
+ /** Red noise */
47
+ RED: "red",
48
+ /** Violet noise */
49
+ VIOLET: "violet",
50
+ /** White noise */
51
+ WHITE: "white",
52
+ /** Brown noise (same as red noise) */
53
+ BROWN: "red"
54
+ };
58
55
 
59
- class Reverb {
60
- static version = meta.version;
61
- static build = meta.date;
62
- ctx;
63
- wetGainNode;
64
- dryGainNode;
65
- filterNode;
66
- convolverNode;
67
- outputNode;
68
- options;
69
- isConnected;
70
- noise = coloredNoise.white;
71
- constructor(ctx, options) {
72
- this.ctx = ctx;
73
- this.options = { ...defaults, ...options };
74
- this.wetGainNode = this.ctx.createGain();
75
- this.dryGainNode = this.ctx.createGain();
76
- this.filterNode = this.ctx.createBiquadFilter();
77
- this.convolverNode = this.ctx.createConvolver();
78
- this.outputNode = this.ctx.createGain();
56
+ class Reverb {
57
+ /** Version strings */
58
+ static version = meta.version;
59
+ /** Build date */
60
+ static build = meta.date;
61
+ /** AudioContext */
62
+ ctx;
63
+ /** Wet Level (Reverberated node) */
64
+ wetGainNode;
65
+ /** Dry Level (Original sound node) */
66
+ dryGainNode;
67
+ /** Impulse response filter */
68
+ filterNode;
69
+ /** Convolution node for applying impulse response */
70
+ convolverNode;
71
+ /** Output gain node */
72
+ outputNode;
73
+ /** Option */
74
+ options;
75
+ /** Connected flag */
76
+ isConnected;
77
+ /** Noise Generator */
78
+ noise = coloredNoise.white;
79
+ /**
80
+ * Constructor
81
+ *
82
+ * @param ctx - Root AudioContext
83
+ * @param options - Configure
84
+ */
85
+ constructor(ctx, options) {
86
+ this.ctx = ctx;
87
+ this.options = { ...defaults, ...options };
88
+ this.wetGainNode = this.ctx.createGain();
89
+ this.dryGainNode = this.ctx.createGain();
90
+ this.filterNode = this.ctx.createBiquadFilter();
91
+ this.convolverNode = this.ctx.createConvolver();
92
+ this.outputNode = this.ctx.createGain();
93
+ this.isConnected = false;
94
+ this.filterType(this.options.filterType);
95
+ this.setNoise(this.options.noise);
96
+ this.buildImpulse();
97
+ this.mix(this.options.mix);
98
+ }
99
+ /**
100
+ * Connect the node for the reverb effect to the original sound node.
101
+ *
102
+ * @param sourceNode - Input source node
103
+ */
104
+ connect(sourceNode) {
105
+ if (this.isConnected && this.options.once) {
79
106
  this.isConnected = false;
80
- this.filterType(this.options.filterType);
81
- this.setNoise(this.options.noise);
82
- this.buildImpulse();
83
- this.mix(this.options.mix);
84
- }
85
- connect(sourceNode) {
86
- if (this.isConnected && this.options.once) {
87
- this.isConnected = false;
88
- return this.outputNode;
89
- }
90
- this.convolverNode.connect(this.filterNode);
91
- this.filterNode.connect(this.wetGainNode);
92
- sourceNode.connect(this.convolverNode);
93
- sourceNode.connect(this.dryGainNode).connect(this.outputNode);
94
- sourceNode.connect(this.wetGainNode).connect(this.outputNode);
95
- this.isConnected = true;
96
107
  return this.outputNode;
97
108
  }
98
- disconnect(sourceNode) {
99
- if (this.isConnected) {
100
- this.convolverNode.disconnect(this.filterNode);
101
- this.filterNode.disconnect(this.wetGainNode);
102
- }
103
- this.isConnected = false;
104
- return sourceNode;
105
- }
106
- mix(mix) {
107
- if (!this.inRange(mix, 0, 1)) {
108
- throw new RangeError("[Reverb.js] Dry/Wet ratio must be between 0 to 1.");
109
- }
110
- this.options.mix = mix;
111
- this.dryGainNode.gain.value = 1 - this.options.mix;
112
- this.wetGainNode.gain.value = this.options.mix;
113
- }
114
- time(value) {
115
- if (!this.inRange(value, 1, 50)) {
116
- throw new RangeError(
117
- "[Reverb.js] Time length of inpulse response must be less than 50sec."
118
- );
119
- }
120
- this.options.time = value;
121
- this.buildImpulse();
122
- }
123
- decay(value) {
124
- if (!this.inRange(value, 0, 100)) {
125
- throw new RangeError(
126
- "[Reverb.js] Inpulse Response decay level must be less than 100."
127
- );
128
- }
129
- this.options.decay = value;
130
- this.buildImpulse();
131
- }
132
- delay(value) {
133
- if (!this.inRange(value, 0, 100)) {
134
- throw new RangeError(
135
- "[Reverb.js] Inpulse Response delay time must be less than 100."
136
- );
137
- }
138
- this.options.delay = value;
139
- this.buildImpulse();
140
- }
141
- reverse(reverse) {
142
- this.options.reverse = reverse;
143
- this.buildImpulse();
144
- }
145
- filterType(type = "allpass") {
146
- this.filterNode.type = this.options.filterType = type;
147
- }
148
- filterFreq(freq) {
149
- if (!this.inRange(freq, 20, 2e4)) {
150
- throw new RangeError(
151
- "[Reverb.js] Filter frequrncy must be between 20 and 20000."
152
- );
153
- }
154
- this.options.filterFreq = freq;
155
- this.filterNode.frequency.value = this.options.filterFreq;
109
+ this.convolverNode.connect(this.filterNode);
110
+ this.filterNode.connect(this.wetGainNode);
111
+ sourceNode.connect(this.convolverNode);
112
+ sourceNode.connect(this.dryGainNode).connect(this.outputNode);
113
+ sourceNode.connect(this.wetGainNode).connect(this.outputNode);
114
+ this.isConnected = true;
115
+ return this.outputNode;
116
+ }
117
+ /**
118
+ * Disconnect the reverb node
119
+ *
120
+ * @param sourceNode - Input source node
121
+ */
122
+ disconnect(sourceNode) {
123
+ if (this.isConnected) {
124
+ this.convolverNode.disconnect(this.filterNode);
125
+ this.filterNode.disconnect(this.wetGainNode);
156
126
  }
157
- filterQ(q) {
158
- if (!this.inRange(q, 0, 10)) {
159
- throw new RangeError(
160
- "[Reverb.js] Filter quality value must be between 0 and 10."
161
- );
162
- }
163
- this.options.filterQ = q;
164
- this.filterNode.Q.value = this.options.filterQ;
127
+ this.isConnected = false;
128
+ return sourceNode;
129
+ }
130
+ /**
131
+ * Dry/Wet ratio
132
+ *
133
+ * @param mix - Ratio (0~1)
134
+ */
135
+ mix(mix) {
136
+ if (!this.inRange(mix, 0, 1)) {
137
+ throw new RangeError("[Reverb.js] Dry/Wet ratio must be between 0 to 1.");
165
138
  }
166
- peaks(p) {
167
- this.options.peaks = p;
168
- this.buildImpulse();
139
+ this.options.mix = mix;
140
+ this.dryGainNode.gain.value = 1 - this.options.mix;
141
+ this.wetGainNode.gain.value = this.options.mix;
142
+ }
143
+ /**
144
+ * Set Impulse Response time length (second)
145
+ *
146
+ * @param value - IR length
147
+ */
148
+ time(value) {
149
+ if (!this.inRange(value, 1, 50)) {
150
+ throw new RangeError(
151
+ "[Reverb.js] Time length of inpulse response must be less than 50sec."
152
+ );
169
153
  }
170
- scale(s) {
171
- this.options.scale = s;
172
- this.buildImpulse();
154
+ this.options.time = value;
155
+ this.buildImpulse();
156
+ }
157
+ /**
158
+ * Impulse response decay rate.
159
+ *
160
+ * @param value - Decay value
161
+ */
162
+ decay(value) {
163
+ if (!this.inRange(value, 0, 100)) {
164
+ throw new RangeError(
165
+ "[Reverb.js] Inpulse Response decay level must be less than 100."
166
+ );
173
167
  }
174
- randomAlgorithm(a) {
175
- this.options.randomAlgorithm = a;
176
- this.buildImpulse();
168
+ this.options.decay = value;
169
+ this.buildImpulse();
170
+ }
171
+ /**
172
+ * Delay before reverberation starts
173
+ *
174
+ * @param value - Time[ms]
175
+ */
176
+ delay(value) {
177
+ if (!this.inRange(value, 0, 100)) {
178
+ throw new RangeError(
179
+ "[Reverb.js] Inpulse Response delay time must be less than 100."
180
+ );
177
181
  }
178
- setNoise(type) {
179
- this.options.noise = type;
180
- switch (type) {
181
- case Noise.BLUE:
182
- this.noise = coloredNoise.blue;
183
- break;
184
- case Noise.GREEN:
185
- this.noise = coloredNoise.green;
186
- break;
187
- case Noise.PINK:
188
- this.noise = coloredNoise.pink;
189
- break;
190
- case Noise.RED:
191
- case Noise.BROWN:
192
- this.noise = coloredNoise.red;
193
- break;
194
- case Noise.VIOLET:
195
- this.noise = coloredNoise.violet;
196
- break;
197
- default:
198
- this.noise = coloredNoise.white;
199
- }
200
- this.buildImpulse();
182
+ this.options.delay = value;
183
+ this.buildImpulse();
184
+ }
185
+ /**
186
+ * Reverse the impulse response.
187
+ *
188
+ * @param reverse - Reverse IR
189
+ */
190
+ reverse(reverse) {
191
+ this.options.reverse = reverse;
192
+ this.buildImpulse();
193
+ }
194
+ /**
195
+ * Filter for impulse response
196
+ *
197
+ * @param type - Filiter Type
198
+ */
199
+ filterType(type = "allpass") {
200
+ this.filterNode.type = this.options.filterType = type;
201
+ }
202
+ /**
203
+ * Filter frequency applied to impulse response
204
+ *
205
+ * @param freq - Frequency
206
+ */
207
+ filterFreq(freq) {
208
+ if (!this.inRange(freq, 20, 2e4)) {
209
+ throw new RangeError(
210
+ "[Reverb.js] Filter frequrncy must be between 20 and 20000."
211
+ );
201
212
  }
202
- setRandomAlgorythm(algorithm) {
203
- this.options.randomAlgorithm = algorithm;
204
- this.buildImpulse();
213
+ this.options.filterFreq = freq;
214
+ this.filterNode.frequency.value = this.options.filterFreq;
215
+ }
216
+ /**
217
+ * Filter quality.
218
+ *
219
+ * @param q - Quality
220
+ */
221
+ filterQ(q) {
222
+ if (!this.inRange(q, 0, 10)) {
223
+ throw new RangeError(
224
+ "[Reverb.js] Filter quality value must be between 0 and 10."
225
+ );
205
226
  }
206
- inRange(x, min, max) {
207
- return (x - min) * (x - max) <= 0;
227
+ this.options.filterQ = q;
228
+ this.filterNode.Q.value = this.options.filterQ;
229
+ }
230
+ /**
231
+ * set IR source noise peaks
232
+ *
233
+ * @param p - Peaks
234
+ */
235
+ peaks(p) {
236
+ this.options.peaks = p;
237
+ this.buildImpulse();
238
+ }
239
+ /**
240
+ * set IR source noise scale.
241
+ *
242
+ * @param s - Scale
243
+ */
244
+ scale(s) {
245
+ this.options.scale = s;
246
+ this.buildImpulse();
247
+ }
248
+ /**
249
+ * set IR source noise generator.
250
+ *
251
+ * @param a - Algorithm
252
+ */
253
+ randomAlgorithm(a) {
254
+ this.options.randomAlgorithm = a;
255
+ this.buildImpulse();
256
+ }
257
+ /**
258
+ * Inpulse Response Noise algorithm.
259
+ *
260
+ * @param type - IR noise algorithm type.
261
+ */
262
+ setNoise(type) {
263
+ this.options.noise = type;
264
+ switch (type) {
265
+ case Noise["BLUE"]:
266
+ this.noise = coloredNoise.blue;
267
+ break;
268
+ case Noise["GREEN"]:
269
+ this.noise = coloredNoise.green;
270
+ break;
271
+ case Noise["PINK"]:
272
+ this.noise = coloredNoise.pink;
273
+ break;
274
+ case Noise["RED"]:
275
+ case Noise["BROWN"]:
276
+ this.noise = coloredNoise.red;
277
+ break;
278
+ case Noise["VIOLET"]:
279
+ this.noise = coloredNoise.violet;
280
+ break;
281
+ default:
282
+ this.noise = coloredNoise.white;
208
283
  }
209
- buildImpulse() {
210
- const rate = this.ctx.sampleRate;
211
- const duration = Math.max(rate * this.options.time, 1);
212
- const delayDuration = rate * this.options.delay;
213
- const impulse = this.ctx.createBuffer(2, duration, rate);
214
- const impulseL = new Float32Array(duration);
215
- const impulseR = new Float32Array(duration);
216
- const noiseL = this.getNoise(duration);
217
- const noiseR = this.getNoise(duration);
218
- for (let i = 0; i < duration; i++) {
219
- let n = 0;
220
- if (i < delayDuration) {
221
- impulseL[i] = 0;
222
- impulseR[i] = 0;
223
- n = this.options.reverse ? duration - (i - delayDuration) : i - delayDuration;
224
- } else {
225
- n = this.options.reverse ? duration - i : i;
226
- }
227
- impulseL[i] = noiseL[i] * (1 - n / duration) ** this.options.decay;
228
- impulseR[i] = noiseR[i] * (1 - n / duration) ** this.options.decay;
284
+ this.buildImpulse();
285
+ }
286
+ /**
287
+ * Set Random Algorythm
288
+ *
289
+ * @param algorithm - Algorythm
290
+ */
291
+ setRandomAlgorythm(algorithm) {
292
+ this.options.randomAlgorithm = algorithm;
293
+ this.buildImpulse();
294
+ }
295
+ /**
296
+ * Return true if in range, otherwise false
297
+ *
298
+ * @param x - Target value
299
+ * @param min - Minimum value
300
+ * @param max - Maximum value
301
+ */
302
+ inRange(x, min, max) {
303
+ return (x - min) * (x - max) <= 0;
304
+ }
305
+ /** Utility function for building an impulse response from the module parameters. */
306
+ buildImpulse() {
307
+ const rate = this.ctx.sampleRate;
308
+ const duration = Math.max(rate * this.options.time, 1);
309
+ const delayDuration = rate * this.options.delay;
310
+ const impulse = this.ctx.createBuffer(2, duration, rate);
311
+ const impulseL = new Float32Array(duration);
312
+ const impulseR = new Float32Array(duration);
313
+ const noiseL = this.getNoise(duration);
314
+ const noiseR = this.getNoise(duration);
315
+ for (let i = 0; i < duration; i++) {
316
+ let n = 0;
317
+ if (i < delayDuration) {
318
+ impulseL[i] = 0;
319
+ impulseR[i] = 0;
320
+ n = this.options.reverse ? duration - (i - delayDuration) : i - delayDuration;
321
+ } else {
322
+ n = this.options.reverse ? duration - i : i;
229
323
  }
230
- impulse.getChannelData(0).set(impulseL);
231
- impulse.getChannelData(1).set(impulseR);
232
- this.convolverNode.buffer = impulse;
233
- }
234
- getNoise(duration) {
235
- return [
236
- ...transducers.take(
237
- duration,
238
- this.noise(
239
- this.options.peaks,
240
- this.options.scale,
241
- this.options.randomAlgorithm
242
- )
243
- )
244
- ];
324
+ impulseL[i] = noiseL[i] * (1 - n / duration) ** this.options.decay;
325
+ impulseR[i] = noiseR[i] * (1 - n / duration) ** this.options.decay;
245
326
  }
327
+ impulse.getChannelData(0).set(impulseL);
328
+ impulse.getChannelData(1).set(impulseR);
329
+ this.convolverNode.buffer = impulse;
246
330
  }
247
- if (!window.Reverb) {
248
- window.Reverb = Reverb;
331
+ /**
332
+ * Noise source
333
+ *
334
+ * @param duration - length of IR.
335
+ */
336
+ getNoise(duration) {
337
+ return [
338
+ ...transducers.take(
339
+ duration,
340
+ this.noise(
341
+ this.options.peaks,
342
+ this.options.scale,
343
+ this.options.randomAlgorithm
344
+ )
345
+ )
346
+ ];
249
347
  }
348
+ }
349
+ if (!window.Reverb) {
350
+ window.Reverb = Reverb;
351
+ }
250
352
 
251
- return Reverb;
353
+ return Reverb;
252
354
 
253
355
  }));
@@ -5,4 +5,3 @@ export default interface MetaInterface {
5
5
  /** Build Date */
6
6
  date: string;
7
7
  }
8
- //# sourceMappingURL=MetaInterface.d.ts.map
@@ -0,0 +1 @@
1
+ export {};
@@ -1,4 +1,4 @@
1
- import { type NoiseType } from '../NoiseType';
1
+ import type { NoiseType } from '../NoiseType';
2
2
  import type { INorm } from '@thi.ng/random';
3
3
  /** Reverb Option */
4
4
  export default interface OptionInterface {
@@ -37,4 +37,3 @@ export default interface OptionInterface {
37
37
  }
38
38
  /** デフォルト値 */
39
39
  export declare const defaults: OptionInterface;
40
- //# sourceMappingURL=OptionInterface.d.ts.map
@@ -0,0 +1,17 @@
1
+ import { SYSTEM } from '@thi.ng/random';
2
+ /** デフォルト値 */
3
+ export const defaults = {
4
+ noise: 'white',
5
+ scale: 1,
6
+ peaks: 2,
7
+ randomAlgorithm: SYSTEM,
8
+ decay: 2,
9
+ delay: 0,
10
+ reverse: false,
11
+ time: 2,
12
+ filterType: 'allpass',
13
+ filterFreq: 2200,
14
+ filterQ: 1,
15
+ mix: 0.5,
16
+ once: false,
17
+ };