@wonderlandengine/spatial-audio
v1.2.5
Published
Spatial audio components for Wonderland Engine
Readme
Wonderland 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.
Usage Guide
Instructions on how to set up and use Wonderland Spatial Audio:
Installation
Install the components in your project:
npm install --save @wonderlandengine/spatial-audioWonderland Editor will automatically detect the components from this package and they will be available to attach to objects.
Audio Listener
For VR audio: attach an
audio-listenercomponent to thePlayer > Headobject. This controls position and orientation of the receiver. Updates occur each frame.For PC/mobile audio: attach the
audio-listenercomponent to theNonVrCamera. Only one listener component should be active at any given time. To achieve this, use thevr-mode-active-switchcomponent.
⚠️ The listener is necessary for spatial panning to work correctly in the AudioManager and AudioSource!
Audio Sources
Add an audio-source component to objects that should play sound. Set the src
property to a URL in the static folder of your project.
(E.g., for static/sfx/sound.mp3 enter sfx/sound.mp3).
If spatial is set to None, all settings below are ignored.
- Changing the
volumeparameter will only take effect when calling theplay()function. If the audio is already playing, usesetVolumeDuringPlayback()instead.
AudioManager
The AudioManager can be used to play audio from anywhere in your project! It is a way to conveniently
manage audio files. This package provides a global instance of the manager called globalAudioManager.
To make use of it, create your own identifiers and then load up the manager with your audio files:
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:
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.
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
control the volume globally. On using play(), the respective channels can be selected via the PlayConfig.
Considerations
Best Practices
HRTF Performance: The
HRTFsetting 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-sourcein a scene will remain at the same position, activate theisStationaryflag to disable position updates each frame for better performance.
Meta Quest 2 Performance
On Meta Quest 2, the maximum number of simultaneously playing audio sources is approximately 30.
