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