Spatial audio components for Wonderland Engine
npm install @wonderlandengine/spatial-audio!build
[![NPM Package][npm]](https://www.npmjs.com/package/@wonderlandengine/spatial-audio)
[![NPM Downloads][npm-downloads]](https://img.shields.io/npm/dw/@wonderlandengine/spatial-audio)
The Wonderland Audio System simplifies audio management with
Wonderland Engine. These components offer efficient control
over audio sources and listeners and enable seamless updates of their positions and
orientations in the WebAudio context.
Instructions on how to set up and use Wonderland Spatial Audio:
Install the components in your project:
``sh`
npm install --save @wonderlandengine/spatial-audio
Wonderland Editor will automatically detect the components from this package and they will
be available to attach to objects.
1. For VR audio: attach an audio-listener component to the Player > Head object.
This controls position and orientation of the receiver. Updates occur each frame.
2. For PC/mobile audio: attach the audio-listener component to the NonVrCamera.vr-mode-active-switch
Only one listener component should be active at any given time. To achieve this, use
the component.
⚠️ The listener is necessary for spatial panning to work correctly in the AudioManager and AudioSource!
Add an audio-source component to objects that should play sound. Set the srcstatic
property to a URL in the folder of your project.static/sfx/sound.mp3
(E.g., for enter sfx/sound.mp3).spatial
If is set to None, all settings below are ignored.
- Changing the volume parameter will only take effect when calling the play() function. If the audio is already setVolumeDuringPlayback()
playing, use instead.
The AudioManager can be used to play audio from anywhere in your project! It is a way to conveniently globalAudioManager
manage audio files. This package provides a global instance of the manager called .
To make use of it, create your own identifiers and then load up the manager with your audio files:
`js
enum MySounds {
Gunshot,
Zombie,
}
globalAudioManager.load('sfx/gunshot.mp3', MySounds.Gunshot);
// You can even load multiple files for one ID. On play, a random file of the provided ones will be selected.
globalAudioManager.load(['sfx/zombie_01.mp3', 'sfx/zombie_02.mp3'], MySounds.Zombie);
`
⚠️ Only load() and loadBatch() can be used in top-level code!
There are two ways of playing an audio file that has loaded:
`js`
globalAudioManager.play(MySounds.Gunshot); // Standard way, returns an ID with which audio can be stopped or paused.
globalAudioManager.autoplay(MySounds.Gunshot); // Plays the audio as soon as the user has interacted with the site.
Checkout the PlayConfig type, for all the configuration settings! It can be added to change the playback behaviour.
`js`
onPress() {
this.object.getPositionWorld(posVec);
globalAudioManager.play(MySounds.Gunshot, {
volume: 0.8,
loop: true,
position: posVec,
channel: AudioChannel.Sfx,
priority: false
});
}
The AudioManager has three main channels: Sfx, Music and Master. Use these to group your audio and play()
control the volume globally. On using , the respective channels can be selected via the PlayConfig.
- HRTF Performance: The HRTF setting demands substantial performance resources.
For less critical audio effects, consider deactivating it and rely on regular
(equal-power) 3D panning.
- Stationary Audio Sources: If an audio-source in a scene will remain at the sameisStationary` flag to disable position updates each frame for
position, activate the
better performance.
On Meta Quest 2, the maximum number of simultaneously playing audio sources is
approximately 30.
[npm]: https://img.shields.io/npm/v/@wonderlandengine/spatial-audio
[npm-downloads]: https://img.shields.io/npm/dw/@wonderlandengine/spatial-audio