# Spatial Sound When Camera Is A Child

**URL:** <https://forum.babylonjs.com/t/spatial-sound-when-camera-is-a-child/4047>\
**Category:** Questions\
**Created:** [June 22, 2019, 5:47am UTC](https://forum.babylonjs.com/t/spatial-sound-when-camera-is-a-child/4047 "2019-06-22T05:47:57Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![MackeyK24](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/mackeyk24/32/3741_2.png) [@MackeyK24](https://forum.babylonjs.com/u/MackeyK24)\
**Post date:** [June 22, 2019, 5:47am UTC](https://forum.babylonjs.com/t/spatial-sound-when-camera-is-a-child/4047/1 "2019-06-22T05:47:57Z")

</div>

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 ?

---

<div class="post-metadata">

**Author:** ![Wingnut](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/wingnut/32/53_2.png) [@Wingnut](https://forum.babylonjs.com/u/Wingnut)\
**Post date:** [June 23, 2019, 1:16pm UTC](https://forum.babylonjs.com/t/spatial-sound-when-camera-is-a-child/4047/2 "2019-06-23T13:16:56Z")

</div>

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

Easy, huh? 😉

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. 🙂

Update: [https://www.babylonjs-playground.com/#EDVU95#18](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). 🙂

I might have found it faster… had I read the docs. [Play Sounds and Music - Babylon.js Documentation](https://doc.babylonjs.com/how_to/playing_sounds_and_music#creating-your-own-custom-attenuation-function) (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! 🙂 Sound-reducing walls - 101. (audio ambient occlusion?)

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

---

<div class="post-metadata">

**Author:** ![MackeyK24](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/mackeyk24/32/3741_2.png) [@MackeyK24](https://forum.babylonjs.com/u/MackeyK24)\
**Post date:** [June 24, 2019, 1:12am UTC](https://forum.babylonjs.com/t/spatial-sound-when-camera-is-a-child/4047/3 "2019-06-24T01:12:40Z")

</div>

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???

---

<div class="post-metadata">

**Author:** ![Deltakosh](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/deltakosh/32/26635_2.png) [@Deltakosh](https://forum.babylonjs.com/u/Deltakosh)\
**Post date:** [June 24, 2019, 3:52pm UTC](https://forum.babylonjs.com/t/spatial-sound-when-camera-is-a-child/4047/4 "2019-06-24T15:52:01Z")

</div>

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

```auto
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

---

<div class="post-metadata">

**Author:** ![MackeyK24](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/mackeyk24/32/3741_2.png) [@MackeyK24](https://forum.babylonjs.com/u/MackeyK24)\
**Post date:** [June 24, 2019, 9:45pm UTC](https://forum.babylonjs.com/t/spatial-sound-when-camera-is-a-child/4047/5 "2019-06-24T21:45:32Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Deltakosh](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/deltakosh/32/26635_2.png) [@Deltakosh](https://forum.babylonjs.com/u/Deltakosh)\
**Post date:** [June 25, 2019, 2:10pm UTC](https://forum.babylonjs.com/t/spatial-sound-when-camera-is-a-child/4047/6 "2019-06-25T14:10:11Z")

</div>

> [@MackeyK24](#):
>
> When the camera is a child of a a transform node. the camera position is ALWAYS ZERO

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?

---

<div class="post-metadata">

**Author:** ![MackeyK24](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/mackeyk24/32/3741_2.png) [@MackeyK24](https://forum.babylonjs.com/u/MackeyK24)\
**Post date:** [June 25, 2019, 2:51pm UTC](https://forum.babylonjs.com/t/spatial-sound-when-camera-is-a-child/4047/7 "2019-06-25T14:51:46Z")

</div>

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

Disable line 31 and enable line 25

---

<div class="post-metadata">

**Author:** ![sebavan](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/sebavan/32/57_2.png) [@sebavan](https://forum.babylonjs.com/u/sebavan)\
**Post date:** [June 26, 2019, 12:30pm UTC](https://forum.babylonjs.com/t/spatial-sound-when-camera-is-a-child/4047/8 "2019-06-26T12:30:22Z")

</div>

[https://www.babylonjs-playground.com/#EDVU95#22](https://www.babylonjs-playground.com/#EDVU95#22)

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

---

<div class="post-metadata">

**Author:** ![sebavan](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/sebavan/32/57_2.png) [@sebavan](https://forum.babylonjs.com/u/sebavan)\
**Post date:** [June 26, 2019, 12:47pm UTC](https://forum.babylonjs.com/t/spatial-sound-when-camera-is-a-child/4047/9 "2019-06-26T12:47:29Z")

</div>

Fix will be in the next nightly [Fix camera spatial audio local space by sebavan · Pull Request #6537 · BabylonJS/Babylon.js · GitHub](https://github.com/BabylonJS/Babylon.js/pull/6537)

Thanks @Wingnut and @MackeyK24 for the easy repro 🙂
