garrysmod-issues icon indicating copy to clipboard operation
garrysmod-issues copied to clipboard

IGModAudioChannel kinda breaks if the 3d channel position gets too close to the local player's eye position.

Open RaphaelIT7 opened this issue 6 months ago • 4 comments

Details

I noticed when I was testing around that the audio kind of breaks when you set the channel position near or exactly to the eye position of the local client.

Broken/Setting it to the EyePos

https://github.com/user-attachments/assets/c1b77841-2f37-425d-8622-e629a0cb94af

Working/Setting it to the normal position:

https://github.com/user-attachments/assets/671bbfaf-5277-416f-8de2-fd8994b15a89

Steps to reproduce

sound.PlayFile("sound/slashco/music/slashco_offering_3.mp3", "3d", function(channel, errCode, errStr)
	if not IsValid(channel) then
		error("[SlashCo] Failed to create audio channel! (" .. errCode .. ", " .. errStr .. ")\n")
		return
	end

	if IsValid(TESTCHANNEL) then
		TESTCHANNEL:__gc()
	end

	TESTCHANNEL = channel
	TESTCHANNEL:Play()

	hook.Add("Think", "TestChannel", function()
		if IsValid(TESTCHANNEL) then
			TESTCHANNEL:SetPos(LocalPlayer():EyePos()) -- If changed to GetPos the audio will sound normal.
		end
	end)
end)

Example audio file from: https://github.com/Mantibro/SlashCo/blob/beta-changes/sound/slashco/music/slashco_offering_3.mp3

RaphaelIT7 avatar May 27 '25 13:05 RaphaelIT7

I am not sure this is really avoidable. You are setting the sound position to be right inside the listener's ear.

It just that the 2 positions (the sound origin and the ear/listener position) become slightly desynced causing the sound file to sound as if it was on the left or on the right very rapidly, because the 2 positions are ever so slightly different.

Lastly, I'd like to point out that it isn't really logical to do this, if you want a global sound, you don't make it 3D.

It is possible to set the sound file position in RenderScene hook (to render.GetViewSetup().origin), that would make it as synched as possible, but I'd like to reiterate how this is just not something you should be doing in general.

robotboy655 avatar May 27 '25 19:05 robotboy655

I did notice that setting it in PreRender seems to work far better, as it seems to not desync in there. Could it be that CGMod_Audio::Update is maybe called before the Lua think hook, which could cause this desync? Also, I will make them not be 3d when they don't have to be, this was just an issue I had found while playing around.

RaphaelIT7 avatar May 27 '25 20:05 RaphaelIT7

You just have to make it a global sound, it doesn't make sense to use 3d sound

Goldermor avatar Jun 04 '25 08:06 Goldermor

You just have to make it a global sound, it doesn't make sense to use 3d sound

As already mentioned, I won't make them use 3d when they don't have to, like in this case. This was just something I found while playing around with the function.

RaphaelIT7 avatar Jun 04 '25 09:06 RaphaelIT7