Hey! I’m trying to make a light show and firework display live event. It looks good so far, however, I’d like to properly synchronize it with the sound (music), so that the lights change how they really should and don’t depend on just wait or loop functions.
Any idea how I can easily do this? I never really worked with synchronizing stuff with sounds.
local runS = game:GetService("RunService")
local audio = game:GetService("SoundService"):WaitForChild("EventMusic")
local bpm = 140
local secPerBeat = 0
local songPosition = 0
local songPositionInBeats = 0
local firstBeatOffset = 0
local dspSongTime = 0
function Start()
secPerBeat = 60.0 / bpm
dspSongTime = audio.TimePosition
audio:Play()
end
function Update()
songPosition = audio.TimePosition - dspSongTime - firstBeatOffset
songPositionInBeats = songPosition / secPerBeat
script:SetAttribute("songPosition", songPosition)
script:SetAttribute("songPositionInBeats", songPositionInBeats)
end
Start()
runS.Heartbeat:Connect(Update)
local runS = game:GetService("RunService")
local conductor = script.Parent.Conductor
local eventFireTime = 1
local eventFireTime2 = 2
runS.Heartbeat:Connect(function()
if eventFireTime <= conductor:GetAttribute("songPositionInBeats") then
local WashLightsModel = workspace:WaitForChild("WashLights")
local Light = WashLightsModel:FindFirstChild("WashLight")
Light.Head.Lens.Transparency = 0
Light.Head.Lens.Beam1.Enabled = true
Light.Head.Lens.SurfaceLight.Enabled = true
end
if eventFireTime2 <= conductor:GetAttribute("songPositionInBeats") then
local WashLightsModel = workspace:WaitForChild("WashLights")
local Light = WashLightsModel:FindFirstChild("WashLight")
Light.Head.Lens.Transparency = 1
Light.Head.Lens.Beam1.Enabled = false
Light.Head.Lens.SurfaceLight.Enabled = false
end
end)
I don’t know if you’re still interested, but luckily for you, a new feature called “PlaybackLoudness” came out, what I recommend doing is logging the last 10 frames of your sound into a table, you can do this by inserting the PlaybackLoudness value from your sound and then clearing the 1st index from your table once its length goes over 10.
PlaybackLoudness is a range from 0 - 1,000 so set the scale at:
local Scale = (PlaybackLoudness / 1_000)
and from there you are able to light up your lights with certain brightness levels, colors, etc!
Again this was almost 2 years ago so im confident you won’t be needing this, but this is for any future developers!
Yes, I indeed know about that feature and managed to properly synchronize events now, even in a more complex way than what I intended originally. So all good now, but thanks for the reply!