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
6 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:

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

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:

script.Parent:Play()

function lerp(a, b, t)
    return a * (1-t) + (b*t)
end

local prePlaybackLoud = 0
local postPlaybackLoud = 0

spawn(function()
    while wait() do
        prePlaybackLoud = script.Parent.PlaybackLoudness
        wait(1)
        postPlaybackLoud = script.Parent.PlaybackLoudness
    end
end)

spawn(function()

    while wait() do
        local ppL = prePlaybackLoud
        local pPL = postPlaybackLoud    
        for i = 0,1,.01 do
            wait()
            workspace.SoundPart.PointLight.Range = lerp(prePlaybackLoud/25, postPlaybackLoud/25, i)
        end
    end
end)

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 — 6y

1 answer

Log in to vote
2
Answered by
XAXA 1569 Moderation Voter
6 years ago
Edited 6 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.

local Sound = script.Parent.Sound
local PointLight = script.Parent.PointLight

Sound:Play()

local samples = {0}
local SAMPLE_SIZE = 3
local VOLUME_SCALE = 1/20
local function GetPlaybackLoudness()
    local sum = 0
    for i, v in pairs(samples) do
        sum = sum + v
    end

    local avg = sum/#samples
    return avg * VOLUME_SCALE
end

while wait() do
    table.insert(samples, Sound.PlaybackLoudness)
    if #samples > SAMPLE_SIZE then
        table.remove(samples, 1)
    end

    PointLight.Range = GetPlaybackLoudness()
end
0
I guess a moving average is what I was looking for instead. :D Azmidium 388 — 6y
Ad

Answer this question