Audio - Music and Sounds
How to add the room music and the sounds played with the animations (for example the footsteps), configured in the admin panel. The audio is the audio feature (enabled in Features).
How it works in the game
- Room music - an audio with the room name as Audio Key and that room in Room ID plays when the player enters the room. In the sample data reldens-new-age-town plays reldens-town.mp3 in the town.
- Animation sounds - an audio or a marker named with a prefix and the animation key plays when that animation runs, on any sprite of the room (players, NPCs and objects): i_ when it starts, u_ on every frame, r_ on every repeat, c_ when it completes and s_ when it stops. The sample footstep audio has the markers r_journeyman_right, r_journeyman_left and so on for each class and direction, so the step sounds while the player walks.
- The audios without Room ID are global (available in every room), the ones with a room are only loaded in that room and have priority over the global ones with the same key.
The players turn each category on and off in the settings box (Audio Settings), the choice is saved per player:
Setting up the audio in the admin panel
The Audio section of the admin sidebar has four lists:
- Categories - the groups the players can turn on and off. Single Audio plays one audio of the category at a time (the music category stops the previous song), the others can sound together (sound).
- Audios - the Audio Key, the Files Name (upload one or more files of the same audio, for example the .mp3 and the .ogg; they are saved in the theme assets/audio folder), the optional Config, the Room ID (empty for global), the Category ID and Enabled.
- Markers - parts of an audio file, with the Marker Key, the Start and the Duration in seconds: a single file can hold many sounds.
- Players Configuration - the categories each player turned on or off, saved by the game.
The Config JSON is merged with the client/general/audio/defaultAudioConfig config and passed to the Phaser sound (for example {"loop":true,"volume":0.5}), and it accepts {"onlyCurrentPlayer":true} to play the sound only for the animations of the player own sprite, as the footsteps do.
With the hot plug enabled (the RELDENS_HOT_PLUG environment variable, enabled by default) saving, disabling or deleting an audio updates the connected players without restarting the server.
Code integration (advanced)
- Server: AudioPlugin (lib/audio/server/plugin.js) loads the global audios and the audios of each room, and sends them with the categories and the player configuration when the player joins.
- Client: AudioManager (lib/audio/client/manager.js) loads the files and markers, SceneAudioPlayer plays the room audio and listens to the sprites animation events, and AudioUi renders the settings.
- See Audio Entity, Audio Categories Entity, Audio Markers Entity and Audio Player Config Entity.
reldens