AudioEngineV2 upgrade questions

Hi,

I see that Sound.attachToMesh() is gone. I see there’s spatialPositon property in options passed to CreateSoundAsync, so I can set it to mesh.position.
Then, what happens when the mesh moves, e.g. animation is applied to the position?
And what happens if I set mesh position directly, like mesh.position = new Vector3(whatever)?

Furthermore, unlike Sound() constructor, CreateStreamingSoundAsync does not take MediaStream argument, only HTMLMediaElement.
That’s kinda blocker for WebRTC streaming: when new stream arrives, I don’t get event with HTMLMediaElement.
Is there a workaround?

Thanks!

You have the same thing with spatial.attach :

this.audioEngine = await BABYLON.CreateAudioEngineAsync({ listenerEnabled: true, disableDefaultUI: true});
this.audioEngine.listener.attach(this.scene.activeCamera, true);


const sound = await BABYLON.CreateSoundAsync(nameSound, pathSound, options);
sound.spatial.maxDistance = 10;
sound.spatial.position = mesh.position;
sound.spatial.rotation = mesh.rotation;
sound.spatial.attach(meshToAttach, true);

And what about my second question?

Looking at the source, I may have a RFE :slight_smile:

packages/dev/core/src/AudioV2/webAudio/webAudioStreamingSound.ts, _initFromMediaElement:

this._sourceNode = new MediaElementAudioSourceNode(this._sound._audioContext, { mediaElement: mediaElement });

I need something like this, but with AudioContext.createMediaStreamSource() instead.

Use case - new user joins the chatroom/virtual world. WebRTC client creates new MediaStream that needs to be attached to user’s avatar.
This would require some constructor changes, also setting up different event listeners etc, but overall, new method _initFromMediaStream is pretty much the same as this one.

Let me add @RaananW to the topic as he is pretty well versed in audio

You’re right that CreateStreamingSoundAsync only takes an HTMLMediaElement. For a MediaStream, use CreateSoundSourceAsync instead — it wraps any Web Audio AudioNode, so you feed it a MediaStreamAudioSourceNode:

const audioEngine = await BABYLON.CreateAudioEngineAsync();

// stream = your incoming WebRTC MediaStream
const node = new MediaStreamAudioSourceNode(audioEngine.audioContext, { mediaStream: stream });
const source = await BABYLON.CreateSoundSourceAsync("webrtc", node, { spatialEnabled: true });
source.spatial.attach(mesh); // spatialization should work here too

This is exactly what the built-in CreateMicrophoneSoundSourceAsync does internally — it just calls getUserMedia and hands the resulting MediaStreamAudioSourceNode to CreateSoundSourceAsync. When a new WebRTC stream arrives, build a fresh MediaStreamAudioSourceNode and create a new sound source for it. Disposing the source stops the underlying stream tracks for you.

Thanks, this kinda works, but not good enough to be useful.
First thing that I got was silence. Tinkering with sound options, silence was caused by spatialMaxDistance of 50. With default 100, I do have sound, but it’s not spatial. Wherever I go and turn, it sounds exactly the same.

BTW AudioContext is private, so audioEngine._audioContext

This could be an issue with spatial audio and WebRTC. Let me look into it. hard to debug, as I don’t have a webrtc audio stream. It does work with a microphone, so it might be a hardware limitation that we will not be able to fix.
I’ll put it on my todo list.

Yeah tell me about it :wink:
I do have publicly available webrtc streaming server you could use. But running the server is simple like
docker run -p 4443:4443 --rm -e FORCE_PLAIN_HTTP=false -e SERVER_SSL_ENABLED=true -e OPENVIDU_SECRET=YOUR_SECRET -e DOMAIN_OR_PUBLIC_IP=YOUR_IP openvidu/openvidu-dev:2.32.1
You’d need to include client lib though. I’m not sure if that can be set up for playground. But that’s also fairly straightforward: Hello World - OpenVidu Docs
No big deal anyway, old audio engine still works.

