Skip to content

Commit

Permalink
Merge pull request #320 from Spongman/fix-callback-crash
Browse files Browse the repository at this point in the history
prevent crashes during completion of callbacks on disposed SoundFiles
  • Loading branch information
therewasaguy authored Sep 11, 2018
2 parents 9f84ca5 + 469c799 commit 429b953
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/soundfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,9 +221,11 @@ define(function (require) {
request.onload = function() {
if (request.status === 200) {
// on sucess loading file:
if (!self.panner) return;
ac.decodeAudioData(request.response,
// success decoding buffer:
function(buff) {
if (!self.panner) return;
self.buffer = buff;
self.panner.inputChannels(buff.numberOfChannels);
if (callback) {
Expand All @@ -232,6 +234,7 @@ define(function (require) {
},
// error decoding buffer. "e" is undefined in Chrome 11/22/2015
function() {
if (!self.panner) return;
var err = new CustomError('decodeAudioData', errorTrace, self.url);
var msg = 'AudioContext error at decodeAudioData for ' + self.url;
if (errorCallback) {
Expand All @@ -245,6 +248,7 @@ define(function (require) {
}
// if request status != 200, it failed
else {
if (!self.panner) return;
var err = new CustomError('loadSound', errorTrace, self.url);
var msg = 'Unable to load ' + self.url + '. The request status was: ' +
request.status + ' (' + request.statusText + ')';
Expand Down Expand Up @@ -276,7 +280,9 @@ define(function (require) {
else if (this.file !== undefined) {
var reader = new FileReader();
reader.onload = function() {
if (!self.panner) return;
ac.decodeAudioData(reader.result, function(buff) {
if (!self.panner) return;
self.buffer = buff;
self.panner.inputChannels(buff.numberOfChannels);
if (callback) {
Expand All @@ -285,6 +291,7 @@ define(function (require) {
});
};
reader.onerror = function(e) {
if (!self.panner) return;
if (onerror) {
onerror(e);
}
Expand Down Expand Up @@ -1301,6 +1308,7 @@ define(function (require) {

// act on the result
offlineContext.oncomplete = function(e) {
if (!self.panner) return;
var filteredBuffer = e.renderedBuffer;
var bufferData = filteredBuffer.getChannelData(0);

Expand Down Expand Up @@ -1652,9 +1660,10 @@ define(function (require) {
* <div><code>
* var inp, button, mySound;
* var fileName = 'cool';
* function setup() {
* function preload() {
* mySound = loadSound('assets/doorbell.mp3');
*
* }
* function setup() {
* btn = createButton('click to save file');
* btn.position(0, 0);
* btn.mouseClicked(handleMouseClick);
Expand Down

0 comments on commit 429b953

Please sign in to comment.