Author Topic: Jittering 3D sound in-game  (Read 327 times)

ItsClonkAndre

  • Guest
Jittering 3D sound in-game
« on: 17 Mar '23 - 21:53 »
Hello!

I've recently made a little modification for the game GTA IV, which allows the user to add custom sounds to the world which use 3D positioning.
It all works great! The only thing that isn't that great at the moment tho, is the fact that the sound is jittering...

I also have a video of that, so you can hear it for youself: https://streamable.com/6iku95
At the beginning of the video i was in the "Brief" tab of the main menu where the sound was fine, then i unpaused and it began jittering. I paused again and landed in the "Map" tab of the main menu where it still was jittering but after i switched to the "Brief" tab it was fine again.

Here's something that might be interesting: The script tick loop stops running when you switch to the "Brief" tab. And the script tick loop is where i update the listener position of Bass. Maybe afterall the problem is related to GTA IV? Not sure!


I'm using Bass version 2.4.17.0 (x86) and the ManagedBass wrapper for .NET. But it don't think it's related to the ManagedBass wrapper.
OS is Windows 10 x64.

This is how i initialize the Bass library (Also tried without RAGE.GetHWND()):
Code: [Select]
isBassInitialized = Bass.Init(-1, 44100, DeviceInitFlags.Mono | DeviceInitFlags.Device3D, RAGE.GetHWND());
The 3D Factors get set to those values (Turning off the doppler effect):
Code: [Select]
Bass.Set3DFactors(1.0f, 1.0f, 0.0f);
Bass.Apply3D();

This code is in the script tick loop which updates the listerner position
Code: [Select]
// - Get camera stuff -
CCam mainCam = CCamera.GetFinalCam();
GTAMatrix matrix = mainCam.Matrix;

Bass.Set3DPosition(matrix.pos.ToLeftHandedVector3D(), null, matrix.at.ToLeftHandedVector3DNegativeZ(), matrix.up.ToLeftHandedVector3D());
Bass.Apply3D();

And this is how i create streams and set the audio position
Code: [Select]
int handle = Bass.CreateStream(file, 0, 0, BassFlags.Bass3D);

Bass.ChannelSet3DPosition(handle, m.pos.ToLeftHandedVector3D(), Vector3.One.ToLeftHandedVector3DNegativeZ(), null);
Bass.Apply3D();


If anyone has some ideas, i would be more then happy to hear them!
If you need additional infos, please let me know!

Ian @ un4seen

  • Administrator
  • Posts: 25718
Re: Jittering 3D sound in-game
« Reply #1 on: 20 Mar '23 - 14:25 »
I also have a video of that, so you can hear it for youself: https://streamable.com/6iku95
At the beginning of the video i was in the "Brief" tab of the main menu where the sound was fine, then i unpaused and it began jittering. I paused again and landed in the "Map" tab of the main menu where it still was jittering but after i switched to the "Brief" tab it was fine again.

Here's something that might be interesting: The script tick loop stops running when you switch to the "Brief" tab. And the script tick loop is where i update the listener position of Bass. Maybe afterall the problem is related to GTA IV? Not sure!

Is BASS playing the "Brief" tab music? Or is BASS not playing anything at that point, and the problems begin when it does start playing? Or does the problem only happen when updating the listener position, eg. if you leave out the Set3DPosition and/or Apply3D calls above then it doesn't happen? Please also confirm what BASS_SetConfig and BASS_ChannelSetAttribute settings you're using, if any.

ItsClonkAndre

  • Guest
Re: Jittering 3D sound in-game
« Reply #2 on: 21 Mar '23 - 15:56 »
Hello Ian, thanks for your response!
I think the video was a bit misleading, the sound is actually placed within the game world, and has nothing to do with the pause menu itself. It was just so i could demonstrate what i meant with the jittering sound.

I tried leaving out the Set3DPosition and Apply3D calls, and the sound actually sounded normal. But the listenser position got reset to X: 0, Y: 0, Z: 0, isn't it supposed to stay at the last position you set with Set3DPosition? Or am i misunderstanding something?

I'm not calling the BASS_SetConfig method in my current code. Tho, i'm calling the BASS_ChannelSetAttribute for the current stream to set it's volume to 0.2. I also tried not doing that, the sounds was also jittering.

Ian @ un4seen

  • Administrator
  • Posts: 25718
Re: Jittering 3D sound in-game
« Reply #3 on: 21 Mar '23 - 17:45 »
I tried leaving out the Set3DPosition and Apply3D calls, and the sound actually sounded normal.

OK, that at least seems to narrow down where the problem is. Please try putting back the Apply3D call but still leave out the Set3DPosition call, and see if the jittery sound problem returns. If it does return, are you also making any other 3D calls (eg. setting 3D position of sounds) and does disabling them prevent the problem again? If it does, what happens if you leave them out but put back the Set3DPosition call? Basically trying to narrow down the problem to a particular call.

But the listenser position got reset to X: 0, Y: 0, Z: 0, isn't it supposed to stay at the last position you set with Set3DPosition? Or am i misunderstanding something?

The listener position should indeed stay at the last set position, with the default being (0,0,0). Did you set a different position and call Apply3D with it having no audible effect? What does Get3DPosition report?

ItsClonkAndre

  • Guest
Re: Jittering 3D sound in-game
« Reply #4 on: 21 Mar '23 - 22:50 »
Thanks for the help Ian, i actually managed to find out what was the problem. I had a mod installed, which i previously made for the game, which also utilized 3D sound, but i haven't quite got the 3D sound right at the time when i made the mod.

The main problem here is, that i initialized bass on the same thread for both mods...

What would be an ideal way to handle such a situation? Should i create a new thread and initialize bass on that thread to get rid of this conflict?

Ian @ un4seen

  • Administrator
  • Posts: 25718
Re: Jittering 3D sound in-game
« Reply #5 on: 22 Mar '23 - 13:46 »
Ah, so the problem was caused by 2 different things applying conflicting 3D settings?

It isn't generally possible to have multiple independent users of BASS in the same process (all threads will be accessing the same BASS.DLL). Are you sure the 2 things can't share the same BASS instance, eg. the listener position should be the same in both cases anyway? If you do absolutely need separate BASS instances then one way you can achieve that is to use different DLLs (eg. BASS2.DLL for the 2nd instance), but note that add-ons won't work with that because they expect BASS.DLL (not BASS2.DLL).

ItsClonkAndre

  • Guest
Re: Jittering 3D sound in-game
« Reply #6 on: 22 Mar '23 - 17:43 »
Quote
Ah, so the problem was caused by 2 different things applying conflicting 3D settings?
Exactly!

Quote
eg. the listener position should be the same in both cases anyway?
Yea it definitely should, i guess i will have to update the mod  :)
Thanks for the help!