diff --git a/src/VisualizerCore.js b/src/VisualizerCore.js index 0db3e9c..e49d0d9 100644 --- a/src/VisualizerCore.js +++ b/src/VisualizerCore.js @@ -1,54 +1 @@ -"use strict"; - -function VisualizerCore(mediaSource, fftSize = 1024) { - this._stream = mediaSource; - this._analyzing = false; - this._updateListeners = []; - this._audioCtx = new window.AudioContext(); - this._source = null; // Prepare for either a MediaStream or a MediaElement. - try { - this.source = this._audioCtx.createMediaStreamSource(this._stream); - } catch (e) { - this._source = this._audioCtx.createMediaElementSource(this._stream); - } - this._analyzer = this._audioCtx.createAnalyser(); - this._analyzer.fftSize = fftSize; - this._source.connect(this._analyzer); - this._analyzer.connect(this._audioCtx.destination); - this._buffer = new Uint8Array(this._analyzer.frequencyBinCount); - - this.lastUpdate = null; - - this.analyze = function () { - if (this._analyzing) { - return; - } - this._analyzing = true; - requestAnimationFrame(this.update); - }; - - let vcore = this; // since calling from requestAnimationFrame means "this" is no longer set to produced object. - this.update = function (timestamp) { - if (!vcore._analyzing) return; - - requestAnimationFrame(vcore.update); - if (!vcore.lastUpdate) { - vcore.lastUpdate = timestamp; - } - let delta = timestamp - vcore.lastUpdate; - vcore._analyzer.getByteFrequencyData(vcore._buffer); - - vcore._updateListeners.forEach(listener => { - listener(delta, vcore._buffer); - }); - }; - this.stop = function () { - this._analyzing = false; - }; - this.addUpdateListener = function (listener) { - this._updateListeners.push(listener); - }; - this.getNumberOfBins = function () { - return this._buffer.length; - }; -} +// TODO: Reorganize such that audioshowkit.js uses VisualizerCore.js. \ No newline at end of file diff --git a/src/audioshowkit.js b/src/audioshowkit.js index 9a390c3..0db3e9c 100644 --- a/src/audioshowkit.js +++ b/src/audioshowkit.js @@ -1 +1,54 @@ -"use strict"; \ No newline at end of file +"use strict"; + +function VisualizerCore(mediaSource, fftSize = 1024) { + this._stream = mediaSource; + this._analyzing = false; + this._updateListeners = []; + this._audioCtx = new window.AudioContext(); + this._source = null; // Prepare for either a MediaStream or a MediaElement. + try { + this.source = this._audioCtx.createMediaStreamSource(this._stream); + } catch (e) { + this._source = this._audioCtx.createMediaElementSource(this._stream); + } + this._analyzer = this._audioCtx.createAnalyser(); + this._analyzer.fftSize = fftSize; + this._source.connect(this._analyzer); + this._analyzer.connect(this._audioCtx.destination); + this._buffer = new Uint8Array(this._analyzer.frequencyBinCount); + + this.lastUpdate = null; + + this.analyze = function () { + if (this._analyzing) { + return; + } + this._analyzing = true; + requestAnimationFrame(this.update); + }; + + let vcore = this; // since calling from requestAnimationFrame means "this" is no longer set to produced object. + this.update = function (timestamp) { + if (!vcore._analyzing) return; + + requestAnimationFrame(vcore.update); + if (!vcore.lastUpdate) { + vcore.lastUpdate = timestamp; + } + let delta = timestamp - vcore.lastUpdate; + vcore._analyzer.getByteFrequencyData(vcore._buffer); + + vcore._updateListeners.forEach(listener => { + listener(delta, vcore._buffer); + }); + }; + this.stop = function () { + this._analyzing = false; + }; + this.addUpdateListener = function (listener) { + this._updateListeners.push(listener); + }; + this.getNumberOfBins = function () { + return this._buffer.length; + }; +} diff --git a/tests/index.html b/tests/index.html index be80ddd..3f493e3 100644 --- a/tests/index.html +++ b/tests/index.html @@ -8,7 +8,7 @@ - +