Audio Engine V2 Usage

Yo @Evgeni_Popov and @sebavan

So as of 7.50…

BABYLON.AbstractEngine.audioEngine references gives deprecated warning and says to Use AudioEngineV2.

I cant seem to find any info on the replacement for BABYLON.AbstractEngine.audioEngine using AudioEngineV2

Let me add @RaananW and @docEdub to the thread

New to the forum, but in case it helps. You can find the documentation for the new AudioEngineV2 here:

AudioEngineV2 | Babylon.js Documentation

And this is a sample playground of playing a sound using it, using the same music sound that is used in the old AudioEngine tutorial:

A Basic Scene | Babylon.js Playground

1 Like

Yo @AlitarSemiramis

Couple Questions:

1… How to handle when no audio context using BABYLON.Engine.audioEngine.onAudioUnlockedObservable ?

2… So is there no dependency on the underlying HTMLAudioElement anymore ?

3… And is there a way to attach to mesh ?

@docEdub Is there not documentation tutorial for use and explanation?
I am interested in learning more about the V2 audio engine

Ya, the new audio engine “V2” went live this week, and we issued a breaking change notification a few weeks ago to announce the disabling of the old audio engine by default instead of enabling it by default when the graphics Engine is created.

The playground automatically enables the old audio engine when it is used, so no change should be needed for new or existing playgrounds.

In your own code you’ll need to set the Engine constructor’s audioEngine option to true to enable the old audio engine, for example:

var engine = BABYLON.Engine(canvas, true, { audioEngine: true }, true);
3 Likes

No docs, yet, but there is an example playground live at https://playground.babylonjs.com/#N1RFVM#2 that should give you a good start!

1 Like
  1. When using the new audio engine v2, you should not use BABYLON.Engine.audioEngine. That is only used for the old audio engine and is only retained for backward compatibility. The new audio engine is not tied to the BABYLON.Engine in any way, so you can just call BABYLON.CreateAudioEngineAsync() and start using it when the promise resolves.

  2. The BABYLON.CreateStreamingSoundAsync function creates a sound that uses the browser’s underlying HTMLAudioElement. There is also a BABYLON.CreateSoundAsync function that creates a sound using the WebAudio API’s underlying AudioBufferSourceNode, which will give better performance for short sound files.

  3. Attaching to meshes and any other scene Node objects will be added soon with PR 16251. I also have working audio analyzer code that will be merged in another PR later this week.

1 Like

There is also no GUI to unlock the browser’s audio context, yet. This will be added soon, too.

By default, the new audio engine should unlock the browser’s audio context automatically when the document is clicked, or you can disable that functionality by setting the resumeonInteraction option to false and add your own click handler …

const audioEngineV2 = await BABYLON.CreateAudioEngineAsync({ resumeOnInteraction: false });
document.addEventListener("click", () => { audioEngineV2.unlock(); });
1 Like

…and of course docs will be coming soon! :slight_smile:

4 Likes

Thanks @docEdub

Yo @docEdub … VERY NICE… Thank you very much :slight_smile:

/**
 * Attaches the audio source to a scene object.
 * @param sceneNode The scene node to attach the audio source to.
 * @param useBoundingBox Whether to use the bounding box of the node for positioning. Defaults to `false`.
 * @param attachmentType Whather to attach to the node's position and/or rotation. Defaults to `PositionAndRotation`.
 */
abstract attach(sceneNode: Node, useBoundingBox?: boolean, attachmentType?: SpatialAudioAttachmentType): void;
/**
 * Detaches the audio source from the currently attached graphics node.
 */
abstract detach(): void;

My Little Wrapper function for setting the data source:

1 Like

Yo @docEdub when trying to add LEGACY audio support to Version 7.52.2

var engine = BABYLON.Engine(canvas, true, { audioEnabled: true }, true);

Give the following Error:

 Argument of type '{ audioEnabled: boolean; }' is not assignable to parameter of type 'EngineOptions'.
  Object literal may only specify known properties, and 'audioEnabled' does not exist in type 'EngineOptions'.

My mistake. The correct option is audioEngine: true, not audioEnabled: true.

It’s the same option for all engines derived from AbstractEngine, including WebGPUEngine.

I would like to say @docEdub that this V2 audio engine is really great. Simple and efficient. It was well thought out and an impressive work was done on it.
Just a big thank you. He had been expected for a long time and he is there.
I can’t wait to see the doc when she is finished.

1 Like

I totally agree, great job @docEdub… I use as the underlying Unity AudioSource component for the Babylon Toolkit.

The overall mixer seems better to… When player Quick Shot sound like the pac man eating the dots and the background music blend together nicely… A little better the V1 when play multiple sounds at once… Or at least it seems that way to me

1 Like

Thanks all for being on the bleeding edge of the new audio engine. It’s been a little bumpy but your feedback is making it better!

Based on your feedback, I’ve made some changes to the new audio engine’s API to address some of the issues you’re running into. See PR 16318 for the list of improvements and note that some of the changes may break code you wrote against the new audio engine, so watch out for these changes and thanks for your patience as I tweak things to 100% before Babylon 8 is released.

Thanks again!

2 Likes

Hey guys, is there a simpler example? My original audio that had like 3 lines of code worked for years. But now it no longer works and the example posted above is like 10 pages of code, I can’t make sense of it. Come on :slight_smile:
Maybe keep simple audio in place and add advanced audio?
Specifically the …AudioEngine.setGlobalVolume(0.25)… seems to be completely ignored now.
…getGlobalVolume() … returns the correct number (e.g. 0.25) but no effect on volume.