Something went wrong. Try again.
Plays short audio files such as a terminal bell through PipeWire on Linux and WASAPI on Windows.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie <jeff@ocjtech.us>// SPDX-License-Identifier: MIT
const std = @import("std");
pub fn build(b: *std.Build) void { const target = b.standardTargetOptions(.{}); const optimize = b.standardOptimizeOption(.{});
// The library, which is what a consumer imports: // // const chime = b.dependency("chime", .{ ... }); // exe.root_module.addImport("chime", chime.module("chime")); const mod = b.addModule("chime", .{ .root_source_file = b.path("src/root.zig"), .target = target, .optimize = optimize, });
// The decoders, one per format a Sound can be loaded from. zig-ogg is // named directly as well as through zig-flac because Opus lives in Ogg // too, and zig-opus does not unwrap it. inline for (.{ "riff", "ogg", "flac", "opus", "mp3" }) |name| { const dep = b.dependency(name, .{ .target = target, .optimize = optimize }); mod.addImport(name, dep.module(name)); }
// The sound output, which is the only part that differs by platform. The // other platform's backend is lazy, so it is never fetched for a build // that cannot use it. switch (target.result.os.tag) { .linux => if (b.lazyDependency("pipewire", .{ .target = target, .optimize = optimize })) |dep| { mod.addImport("pipewire", dep.module("pipewire")); }, .windows => if (b.lazyDependency("wasapi", .{ .target = target, .optimize = optimize })) |dep| { mod.addImport("wasapi", dep.module("wasapi")); }, else => {}, }
const test_step = b.step("test", "Run the unit tests"); // LLVM rather than the self-hosted backend, which emits no coverage // instrumentation for kcov or the fuzzer to read. const mod_tests = b.addTest(.{ .root_module = mod, .use_llvm = true }); test_step.dependOn(&b.addRunArtifact(mod_tests).step);
// Compiling everything without running it, for a quick check that an edit // did not break a path the tests do not reach. const check_step = b.step("check", "Compile everything without running it"); check_step.dependOn(&mod_tests.step);
// The library end to end, against the real sound output. const play = b.addExecutable(.{ .name = "play", .root_module = b.createModule(.{ .root_source_file = b.path("tools/play.zig"), .target = target, .optimize = optimize, .imports = &.{.{ .name = "chime", .module = mod }}, }), }); const play_run = b.addRunArtifact(play); play_run.addPassthruArgs(); const play_step = b.step("play", "Play audio files: zig build play -- FILE..."); play_step.dependOn(&play_run.step); check_step.dependOn(&play.step);
// The documentation is a side effect of compiling the module, so the library // is built purely to get at it. What comes out is not a page but a program: // a WebAssembly viewer, its javascript, and a tar of the sources it reads // from -- which is why it has to be served rather than opened. const library = b.addLibrary(.{ .name = "chime", .root_module = mod }); const install_docs = b.addInstallDirectory(.{ .source_dir = library.getEmittedDocs(), .install_dir = .prefix, .install_subdir = "docs", }); const docs_step = b.step("docs", "Build the API documentation into zig-out/docs"); docs_step.dependOn(&install_docs.step);
const docs_port = b.option(u16, "docs-port", "Port for `zig build docs-serve` (default 8000)") orelse 8000;
const docs_server = b.addExecutable(.{ .name = "docs-server", .root_module = b.createModule(.{ .root_source_file = b.path("tools/docs_server.zig"), // Always the machine running the build, never whatever -Dtarget the // library is being built for. .target = b.graph.host, .optimize = .Debug, }), });
const run_docs_server = b.addRunArtifact(docs_server); run_docs_server.step.dependOn(&install_docs.step); run_docs_server.addDirectoryArg(library.getEmittedDocs()); run_docs_server.addArg(b.fmt("{d}", .{docs_port})); // It runs until interrupted, so its output has to reach the terminal rather // than being captured by the build runner. run_docs_server.stdio = .inherit;
const docs_serve_step = b.step("docs-serve", "Serve the API documentation over HTTP"); docs_serve_step.dependOn(&run_docs_server.step);
// It has a test of its own; without this it would never run. And nothing // else builds it, so it belongs in `check` too, or it can stop compiling and // no test will notice. test_step.dependOn(&b.addRunArtifact( b.addTest(.{ .root_module = docs_server.root_module }), ).step); check_step.dependOn(&docs_server.step);}