Problems with using the new AudioAPI

(I don’t have access to bugs report :pensive: )

But I do hope to get some help here, I have a problem with the new AudioAPI and I played with it around and it seems that the new audioAPI ruined the proximity of voice chat bc we can hear everyone even from a far away.

1 Like

According to the announcement this script should do what you’re looking for

​​local function wireUp(source: Instance, target: Instance) : Wire
	local wire = Instance.new("Wire")
	wire.SourceInstance = source
	wire.TargetInstance = target
	wire.Parent = target
	return wire
end

local function split(wire: Wire, effect: Instance)
	local source = wire.SourceInstance
	local target = wire.TargetInstance
	wire.TargetInstance = effect
	wireUp(effect, target)
end

local function onWireConnected(wire: Wire)
	local fader = Instance.new("AudioFader")
	fader.Parent = wire
	split(wire, fader)
end

local function onWireAdded(wire: Wire)
	if wire.Connected then
		onWireConnected(wire)
	end
	local connection: RBXScriptConnection = nil
	connection = wire:GetPropertyChangedSignal("Connected"):Connect(function()
		onWireConnected(wire)
		connection:Disconnect()
	end)
end

local function onEmitterAdded(emitter: AudioEmitter)
	local wire = emitter:FindFirstChild("Wire")
	if wire then
		onWireAdded(wire)
	end
	emitter.ChildAdded:Connect(function(child)
		if child:IsA("Wire") then
			onWireAdded(child)
		end
	end)
end

local function onCharacterAdded(character: Model)
	local emitter = character:FindFirstChild("AudioEmitter")
	if emitter then
		onEmitterAdded(emitter)
	end
	character.ChildAdded:Connect(function(child: Instance)
		if child:IsA("AudioEmitter") then
			onEmitterAdded(child)
		end
	end)
end

local function onPlayerAdded(player: Player)
	local character = player.Character
	if character then
		onCharacterAdded(character)
	end
	player.CharacterAdded:Connect(onCharacterAdded)
end

local players = game:GetService("Players")
for _, player in players:GetPlayers() do
	onPlayerAdded(player)
end
players.PlayerAdded:Connect(onPlayerAdded)

local function oldRollOff(from: Vector3, to: Vector3) : number
	local distance = (to - from).Magnitude
	
	local minDistance = script:GetAttribute("RollOffMinDistance") or 7
	local maxDistance = script:GetAttribute("RollOffMaxDistance") or 80
	
	if maxDistance <= minDistance or distance < minDistance then
		return 1
	elseif distance > maxDistance then
		return 0
	end
	local linearGain = 1 - (distance - minDistance) / (maxDistance - minDistance)
	return linearGain * linearGain
end

local function newRollOff(from: Vector3, to: Vector3) : number
	local distance = (to - from).Magnitude
	local gain = 4 / math.min(1, distance)
	return math.clamp(gain, 0, 1)
end

local function updateCharacter(character: Model)
	local primaryPart = character.PrimaryPart
	if not primaryPart then
		return
	end
	
	local emitter = character:FindFirstChild("AudioEmitter")
	if not emitter then
		return
	end
	
	local wire = emitter:FindFirstChild("Wire")
	if not wire then
		return
	end
	
	local fader = wire:FindFirstChild("AudioFader")
	if not fader then
		return
	end
	
	local emitterPosition = primaryPart.Position
	local listenerPosition = workspace.CurrentCamera.CFrame.Position
	
	local newAttenuation = newRollOff(emitterPosition, listenerPosition)
	local oldAttenuation = oldRollOff(emitterPosition, listenerPosition)
	fader.Volume = oldAttenuation / newAttenuation
end

local function updatePlayer(player: Player)
	local character = player.Character
	if character then
		updateCharacter(character)
	end
end

local function update()
	for _, player in players:GetPlayers() do
		updatePlayer(player)
	end
end

while true do
	update()
	task.wait()
end
1 Like

omg I’m going to try this one. Is this going to be in the server script?

1 Like

Local script I believe since it’s indexing the current camera

1 Like

do I move it on StarterPlayerScripts or just the StarterGui?

I’m getting this error:
image

as well as this is the output
image

Here if you scroll down a bit that’s where I got the script New Audio API [Beta]: Elevate Sound and Voice in Your Experiences

1 Like