@logue/reverb 1.2.1 → 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.
@@ -5,236 +5,246 @@
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.2.1
8
+ * @version 1.2.2
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
- const Noise = {
16
- BLUE: "blue",
17
- GREEN: "green",
18
- PINK: "pink",
19
- RED: "red",
20
- VIOLET: "violet",
21
- WHITE: "white",
22
- BROWN: "red"
23
- };
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
+ };
24
32
 
25
- const defaults = {
26
- noise: Noise.WHITE,
27
- scale: 1,
28
- peaks: 2,
29
- randomAlgorithm: random.SYSTEM,
30
- decay: 2,
31
- delay: 0,
32
- reverse: false,
33
- time: 2,
34
- filterType: "allpass",
35
- filterFreq: 2200,
36
- filterQ: 1,
37
- mix: 0.5,
38
- once: false
39
- };
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
+ };
40
49
 
41
- const meta = {
42
- version: "1.2.1",
43
- date: "2022-11-24T14:46:07.438Z"
44
- };
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
+ };
45
55
 
46
- class Reverb {
47
- static version = meta.version;
48
- static build = meta.date;
49
- ctx;
50
- wetGainNode;
51
- dryGainNode;
52
- filterNode;
53
- convolverNode;
54
- outputNode;
55
- options;
56
- isConnected;
57
- noise = coloredNoise.white;
58
- constructor(ctx, options) {
59
- this.ctx = ctx;
60
- this.options = { ...defaults, ...options };
61
- this.wetGainNode = this.ctx.createGain();
62
- this.dryGainNode = this.ctx.createGain();
63
- this.filterNode = this.ctx.createBiquadFilter();
64
- this.convolverNode = this.ctx.createConvolver();
65
- this.outputNode = this.ctx.createGain();
66
- this.isConnected = false;
67
- this.filterType(this.options.filterType);
68
- this.setNoise(this.options.noise);
69
- this.buildImpulse();
70
- this.mix(this.options.mix);
71
- }
72
- connect(sourceNode) {
73
- if (this.isConnected && this.options.once) {
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();
74
76
  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;
75
93
  return this.outputNode;
76
94
  }
77
- this.convolverNode.connect(this.filterNode);
78
- this.filterNode.connect(this.wetGainNode);
79
- sourceNode.connect(this.convolverNode);
80
- sourceNode.connect(this.dryGainNode).connect(this.outputNode);
81
- sourceNode.connect(this.wetGainNode).connect(this.outputNode);
82
- this.isConnected = true;
83
- return this.outputNode;
84
- }
85
- disconnect(sourceNode) {
86
- if (this.isConnected) {
87
- this.convolverNode.disconnect(this.filterNode);
88
- this.filterNode.disconnect(this.wetGainNode);
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;
89
102
  }
90
- this.isConnected = false;
91
- return sourceNode;
92
- }
93
- mix(mix) {
94
- if (!this.inRange(mix, 0, 1)) {
95
- throw new RangeError("[Reverb.js] Dry/Wet ratio must be between 0 to 1.");
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;
96
110
  }
97
- this.options.mix = mix;
98
- this.dryGainNode.gain.value = 1 - this.options.mix;
99
- this.wetGainNode.gain.value = this.options.mix;
100
- }
101
- time(value) {
102
- if (!this.inRange(value, 1, 50)) {
103
- throw new RangeError(
104
- "[Reverb.js] Time length of inpulse response must be less than 50sec."
105
- );
106
- }
107
- this.options.time = value;
108
- this.buildImpulse();
109
- }
110
- decay(value) {
111
- if (!this.inRange(value, 0, 100)) {
112
- throw new RangeError(
113
- "[Reverb.js] Inpulse Response decay level must be less than 100."
114
- );
115
- }
116
- this.options.decay = value;
117
- this.buildImpulse();
118
- }
119
- delay(value) {
120
- if (!this.inRange(value, 0, 100)) {
121
- throw new RangeError(
122
- "[Reverb.js] Inpulse Response delay time must be less than 100."
123
- );
124
- }
125
- this.options.delay = value;
126
- this.buildImpulse();
127
- }
128
- reverse(reverse) {
129
- this.options.reverse = reverse;
130
- this.buildImpulse();
131
- }
132
- filterType(type = "allpass") {
133
- this.filterNode.type = this.options.filterType = type;
134
- }
135
- filterFreq(freq) {
136
- if (!this.inRange(freq, 20, 2e4)) {
137
- throw new RangeError(
138
- "[Reverb.js] Filter frequrncy must be between 20 and 20000."
139
- );
140
- }
141
- this.options.filterFreq = freq;
142
- this.filterNode.frequency.value = this.options.filterFreq;
143
- }
144
- filterQ(q) {
145
- if (!this.inRange(q, 0, 10)) {
146
- throw new RangeError(
147
- "[Reverb.js] Filter quality value must be between 0 and 10."
148
- );
149
- }
150
- this.options.filterQ = q;
151
- this.filterNode.Q.value = this.options.filterQ;
152
- }
153
- peaks(p) {
154
- this.options.peaks = p;
155
- this.buildImpulse();
156
- }
157
- scale(s) {
158
- this.options.scale = s;
159
- this.buildImpulse();
160
- }
161
- randomAlgorithm(a) {
162
- this.options.randomAlgorithm = a;
163
- this.buildImpulse();
164
- }
165
- setNoise(type) {
166
- this.options.noise = type;
167
- switch (type) {
168
- case Noise.BLUE:
169
- this.noise = coloredNoise.blue;
170
- break;
171
- case Noise.GREEN:
172
- this.noise = coloredNoise.green;
173
- break;
174
- case Noise.PINK:
175
- this.noise = coloredNoise.pink;
176
- break;
177
- case Noise.RED:
178
- case Noise.BROWN:
179
- this.noise = coloredNoise.red;
180
- break;
181
- case Noise.VIOLET:
182
- this.noise = coloredNoise.violet;
183
- break;
184
- default:
185
- this.noise = coloredNoise.white;
186
- }
187
- this.buildImpulse();
188
- }
189
- setRandomAlgorythm(algorithm) {
190
- this.options.randomAlgorithm = algorithm;
191
- this.buildImpulse();
192
- }
193
- inRange(x, min, max) {
194
- return (x - min) * (x - max) <= 0;
195
- }
196
- buildImpulse() {
197
- const rate = this.ctx.sampleRate;
198
- const duration = Math.max(rate * this.options.time, 1);
199
- const delayDuration = rate * this.options.delay;
200
- const impulse = this.ctx.createBuffer(2, duration, rate);
201
- const impulseL = new Float32Array(duration);
202
- const impulseR = new Float32Array(duration);
203
- const noiseL = this.getNoise(duration);
204
- const noiseR = this.getNoise(duration);
205
- for (let i = 0; i < duration; i++) {
206
- let n = 0;
207
- if (i < delayDuration) {
208
- impulseL[i] = 0;
209
- impulseR[i] = 0;
210
- n = this.options.reverse ? duration - (i - delayDuration) : i - delayDuration;
211
- } else {
212
- n = this.options.reverse ? duration - i : i;
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
+ );
213
116
  }
214
- impulseL[i] = noiseL[i] * (1 - n / duration) ** this.options.decay;
215
- impulseR[i] = noiseR[i] * (1 - n / duration) ** this.options.decay;
117
+ this.options.time = value;
118
+ this.buildImpulse();
216
119
  }
217
- impulse.getChannelData(0).set(impulseL);
218
- impulse.getChannelData(1).set(impulseR);
219
- this.convolverNode.buffer = impulse;
220
- }
221
- getNoise(duration) {
222
- return [
223
- ...transducers.take(
224
- duration,
225
- this.noise(
226
- this.options.peaks,
227
- this.options.scale,
228
- this.options.randomAlgorithm
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;
153
+ }
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;
162
+ }
163
+ peaks(p) {
164
+ this.options.peaks = p;
165
+ this.buildImpulse();
166
+ }
167
+ scale(s) {
168
+ this.options.scale = s;
169
+ this.buildImpulse();
170
+ }
171
+ randomAlgorithm(a) {
172
+ this.options.randomAlgorithm = a;
173
+ this.buildImpulse();
174
+ }
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();
198
+ }
199
+ setRandomAlgorythm(algorithm) {
200
+ this.options.randomAlgorithm = algorithm;
201
+ this.buildImpulse();
202
+ }
203
+ inRange(x, min, max) {
204
+ return (x - min) * (x - max) <= 0;
205
+ }
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;
226
+ }
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
+ )
229
240
  )
230
- )
231
- ];
241
+ ];
242
+ }
243
+ }
244
+ if (!window.Reverb) {
245
+ window.Reverb = Reverb;
232
246
  }
233
- }
234
- if (!window.Reverb) {
235
- window.Reverb = Reverb;
236
- }
237
247
 
238
- return Reverb;
248
+ return Reverb;
239
249
 
240
250
  })(random, colordNoise, transducers);