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!
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?
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. Sound | Documentation - Roblox Creator Hub 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.
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
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.
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.
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.
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.
Thank you for @Vmena and @Dysche for helping! I shall try to to make a solution using the code and information you provided me!
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.
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.