From 5ba49351d9813de6bf6f686aaf39ae98f74f20c4 Mon Sep 17 00:00:00 2001 From: Yotam Mann Date: Thu, 28 Dec 2017 18:43:53 -0500 Subject: [PATCH] additional safety flag for if the internal BufferSourceNode is actually started checking if the offset < duration keeps safari from going into invalid state --- Tone/source/BufferSource.js | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/Tone/source/BufferSource.js b/Tone/source/BufferSource.js index f71212f3..12d0838a 100644 --- a/Tone/source/BufferSource.js +++ b/Tone/source/BufferSource.js @@ -27,6 +27,15 @@ define(["Tone/core/Tone", "Tone/core/Buffer", "Tone/source/Source", "Tone/core/G */ this._startTime = -1; + /** + * An additional flag if the actual BufferSourceNode + * has been started. b/c stopping an unstarted buffer + * will throw it into an invalid state + * @type {Boolean} + * @private + */ + this._sourceStarted = false; + /** * The time that the buffer is scheduled to stop. * @type {Number} @@ -211,7 +220,10 @@ define(["Tone/core/Tone", "Tone/core/Buffer", "Tone/source/Source", "Tone/core/G this._source.buffer = this.buffer.get(); this._source.loopEnd = this.loopEnd || this.buffer.duration; Tone.isPast(time); - this._source.start(time, offset); + if (offset < this.buffer.duration){ + this._sourceStarted = true; + this._source.start(time, offset); + } } else { throw new Error("Tone.BufferSource: buffer is either not set or not loaded."); } @@ -285,7 +297,9 @@ define(["Tone/core/Tone", "Tone/core/Buffer", "Tone/source/Source", "Tone/core/G Tone.BufferSource.prototype._onended = function(){ //allow additional time for the exponential curve to fully decay var additionalTail = this.curve === "exponential" ? this.fadeOut * 2 : 0; - this._source.stop(this._stopTime + additionalTail); + if (this._sourceStarted){ + this._source.stop(this._stopTime + additionalTail); + } this.onended(this); };