From be7c9adb55a8886f504b2be768cad1e2aabfe7a0 Mon Sep 17 00:00:00 2001 From: William Toohey Date: Thu, 31 Mar 2016 15:28:06 +1000 Subject: [PATCH] Fix missing beats on song change --- src/js/HuesCore.js | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/js/HuesCore.js b/src/js/HuesCore.js index 0123878..42a37f5 100644 --- a/src/js/HuesCore.js +++ b/src/js/HuesCore.js @@ -331,8 +331,10 @@ HuesCore.prototype.animationLoop = function() { this.callEventListeners("frame"); }; -HuesCore.prototype.recalcBeatIndex = function() { - this.beatIndex = Math.floor(this.soundManager.currentTime() / this.getBeatLength()); +HuesCore.prototype.recalcBeatIndex = function(forcedNow) { + let now = typeof forcedNow === "number" ? forcedNow : this.soundManager.currentTime(); + // getBeatLength isn't updated with the right beatIndex yet + this.beatIndex = Math.floor(now / (now < 0 ? this.buildLength : this.loopLength)); // beatIndex is NaN, abort if(this.beatIndex != this.beatIndex) { this.setInvert(false); @@ -495,7 +497,8 @@ HuesCore.prototype.fillBuildup = function() { // update with a buildup of possibly different length this.updateBeatLength(); // If we're in the build or loop this will adjust - this.recalcBeatIndex(); + // If we've lagged a bit, we'll miss the first beat. Rewind! + this.recalcBeatIndex(this.doBuildup ? -this.soundManager.buildLength : 0); }; HuesCore.prototype.randomSong = function() {