An equalizer made of bars
Updated
Every frame a script reads an Audio Analyzer’s frequency data, splits the lower half into eight bands and scales a bar to each band’s level.
Click once to let the browser start the sound. Then watch the bars jump with the beat. Drag to orbit the camera, scroll to zoom.
An equalizer made of bars
#pragma lifecycle(startup, update)
// Scene: an Audio entity "Music" with an Audio Analyzer (fftSize: 512),// and eight thin box meshes named "Bar 1" … "Bar 8" standing in a row.const BAR_COUNT = 8;let analyzer;const bars = [];
function startup() { analyzer = findEntity('Music').getComponent(AudioAnalyzerComponent); for (let i = 1; i <= BAR_COUNT; i++) bars.push(findEntity(`Bar ${i}`));}
function update() { const bins = analyzer.getFrequencyData(); if (!bins) return;
// Split the lower half of the spectrum into equal bands, one per bar. const usable = Math.floor(bins.length / 2); const perBar = Math.floor(usable / BAR_COUNT); for (let b = 0; b < BAR_COUNT; b++) { let sum = 0; for (let i = b * perBar; i < (b + 1) * perBar; i++) sum += bins[i]; const level = sum / (perBar * 255); bars[b].scale.y = 0.1 + level * 3; }}How it works
Section titled “How it works”The script is explained step by step in An equalizer made of bars.
Open it in XR Creator
Section titled “Open it in XR Creator”Download the project file below, then import it in XR Hub with Projects › New › Import, as described in Projects and templates. It opens in XR Creator with the same scene you see running above, each script already attached to its Behavior entity. From there you can edit the scripts, adjust components and their props, preview your changes, and deploy the result as a world of your own.
EqualizerBars.xrproj