diff --git a/src/components/audio-feedback.js b/src/components/audio-feedback.js index cdae3ef89a7b42c4745e2227a08836bc5388380b..b50980b860c8fca71ef1fb9803274285d96ffd4f 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;