Microaudio Module
A minimal audio module for loading and playing WAV files. Uses the Windows
waveOut API directly — zero external dependencies.
Usage
import microaudio.audio
Build with -I modules:
gx myapp.gx -I modules -o myapp.exe
Quick Example
import microaudio.audio
fn main() {
audio_init()
defer audio_shutdown()
var snd = audio_load("explosion.wav")
if (snd < 0) {
print("Failed to load sound\n")
return
}
audio_set_volume(snd, 0.8)
audio_play(snd)
print("Playing... press Enter to stop.\n")
input()
audio_stop(snd)
audio_free(snd)
}
Initialization
audio_init() // initialize audio system
defer audio_shutdown() // clean up on scope exit
| Function | Signature | Description |
|---|
audio_init | () → i32 | Initialize audio system. Returns 0 on success. |
audio_shutdown | () | Shutdown and release all resources |
Loading & Freeing
var snd = audio_load("sound.wav") // returns sound ID (>= 0) or -1 on error
audio_free(snd) // release sound resources
| Function | Signature | Description |
|---|
audio_load | (str) → i32 | Load WAV file. Returns sound ID or -1 on failure. |
audio_free | (i32) | Free a loaded sound |
Playback
audio_play(snd) // play once
audio_play_loop(snd) // play looping
audio_stop(snd) // stop playback
audio_stop_all() // stop all sounds
if (audio_is_playing(snd)) {
print("Still playing\n")
}
| Function | Signature | Description |
|---|
audio_play | (i32) | Play sound once |
audio_play_loop | (i32) | Play sound in a loop |
audio_stop | (i32) | Stop a specific sound |
audio_stop_all | () | Stop all sounds |
audio_is_playing | (i32) → bool | Check if sound is currently playing |
Volume
audio_set_volume(snd, 0.5) // 50% volume for this sound
var vol = audio_get_volume(snd)
audio_set_master_volume(0.8) // 80% master volume
var master = audio_get_master_volume()
| Function | Signature | Description |
|---|
audio_set_volume | (i32, f32) | Set sound volume (0.0 - 1.0) |
audio_get_volume | (i32) → f32 | Get sound volume |
audio_set_master_volume | (f32) | Set master volume (0.0 - 1.0) |
audio_get_master_volume | () → f32 | Get master volume |
- Windows: Uses waveOut API. Links
winmm automatically.
- Linux/macOS: Not yet supported (planned: ALSA/CoreAudio backends).
- Supports WAV files only (PCM format).
- Sound IDs are integer handles. Negative return values indicate errors.
Comparison with Raylib Audio
| Microaudio | Raylib Audio |
|---|
| Dependencies | None (Windows API) | Raylib library |
| Formats | WAV only | WAV, OGG, MP3, FLAC |
| Streaming | No | Yes (LoadMusicStream) |
| Spatial audio | No | No |
| Cross-platform | Windows only (currently) | Windows, Linux, macOS |
| Setup | audio_init() | InitAudioDevice() |
Use microaudio for simple sound effects in standalone GX apps without
Raylib. Use Raylib audio for games that already use Raylib, or when
you need music streaming and more audio formats.
Module Layout
modules/
microaudio/
c/
microaudio.h C implementation header
gx/
audio.gx Module API
Example
| Example | Description |
|---|
examples/44_microaudio.gx | Load and play a WAV file with volume control |