Frequency-Based Sound Attenuation Script

Hello Everyone

Today I would like to introduce my frequency-based sound attenuation script, which is a part of a larger project dedicated to providing the most realistic sound physics ever seen in Roblox.

This script uses real-world sound attenuation data to reduce the volume of frequencies of a sound source over a distance. This script has three quality levels, where the lowest introduces three equalizer instances, while the highest introduces eight equalizer instances. Increasing the quality will increase system load.

You can find the script down below:

The instructions on how to use it are inside the model, but I will also provide some reference images below to showcase how your sound system should be set up.

CleanShot 2024-10-26 at 22.13.16@2x
This is where both scripts should be present

CleanShot 2024-10-26 at 22.22.47@2x
Example setup with two sound sources.

CleanShot 2024-10-26 at 22.23.16@2x
How your Audio folder and its subfolders should be arranged in the workspace.

Feel free to use my script in your games and wherever. Remember to credit me if you do end up using it in any of your games. Thanks

11 Likes

Seems very interesting. Could you include some videos of this in effect?

2 Likes

Hello, this is a really cool idea! We don’t have enough sound realism on Roblox. I didn’t like how this system was laid out, so I reorganized it and stuff for my version, as well as fixing some bugs I found.

Summary

Very similar naming can be confusing!

image

I changed them to this:

I wasn’t able to figure out what the second function did, so I couldn’t change it. Since, quality was always set to eqQuality, I removed those parameters and made the functions just use eqQuality.


This module would only work when parented to StarterCharacterScripts (unlike StarterPlayerScripts like you’ve said), which is restricting.

I’ve removed the character and head variables, and made new ones down in the update function.


This part gets the AudioPlayer and AudioEmitter incorrectly. The command suggests an and, while the code uses an or.

I turned it into this:


The attenuation list was being calculated the same way every time!

So I cached it:


There’s more, but I’m tired of writing this!

The main things I changed is the whole sound source system and the folders. The idea that each part may only have one AudioPlayer is very restricting. You can only initialize all of them once, and new ones are impossible to initialize because the createEq function initializes all of them over again, creating unnecessary equalizers and stuff. Very restricting! So instead, I made it so you have to initialize individual AudioPlayers (not soundSources), that don’t need to be parented to a specific folder…

After making my version, it doesn’t seem to work right… so I tested out the original one.

I was disappointed to see that it worked exactly the same. I tested out Lightning sounds, and expected to hear something like this.

In short, higher frequencies get quieter faster over distance. So, the further way you are, you hear more low frequencies, giving lightning multiple flavors. If that’s what this resource does, I believe it is bugged!

What it actually sounds like:

@DaDude_89


UpgradedAttenuation.rbxm (9.8 KB)

Issues:

  • Volume decreases based on distance, but all frequencies sound like they decrease at the same rate?
  • Calculations are head-focused, when they should be camera-focused.
1 Like

I wasn’t able to figure out what the second function did, so I couldn’t change it. Since, quality was always set to eqQuality , I removed those parameters and made the functions just use eqQuality .

eqNums returns the necessary values required for the calculation in attenuationCalculation(freq). This function takes in a list, of which contains the values that eqNums returns.

After making my version, it doesn’t seem to work right… so I tested out the original one.

I was disappointed to see that it worked exactly the same. I tested out Lightning sounds, and expected to hear something like this.

Perhaps the effect isn’t strong enough. It should work like in real life, as the this table:

shows that lower frequencies get attenuated less compared to higher frequencies over a distance. Perhaps try increasing the quality factor, or try change the equation to use a higher humidity entry. It should be known, the default equation, that is:

return (math.pow(0.2866 * freq, 2) + 0.2033 * freq + 0.5444) * math.pow(10, -3)

which uses the 60% relative humidity entry in the table. During a storm the humidity generally reaches 100%. You can refer to the following pdf (https://cdn.standards.iteh.ai/samples/17426/3a2d69b767024b74805b83b063a91445/ISO-9613-1-1993.pdf) to see how sound is attenuated at 100% humidity. I may make my script use the tabulated values in that report.

I’ve also ordered an extremely comprehensive sound attenuation data list (ANSI/ASA S1.26-2014) so when that comes I will be switching to that.

The main things I changed is the whole sound source system and the folders. The idea that each part may only have one AudioPlayer is very restricting. You can only initialize all of them once, and new ones are impossible to initialize because the createEq function initializes all of them over again, creating unnecessary equalizers and stuff. Very restricting! So instead, I made it so you have to initialize individual AudioPlayers (not soundSources), that don’t need to be parented to a specific folder…

This is a good change and I will implement this. I forgot in the real world some speakers can be multidirectional. The only issue with this is that some players may not want certain sound emitters to be affected by this script, so I think there would need to be a blacklist list correct?

The rest are good optimizations which I will implement. Thank you.

1 Like

There shouldn’t be any restrictions in mine, because you have to manually initialize each AudioPlayer you want to be attenuated. You can then implement your own auto-initialize system like in your original module! A blacklist wouldn’t be necessary because by default, it is not automatic.

I suppose the conditions I used in my lightning test were not realistic, and so I didn’t get realistic results. I couldn’t figure out how to get it as loud as real lightning, and I didn’t know how far I needed to push the emitter to get it to sound different. Any ideas on how I could solve this? I don’t know much about sound…

Any ideas on how I could solve this? I don’t know much about sound…

First you want to consider the distance from the player to the sound source (i.e lighting strike). My script uses meters (m) while Roblox uses studs, so the distance you set the strike location to in relation to the player needs to be reflective of the real world. Secondly to consider how loud the lightning should be, use large distances, as that way the frequency attenuation should kick in at large scale. Then you can play around with the volume.

My second iteration of this script will use a far better dataset, so you could wait for that to come out and try using that. Up to you though.

1 Like

A problem I haven’t fixed yet is how it uses the player’s head for calculations, but their camera picks up the sound. Ideally, the camera should be the focus point of the calculations instead because it would be more realistic that way.

CleanShot 2024-10-27 at 08.51.09@2x
it should be set to the character. you try use the camera but you’d need to rework the entire set of calculations and that imo would overcomplicate it.

Where’s that property?

Inside here (scroll to the bottom of the workspace)
CleanShot 2024-10-27 at 09.09.09@2x

1 Like

hey my second iteration of this script has been improved so that lightning (and any other sound to be exact) sounds muffled at a great distance: