Renaming to conform to spec.

This commit is contained in:
Harrison Deng 2022-03-20 20:13:14 -05:00
parent c7716a58fa
commit b4702bfad2
3 changed files with 56 additions and 56 deletions

View File

@ -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.

View File

@ -1 +1,54 @@
"use strict";
"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;
};
}

View File

@ -8,7 +8,7 @@
<meta name='viewport' content='width=device-width, initial-scale=1'>
<link rel='stylesheet' type='text/css' media='screen' href='main.css'>
<script defer src="../src/VisualizerCore.js"></script>
<script defer src="../src/audioshowkit.js"></script>
<script defer src="../src/patterns/HorizontalBar.js"></script>
<script defer src='script.js'></script>