@logue/reverb 1.5.1 → 1.6.0

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 CHANGED
@@ -16,6 +16,45 @@ This script is originally a spin out of [sf2synth.js](https://github.com/logue/s
16
16
  - <https://logue.dev/Reverb.js/>
17
17
  - <https://logue.dev/Reverb.js/localaudio.html>
18
18
 
19
+ ## Installation
20
+
21
+ ```bash
22
+ pnpm install @logue/reverb @thi.ng/colored-noise @thi.ng/transducers @thi.ng/random
23
+ ```
24
+
25
+ ## Usage
26
+
27
+ ```js
28
+ import Reverb from '@logue/reverb';
29
+
30
+ // Setup Audio Context
31
+ const ctx = new window.AudioContext();
32
+
33
+ // iOS fix.
34
+ document.addEventListener('touchstart', initAudioContext);
35
+ function initAudioContext() {
36
+ document.removeEventListener('touchstart', initAudioContext);
37
+ // wake up AudioContext
38
+ const emptySource = ctx.createBufferSource();
39
+ emptySource.start();
40
+ emptySource.stop();
41
+ }
42
+
43
+ // Setup Reverb Class
44
+ const reverb = new Reverb(ctx, {});
45
+
46
+ // put Audio data to audio buffer source
47
+ const sourceNode = ctx.createBufferSource();
48
+ sourceNode.buffer = [AudioBuffer];
49
+
50
+ // Connect Reverb
51
+ reverb.connect(sourceNode);
52
+ sourceNode.connect(ctx.destination);
53
+
54
+ // fire
55
+ sourceNode.play();
56
+ ```
57
+
19
58
  ## Syntax
20
59
 
21
60
  ```js
@@ -56,37 +95,6 @@ const reverb = new Reverb(ctx, {
56
95
  });
57
96
  ```
58
97
 
59
- ## Usage
60
-
61
- ```js
62
- // Setup Audio Context
63
- const ctx = new window.AudioContext();
64
-
65
- // iOS fix.
66
- document.addEventListener('touchstart', initAudioContext);
67
- function initAudioContext() {
68
- document.removeEventListener('touchstart', initAudioContext);
69
- // wake up AudioContext
70
- const emptySource = ctx.createBufferSource();
71
- emptySource.start();
72
- emptySource.stop();
73
- }
74
-
75
- // Setup Reverb Class
76
- const reverb = new Reverb(ctx, {});
77
-
78
- // put Audio data to audio buffer source
79
- const sourceNode = ctx.createBufferSource();
80
- sourceNode.buffer = [AudioBuffer];
81
-
82
- // Connect Reverb
83
- reverb.connect(sourceNode);
84
- sourceNode.connect(ctx.destination);
85
-
86
- // fire
87
- sourceNode.play();
88
- ```
89
-
90
98
  ## Testing
91
99
 
92
100
  This project includes a comprehensive test suite to ensure reliability and code quality.
package/dist/Reverb.d.ts CHANGED
@@ -1,141 +1,185 @@
1
- import { default as OptionInterface } from './interfaces/OptionInterface';
1
+ import { ColoredNoiseOpts } from '@thi.ng/colored-noise';
2
2
  import { INorm } from '@thi.ng/random';
3
- import { NoiseType } from './NoiseType';
3
+
4
+ /** Noise Type */
5
+ export type NoiseType = "blue" | "brown" | "green" | "pink" | "red" | "violet" | "white";
6
+ declare const NoiseType: Record<NoiseType, (opts?: Partial<ColoredNoiseOpts>) => Generator<number, void, unknown>>;
7
+ /** Reverb Option */
8
+ export interface OptionInterface {
9
+ /**
10
+ * IR (Inpulse Response) colord noise algorithm (BLUE, GREEN, PINK, RED, VIOLET, WHITE)
11
+ * @see {@link https://github.com/thi-ng/umbrella/tree/develop/packages/colored-noise}
12
+ */
13
+ noise: NoiseType;
14
+ /** IR source noise scale */
15
+ scale: number;
16
+ /** Number of IR source noise peaks */
17
+ peaks: number;
18
+ /**
19
+ * Randam noise algorythm
20
+ * @see {@link https://github.com/thi-ng/umbrella/tree/develop/packages/random}
21
+ */
22
+ randomAlgorithm: INorm;
23
+ /** Decay */
24
+ decay: number;
25
+ /** Delay until impulse response is generated */
26
+ delay: number;
27
+ /** Filter frequency applied to impulse response[Hz] */
28
+ filterFreq: number;
29
+ /** Filter quality for impulse response */
30
+ filterQ: number;
31
+ /** Filter type for impulse response */
32
+ filterType: BiquadFilterType;
33
+ /** Dry/Wet ratio */
34
+ mix: number;
35
+ /** Invert the impulse response */
36
+ reverse?: boolean;
37
+ /** Impulse response length */
38
+ time: number;
39
+ /** Prevents multiple effectors from being connected. */
40
+ once: boolean;
41
+ }
4
42
  /**
5
43
  * Reverb effect class
6
44
  */
7
- export default class Reverb {
8
- /** Version strings */
9
- static readonly version: string;
10
- /** Build date */
11
- static readonly build: string;
12
- /** AudioContext */
13
- private readonly ctx;
14
- /** Wet Level (Reverberated node) */
15
- private readonly wetGainNode;
16
- /** Dry Level (Original sound node) */
17
- private readonly dryGainNode;
18
- /** Impulse response filter */
19
- private readonly filterNode;
20
- /** Convolution node for applying impulse response */
21
- private readonly convolverNode;
22
- /** Output gain node */
23
- private readonly outputNode;
24
- /** Option */
25
- private readonly options;
26
- /** Connected flag */
27
- private isConnected;
28
- /** Noise Generator */
29
- private noise;
30
- /**
31
- * Map of noise types to their respective generator functions.
32
- */
33
- private readonly noiseMap;
34
- /**
35
- * Constructor
36
- *
37
- * @param ctx - Root AudioContext
38
- * @param options - Configure
39
- */
40
- constructor(ctx: AudioContext, options?: Partial<OptionInterface>);
41
- /**
42
- * Connect the node for the reverb effect to the original sound node.
43
- *
44
- * @param sourceNode - Input source node
45
- */
46
- connect(sourceNode: AudioNode): AudioNode;
47
- /**
48
- * Disconnect the reverb node
49
- *
50
- * @param sourceNode - Input source node
51
- */
52
- disconnect(sourceNode?: AudioNode): AudioNode | undefined;
53
- /**
54
- * Dry/Wet ratio
55
- *
56
- * @param mix - Ratio (0~1)
57
- */
58
- mix(mix: number): void;
59
- /**
60
- * Set Impulse Response time length (second)
61
- *
62
- * @param value - IR length
63
- */
64
- time(value: number): void;
65
- /**
66
- * Impulse response decay rate.
67
- *
68
- * @param value - Decay value
69
- */
70
- decay(value: number): void;
71
- /**
72
- * Delay before reverberation starts
73
- *
74
- * @param value - Time[ms]
75
- */
76
- delay(value: number): void;
77
- /**
78
- * Reverse the impulse response.
79
- *
80
- * @param reverse - Reverse IR
81
- */
82
- reverse(reverse: boolean): void;
83
- /**
84
- * Filter for impulse response
85
- *
86
- * @param type - Filiter Type
87
- */
88
- filterType(type?: BiquadFilterType): void;
89
- /**
90
- * Filter frequency applied to impulse response
91
- *
92
- * @param freq - Frequency
93
- */
94
- filterFreq(freq: number): void;
95
- /**
96
- * Filter quality.
97
- *
98
- * @param q - Quality
99
- */
100
- filterQ(q: number): void;
101
- /**
102
- * set IR source noise peaks
103
- *
104
- * @param p - Peaks
105
- */
106
- peaks(p: number): void;
107
- /**
108
- * set IR source noise scale.
109
- *
110
- * @param s - Scale
111
- */
112
- scale(s: number): void;
113
- /**
114
- * Noise source
115
- *
116
- * @param duration - length of IR.
117
- */
118
- private getNoise;
119
- /**
120
- * Inpulse Response Noise algorithm.
121
- *
122
- * @param type - IR noise algorithm type.
123
- */
124
- setNoise(type: NoiseType): void;
125
- /**
126
- * Set the random number algorithm used for noise generation.
127
- *
128
- * @param algorithm - Random algorithm implementing {@link INorm}
129
- */
130
- setRandomAlgorithm(algorithm: INorm): void;
131
- /**
132
- * Return true if in range, otherwise false
133
- *
134
- * @param x - Target value
135
- * @param min - Minimum value
136
- * @param max - Maximum value
137
- */
138
- private static inRange;
139
- /** Builds the impulse response buffer from the current options. */
140
- private buildImpulse;
45
+ declare class Reverb {
46
+ /** Version strings */
47
+ static readonly version: string;
48
+ /** Build date */
49
+ static readonly build: string;
50
+ /** AudioContext */
51
+ private readonly ctx;
52
+ /** Wet Level (Reverberated node) */
53
+ private readonly wetGainNode;
54
+ /** Dry Level (Original sound node) */
55
+ private readonly dryGainNode;
56
+ /** Impulse response filter */
57
+ private readonly filterNode;
58
+ /** Convolution node for applying impulse response */
59
+ private readonly convolverNode;
60
+ /** Output gain node */
61
+ private readonly outputNode;
62
+ /** Option */
63
+ private readonly options;
64
+ /** Connected flag */
65
+ private isConnected;
66
+ /** Noise Generator */
67
+ private noise;
68
+ /**
69
+ * Map of noise types to their respective generator functions.
70
+ */
71
+ private readonly noiseMap;
72
+ /**
73
+ * Constructor
74
+ *
75
+ * @param ctx - Root AudioContext
76
+ * @param options - Configure
77
+ */
78
+ constructor(ctx: AudioContext, options?: Partial<OptionInterface>);
79
+ /**
80
+ * Connect the node for the reverb effect to the original sound node.
81
+ *
82
+ * @param sourceNode - Input source node
83
+ */
84
+ connect(sourceNode: AudioNode): AudioNode;
85
+ /**
86
+ * Disconnect the reverb node
87
+ *
88
+ * @param sourceNode - Input source node
89
+ */
90
+ disconnect(sourceNode?: AudioNode): AudioNode | undefined;
91
+ /**
92
+ * Dry/Wet ratio
93
+ *
94
+ * @param mix - Ratio (0~1)
95
+ */
96
+ mix(mix: number): void;
97
+ /**
98
+ * Set Impulse Response time length (second)
99
+ *
100
+ * @param value - IR length
101
+ */
102
+ time(value: number): void;
103
+ /**
104
+ * Impulse response decay rate.
105
+ *
106
+ * @param value - Decay value
107
+ */
108
+ decay(value: number): void;
109
+ /**
110
+ * Delay before reverberation starts
111
+ *
112
+ * @param value - Time[ms]
113
+ */
114
+ delay(value: number): void;
115
+ /**
116
+ * Reverse the impulse response.
117
+ *
118
+ * @param reverse - Reverse IR
119
+ */
120
+ reverse(reverse: boolean): void;
121
+ /**
122
+ * Filter for impulse response
123
+ *
124
+ * @param type - Filiter Type
125
+ */
126
+ filterType(type?: BiquadFilterType): void;
127
+ /**
128
+ * Filter frequency applied to impulse response
129
+ *
130
+ * @param freq - Frequency
131
+ */
132
+ filterFreq(freq: number): void;
133
+ /**
134
+ * Filter quality.
135
+ *
136
+ * @param q - Quality
137
+ */
138
+ filterQ(q: number): void;
139
+ /**
140
+ * set IR source noise peaks
141
+ *
142
+ * @param p - Peaks
143
+ */
144
+ peaks(p: number): void;
145
+ /**
146
+ * set IR source noise scale.
147
+ *
148
+ * @param s - Scale
149
+ */
150
+ scale(s: number): void;
151
+ /**
152
+ * Noise source
153
+ *
154
+ * @param duration - length of IR.
155
+ */
156
+ private getNoise;
157
+ /**
158
+ * Inpulse Response Noise algorithm.
159
+ *
160
+ * @param type - IR noise algorithm type.
161
+ */
162
+ setNoise(type: NoiseType): void;
163
+ /**
164
+ * Set the random number algorithm used for noise generation.
165
+ *
166
+ * @param algorithm - Random algorithm implementing {@link INorm}
167
+ */
168
+ setRandomAlgorithm(algorithm: INorm): void;
169
+ /**
170
+ * Return true if in range, otherwise false
171
+ *
172
+ * @param x - Target value
173
+ * @param min - Minimum value
174
+ * @param max - Maximum value
175
+ */
176
+ private static inRange;
177
+ /** Builds the impulse response buffer from the current options. */
178
+ private buildImpulse;
141
179
  }
180
+
181
+ export {
182
+ Reverb as default,
183
+ };
184
+
185
+ export {};
@@ -0,0 +1,240 @@
1
+ /**
2
+ * @logue/reverb
3
+ *
4
+ * @description JavaScript Reverb effect class
5
+ * @author Logue <logue@hotmail.co.jp>
6
+ * @copyright 2019-2026 By Masashi Yoshikawa All rights reserved.
7
+ * @license MIT
8
+ * @version 1.6.0
9
+ * @see {@link https://github.com/logue/Reverb.js}
10
+ */
11
+
12
+ import { blue, green, pink, red, violet, white } from "@thi.ng/colored-noise";
13
+ import { take } from "@thi.ng/transducers";
14
+ import { SYSTEM } from "@thi.ng/random";
15
+ const defaults = {
16
+ noise: 'white',
17
+ scale: 1,
18
+ peaks: 2,
19
+ randomAlgorithm: 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
+ };
30
+ const meta = {
31
+ version: "1.6.0",
32
+ date: "2026-06-27T10:08:29.918Z"
33
+ };
34
+ const Meta = meta;
35
+ const NoiseType = {
36
+ blue: blue,
37
+ brown: red,
38
+ green: green,
39
+ pink: pink,
40
+ red: red,
41
+ violet: violet,
42
+ white: white
43
+ };
44
+ function _define_property(obj, key, value) {
45
+ if (key in obj) Object.defineProperty(obj, key, {
46
+ value: value,
47
+ enumerable: true,
48
+ configurable: true,
49
+ writable: true
50
+ });
51
+ else obj[key] = value;
52
+ return obj;
53
+ }
54
+ class Reverb {
55
+ connect(sourceNode) {
56
+ if (this.isConnected && this.options.once) {
57
+ this.isConnected = false;
58
+ return this.outputNode;
59
+ }
60
+ this.convolverNode.connect(this.filterNode);
61
+ this.filterNode.connect(this.wetGainNode);
62
+ sourceNode.connect(this.convolverNode);
63
+ sourceNode.connect(this.dryGainNode);
64
+ sourceNode.connect(this.wetGainNode);
65
+ this.dryGainNode.connect(this.outputNode);
66
+ this.wetGainNode.connect(this.outputNode);
67
+ this.isConnected = true;
68
+ return this.outputNode;
69
+ }
70
+ disconnect(sourceNode) {
71
+ if (this.isConnected) {
72
+ this.convolverNode.disconnect(this.filterNode);
73
+ this.filterNode.disconnect(this.wetGainNode);
74
+ }
75
+ this.isConnected = false;
76
+ return sourceNode;
77
+ }
78
+ mix(mix) {
79
+ if (!Reverb.inRange(mix, 0, 1)) throw new RangeError('[Reverb.js] Dry/Wet ratio must be between 0 to 1.');
80
+ this.options.mix = mix;
81
+ this.dryGainNode.gain.value = 1 - mix;
82
+ this.wetGainNode.gain.value = mix;
83
+ console.debug(`[Reverb.js] Set dry/wet ratio to ${100 * mix}%`);
84
+ }
85
+ time(value) {
86
+ if (!Reverb.inRange(value, 1, 50)) throw new RangeError('[Reverb.js] Time length of impulse response must be less than 50sec.');
87
+ this.options.time = value;
88
+ this.buildImpulse();
89
+ console.debug(`[Reverb.js] Set impulse response time length to ${value}sec.`);
90
+ }
91
+ decay(value) {
92
+ if (!Reverb.inRange(value, 0, 100)) throw new RangeError('[Reverb.js] Impulse Response decay level must be less than 100.');
93
+ this.options.decay = value;
94
+ this.buildImpulse();
95
+ console.debug(`[Reverb.js] Set impulse response decay level to ${value}.`);
96
+ }
97
+ delay(value) {
98
+ if (!Reverb.inRange(value, 0, 100)) throw new RangeError('[Reverb.js] Impulse Response delay time must be less than 100.');
99
+ this.options.delay = value;
100
+ this.buildImpulse();
101
+ console.debug(`[Reverb.js] Set impulse response delay time to ${value}sec.`);
102
+ }
103
+ reverse(reverse) {
104
+ this.options.reverse = reverse;
105
+ this.buildImpulse();
106
+ console.debug(`[Reverb.js] Inpulse response is ${reverse ? '' : 'not '}reversed.`);
107
+ }
108
+ filterType(type = 'allpass') {
109
+ this.filterNode.type = this.options.filterType = type;
110
+ console.debug(`[Reverb.js] Set filter type to ${type}`);
111
+ }
112
+ filterFreq(freq) {
113
+ if (!Reverb.inRange(freq, 20, 20000)) throw new RangeError('[Reverb.js] Filter frequrncy must be between 20 and 20000.');
114
+ this.options.filterFreq = freq;
115
+ this.filterNode.frequency.value = this.options.filterFreq;
116
+ console.debug(`[Reverb.js] Set filter frequency to ${freq}Hz.`);
117
+ }
118
+ filterQ(q) {
119
+ if (!Reverb.inRange(q, 0, 10)) throw new RangeError('[Reverb.js] Filter Q value must be between 0 and 10.');
120
+ this.options.filterQ = q;
121
+ this.filterNode.Q.value = this.options.filterQ;
122
+ console.debug(`[Reverb.js] Set filter Q to ${q}.`);
123
+ }
124
+ peaks(p) {
125
+ this.options.peaks = p;
126
+ this.buildImpulse();
127
+ console.debug(`[Reverb.js] Set IR source noise peaks to ${p}.`);
128
+ }
129
+ scale(s) {
130
+ this.options.scale = s;
131
+ this.buildImpulse();
132
+ console.debug(`[Reverb.js] Set IR source noise scale to ${s}.`);
133
+ }
134
+ getNoise(duration) {
135
+ return [
136
+ ...take(duration, this.noise({
137
+ bins: this.options.peaks,
138
+ scale: this.options.scale,
139
+ rnd: this.options.randomAlgorithm
140
+ }))
141
+ ];
142
+ }
143
+ setNoise(type) {
144
+ this.options.noise = type;
145
+ switch(type){
146
+ case 'blue':
147
+ this.noise = this.noiseMap.blue;
148
+ break;
149
+ case 'brown':
150
+ this.noise = this.noiseMap.brown;
151
+ break;
152
+ case 'green':
153
+ this.noise = this.noiseMap.green;
154
+ break;
155
+ case 'pink':
156
+ this.noise = this.noiseMap.pink;
157
+ break;
158
+ case 'red':
159
+ this.noise = this.noiseMap.red;
160
+ break;
161
+ case 'violet':
162
+ this.noise = this.noiseMap.violet;
163
+ break;
164
+ case 'white':
165
+ this.noise = this.noiseMap.white;
166
+ break;
167
+ default:
168
+ this.noise = white;
169
+ break;
170
+ }
171
+ this.buildImpulse();
172
+ console.debug(`[Reverb.js] Set IR generator source noise type to ${type}.`);
173
+ }
174
+ setRandomAlgorithm(algorithm) {
175
+ this.options.randomAlgorithm = algorithm;
176
+ this.buildImpulse();
177
+ console.debug("[Reverb.js] Set IR source noise generator.");
178
+ }
179
+ static inRange(x, min, max) {
180
+ return x >= min && x <= max;
181
+ }
182
+ buildImpulse() {
183
+ const rate = this.ctx.sampleRate;
184
+ const duration = Math.max(rate * this.options.time, 1);
185
+ const delayDuration = rate * this.options.delay;
186
+ const impulse = this.ctx.createBuffer(2, duration, rate);
187
+ const impulseL = new Float32Array(duration);
188
+ const impulseR = new Float32Array(duration);
189
+ const sampleL = new Float32Array(1);
190
+ const sampleR = new Float32Array(1);
191
+ const noiseL = this.getNoise(duration);
192
+ const noiseR = this.getNoise(duration);
193
+ for(let i = 0; i < duration; i++){
194
+ let n;
195
+ if (i < delayDuration) {
196
+ sampleL[0] = 0;
197
+ sampleR[0] = 0;
198
+ impulseL.set(sampleL, i);
199
+ impulseR.set(sampleR, i);
200
+ n = this.options.reverse ?? false ? duration - (i - delayDuration) : i - delayDuration;
201
+ } else n = this.options.reverse ?? false ? duration - i : i;
202
+ sampleL[0] = (noiseL.at(i) ?? 0) * (1 - n / duration) ** this.options.decay;
203
+ sampleR[0] = (noiseR.at(i) ?? 0) * (1 - n / duration) ** this.options.decay;
204
+ impulseL.set(sampleL, i);
205
+ impulseR.set(sampleR, i);
206
+ }
207
+ impulse.getChannelData(0).set(impulseL);
208
+ impulse.getChannelData(1).set(impulseR);
209
+ this.convolverNode.buffer = impulse;
210
+ }
211
+ constructor(ctx, options = {}){
212
+ _define_property(this, "ctx", void 0);
213
+ _define_property(this, "wetGainNode", void 0);
214
+ _define_property(this, "dryGainNode", void 0);
215
+ _define_property(this, "filterNode", void 0);
216
+ _define_property(this, "convolverNode", void 0);
217
+ _define_property(this, "outputNode", void 0);
218
+ _define_property(this, "options", void 0);
219
+ _define_property(this, "isConnected", void 0);
220
+ _define_property(this, "noise", white);
221
+ _define_property(this, "noiseMap", NoiseType);
222
+ this.ctx = ctx;
223
+ this.options = Object.assign({}, defaults, options);
224
+ this.wetGainNode = this.ctx.createGain();
225
+ this.dryGainNode = this.ctx.createGain();
226
+ this.filterNode = this.ctx.createBiquadFilter();
227
+ this.convolverNode = this.ctx.createConvolver();
228
+ this.outputNode = this.ctx.createGain();
229
+ this.isConnected = false;
230
+ this.filterType(this.options.filterType);
231
+ this.setNoise(this.options.noise);
232
+ this.buildImpulse();
233
+ this.mix(this.options.mix);
234
+ }
235
+ }
236
+ _define_property(Reverb, "version", Meta.version);
237
+ _define_property(Reverb, "build", Meta.date);
238
+ export default Reverb;
239
+
240
+ //# sourceMappingURL=Reverb.es.js.map