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?

4 Likes

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!

3 Likes

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

2 Likes

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?

3 Likes

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.

2 Likes

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.

3 Likes

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

2 Likes

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

2 Likes

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.

3 Likes

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

2 Likes

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

2 Likes

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

2 Likes

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

5 Likes

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

2 Likes

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.

3 Likes

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.

2 Likes

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

2 Likes

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.

1 Like

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.

1 Like

If memory serves me correctly, Volume doesn’t affect PlaybackLoudness. PlaybackLoudness is part of the audio file you uploaded itself and doesn’t take volume into account. Therefore, if you wanted to sample PlaybackLoudness, you can play an audio at volume 0 and start marking.

With that being said: I still don’t think this is the best way to go about it. Sounds with a louder volume automatically play over those with lower volumes. You can make use of the volume property, as well as SoundGroups, to throw something together.

2 Likes