Skip to content

Commit ac65fcf

Browse files
authored
dispose of ConvolverNode before resetting buffer (#321)
Fix a new error that says `Cannot set buffer to non-null after it has been already been set to a non-null buffer`
1 parent c9d4374 commit ac65fcf

File tree

1 file changed

+29
-24
lines changed

1 file changed

+29
-24
lines changed

src/reverb.js

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,11 @@ define(function (require) {
4444
p5.Reverb = function() {
4545
Effect.call(this);
4646

47-
this.convolverNode = this.ac.createConvolver();
47+
this._initConvolverNode();
4848

4949
// otherwise, Safari distorts
5050
this.input.gain.value = 0.5;
5151

52-
this.input.connect(this.convolverNode);
53-
this.convolverNode.connect(this.wet);
54-
5552
// default params
5653
this._seconds = 3;
5754
this._decay = 2;
@@ -62,6 +59,25 @@ define(function (require) {
6259
};
6360

6461
p5.Reverb.prototype = Object.create(Effect.prototype);
62+
63+
p5.Reverb.prototype._initConvolverNode = function() {
64+
this.convolverNode = this.ac.createConvolver();
65+
this.input.connect(this.convolverNode);
66+
this.convolverNode.connect(this.wet);
67+
};
68+
69+
p5.Reverb.prototype._teardownConvolverNode = function() {
70+
if (this.convolverNode) {
71+
this.convolverNode.disconnect();
72+
delete this.convolverNode;
73+
}
74+
};
75+
76+
p5.Reverb.prototype._setBuffer = function(audioBuffer) {
77+
this._teardownConvolverNode();
78+
this._initConvolverNode();
79+
this.convolverNode.buffer = audioBuffer;
80+
};
6581
/**
6682
* Connect a source to the reverb, and assign reverb parameters.
6783
*
@@ -164,15 +180,12 @@ define(function (require) {
164180
impulseL[i] = (Math.random() * 2 - 1) * Math.pow(1 - n / length, decay);
165181
impulseR[i] = (Math.random() * 2 - 1) * Math.pow(1 - n / length, decay);
166182
}
167-
this.convolverNode.buffer = impulse;
183+
this._setBuffer(impulse);
168184
};
169185

170186
p5.Reverb.prototype.dispose = function() {
171187
Effect.prototype.dispose.apply(this);
172-
if (this.convolverNode) {
173-
this.convolverNode.buffer = null;
174-
this.convolverNode = null;
175-
}
188+
this._teardownConvolverNode();
176189
};
177190

178191
// =======================================================================
@@ -233,23 +246,20 @@ define(function (require) {
233246
* </code></div>
234247
*/
235248
p5.Convolver = function(path, callback, errorCallback) {
236-
Effect.call(this);
249+
p5.Reverb.call(this);
237250

238251
/**
239252
* Internally, the p5.Convolver uses the a
240253
* <a href="http://www.w3.org/TR/webaudio/#ConvolverNode">
241254
* Web Audio Convolver Node</a>.
242255
*
243-
* @property {ConvolverNode} convolverNod
256+
* @property {ConvolverNode} convolverNode
244257
*/
245-
this.convolverNode = this.ac.createConvolver();
258+
this._initConvolverNode();
246259

247260
// otherwise, Safari distorts
248261
this.input.gain.value = 0.5;
249262

250-
this.input.connect(this.convolverNode);
251-
this.convolverNode.connect(this.wet);
252-
253263
if (path) {
254264
this.impulses = [];
255265
this._loadBuffer(path, callback, errorCallback);
@@ -358,7 +368,7 @@ define(function (require) {
358368
buffer.name = chunks[chunks.length - 1];
359369
buffer.audioBuffer = buff;
360370
self.impulses.push(buffer);
361-
self.convolverNode.buffer = buffer.audioBuffer;
371+
self._setBuffer(buffer.audioBuffer);
362372
if (callback) {
363373
callback(buffer);
364374
}
@@ -509,32 +519,27 @@ define(function (require) {
509519
*/
510520
p5.Convolver.prototype.toggleImpulse = function(id) {
511521
if (typeof id === 'number' && id < this.impulses.length) {
512-
this.convolverNode.buffer = this.impulses[id].audioBuffer;
522+
this._setBuffer(this.impulses[id].audioBuffer);
513523
}
514524
if (typeof id === 'string') {
515525
for (var i = 0; i < this.impulses.length; i++) {
516526
if (this.impulses[i].name === id) {
517-
this.convolverNode.buffer = this.impulses[i].audioBuffer;
527+
this._setBuffer(this.impulses[i].audioBuffer);
518528
break;
519529
}
520530
}
521531
}
522532
};
523533

524534
p5.Convolver.prototype.dispose = function() {
525-
Effect.prototype.dispose.apply(this);
535+
p5.Reverb.prototype.dispose.apply(this);
526536

527537
// remove all the Impulse Response buffers
528538
for (var i in this.impulses) {
529539
if (this.impulses[i]) {
530540
this.impulses[i] = null;
531541
}
532542
}
533-
534-
if (this.convolverNode) {
535-
this.convolverNode.disconnect();
536-
this.concolverNode = null;
537-
}
538543
};
539544

540545
});

0 commit comments

Comments
 (0)