How to change listening position in AudioEngineV2

Hello!

I tried for 4 hours to fix it, but failed.
Basically, i already setup the audio, i can hear it, it works.. BUT! the audio can only be heard when i go towards the center of the map (0,0,0).
I don’t hear things based on the position and rotation of the camera at all. I hear whats around position (0,0,0).
I already tried audioEngine.listener.attach(camera).

I already read through the whole docs and tried to find answers, i even spent hours trying to solve it myself. I think i either have a bug somewhere that messes things up or im extremely stupid. Does anyone have a simple playground where audio can be heard if its close to the camera instead of being close to position(0,0,0)? Thanks in advance!

Are you using an audio bus? I had a similar issue before and I realized it was about audioBus config.

If i create the audioBus with spatialEnabled parameter like below and create a sound in that bus, I only heard the sound near the origin position. But if i don’t set spatialEnabled on audioBus and set it on individual sound, it all works fine.

 const effectsBus = await CreateAudioBusAsync("effectsBus", {
      spatialEnabled: true,
    });

so if you have a similar config, removing the spatialEnabled from your audio bus config and enabling it per sound might solve your issue as well.

Sadly didn’t work.. i tried with and without audiobus, tried with and without spatialEnabled, tried with mesh, camera, different mesh, still nothing.
Here is my current code:

World.audio = await BABYLON.CreateAudioEngineAsync({volume: 0.5,listenerAutoUpdate: true,listenerEnabled: true,resumeOnInteraction: true});
World.audio.listener.attach(World.scene.activeCamera);
this.tank_idle = await World.audio.createSoundAsync("tank_idle2", "/sounds/tank_idle2.wav", {maxInstances: 1, loop: true, volume: 0.5, spatialEnabled: true});
this.tank_idle.spatial.maxDistance = 50;
this.tank_idle.spatial.attach(mesh);
await World.audio.unlockAsync();

I tried with a lot of variations of this code, but still hearing it from the center of the map only.
i even made a new different camera, didn’t work.. changed position of the camera, of the mesh, still nothing.. i don’t get it at this point.

cc @docEdub

Thanks, I’ll look into this first thing tomorrow.

Have you tried adding spatialMaxDistance in the options?

this.tank_idle = await World.audio.createSoundAsync("tank_idle2", "/sounds/tank_idle2.wav", {
        maxInstances: 1,
        loop: true,
        volume: 0.5,
        spatialEnabled: true,
        spatialMinDistance: 0,
        spatialMaxDistance: 50
    });

If you have a PG we could better check what is wrong.

Otherwise I would say that it looks more like the fact that the audioEngine is not attached to the camera.
audioEngine.listener.attach(camera)

Check that the camera is the right one to use (in case you have more than one)

Yes i already tried:( but then it said that the sound have 10000 maxDistance when i did that. This is why i wrote maxDistance separately.
Also, the camera is the right one, tried multiple ways to add it, didnt work.
Though when adding mesh/camera to the audioEngine listener, sometimes it said this:


Might be because im adding it “too late”? or because there is already a listener.
It works when i add it right after creating the engine.. but still.. i can only hear whats around position (0,0,0) :frowning:

Also thought about pg but it would be basically the same as the code i wrote previously. Also, if the code would work in PG i would loose my mind.. hmm maybe i will try to make a PG, but if it works there, then im out of ideas :man_shrugging:

Okay i made a PG quickly, but now i wasn’t even able to make spatial work lol i definitely missed something out. Any help would be appreciated.
https://playground.babylonjs.com/#ICXH00

Your PG works with me.
I did a little thing on the PG to check the attachment of the sound to the sphere and you can hear that the sound is moving away.

Then if you bring the camera closer, the sound increases. I don"t see any problem.
I use Chrome.

On the other hand I do notice a problem with Firefox. @docEdub On firefox the audio engine does not seem to be attached to the camera. There is no spatial effect.

2 Likes

NOWAY… i just spent hours upon hours feeling like im stupid, and it was just not supporting firefox :sob: i only use firefox because i had to fight a lot with chrome.. it always have a problem… firefox just works… for me at least. Im glad at least i found a problem :smiley:

(btw i did check my code on chrome, but didn’t work because i attached the camera to the listener too soon. thus i thought chrome has the same problem lol)

Thanks for the help! Hopefully this will be fixed on firefox:D Im developing with babylonjs since 2020 and im glad to see it become better and better!

Yep, Firefox’s WebAudio API has not been updated in a long time, so the newer features are not available and probably never will be.

I missed this when testing the listener features. Should be fixed with PR 16797 when it merges.

Thanks for reporting this!

1 Like

That"s cool. Thank you.

This is merged now, but be aware that the audio quality of the listener is reduced on Firefox since the fallback implementation does not support ramping of audio parameters. This will cause small glitches and zippering audio artifacts with rapid listener movements. The 10 millisecond smoothing done in the full-featured implementation to reduce these artifacts does not apply to Firefox since it does not support audio parameters on the WebAudio listener node.

1 Like

Found a 9 year old issue on bugzilla about this:

  1. years.

Firefox is dead to me :rofl:

2 Likes

Firefox is an excellent alternative to the time when Internet Explorer was ubiquitous. But Chrome has overtaken Firefox.
I use Chrome, Opera, Brave and Edge, all very well.