Audio¶
Doriax uses SoLoud for audio playback and exposes sound behavior through the
Sound object class, SoundComponent, SoundPool, and AudioSystem. Audio entities
are regular ECS entities — you position them in the world just like any other object,
and the audio system reads their Transform to compute spatial attenuation.
Adding a sound to a scene¶
Lua uses properties, C++ uses setters
Most sound parameters are exposed to Lua as properties (sound.volume = 0.8),
while C++ uses setter methods (sound.setVolume(0.8f)). The property/method names
below list both forms.
Core features¶
| Feature | Lua property | C++ method | Notes |
|---|---|---|---|
| Playback control | play(), pause(), stop() |
same | Transport controls (call play() again to resume a paused sound) |
| Looping | sound.looping |
setLooping(bool) |
Loop music or ambient sound |
| Volume | sound.volume |
setVolume(float) |
Per-sound level from 0.0 to 1.0+ |
| Pitch / speed | sound.speed |
setSpeed(float) |
1.0 = normal, 2.0 = double speed |
| Pan | sound.pan |
setPan(float) |
–1.0 = full left, 0 = center, +1.0 = full right |
| Seek | seek(seconds) |
same | Jump to a position in the audio |
| 3D mode | sound.sound3D |
setSound3D(bool) |
Enable spatialization (or pass true to the constructor) |
| Attenuation | sound.attenuationModel, sound.minDistance, sound.maxDistance |
setAttenuationModel(), setMinDistance(), setMaxDistance() |
Distance-based volume falloff |
| Doppler effect | sound.dopplerFactor |
setDopplerFactor(float) |
Pitch shift from relative motion |
3D spatial audio¶
Attach a sound to a moving entity to get automatic position-based spatialization. The audio system reads the entity's world transform every frame.
Pass true to the Sound constructor (or set sound.sound3D = true) to make a sound
spatial. A 3D sound is positioned by its entity's Transform, so parent it under the
moving entity in the editor's Structure panel (or in the scene hierarchy).
Attenuation models¶
SoundAttenuation |
Behavior |
|---|---|
NO_ATTENUATION |
No distance attenuation — volume is constant regardless of distance |
INVERSE_DISTANCE |
Volume decreases inversely with distance (realistic for point sources) |
LINEAR_DISTANCE |
Volume falls off linearly between min and max distance |
EXPONENTIAL_DISTANCE |
Exponential falloff, louder near the source |
Global controls¶
AudioSystem provides scene-wide controls:
SoundPool¶
For sounds that play frequently (gunshots, footsteps, coins), the engine caches decoded
audio data in an internal SoundPool so the same file is not loaded multiple times.
Loading the same path from several Sound instances reuses the cached data
automatically — you do not need to manage the pool yourself.
// Each Sound that loads the same path shares the cached audio data.
Sound coin1(&scene);
coin1.loadSound("audio/coin.ogg");
Sound coin2(&scene);
coin2.loadSound("audio/coin.ogg"); // served from the shared cache
Background music¶
Music typically loops, has no 3D attenuation, and plays at a lower priority than sound effects:
Audio workflow checklist¶
- Add audio files to the project resources folder (OGG or WAV recommended).
- Create a
Soundentity, set the resource path, and configure volume and loop. - For 3D audio: parent the sound to a spatial entity and configure attenuation.
- For music: mark the sound as looping; keep it non-spatial.
- Use
AudioSystemfor master volume and global pause. - Test on target hardware — mobile speakers and headphones differ significantly.
Practical tips¶
- Keep sound effect files short and compressed; use OGG/Vorbis for best size-to-quality ratio.
- Use WAV for very short, frequently-triggered sounds where decoding latency matters.
- Non-spatial sounds (UI clicks, music) should not be 3D — leave
sound3Doff (the default) so they play at full volume regardless of listener position. - Tune attenuation
minDistanceandmaxDistancein the actual scene scale so the falloff feels natural. - Separate music, ambient, and SFX into named volume categories so players can control them independently in settings.
See also¶
- Sound — full API reference
- AudioSystem — global controls