From bb39eb19f9df43162f81910b07571c8f16fe0615 Mon Sep 17 00:00:00 2001 From: Kevin Ngo Date: Fri, 12 Oct 2018 00:28:52 -0700 Subject: [PATCH] [bump npm for audioanalyser]hook up to gain node, clean up song.js a bit --- package.json | 2 +- src/components/song.js | 33 +++++++++++++++------------------ 2 files changed, 16 insertions(+), 19 deletions(-) diff --git a/package.json b/package.json index b8caf99..1569c3e 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,7 @@ "start": "webpack-dev-server --host 0.0.0.0 --progress --colors --hot --inline --port 3000" }, "dependencies": { - "aframe-audioanalyser-component": "^5.0.0-beta1", + "aframe-audioanalyser-component": "^5.0.0-beta2", "aframe-atlas-uvs-component": "^2.0.0", "aframe-event-decorators": "^1.0.2", "aframe-event-set-component": "^4.0.1", diff --git a/src/components/song.js b/src/components/song.js index cb4cfd4..fea7336 100644 --- a/src/components/song.js +++ b/src/components/song.js @@ -16,7 +16,12 @@ AFRAME.registerComponent('song', { init: function () { this.analyserSetter = {buffer: true}; - this.context = this.data.analyserEl.components.audioanalyser.context; + this.audioAnalyser = this.data.analyserEl.components.audioanalyser; + this.context = this.audioAnalyser.context; + this.victory = this.victory.bind(this); + + // Base volume. + this.audioAnalyser.gainNode.gain.value = 0.75; // Restart, get new buffer source node and play. this.el.addEventListener('pausemenurestart', () => { @@ -25,22 +30,8 @@ AFRAME.registerComponent('song', { this.source = evt.detail; if (this.data.isBeatsPreloaded) { this.source.start(); } }, once); - this.data.analyserEl.components.audioanalyser.refreshSource(); + this.audioAnalyser.refreshSource(); }); - - /* - this.el.addEventListener('pausemenuexit', () => { - this.data.analyserEl.components.audioanalyser.suspendContext(); - }); - - audio.addEventListener('ended', () => { - if (this.data.isPlaying) { - this.el.sceneEl.emit('victory', null, false); - audio.pause(); - audio.currentTime = 0; - } - }); - */ }, update: function (oldData) { @@ -78,12 +69,12 @@ AFRAME.registerComponent('song', { // Pause / stop. if (oldData.isPlaying && !data.isPlaying) { - data.analyserEl.components.audioanalyser.suspendContext(); + this.audioAnalyser.suspendContext(); } // Resume. if (!oldData.isPlaying && data.isPlaying && this.source) { - data.analyserEl.components.audioanalyser.resumeContext(); + this.audioAnalyser.resumeContext(); } }, @@ -92,6 +83,7 @@ AFRAME.registerComponent('song', { return new Promise(resolve => { data.analyserEl.addEventListener('audioanalyserbuffersource', evt => { this.source = evt.detail; + this.source.onended = this.victory; resolve(this.source); }, once); this.analyserSetter.src = utils.getS3FileUrl(data.challengeId, 'song.ogg'); @@ -107,5 +99,10 @@ AFRAME.registerComponent('song', { this.source.stop(); this.source.disconnect(); this.source = null; + }, + + victory: function () { + if (!this.data.isPlaying) { return; } + this.el.sceneEl.emit('victory', null, false); } });