Skip to content
haruki7049 edited this page Mar 29, 2026 · 4 revisions

lightmix

lightmix is an audio processing library written in Zig. It enables developers to generate, mix, and process audio waveforms programmatically as part of the standard build workflow.

Why lightmix?

  • Build-time audio generation: Audio files (WAV) are treated as build artifacts. Running zig build produces WAV files just as it would a binary executable.
  • Standard toolchain integration: No specialized IDEs or runtimes—just the Zig build system and your preferred editor.
  • Type-safe, generic API: Wave(T) and Composer(T) use Zig's type system for compile-time sample-type checking.

Pages

Page Description
Installation How to add lightmix to your Zig project
Wave-API Wave(T) type — creating, mixing, filtering, reading, writing, and playing audio
Composer-API Composer(T) type — sequencing and overlaying multiple waveforms
Build-Helpers Build-time helpers: addWave, installWave, addPlay, installPlay

Quick Start

const std = @import("std");
const lightmix = @import("lightmix");
const Wave = lightmix.Wave;

pub fn main() !void {
    const allocator = std.heap.page_allocator;

    // Generate a 440 Hz sine wave (A4 note), 1 second at 44100 Hz
    var samples: [44100]f64 = undefined;
    for (0..samples.len) |i| {
        const t = @as(f64, @floatFromInt(i)) / 44100.0;
        samples[i] = 0.5 * @sin(440.0 * 2.0 * std.math.pi * t);
    }

    const wave = try Wave(f64).init(samples[0..], allocator, .{
        .sample_rate = 44100,
        .channels = 1,
    });
    defer wave.deinit();

    const file = try std.fs.cwd().createFile("result.wav", .{});
    defer file.close();
    const buf = try allocator.alloc(u8, 10 * 1024 * 1024);
    defer allocator.free(buf);
    var writer = file.writer(buf);

    try wave.write(.wav, &writer.interface, .{
        .format_code = .pcm,
        .bits = 16,
    });
    try writer.interface.flush();
}

See Installation to add lightmix as a dependency, then browse the examples directory for more.

Version

lightmix 0.19.0 — requires Zig 0.15.2 or later.

This project follows Ziglang's minor version.

Resources

Clone this wiki locally