Audio Reactive Stage
A synced music video with a stage that dances to it: a ring of spectrum columns, a rippling floor grid and a galaxy (VFX Graphs), equaliser bars, and spotlights that sweep and flash on the beat.
Get it from the StageTime-Worlds-Examples repository: copy the Worlds/AudioReactive folder into a Unity
project that has the StageTime World SDK, and open its scene.
The sample doesn't include a video. Set the Music Video object's Video Player clip, or its ST Video Player link, to music you have the rights to use.
| Piece | Built with |
|---|---|
| Video | ST Video Player (synced, auto play, loop); its Video Player's Audio Output Mode is Audio Source |
| Analysis | An ST Audio Analyser listening to that Audio Source, driving three VFX Graphs through their Audio… properties |
| Bars, lights, label | This script, reading the analyser every frame and reacting to OnAudioBeat |
Every device analyses its own copy of the synced video's sound, so nothing here is sent over the network.
AudioReactiveStage.cs
using StageTime.SDK;
using TMPro;
using UnityEngine;
/// <summary>
/// Makes a stage dance to a synced music video. An ST Audio Analyser listens to the video's Audio Source and drives the
/// VFX Graphs itself (it is given them in the Inspector); this script adds the rest: equaliser bars that follow the
/// spectrum, spotlights that sweep with the room clock and flash on every beat, and a label with the levels.
/// Nothing here is synced: the video is, and every device analyses its own copy of the sound.
/// </summary>
public class AudioReactiveStage : StageTimeBehaviour
{
public STAudioAnalyser analyser;
public STVideoPlayer video;
[Tooltip("Equaliser bars, lowest frequency first; each is scaled on Y.")]
public Transform[] bars;
[Tooltip("Tallest bar height (Y scale).")]
public float barHeight = 2.5f;
[Tooltip("Spotlights that flash on the beat.")]
public Light[] spotlights;
[Tooltip("Turned around Y with the room clock, one per spotlight.")]
public Transform[] spotlightPivots;
public float spotlightIntensity = 40f;
public TMP_Text label;
private float flash;
private float hue = 0.6f;
private void Update()
{
if (analyser == null) return;
// Each bar takes the average of its share of the 64 spectrum bars.
int perBar = Mathf.Max(1, 64 / Mathf.Max(1, bars.Length));
for (int i = 0; i < bars.Length; i++)
{
float level = 0f;
for (int j = 0; j < perBar; j++) level += analyser.GetSpectrum(i * perBar + j);
level /= perBar;
Vector3 scale = bars[i].localScale;
scale.y = 0.05f + level * level * barHeight;
bars[i].localScale = scale;
}
flash = Mathf.Max(flash - Time.deltaTime * 3f, analyser.beat);
float t = (float)(Networking.GetServerTimeInSeconds() % 3600.0);
for (int i = 0; i < spotlights.Length; i++)
{
spotlights[i].intensity = spotlightIntensity * (0.15f + 0.85f * flash);
spotlights[i].color = Color.HSVToRGB(Mathf.Repeat(hue + i * 0.12f, 1f), 0.7f, 1f);
}
for (int i = 0; i < spotlightPivots.Length; i++)
spotlightPivots[i].localRotation = Quaternion.Euler(0f, Mathf.Sin(t * 0.6f + i * 1.7f) * 35f, 0f);
// The label changes ten times a second: text layout is far dearer than the bars.
if (label != null && Time.frameCount % 6 == 0)
label.text = "BASS " + Bar(analyser.bass) + "\nMID " + Bar(analyser.mid) + "\nHIGH " + Bar(analyser.treble) + "\nbeats " + analyser.beatCount;
}
public override void OnAudioBeat()
{
hue = Mathf.Repeat(hue + 0.17f, 1f);
}
/// <summary>Button: play or pause the video for everyone.</summary>
public void PlayPause()
{
if (video.isPlaying) video.Pause();
else video.Play();
}
private const string BarCells = "||||||||||||............";
// Twelve cells, filled up to the level.
private string Bar(float level)
{
int n = Mathf.Clamp(Mathf.RoundToInt(level * 12f), 0, 12);
return BarCells.Substring(12 - n, 12);
}
}