Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
2

How to lerp PlaybackLoudness?

Asked by
Azmidium 388 Moderation Voter
7 years ago

I wanted to learn how to make stuff react to audio. I did some research the property I needed to loop into was PlaybackLoudness. I tested out this feature while changing the range of a PointLight on a part and it worked! Here is the code:

1while wait() do                            -- \/ Located inside sound
2    workspace.SoundPart.PointLight.Range = script.Parent.PlaybackLoudness/25
3end

The problem with the script above is it updates REALLY REALLY quicky and the light flashes a ton! So I tried lerping.

To implement a lerp, I made this code below:

01script.Parent:Play()
02 
03function lerp(a, b, t)
04    return a * (1-t) + (b*t)
05end
06 
07local prePlaybackLoud = 0
08local postPlaybackLoud = 0
09 
10spawn(function()
11    while wait() do
12        prePlaybackLoud = script.Parent.PlaybackLoudness
13        wait(1)
14        postPlaybackLoud = script.Parent.PlaybackLoudness
15    end
View all 28 lines...

It seems to update 2 seconds late and it runs the lerp on the second before in the song. This was my final attempt before writing this question.

GOAL: I want to learn how to smoothly visualize audio on the range of a point light.

0
I think a moving average of the PlaybackLoudness produces better results (and is waaay less complicated) than tweening. XAXA 1569 — 7y

1 answer

Log in to vote
2
Answered by
XAXA 1569 Moderation Voter
7 years ago
Edited 7 years ago

Here's a simpler way of doing this using a moving average of the playback loudness. SAMPLE_SIZE and VOLUME_SCALE can be tweaked for your purposes. Larger samples produce smoother results, but cause peaks in volume ("beats") to be less pronounced.

01local Sound = script.Parent.Sound
02local PointLight = script.Parent.PointLight
03 
04Sound:Play()
05 
06local samples = {0}
07local SAMPLE_SIZE = 3
08local VOLUME_SCALE = 1/20
09local function GetPlaybackLoudness()
10    local sum = 0
11    for i, v in pairs(samples) do
12        sum = sum + v
13    end
14 
15    local avg = sum/#samples
View all 26 lines...
0
I guess a moving average is what I was looking for instead. :D Azmidium 388 — 7y
Ad

Answer this question