From d967c3e6d12ee6951bc45cbb5af2515f26c10271 Mon Sep 17 00:00:00 2001
From: Marshall Quander <marshall@quander.me>
Date: Mon, 30 Jul 2018 18:12:15 -0700
Subject: [PATCH] Fix Safari incompatibility with audio feedback

---
 src/components/audio-feedback.js | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/src/components/audio-feedback.js b/src/components/audio-feedback.js
index cdae3ef89..b50980b86 100644
--- a/src/components/audio-feedback.js
+++ b/src/components/audio-feedback.js
@@ -12,7 +12,7 @@ AFRAME.registerComponent("networked-audio-analyser", {
       const ctx = THREE.AudioContext.getContext();
       this.analyser = ctx.createAnalyser();
       this.analyser.fftSize = 32;
-      this.levels = new Float32Array(this.analyser.frequencyBinCount);
+      this.levels = new Uint8Array(this.analyser.frequencyBinCount);
       event.detail.soundSource.connect(this.analyser);
     });
   },
@@ -20,11 +20,12 @@ AFRAME.registerComponent("networked-audio-analyser", {
   tick: function() {
     if (!this.analyser) return;
 
-    this.analyser.getFloatTimeDomainData(this.levels);
+    // take care with compatibility, e.g. safari doesn't support getFloatTimeDomainData
+    this.analyser.getByteTimeDomainData(this.levels);
 
     let sum = 0;
     for (let i = 0; i < this.levels.length; i++) {
-      const amplitude = this.levels[i];
+      const amplitude = (this.levels[i] - 128) / 128;
       sum += amplitude * amplitude;
     }
     this.volume = this.smoothing * Math.sqrt(sum / this.levels.length) + (1 - this.smoothing) * this.prevVolume;
-- 
GitLab