From 41a7bae85b3521471cd8ef6bcc4f3e08a65ae56c Mon Sep 17 00:00:00 2001 From: Kevin Ngo Date: Tue, 16 Oct 2018 22:03:02 -0700 Subject: [PATCH] fixes with restoring gain --- src/components/song.js | 34 +++++++++++++++++++++++----------- src/index.html | 2 +- 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/src/components/song.js b/src/components/song.js index 82db593..3d2e1a0 100644 --- a/src/components/song.js +++ b/src/components/song.js @@ -35,14 +35,8 @@ AFRAME.registerComponent('song', { var data = this.data; // Game over, slow down audio, and then stop. - if (!oldData.isGameOver && data.isGameOver && this.source) { - this.source.playbackRate.setValueAtTime(this.source.playbackRate.value, - this.context.currentTime); - this.source.playbackRate.linearRampToValueAtTime(0, this.context.currentTime + 3.5); - this.audioAnalyser.gainNode.gain.setValueAtTime(this.audioAnalyser.gainNode.gain.value, - this.context.currentTime); - this.audioAnalyser.gainNode.gain.linearRampToValueAtTime(0, this.context.currentTime + 3.5); - setTimeout(() => { this.stopAudio(); }, 3500); + if (!oldData.isGameOver && data.isGameOver) { + this.onGameOver(); return; } @@ -52,7 +46,7 @@ AFRAME.registerComponent('song', { // New challenge, play if we have loaded and were waiting for beats to preload. if (!oldData.isBeatsPreloaded && this.data.isBeatsPreloaded && this.source) { - this.source.start(); + this.startAudio(); } if (oldData.challengeId && !data.challengeId) { @@ -65,7 +59,7 @@ AFRAME.registerComponent('song', { this.el.sceneEl.emit('songloadstart', null, false); this.getAudio().then(source => { this.el.sceneEl.emit('songloadfinish', null, false); - if (this.data.isBeatsPreloaded) { source.start(); } + if (this.data.isBeatsPreloaded) { this.startAudio(); } }).catch(console.error); } @@ -119,13 +113,26 @@ AFRAME.registerComponent('song', { if (progress >= 1) { this.el.sceneEl.emit('songfetchfinish', null, false); } }, + onGameOver: function () { + this.source.playbackRate.setValueAtTime(this.source.playbackRate.value, + this.context.currentTime); + this.source.playbackRate.linearRampToValueAtTime(0, this.context.currentTime + 3.5); + this.audioAnalyser.gainNode.gain.setValueAtTime(this.audioAnalyser.gainNode.gain.value, + this.context.currentTime); + this.audioAnalyser.gainNode.gain.linearRampToValueAtTime(0, this.context.currentTime + 3.5); + setTimeout(() => { + if (!this.data.isGameOver) { return; } + this.stopAudio(); + }, 3500); + }, + onRestart: function () { // Restart, get new buffer source node and play. if (this.source) { this.source.disconnect(); } this.data.analyserEl.addEventListener('audioanalyserbuffersource', evt => { this.source = evt.detail; this.el.sceneEl.emit('songloadfinish', null, false); - if (this.data.isBeatsPreloaded) { this.source.start(); } + if (this.data.isBeatsPreloaded) { this.startAudio(); } }, ONCE); this.audioAnalyser.refreshSource(); }, @@ -140,5 +147,10 @@ AFRAME.registerComponent('song', { const gain = this.audioAnalyser.gainNode.gain; gain.linearRampToValueAtTime(BASE_VOLUME, this.context.currentTime + 0.2); this.source.detune.linearRampToValueAtTime(0, this.context.currentTime + 0.2); + }, + + startAudio: function () { + this.audioAnalyser.gainNode.gain.setValueAtTime(BASE_VOLUME, this.context.currentTime); + this.source.start(); } }); diff --git a/src/index.html b/src/index.html index 1fae2ea..dc50ea6 100644 --- a/src/index.html +++ b/src/index.html @@ -176,7 +176,7 @@