Monograph · 10 · Audio
Systems · full depth
Audio
miniaudio behind a small facade: init, SoundCategory buses, spatial vs non-spatial play, listener = camera, volume product, handle lifecycle. Aligned with audio_system.hpp.
Initialize / shutdown
- If already initialized, return true
new ma_engine+ma_engine_config_initma_engine_init— on failure log code and return false- Store unique_ptr with custom deleter →
ma_engine_uninit - m_initialized = true
Shutdown stops sounds, clears map, resets engine. Uninitialized play APIs return INVALID_SOUND_HANDLE (0).
SoundCategory buses
| Enum | Index | Use |
|---|---|---|
SoundCategory::SFX | 0 | One-shots |
SoundCategory::Music | 1 | Looping score |
SoundCategory::Ambient | 2 | Spatial beds |
Helpers: soundCategoryName, isValidSoundCategory. Convenience: playSfx, playMusic (loop default true), playAmbientAt.
playSound workflow
- Guard path + initialized
ma_sound_init_from_filema_sound_set_spatialization_enabled(false)- Loop flag; volume; start
- Insert into handle map; return handle
playSoundAt workflow
- Init from file; enable spatialization
- Set position; min distance 1; max 50; inverse attenuation model
- Loop/volume; start; store handle
- Update with
setSoundPositionif emitter moves
Listener sync
setListenerPosition(pos, forward, up) → ma_engine_listener_set_* on listener 0. Call every frame from camera. Godot host docs: synced automatically.
Spatial audio without matching viewpoint feels “wrong room” even when emitter coordinates are correct.
Volume product
Clamped buses let users mute Music without killing SFX.
Handles & bulk ops
SoundHandle = uint32; 0 invalid. stop/pause/resume/setVolume/isPlaying/activeSoundCount. Bulk: stop category, pause category, stop all. Facade never returns raw ma_sound* — deleters live in .cpp with full miniaudio types (header only forward-declares).
Forward-declare ma_engine
miniaudio header is huge; keep includes out of engine-wide headers.
audio_system.hppSoundCategory · APIaudio_system.cppma_* callsdocs/audio.mdHost/GDScripttests/audio/*Unit testsDesign units in this module
Each card is a focused design page (what / how / why + sources). Full tree: Sitemap.