 # Material-based audio distortion

Bold text has main question

I want to achieve a system to allow for audio distortion using the `LowGain` and `MidGain` with an `EqualizerSoundEffect` (I’m using the `MidGain` for boatbomber’s 3D Sound System).

I currently have a system to cast a ray between parts, getting the distance traveled within solid objects and their materials (information shown below). Using my known values (or any I may need) I want to do some calculations to get the `LowGain` and `MidGain` (doesn’t matter which values) to distort sound based upon parts in-between the speaker and listener. Basically muffling audio if it’s heard through walls.

An issue might be that `LowGain` and or `MidGain` just aren’t the correct values to edit for this, in this case I can just pick one, but if it’s `HighGain` I should edit, i’ll have to test stacking `EqualizerSoundEffect`, or mathematically combining two values.
Edit: Just tested it, they do, in fact, stack

Another red flag that I have realised is, when using the densities that I can calculated, how should I use them in tangent with ray, as the ray is an infinitely small cast (most likely, I just need to give it a radius and so it like that)

Since the release of the world panel, we now know the canonical size of one stud (28 cm / 11 in), I can use this to calculate rough densities of materials in `grams/stud³` with some quick calculations (made a C# script to do them for me, formulas are most likely wrong so feel free to correct me on them).

Main Script
``````-- This can be used to get maximum distance ray can travel within part
function module:GetPartHypotenuse(instance)
-- Omitted to shorten code block

function module:CastRay(rayOrigin, rayDirection, range)
--// Initialize Values \\--
local returnValue = {}
local totalDistance = 0

--// Cast Ray \\--
local raycastResult = workspace:Raycast(rayOrigin, rayDirection * range)
if raycastResult then
-- Create new params and whitelist hit part so it doesn't hit new parts
local inverseRaycastParams = RaycastParams.new()
inverseRaycastParams.FilterType = Enum.RaycastFilterType.Whitelist
inverseRaycastParams.FilterDescendantsInstances = { raycastResult.Instance }

-- Get maximum distance ray can travel, add a padding value (set to 1)
local hypotenuse = module:GetPartHypotenuse(raycastResult.Instance) + padding
local inversePosition = raycastResult.Position + rayDirection * hypotenuse
local inverseDirection = -1 * ( rayDirection * hypotenuse )

local inverseRaycastResult = workspace:Raycast(inversePosition, inverseDirection, inverseRaycastParams)
local newPosition = inverseRaycastResult.Position - rayDirection
local remainingRange = ( rayOrigin - inverseRaycastResult.Position ).Magnitude + 1

-- Loop until the ray length is finished and all parts are found
local returnDistance = 0; if remainingRange > 0 then
returnDistance, returnValue = module:CastRay(newPosition, rayDirection, remainingRange)
end

-- Calculate distance traveled within the part via the magnitude, add this to the TotalDistance
local distanceWithinPart = (raycastResult.Position - inverseRaycastResult.Position).Magnitude
totalDistance += distanceWithinPart + returnDistance

-- Add this runs data to the array
returnValue[#returnValue + 1] = { ["Distance"] = distanceWithinPart, ["Material"] = raycastResult.Material }
end
``````
Basic Raycast Test (Example Output) ``````{
 =  ▼  {
["Distance"] = 2.75,
["Material"] = Cobblestone
},
 =  ▼  {
["Distance"] = 2.75,
["Material"] = Fabric
},
 =  ▶ {...},
 =  ▶ {...},
 =  ▶ {...},
 =  ▶ {...}
}
``````
Density Calculation Script (C#)
``````using System;

namespace RobloxDensityCalculator {
class Program {
const double CentimetreToStud = 28;

static void Main ( string[] args ) {
Console.Write("Input (kg/m³): ");

double GramsPerCentimetreCubed = input / 1000;
double GramsPerStudCubed = GramsPerCentimetreCubed * CentimetreToStud;
Console.WriteLine("Output: {0} g/stud³", GramsPerStudCubed);
}
}
}
``````
Densities (Most likely will be changed)
``````--// List Values in g/stud³ \\--
module.MaterialDensities = {
[Enum.Material.Brick] = 50.4,
[Enum.Material.Cobblestone] = 3.5,
[Enum.Material.Concrete] = 67.2,
[Enum.Material.CorrodedMetal] = 172,
[Enum.Material.DiamondPlate] = 224,
[Enum.Material.Fabric] = 15.764,
[Enum.Material.Foil] = 28,
[Enum.Material.ForceField] = 0,
[Enum.Material.Glass] = 70,
[Enum.Material.Granite] = 75.348,
[Enum.Material.Grass] = 52,
[Enum.Material.Ice] = 25.676,
[Enum.Material.Marble] = 75.908,
[Enum.Material.Metal] = 224,
[Enum.Material.Neon] = 56.234,
[Enum.Material.Pebble] = 75.6,
[Enum.Material.Plastic] = 32.564,
[Enum.Material.Sand] = 40.376,
[Enum.Material.Slate] = 75.348,
[Enum.Material.SmoothPlastic] = 32.564,
[Enum.Material.Wood] = 18.76,
[Enum.Material.WoodPlanks] = 16.23
}
``````

Many thanks,
Arvid