Skip to content

Commit d1c5bb5

Browse files
committed
fix soundfile memory leak
1 parent b01a56b commit d1c5bb5

File tree

1 file changed

+23
-25
lines changed

1 file changed

+23
-25
lines changed

src/soundfile.js

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,6 @@ define(function (require) {
117117
// time that playback was started, in millis
118118
this.startMillis = null;
119119

120-
this.amplitude = new p5.Amplitude();
121-
this.output.connect(this.amplitude.input);
122-
123120
// stereo panning
124121
this.panPosition = 0.0;
125122
this.panner = new p5.Panner(this.output, p5sound.input, 2);
@@ -344,6 +341,9 @@ define(function (require) {
344341

345342
// make a new source and counter. They are automatically assigned playbackRate and buffer
346343
this.bufferSourceNode = this._initSourceNode();
344+
345+
// garbage collect counterNode and create a new one
346+
if (this._counterNode) this._counterNode = undefined;
347347
this._counterNode = this._initCounterNode();
348348

349349
if (_cueStart) {
@@ -401,9 +401,9 @@ define(function (require) {
401401
this.bufferSourceNode._arrayIndex = this.bufferSourceNodes.length - 1;
402402

403403
// delete this.bufferSourceNode from the sources array when it is done playing:
404-
this.bufferSourceNode.onended = function(e) {
404+
var clearOnEnd = function(e) {
405405
this._playing = false;
406-
406+
this.removeEventListener('ended', clearOnEnd, false);
407407
// call the onended callback
408408
self._onended(self);
409409

@@ -416,7 +416,9 @@ define(function (require) {
416416
if (self.bufferSourceNodes.length === 0) {
417417
self._playing = false;
418418
}
419-
}
419+
};
420+
421+
this.bufferSourceNode.onended = clearOnEnd;
420422
}
421423
// If soundFile hasn't loaded the buffer yet, throw an error
422424
else {
@@ -1063,6 +1065,11 @@ define(function (require) {
10631065

10641066
p5.SoundFile.prototype.dispose = function() {
10651067
var now = p5sound.audiocontext.currentTime;
1068+
1069+
// remove reference to soundfile
1070+
var index = p5sound.soundArray.indexOf(this);
1071+
p5sound.soundArray.splice(index, 1);
1072+
10661073
this.stop(now);
10671074
if (this.buffer && this.bufferSourceNode) {
10681075
for (var i = 0; i < this.bufferSourceNodes.length - 1; i++) {
@@ -1124,21 +1131,9 @@ define(function (require) {
11241131
};
11251132

11261133
/**
1127-
* Read the Amplitude (volume level) of a p5.SoundFile. The
1128-
* p5.SoundFile class contains its own instance of the Amplitude
1129-
* class to help make it easy to get a SoundFile's volume level.
1130-
* Accepts an optional smoothing value (0.0 < 1.0).
1131-
*
1132-
* @method getLevel
1133-
* @param {Number} [smoothing] Smoothing is 0.0 by default.
1134-
* Smooths values based on previous values.
1135-
* @return {Number} Volume level (between 0.0 and 1.0)
11361134
*/
11371135
p5.SoundFile.prototype.getLevel = function(smoothing) {
1138-
if (smoothing) {
1139-
this.amplitude.smoothing = smoothing;
1140-
}
1141-
return this.amplitude.getLevel();
1136+
console.warn('p5.SoundFile.getLevel has been removed from the library. Use p5.Amplitude instead')
11421137
};
11431138

11441139
/**
@@ -1200,6 +1195,7 @@ define(function (require) {
12001195
// dispose of scope node if it already exists
12011196
if (self._scopeNode) {
12021197
self._scopeNode.disconnect();
1198+
self._scopeNode.onaudioprocess = undefined;
12031199
self._scopeNode = null;
12041200
}
12051201

@@ -1580,7 +1576,8 @@ define(function (require) {
15801576
* @param {Number} id ID of the cue, as returned by addCue
15811577
*/
15821578
p5.SoundFile.prototype.removeCue = function(id) {
1583-
for (var i = 0; i < this._cues.length; i++) {
1579+
var cueLength = this._cues.length;
1580+
for (var i = 0; i < cueLength; i++) {
15841581
var cue = this._cues[i];
15851582
if (cue.id === id) {
15861583
this.cues.splice(i, 1);
@@ -1608,16 +1605,17 @@ define(function (require) {
16081605
// have been scheduled using addCue(callback, time).
16091606
p5.SoundFile.prototype._onTimeUpdate = function(position) {
16101607
var playbackTime = position/this.buffer.sampleRate;
1608+
var cueLength = this._cues.length;
16111609

1612-
for (var i = 0 ; i < this._cues.length; i++) {
1613-
var callbackTime = this._cues[i].time;
1614-
var val = this._cues[i].val;
1615-
1610+
for (var i = 0 ; i < cueLength; i++) {
1611+
var cue = this._cues[i];
1612+
var callbackTime = cue.time;
1613+
var val = cue.val;
16161614

16171615
if (this._prevTime < callbackTime && callbackTime <= playbackTime) {
16181616

16191617
// pass the scheduled callbackTime as parameter to the callback
1620-
this._cues[i].callback(val);
1618+
cue.callback(val);
16211619
}
16221620

16231621
}

0 commit comments

Comments
 (0)