Microaudio Module

GX includes a microaudio module for simple WAV sound playback and for streams that a callback fills. It is a zero-dependency, header-only C library that uses the native platform audio API (waveOut on Windows). Designed for games and tools that need sound effects and background music without pulling in a large audio framework.

Prerequisites

No external dependencies. On Windows, links against winmm automatically. Linux/macOS stubs are provided (playback not yet implemented on those platforms).

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 sfx = audio_load("explosion.wav")
    var music = audio_load("background.wav")

    audio_set_volume(sfx, 0.8)
    audio_play(sfx)

    audio_set_volume(music, 0.4)
    audio_play_loop(music)

    // ... game loop ...

    audio_stop_all()
    audio_free(sfx)
    audio_free(music)
}

Initialization

FunctionSignatureDescription
audio_init() → i32Initialize audio system. Returns 1 on success.
audio_shutdown()Stop all sounds and free resources
audio_init()
defer audio_shutdown()

Loading & Freeing

FunctionSignatureDescription
audio_load(str) → i32Load WAV file, returns sound ID (0-63). Returns -1 on error.
audio_load_into(str, []u8) → i32Load WAV file into the program’s memory, kept until audio_free; -1 when it does not fit
audio_free(i32)Free a loaded sound by ID
var jump_sfx = audio_load("assets/jump.wav")
if (jump_sfx < 0) {
    print("Failed to load sound!\n")
}

// When done:
audio_free(jump_sfx)

Supported formats: 8-bit and 16-bit PCM WAV, mono or stereo, any sample rate. Max file size: 64 MB. Up to 64 sounds can be loaded simultaneously.

Playback

FunctionSignatureDescription
audio_play(i32)Play sound once
audio_play_loop(i32)Play sound on loop
audio_stop(i32)Stop all instances of a sound
audio_is_playing(i32) → boolCheck if sound is playing
audio_stop_all()Stop all sounds
// Play hit sound on collision
if (hit_detected) {
    audio_play(hit_sfx)
}

// Loop background music
audio_play_loop(bgm)

// Stop music when entering menu
audio_stop(bgm)

Up to 32 sounds can play simultaneously. If all channels are busy, the oldest non-looping channel is stolen for the new sound.

Volume

FunctionSignatureDescription
audio_set_volume(i32, f32)Set per-sound volume (0.0 - 1.0)
audio_get_volume(i32) → f32Get per-sound volume
audio_set_master_volume(f32)Set master volume (0.0 - 1.0)
audio_get_master_volume() → f32Get master volume

Final volume = sound_volume * master_volume. Both default to 1.0.

// Quiet background music, louder effects
audio_set_master_volume(0.8)
audio_set_volume(bgm, 0.3)
audio_set_volume(explosion, 1.0)

// Mute all audio
audio_set_master_volume(0.0)

Streams

FunctionSignatureDescription
audio_stream_open(i32, i32, fn(out *f32, i32)) → i32Open a stream at a rate with 1 or 2 channels; returns a stream id, -1 on error
audio_stream_close(i32)Stop the stream; the callback is not running once this returns

A stream plays whatever its callback writes. The callback gets a buffer and a frame count, and writes frames * channels samples from -1 to 1, channels interleaved. The buffer starts as silence, so a callback with nothing to play can return. A 440 Hz tone looks like this:

import math.scalar

var phase: f32 = 0.0

fn tone(out buf: *f32, frames: i32) {
    var step: f32 = 2.0 * PI() * 440.0 / 44100.0
    for (i = 0:frames - 1) {
        buf[i] = sinf(phase) * 0.2
        phase = phase + step
        if (phase > 2.0 * PI()) {
            phase = phase - 2.0 * PI()
        }
    }
}

var id = audio_stream_open(44100, 1, tone)
// ...
audio_stream_close(id)

The callback runs on the stream’s own thread, about every 10 ms, and must not call audio_* functions. Data it shares with the main thread needs atomics or a ring buffer with one writer and one reader. Each stream queues about 80 ms of samples. Master volume applies to streams; audio_stop_all leaves them playing, and audio_shutdown closes them. Streams play alongside sounds, and the system mixes them.

Common Patterns

Sound Effects in a Game Loop

When a program uses both, import raylib before microaudio.audio. On Windows the audio header pulls in PlaySound from the system headers, which clashes with raylib’s own PlaySound if it comes first.

import raylib.core
import raylib.input
import microaudio.audio

fn main() {
    InitWindow(800, 600, "Sound Demo")
    audio_init()
    defer audio_shutdown()

    var shoot = audio_load("assets/shoot.wav")
    var jump = audio_load("assets/jump.wav")
    audio_set_volume(shoot, 0.7)

    while (!WindowShouldClose()) {
        if (IsKeyPressed(KEY_SPACE)) {
            audio_play(jump)
        }
        if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) {
            audio_play(shoot)
        }
        BeginDrawing()
        // ... render ...
        EndDrawing()
    }
    CloseWindow()
}

Background Music with Fade

var bgm = audio_load("music.wav")
audio_set_volume(bgm, 0.0)
audio_play_loop(bgm)

// Fade in over time
var vol: f32 = 0.0
while (vol < 1.0) {
    vol = vol + 0.01
    audio_set_volume(bgm, vol)
    time.gx_time_sleep_ms(16)
}

Checking Playback State

audio_play(fanfare)

// Wait until sound finishes
while (audio_is_playing(fanfare)) {
    time.gx_time_sleep_ms(10)
}
print("Sound finished!\n")

Limits

LimitValue
Max loaded sounds64
Max simultaneous playback channels32
Max WAV file size64 MB
Max open streams4
Supported formats8/16-bit PCM WAV, mono/stereo

Platform Notes

  • Windows: Uses waveOut API (winmm.lib, auto-linked via @link("winmm")). Each playing sound has its own device, and its volume is the device’s volume, so playing copies nothing. A loop is a buffer waveOut repeats itself; sounds need no thread. A stream is one more waveOut device with a thread that refills it.
  • Linux/macOS: Stub implementation — audio_load and audio_stream_open return -1, playback functions are no-ops. Future versions may add ALSA/PulseAudio/CoreAudio.
  • TCC: Fully supported on Windows.
  • Freeing: audio_free stops the sound first when it is playing.

Module Layout

modules/
  microaudio/
    c/
      microaudio.h       Header-only C implementation (WAV parser, waveOut)
    gx/
      audio.gx           Module: GX wrapper functions around ma_* C API