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¶
Core features¶
| Feature | API | Notes |
|---|---|---|
| Playback control | play(), pause(), stop(), resume() |
Standard transport controls |
| Looping | setLoop(bool) |
Loop music or ambient sound |
| Volume | setVolume(float) |
Per-sound level from 0.0 to 1.0+ |
| Pitch / speed | setSpeed(float) |
1.0 = normal, 2.0 = double speed |
| Pan | setPan(float) |
–1.0 = full left, 0 = center, +1.0 = full right |
| Seek | seek(seconds) |
Jump to a position in the audio |
| 3D position | setPosition() or entity Transform |
Spatial audio uses the entity's world position |
| Attenuation | setSoundAttenuation(), setMinDistance(), setMaxDistance() |
Distance-based volume falloff |
| Doppler effect | 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.
Attenuation models¶
SoundAttenuation |
Behavior |
|---|---|
NONE |
No distance attenuation — volume is constant regardless of distance |
INVERSE |
Volume decreases inversely with distance (realistic for point sources) |
LINEAR |
Volume falls off linearly between min and max distance |
EXPONENTIAL |
Exponential falloff, louder near the source |
Global controls¶
AudioSystem provides scene-wide controls:
SoundPool¶
For sounds that play frequently (gunshots, footsteps, coins), use SoundPool to avoid
loading the same audio data multiple times. The pool caches loaded audio and reuses it
across multiple Sound instances.
SoundPool::load("audio/coin.ogg");
// Later, play from any number of Sound objects without re-loading
Sound coin1(&scene);
coin1.load("audio/coin.ogg"); // served from pool
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 have a parent Transform entity — set
attenuation to
NONE. - 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