diff --git a/src/visualization/VisUpdateRouter.js b/src/visualization/VisUpdateRouter.js index 0c996d7..72a3a48 100644 --- a/src/visualization/VisUpdateRouter.js +++ b/src/visualization/VisUpdateRouter.js @@ -15,17 +15,11 @@ export default class VisUpdateRouter { #lastBins; #visualizer; - /** - * @callback VisUpdateRouter~visualizerBinUpdateListener - * @param {number} timeDelta elapsed time since last update. - * @param {number} amplitude The amplitude of the associated bin. - * @param {number} ampDelta change in amplitude of the frequency bin. - */ - /** * @callback VisUpdateRouter~visualizerRangedUpdateListener * @param {number} timeDelta elapsed time since last update. * @param {number} bins the bins of the range. + * @param {number} sigBin The bin with the greatest amplitude. If this bin is outside of the given bounds for the updater, it is rounded to the nearest bound. */ /** @@ -39,7 +33,7 @@ export default class VisUpdateRouter { this.#visualizer.addUpdateListener(this.#visualizerListener); } - #visualizerListener = (delta, bins) => { + #visualizerListener = (delta, bins, sigBin) => { const copyOfRangedListeners = [... this.listeners]; // We assume this is sorted properly. A priority queue could be better. for (let binInd = 0; binInd < this.#lastBins.length; binInd++) { const lastBin = this.#lastBins[binInd]; @@ -48,7 +42,7 @@ export default class VisUpdateRouter { const { lower, upper, listener } = copyOfRangedListeners[rangedInd]; if (lower > binInd) break; // Don't need to check the rest if the current lowest minimum is greater than the current bin index. if (binInd <= upper) { - listener(delta, bins.slice(lower, upper)); + listener(delta, bins.slice(lower, upper), Math.min(Math.max(sigBin, lower), upper)); copyOfRangedListeners.shift(); rangedInd--; } diff --git a/src/visualization/Visualizer.js b/src/visualization/Visualizer.js index 74484ff..93abd29 100644 --- a/src/visualization/Visualizer.js +++ b/src/visualization/Visualizer.js @@ -17,6 +17,7 @@ export default class Visualizer { * @callback Visualizer~visualizerUpdateListener * @param {number} delta elapsed time since last update. * @param {Uint8Array} bins the bins with varying frequency values. + * @param {number} sigBin The bin with the greatest amplitude. */ /** @@ -66,7 +67,7 @@ export default class Visualizer { self.#analyzer.getByteFrequencyData(self.#buffer); self.#updateListeners.forEach(listener => { - listener(delta, self.#buffer); + listener(delta, self.#buffer, self.#buffer.indexOf(Math.max(self.#buffer))); }); requestAnimationFrame(update); };