Live Stream Stage
Only StageTime Event Hosts can start, change or stop a live stream. Anyone can open this world and look at how it is built, but for everyone else the screen stays idle and the Go live and Stop buttons do nothing, including in Build & Test. This means official StageTime Event Hosts in a room they created (and StageTime admins); StagePass hosting doesn't include live streams yet. See ST Live Stream.
A concert stage for live events: a big screen that shows an Event Host's live stream to everyone in the room, and a stage that dances to the live sound with 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. It is the Audio Reactive Stage with a live stream in place of the music video.
Get it from the StageTime-Worlds-Examples repository: copy
the Worlds/LiveStreamStage folder into a Unity project with the SDK and open its scene. The full script is below. In
your own copy, set the Live Stream object's Default Stream to the stream name StageTime gave you.
| Piece | Built with |
|---|---|
| Live stream | ST Live Stream on the Live Stream object (Default Stream: your stream name): the picture goes into the screen's Render Texture (LiveScreen), the sound plays from that object's Audio Source |
| Analysis | An ST Audio Analyser on the same object, listening to the stream's Audio Source and driving three VFX Graphs through their Audio… properties |
| Controls | Red Go live and Stop buttons calling GoLive and StopStream, and a sign: "Only StageTime Event Hosts can start the stream." |
| Status and levels | A status line (LIVE my-event, Connecting to my-event..., Waiting for my-event to go live..., Can't play: ..., or |
Off - waiting for an Event Host, plus "You can start and stop the stream." for players who can control it) and a bass, mid and treble readout | |
| Bars, lights | This script, reading the analyser every frame and reacting to OnAudioBeat |
The ST Live Stream, its Audio Source, the ST Audio Analyser and this script are all on the one Live Stream
GameObject. That's on purpose: the live-stream events (OnLiveStreamStart, OnLiveStreamEnd, OnLiveStreamError)
and OnAudioBeat only reach scripts on the same GameObject as the component that raises them.
When an Event Host presses Go live, every device in the room connects to the stream, and players who join later connect by themselves. Every device analyses its own copy of the live sound, so the bars, lights and VFX need nothing sent over the network. Until an Event Host starts the stream (and always, for players who aren't Event Hosts, including in Build & Test), the screen is black, the stage comes to rest and the buttons do nothing.
using StageTime.SDK;
using TMPro;
using UnityEngine;
/// <summary>
/// A stage that dances to a live stream. The ST Live Stream on this GameObject shows the stream on the big screen and
/// plays its sound from the Audio Source here; an ST Audio Analyser listens to that source and drives the VFX Graphs
/// itself. This script adds the rest: Go live / Stop buttons, a status line, equaliser bars that follow the spectrum,
/// and spotlights that sweep with the room clock and flash on every beat.
/// For now only StageTime Event Hosts can start or stop a live stream: for everyone else the buttons do nothing.
/// The stream itself is synced by StageTime; every device analyses its own copy of the sound.
/// </summary>
public class LiveStreamStage : StageTimeBehaviour
{
public STLiveStream stream;
public STAudioAnalyser analyser;
[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;
[Tooltip("Shows whether the stream is live, connecting or off.")]
public TMP_Text status;
[Tooltip("Shows the sound levels.")]
public TMP_Text label;
private float flash;
private float hue = 0.6f;
private void Start()
{
UpdateStatus();
}
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 labels change ten times a second: text layout is far dearer than the bars.
if (Time.frameCount % 6 != 0) return;
UpdateStatus();
if (label != null)
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);
}
public override void OnLiveStreamStart()
{
Debug.Log("Live stream started: " + stream.currentStream);
UpdateStatus();
}
public override void OnLiveStreamEnd()
{
Debug.Log("Live stream ended.");
UpdateStatus();
}
public override void OnLiveStreamError()
{
Debug.Log("Live stream: " + stream.error);
UpdateStatus();
}
/// <summary>Button: start the stream for everyone (StageTime Event Hosts only).</summary>
public void GoLive()
{
stream.Play();
}
/// <summary>Button: stop the stream for everyone (StageTime Event Hosts only).</summary>
public void StopStream()
{
stream.Stop();
}
private void UpdateStatus()
{
if (status == null || stream == null) return;
string line;
if (stream.isLive) line = "<color=#FF4D5E>LIVE</color> " + stream.currentStream;
else if (stream.isConnecting && !string.IsNullOrEmpty(stream.error)) line = "Waiting for " + stream.currentStream + " to go live... (" + stream.error + ")";
else if (!string.IsNullOrEmpty(stream.error)) line = "Can't play: " + stream.error;
else if (stream.isConnecting) line = "Connecting to " + stream.currentStream + "...";
else line = "Off - waiting for an Event Host";
if (stream.canControl) line += "\n<size=70%>You can start and stop the stream.</size>";
status.text = line;
}
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);
}
}