How can I get a song's max playback loudness?

So I want a script too run through a song, like at literally .01 second speed, and to find that songs max playback loudness. Is this possible, or impossible?

I’m a bit confused on what you are trying to accomplish.

Are you trying to set a sound as loud as possible, as fast as possible, and find the loudest part? Please explain more thoroughly what you are trying to do and how it’s supposed to work. Code examples and previous failed attempts would also be very helpful!

1 Like

I am trying to get max playback loudness a song will give, so I can put it in my script.

Please be more specific as you aren’t making a whole lot of sense.

What is max playback loudness?
What are you trying to get it for?
Are you trying to make a sound be as loud as possible?

1 Like

I am trying to get the max playbackloudness a song will reach in its entire play time. I want a way to find that out. https://developer.roblox.com/en-us/api-reference/property/Sound/PlaybackLoudness I want to get that value so I can use it as a base.

Every frame you could check the playbackloudness and keep a variable of the loudest it gets.

Here’s a code example:

local RunService = game:GetService("RunService")

local MaxPlaybackLoudness = 0

local Song = script.Parent.Song
Song:Play()

RunService.Heartbeat:connect(function()
    if Song.PlaybackLoudness > MaxPlaybackLoudness then
        MaxPlaybackLoudness = Song.PlaybackLoudness
    end
end)

Sound.Ended:Wait()

print(MaxPlaybackLoudness)

This will print out the highest level of playbackloudness that the sound reaches.

Note:
This method will fire about 100 times per second, so it should be pretty accurate. If you are trying to play the sound super fast then it will be less accurate so your best option is to let the song last at least 2 seconds to get accurate results. The longer the sound lasts the more accurate it will be so keep that in mind.

If this doesn’t work please let me know.

1 Like

I am not looking for that. I am looking for the maxplaybackloudness before the song starts. Players can enter their own songs.

You can maybe do something like this:

local sound = Instance.new("Sound", screenGui) -- creates a sound instance
sound.SoundId = "rbxassetid://SOUNDIDHERE"
local maxLoudness = 100
sound.PlaybackLoudness = maxLoudness

Not sure if this is what you meant cuz im not sure of all the details but here you go

1 Like

You could try something like this, but i don’t know if it would work as I haven’t tested it yet.

Filter through the song using TimePosition, recording the playbackloudness at every position.
You could do this in an instant by using a for loop and increasing by a small increment.

I am not aware of any more efficient method to find the maximum loudness that a sound will reach.

Code example:

local MaxPlaybackLoudness = 0

local Song = script.Parent.Song

for i=0,Sound.TimeLength,0.01 end
    Song.TimePosition = i
    if Song.PlaybackLoudness > MaxPlaybackLoudness then
        MaxPlaybackLoudness = Song.PlaybackLoudness
    end
end)

print(MaxPlaybackLoudness)

Hope this works better for you.

1 Like

That wouldn’t work because a sound’s playbackloudness can’t be set, it’s something that is on goingly changed.

For some reason, the loop only goes once before it prints the bottom print statement.

I got it to work, but the song playback loudness is always 0 for some reason.

PlaybackLoudness only works on the client as far as I remember. It returns 0 if used on the server.

1 Like

I am using it on the client and it can work for the server.

I seem to have found a solution that works, but it takes a second to run.

local MaxPlaybackLoudness = 0

local Song = script.Sound
Song:Play()

wait() -- necessary

for i=0,Song.TimeLength,0.01 do
    Song.TimePosition = i
	
	game["Run Service"].Heartbeat:Wait() -- necessary
	
    if Song.PlaybackLoudness > MaxPlaybackLoudness then
        MaxPlaybackLoudness = Song.PlaybackLoudness
    end
end
Song:Stop()

print(MaxPlaybackLoudness)

For me it always prints the highest loudness as non-zero.

The accuracy of the max loudness is exponentially less effective as you decrease the incriment. 0.001 is just as accurate as 0.01 but takes way longer to run. 0.1 is way less accurate than 0.01 but completes super fast. Just find your sweet balance between accuracy and speed, you should do fine.

It doesn’t hurt to make the client wait a second or two before allowing them to play their song.

1 Like

Ah I remembered it wrongly then, nice to know it does work on the server.

Heartbeat runs at 60hz, so 60x per second. (If the machine runs at 60fps) If a song is 3 minutes, so 180 seconds, that means it’ll have to do 100*180=18000 iterations. (18000/60 = 300 seconds) That function would take 300 seconds to run with a 3 minute song, 30 seconds if using a step of 0.1. So if my calculations are right, it’d actually take longer at a step of 0.01 to calculate the max than if you were to just play the song, 1.6666…x as long.

Edit: I tested it, a 15 second song took 25.29 seconds to calculate. It took 1.68x as long, so pretty close to what I described above. (These are results taken from studio, may differ from proper servers.)

Edit 2: I tested it on an empty baseplate game in play mode (proper server), result was still 25 seconds.

1 Like

Alright i don’t really know what else to tell you.

You could make like 5 of the same audio and do them all at once to maximize speed, but I personally don’t have any better working solutions.

I know I’ve seen something like this in games before though so I hope someone comes around soon and can give you a better option.

1 Like

Thank you for @Vmena and @Dysche for helping! I shall try to to make a solution using the code and information you provided me!

1 Like

PlaybackLoudness is arbitrary. There isn’t really a way to get this without hacking something together or running the song once over and recording the highest PlaybackLoudness received at the time of reading (which is under constant fluctuation).

Is there a specific reason why you need it? A use case wasn’t provided.

I want it so I can use it as the max volume a song will reach, so I can use that as 1 and put all other volumes below.