can’t promise to dive into it today, but I will.

Sorry, forgot to update - I am still investigating this. So far it seems like a browser limitation that we can’t overcome, but I want to be 100% sure before I say that with confidence.

Silly question, how come with V1 audio WebRTC streams get attached to mesh and are spatial?
Maybe it helps with overcoming the browser limitation.
V1 and V2 don’t look all that different to me, like, new one looks cleaner and bigger, but I’m looking from outside, I don’t really see how audio nodes are arranged.

Please bear with us, @RaananW will be back from summer vacations shortly.

So - this isn’t a Babylon problem, and it’s now fixed so it works automatically.

What’s actually going on is a browser quirk (mainly in Chrome). When you take an audio stream from a WebRTC call and feed it into Web Audio, the browser only produces sound from that stream while it’s also connected to a normal HTML <audio> (or <video>) element. If nothing is “playing” the stream through such an element, the browser hands Web Audio pure silence - and silence obviously can’t be spatialized. That’s why your audio seemed non-spatial: the panner was working perfectly, it just had nothing coming in.

I proved this by sending the exact same WebRTC stream through the same spatial panner two ways: without an <audio> element it was silent, and with a muted one attached it spatialized perfectly - identical to a plain test tone. So the panner and Babylon’s spatial code were never the issue.

The fix (PR submitted - fix(AudioV2): spatialize MediaStream/WebRTC sound sources out of the box by RaananW · Pull Request #18745 · BabylonJS/Babylon.js · GitHub): CreateSoundSourceAsync now quietly attaches a hidden, muted <audio> element for you whenever you hand it a MediaStream-based node. So you can just do:

const streamNode = audioContext.createMediaStreamSource(remoteStream);
const sound = await BABYLON.CreateSoundSourceAsync("remote", streamNode, { spatialEnabled: true });
sound.spatial.attach(mesh);

…and it will be spatial, no extra steps.

Once the PR provides a snapshot you can test it yourself and see if it works as expected. I will be happy to hear if it is fixed before merging the PR :slight_smile:

Thanks for confirmation!

So I messed it up somehow :confused: But I most certainly didn’t mistake silence for sound :slight_smile:
This patch doesn’t apply to my case though, I have video elements (that may or may not have video or audio tracks) that are properly managed and muted.

I will be happy to see a reproduction, and will be happy to test a real-world scenario. otherwise it is very hard to udnerstand what the issues is (that is, if this patch hasn’t solved the issue, which, if i understand correctly, it didn’t).

No worries, V1 works for me. I’m just upgrading for babylon 7 to 8, so I thought I’d give audio V2 a try.
Guess I’ll try again when I upgrade to 9.

I did some more analysis, read through your code, etc. Starting from the above snippet, I don’t see how it can work. Like, CreateSoundSourceAsync does not pass sound options to spatial, then it gets defaults like maxDistance 10000. I don’t see how the position updates, it just remains at 0,0,0. Etc, and it’s really easy to mistake for non-spatial.
Definitely not ‘no extra steps’ :slight_smile:

Real world scenario is quite complicated though. You know VRChat, right? Well, some youtuber described vrspace.org as ‘like early version of VRChat’. (which is quite an accomplishment considering I’m just a guy, and it’s all open source)
I can’t really say what people use it for, handful that came forward were different startups. One of them was doing virtual parties during covid lockdowns :slight_smile:
Overall, small but important niche.

Anyway, a reproduction would require publicly available webrtc server (that I provide at vrspace.org), and some code that ‘glues’ video/audio stream(s) to moving mesh(es). That part is heavily dependent on streaming client lib. Worse yet, it requires html arranged as that client lib requires.
Doable, I’m not sure if it’s doable in the playground though.
I might be able to do it, but right away I can say there’s no way I could commit any time to it before December. Because I’m just a guy :wink: