Audio & Spatial Sound (`engine.Audio`)
Powered by the high-performance miniaudio backend, PyLua delivers low-latency decoding for WAV, MP3, and FLAC files with 3D spatial listener positioning.
Functions & Methods Reference
| Function / Method | Signature | Description |
|---|---|---|
engine.Audio.playSound(path) |
(string) → void |
One-shot sound trigger (e.g. gunshots, UI clicks, explosions). Automatically cleans up memory upon completion. |
engine.Audio.createSound(path) |
(string) → Sound |
Creates an instantiated audio object for fine-grained volume, looping, and positional control. |
sound:play() |
() → void |
Starts or resumes playback of the sound instance. |
sound:stop() |
() → void |
Stops playback and rewinds to the start. |
sound:setVolume(vol) |
(number) → void |
Sets playback volume (0.0 is silence, 1.0 is nominal full volume). |
sound:setLooping(loop) |
(boolean) → void |
Toggles seamless looping for background ambient tracks and music. |
sound:setPosition(x, y, z) |
(num, num, num) → void |
Sets 3D emitter coordinates for spatial distance attenuation and stereo panning. |
engine.Audio.setListenerPosition(x, y, z) |
(num, num, num) → void |
Updates listener (ears/camera) position in world coordinates. |
Background Music & Positional Audio Example
spatial_audio.lua
import engine
-- Loop ambient background music:
bgm = engine.Audio.createSound("assets/bgm_dungeon.mp3")
bgm:setLooping(true)
bgm:setVolume(0.4)
bgm:play()
-- Spatial torch sound emitter in the dungeon:
torch = engine.Audio.createSound("assets/torch_crackle.wav")
torch:setLooping(true)
torch:setPosition(400, 300, 0)
torch:play()
-- In player update loop:
function onPlayerMove(px, py) {
-- Stereo panning changes dynamically as the player moves relative to the torch!
engine.Audio.setListenerPosition(px, py, 0)
}