Moved logic for calculating loop time into separate method

This commit is contained in:
Pavle Goloskokovic 2017-12-21 17:21:59 +01:00
parent 4f6a665451
commit 15db2bac74

View file

@ -155,14 +155,7 @@ var WebAudioSound = new Class({
* @private * @private
*/ */
createAndStartLoopBufferSource: function () { createAndStartLoopBufferSource: function () {
var lastRateUpdateCurrentTime = 0; var when = this.getLoopTime();
for (var i = 0; i < this.rateUpdates.length - 1; i++) {
lastRateUpdateCurrentTime +=
(this.rateUpdates[i + 1].time - this.rateUpdates[i].time) * this.rateUpdates[i].rate;
}
var lastRateUpdate = this.rateUpdates[this.rateUpdates.length - 1];
var when = this.playTime + lastRateUpdate.time
+ (this.duration - lastRateUpdateCurrentTime) / lastRateUpdate.rate;
var offset = this.currentMarker ? this.currentMarker.start : 0; var offset = this.currentMarker ? this.currentMarker.start : 0;
var duration = this.duration; var duration = this.duration;
this.loopSource = this.createBufferSource(); this.loopSource = this.createBufferSource();
@ -271,6 +264,19 @@ var WebAudioSound = new Class({
currentTime += (nextTime - this.rateUpdates[i].time) * this.rateUpdates[i].rate; currentTime += (nextTime - this.rateUpdates[i].time) * this.rateUpdates[i].rate;
} }
return currentTime; return currentTime;
},
/**
* @private
*/
getLoopTime: function () {
var lastRateUpdateCurrentTime = 0;
for (var i = 0; i < this.rateUpdates.length - 1; i++) {
lastRateUpdateCurrentTime +=
(this.rateUpdates[i + 1].time - this.rateUpdates[i].time) * this.rateUpdates[i].rate;
}
var lastRateUpdate = this.rateUpdates[this.rateUpdates.length - 1];
return this.playTime + lastRateUpdate.time
+ (this.duration - lastRateUpdateCurrentTime) / lastRateUpdate.rate;
} }
}); });
/** /**