Spatial Sound When Camera Is A Child

Yo @Deltakosh … I got a problem with sound when the camera is a child of a transform and i am moving the transform around…

I need to be able to set a custom transform instead of always hard coding the position of the actual camera for spatial sounds…

How should i go about doing that ?

Hi guys, sorry to butt-in. https://www.babylonjs-playground.com/#EDVU95#15

Easy, huh? :wink:

Ok, ok, I faked it with line 31. Disable line 31 and activate line 25, to see that I solved nothing. But heck, we have a testing PG, now. :slight_smile:

Update: https://www.babylonjs-playground.com/#EDVU95#18 Lots of testing debris in there, but it appears that line 49 is a solution (in 40 mins of experimenting - ahem - hug). :slight_smile:

I might have found it faster… had I read the docs. Play Sounds and Music - Babylon.js Documentation (Wingy accidentally skips research steps, too.)

Perhaps we should add a few notes in that docs section… about parented cameras (with human ears), and about using useCustomAttenuation = true by itself (like PG #18), without adding music.setAttenuationFunction = someCustomAttenuationFunc().

Fun challenge for someCustomAttenuationFunc() programmers: Put TWO musical crates (as speaker cabinets for the DJ)… inside the nightclub. When the door to the nightclub opens, the music gets louder, based-upon how WIDELY the door is opened.

Then move camera/player around outside the nightclub’s open door, and use raycasting to determine WHEN the camera is within DIRECT sight/ear-shot of either speaker (shooting the ray thru the open door). When camera can “see” either speaker thru open door (cam-to-either-crate ray == direct-hit), the music gets even louder. FUN! :slight_smile: Sound-reducing walls - 101. (audio ambient occlusion?)

Now where is that music.muffle(muffleValue) func? :slight_smile: err… perhaps mesh.audioOpacity? Extra credit if each speaker cabinet… is a Leslie speaker (dual spinning sound projectors in each cab). OMG! heh

1 Like

Yo @Deltakosh … I was thinking since the web audio code is just checking the active or first camera position… can’t we just inverse transform that camera position if it has a parent???

So let me try to rephrase your need :slight_smile:
so far we use this code to get the sound location:

var distance = this._connectedTransformNode.getDistanceToCamera(this._scene.activeCamera);

What do you want to change here? We could add an option for you to provide your own function but you can also do as @Wingnut mentioned and provide your own attenuation function

The problem with the getDistanceToCamera is that is take the camera position. When the camera is a child of a a transform node. the camera position is ALWAYS ZERO. So when you move the parent transform around… the sound system always thinks its a zero and you ALWAYS here full sound even when the parent transform (that has the camera as a child as local position zero) is far away.

We need to take into account that that _scene.activeCamera might be a child and its repotered position are in local space… for me ZERO.

For now… i just had to break the GLTF Child Camera relationship and de-parent my camera and directly move my camera around the track… Then all my spacial sounds play perfect… For me its the audience on the the side of the race track… when the car goes by (dragging along its camera) you hear the audience cheering and you get past that section the sound fades away.

That should NOT be the case as we are getting the ABSOLUTE position and not the relative

Can you repro the issue in a SIMPLE PG?

I think @Wingnut did at https://www.babylonjs-playground.com/#EDVU95#15

Disable line 31 and enable line 25

https://www.babylonjs-playground.com/#EDVU95#22

Yup it looks like a bug, I am digging into it

1 Like

Fix will be in the next nightly Fix camera spatial audio local space by sebavan · Pull Request #6537 · BabylonJS/Babylon.js · GitHub

Thanks @Wingnut and @MackeyK24 for the easy repro :slight_smile:

3 Likes