Is this an efficient way of muffling audio

How efficient is this and how heavy on the server is it.

I’d assume its pretty server heavy since its spamming pathfinding.

I minimized the amount of functions used to reduce lag and the part visualization is optional by settings and not final.

Code:

local debris = game:GetService("Debris")

spawn(function()
	while true do
		task.wait(0.1)
		for i,v in pairs(workspace.pathfindingcache.nodes:GetChildren()) do
			spawn(function()
				local agentparams = {
					AgentHeight = 2.5,
					AgentRadius = 2,
					AgentCanJump = workspace.CanJump.Value
				}
				local path = game:GetService("PathfindingService"):CreatePath(agentparams)
				path:ComputeAsync(script.Parent.HumanoidRootPart.Parent.Torso.Position, v.Position)
				local waypoints = path:GetWaypoints()
				local count = #waypoints
				local total = count*4
				v:WaitForChild("g2").EqualizerSoundEffect.HighGain = -total
				v:WaitForChild("g2").EqualizerSoundEffect.MidGain = -total
				if workspace.DoVisualize.Value == true then
					for i,v in pairs(waypoints) do
						local part = Instance.new("Part",workspace.pathfindingcache.pathpoints)
						part.Size = Vector3.new(0.2,0.2,0.2)
						part.Anchored = true
						part.Material = Enum.Material.Neon
						part.Position = v.Position
						part.CanCollide = false
						part.Color = Color3.new(count/100,-count/100,0)
						debris:AddItem(part,0.1)
					end
				end
			end)
		end
	end
end)

Any help appreciated.

Thanks,
-Xarciti

2 Likes

Sorry but I didn’t get what you are trying to do exactly from your code, could you explain what you are trying to achive?

1 Like

It works as a realistic audio dampener. Heres an example:


(dont mind the random noises in the background i was in a discord call at the time)

2 Likes

Well I’m in a bit of a rush, so I can’t read it all, but here’s 2 suggestions:

If possible replace this with

while RunService.HeartBeat:Wait() do
    -- code here
end

to make it more efficient

Also, in this for loop, replace pairs with ipairs:

I mostly didn’t use RunService because of lag reasons.

I also changed it to automatically disable itself if you aren’t in range of a sound that’s set to use the dampening.

Here you are setting the EqualizerSoundEffect depending on the number of way points while that number could be just 4 and the distance between them is sooo far, the sound will still play, you may say no roblox Sound instance will adjust the sound automatically depending on how far are you from the sound source, well take a look at this exemple here:


Roblox sound instance ajust depending on the distance between you and the source not the distance between waypoints, here we have only 4 wapoints so even that the distance is so far you can still hear the sound.

1 Like

I’d say his method still works for this though, because reducing mid and high also effectively makes the sound quieter…

What I’d recommend, though, to work on @NarakuLite’s point is to lower the lowgain too to make the sound even quieter on distance too:

v:WaitForChild("g2").EqualizerSoundEffect.HighGain = -total
v:WaitForChild("g2").EqualizerSoundEffect.MidGain = -total
v:WaitForChild("g2").EqualizerSoundEffect.LowGain = -total * 0.25

-or some other configuration, as long as the lowgain is still louder than the rest for the muffle.

Also @KristinaMoment I’d imagine ipairs slows the script down rather than makes it more efficient?

pairs will also check for non-numeric values, therefore making it less efficient.

2 Likes

Nope this is definitely not the best way to do this. I had done a good amount of research on this issue but I still hadn’t been able to choose a method for they were so many with their own pros and cons but ye with that aside this is the worst way to go abt this.

Also, I can provide a valid method if you could mention where you are calculating this for, for an outdoor space or an indoor one.

Indoor space is what I’m looking for, at the time of the posts here I was actually away from home, so sorry for the inconvenient wait.

(Did I forget to mention that I am making this muffler togglable in the game I plan to use this for as part of a “Realistic Settings” package setting.)

Hey uh, could you follow through on the valid method?

I plan on adding this to a game soon and if I can’t find anything else (which I haven’t), I’m going to just implement this as my system for the dampening.

For indoor spaces, the best approach would be a portal based one. Basically at each point of entry and exit to a room (basically doors or doorways), you place a portal which acts like a waypoint through which sound will propagate. Unfortunately, I’m not aware of any method that will automatically do this for you, so you have to manually place these portals across the map (portal can just be an attachment or part btw). Then at runtime, you can use an algorithm to find the closest portal/waypoint from the sound source to the listener and then repeat the process until you reach a waypoint within a certain radius of the player. However, of course you have to take in the performance costs of doing this each frame. This research paper goes more in depth into the approach but ye it’s a bit complicated to say the least.

One optimization you could possibly do is using the concept of rooms. Basically rooms act like a container for these sound objects and portals (windows, doors and so on) act as a link/connection between multiple rooms. A room could just be a part which represents the original geometry of a level. Basically think of them as boxes which enclose a space. And you can have occlusion values for each room (seperate value for each wall depending on material) and portal, which can be dynamic and modified at runtime, for eg a certain wall gets blown off, you can reduce the occlusion value of the said wall.

FYI, I just found a gold mine aka PPT that goes about the sound propagation in hitman. Imo, it is 100x better than the research paper; simple, concise and easy to understand, definitely check it out. It even goes about how to create the algorithm I mentioned earlier.

1 Like

This will be extremely useful.

Thank you for showing me this!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.