Audio Engine V2 Usage

Yo @troymius

Its really not that bad. @docEdub did an excellent job in implementing the new audio engine (Kudos to him).

The main thing to take away is that you have to create (and save a reference to) an AudioEngineV2 object. Think of it like a master mixer and the volume like properties at the this master mixer level is for ALL the sounds playing.

So you create your main audio engine (can be one line of code):

const audioEngine = await BABYLON.CreateAudioEngineAsync(...options...go...here);
await audioEngine.unlock(); // Note: You can optionally unlock audio here or before you play a track 
audioEngine.volume = 0.5; // Note: This is optional to set the master volume level

Then create your sound type. So instead of directly instantiating a legacy new BABYLON.Sound object, after you create an audio engine. You create an instance of the sound type want, static, streaming or buffer:

const sound = await BABYLON.CreateSoundAsync("Sound 1", "assets/sounds/test.mp3", { loop: true, autoplay: true });
sound.volume = 1.0; // Note: This is optional to set the track volume level

So you can still create and play a sound with just a few lines of code:

// Create master audio engine
const audioEngine = await BABYLON.CreateAudioEngineAsync(...options...go...here);
await audioEngine.unlock();

// Create and auto play a track 
await BABYLON.CreateSoundAsync("Sound 1", "assets/sounds/test.mp3", { loop: true, autoplay: true });
2 Likes

Ok, I do appreciate your help here. This indeed looks easy. But if you have, in your existing app, sounds defined as attributes of many different object, this messes things up quite a bit.

You can always use the old audio engine if you prefer.
Simply add audioEnabled: true in the Babylon engine options:

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

I find this new V2 audio engine much more flexible for my part.

In that case you can enable the legacy audio engine when you create your BABYLON.Engine or BABYLON.WebEngine. Then you dont have to change your existing code.

Its actually audioEngine not audioEnabled

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

Yo @docEdub … Can you add your two cents about how long the legacy audio engine will be supported, Always ? Or will it eventually be phased out ?

Well seen. I missed this one.

const sound = await BABYLON.CreateSoundAsync( … )

So if I define a sound with this line, how do I make it play or stop playing?

The old way ( sound.play() ) does not seem to work

You have play () or stop ()

const sound = await BABYLON.CreateSoundAsync( … )
sound.play()
sound.pause();
sound.stop();

You have some example here :

And the documentation should soon arrive.

THANK YOU :slight_smile:

Actually this is probably a better way

const audioEngine = await BABYLON.CreateAudioEngineAsync(options);
const sound = await audioEngine.createSoundAsync(name, source, options);
await audioEngine.unlock();
sound.play();

I use audioEngine.createSoundAsync instead of BABYLON.CreateAudioEngineAsync. But its all probably the same underneath. I just like the flow better, Create my audio engine and use that to create my sounds. I use BABYLON.StaticSound mostly… and it has the normal play, stop, pause, pitch, volume, etc properties.

Hey all, just a quick heads up that I changed the names of the async functions for the 8.0 release to have Async at the end to make them consistent with the rest of the Babylon.js framework. So for example audioEngine.unlock() is now audioEngine.unlockAsync().

1 Like