Keel’s audio system is backed by miniaudio — a single stereo playback device running at 44.1 kHz / signed-16-bit. Sound effects are decoded once into a cache and mixed in a background thread; music is streamed file-by-file. The public API is a handful of module-level functions that delegate to anDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/VKSFY/keel/llms.txt
Use this file to discover all available pages before exploring further.
AudioEngine world resource.
Requires: pip install miniaudio.
Setup
setup_audio
AudioEngine, inserts it as a world resource, and registers a Phase.PRE_UPDATE system that calls engine.update(dt) each frame to drain finished SFX handles and advance music fades. Also registers engine.shutdown as an app shutdown hook. Idempotent.
AudioSetup fields
The wired
AudioEngine instance. Most users interact with the module-level helpers (play_sound, play_music, etc.) rather than the engine directly. Use audio.engine when you need lower-level access such as engine.is_playing(handle) or engine.is_music_playing().Sound effects
play_sound
path (if not already cached) and start playback. Returns a SoundHandle you can pass to stop_sound to cancel it early. Multiple simultaneous plays of the same file are supported — each returns a distinct handle.
Supported formats: .wav, .mp3, .ogg, .flac (all miniaudio-supported formats).
The Keel
App instance. The function looks up AudioEngine from app.world.Path to the audio file. Decoded and cached in memory on first play; subsequent calls with the same path are free (no disk I/O). Raises
FileNotFoundError if the path does not exist.Per-instance playback volume, clamped to
[0.0, 1.0]. Multiplied by the SFX channel gain and master gain in the mixer.When
True, the sound restarts from the beginning when it reaches the end.SoundHandle — an opaque, hashable, frozen dataclass wrapping a monotonically increasing integer id and the source path. Equality and hashing are by id only.
stop_sound
handle. The effect is immediate — the mixer drops the _PlayState before the next audio callback. No-op if the handle is unknown or already finished.
SoundHandle
SoundHandle is a frozen dataclass (@dataclass(frozen=True)) returned by play_sound. Store it if you need to stop or check a specific sound instance.
Monotonically increasing integer identifying this particular play instance. Unique per
AudioEngine lifetime.The file path that was passed to
play_sound. Informational only.Music streaming
play_music
path as background music, replacing any currently playing track. Only one music stream plays at a time. Music is streamed rather than decoded fully into memory, making it suitable for long tracks.
Path to the audio file to stream. Raises
FileNotFoundError if the path does not exist.When
True, the stream restarts from the beginning when it reaches the end.Fade-in duration in seconds. When
> 0, the music starts at gain 0.0 and ramps up to the current music volume over fade_in seconds. Fade progress is advanced by engine.update(dt) on the main thread.stop_music
fade_out > 0, the gain ramps to 0.0 over the specified number of seconds before the stream is discarded. No-op if no music is playing.
Fade-out duration in seconds.
0.0 stops immediately.Volume control
set_volume
None for any channel you want to leave unchanged. All values are clamped to [0.0, 1.0].
The mixer’s effective gain hierarchy is:
Master gain applied to all output (SFX + music).
None = leave unchanged.SFX channel gain applied to all one-shot and looping sound effects.
None = leave unchanged.Music channel gain applied to the active music stream.
None = leave unchanged. Re-aims any active fade’s target gain.AudioEngine
AudioEngine is the low-level class that owns the miniaudio PlaybackDevice, the SFX cache, the active sound table, and the at-most-one music stream. It is inserted as a world resource by setup_audio and can be injected into systems.
You generally do not need to use AudioEngine directly — the module-level functions cover all common cases. The engine is useful for status queries:
Properties
Current master gain. Read-only property; use
set_master_volume to change it.Current SFX channel gain.
Current music channel gain.
Key methods
Returns
True if the sound referenced by handle is still active and not yet finished.Returns
True while a music stream is active, including during a fade-out ramp.Stop every active SFX instance and the music stream simultaneously.
Stop all playback and close the miniaudio device. Called automatically by
App.run() on exit. Idempotent.Supported audio formats
| Format | Extension | Notes |
|---|---|---|
| WAV | .wav | Uncompressed or PCM; fastest decode |
| MP3 | .mp3 | Requires miniaudio’s libmp3lame backend |
| OGG Vorbis | .ogg | Recommended for music |
| FLAC | .flac | Lossless |
