diff --git a/src/components/beat-loader.js b/src/components/beat-loader.js index 0d671f4..a460fa5 100644 --- a/src/components/beat-loader.js +++ b/src/components/beat-loader.js @@ -133,19 +133,21 @@ AFRAME.registerComponent('beat-loader', { * Generate beats and stuff according to timestamp. */ tick: function (time, delta) { - const beatsTime = this.beatsTime; var i; var noteTime; if (!this.data.isPlaying || !this.data.challengeId || !this.beatData) { return; } // Re-sync song with beats playback. - if (this.beatsTimeOffset !== undefined && - this.songCurrentTime !== this.el.components.song.context.currentTime) { - this.songCurrentTime = this.el.components.song.context.currentTime; + const songComponent = this.el.components.song; + const currentTime = songComponent.context.currentTime - songComponent.songStartTime; + if (songComponent.songStartTime && this.beatsTimeOffset !== undefined && + this.songCurrentTime !== currentTime) { + this.songCurrentTime = currentTime; this.beatsTime = (this.songCurrentTime + this.data.beatAnticipationTime) * 1000; } + const beatsTime = this.beatsTime; const bpm = this.beatData._beatsPerMinute; const msPerBeat = 1000 * 60 / this.beatData._beatsPerMinute; diff --git a/src/components/song.js b/src/components/song.js index f1a0dd2..b068d8b 100644 --- a/src/components/song.js +++ b/src/components/song.js @@ -22,6 +22,7 @@ AFRAME.registerComponent('song', { this.audioAnalyser = this.data.analyserEl.components.audioanalyser; this.context = this.audioAnalyser.context; this.songLoadingIndicator = document.getElementById('songLoadingIndicator'); + this.songStartTime = 0; this.victory = this.victory.bind(this); @@ -194,6 +195,7 @@ AFRAME.registerComponent('song', { startAudio: function () { const gain = this.audioAnalyser.gainNode.gain; gain.setValueAtTime(BASE_VOLUME, this.context.currentTime); + this.songStartTime = this.context.currentTime; this.source.start(); } });