diff options
-rw-r--r-- | build.zig | 46 | ||||
-rw-r--r-- | src/al.zig | 34 | ||||
-rw-r--r-- | src/alc.zig | 8 | ||||
-rw-r--r-- | src/sf.zig | 14 | ||||
-rw-r--r-- | src/zeal.zig | 19 |
5 files changed, 61 insertions, 60 deletions
diff --git a/build.zig b/build.zig index 7b087a2..67f6993 100644 --- a/build.zig +++ b/build.zig @@ -1,5 +1,5 @@ // Build recipe -// Copyright (C) 2021-2022 Nguyễn Gia Phong +// Copyright (C) 2021-2023 Nguyễn Gia Phong // // This file is part of zeal. // @@ -17,16 +17,22 @@ // along with zeal. If not, see <https://www.gnu.org/licenses/>. const std = @import("std"); -const Builder = std.build.Builder; +const Build = std.Build; +const CrossTarget = std.zig.CrossTarget; const LibExeObjStep = std.build.LibExeObjStep; -const Mode = std.builtin.Mode; +const OptimizeMode = std.builtin.OptimizeMode; fn addExampleStep(comptime name: []const u8, description: []const u8, - b: *Builder, lib: *LibExeObjStep, mode: Mode) void { - const bin = b.addExecutable(name, "examples/" ++ name ++ ".zig"); + b: *Build, lib: *LibExeObjStep, + target: CrossTarget, optimize: OptimizeMode) void { + const bin = b.addExecutable(.{ + .name = name, + .root_source_file = .{ .path = "examples/" ++ name ++ ".zig" }, + .target = target, + .optimize = optimize, + }); bin.addPackagePath("zeal", "src/zeal.zig"); bin.linkLibrary(lib); - bin.setBuildMode(mode); const cmd = bin.run(); cmd.step.dependOn(b.getInstallStep()); @@ -42,21 +48,25 @@ pub fn link(lib_exe_obj: *LibExeObjStep) void { lib_exe_obj.linkSystemLibrary("c"); } -pub fn build(b: *Builder) void { - // Standard release options allow the person running `zig build` to select - // between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. - const mode = b.standardReleaseOptions(); - - const lib = b.addStaticLibrary("zeal", "src/zeal.zig"); +pub fn build(b: *Build) void { + const target = b.standardTargetOptions(.{}); + const optimize = b.standardOptimizeOption(.{}); + const lib = b.addStaticLibrary(.{ + .name = "zeal", + .root_source_file = .{ .path = "src/zeal.zig" }, + .target = target, + .optimize = optimize, + }); link(lib); - lib.setBuildMode(mode); - lib.install(); - var main_tests = b.addTest("src/zeal.zig"); + var main_tests = b.addTest(.{ + .root_source_file = .{ .path = "src/zeal.zig" }, + .target = target, + .optimize = optimize, + }); main_tests.linkLibrary(lib); - main_tests.setBuildMode(mode); b.step("test", "Run library tests").dependOn(&main_tests.step); - addExampleStep("play", "Play audio", b, lib, mode); - addExampleStep("hrtf", "Play audio with HRTF", b, lib, mode); + //addExampleStep("play", "Play audio", b, lib, target, optimize); + //addExampleStep("hrtf", "Play audio with HRTF", b, lib, target, optimize); } diff --git a/src/al.zig b/src/al.zig index 9e52786..ccabc7e 100644 --- a/src/al.zig +++ b/src/al.zig @@ -1,5 +1,5 @@ // OpenAL wrapper -// Copyright (C) 2021-2022 Nguyễn Gia Phong +// Copyright (C) 2021-2023 Nguyễn Gia Phong // // This file is part of zeal. // @@ -22,7 +22,7 @@ const meta = @import("std").meta; const c = @import("cimport.zig"); -pub const Error = error { +pub const Error = error{ /// Bad name (ID) passed to an OpenAL function. InvalidName, /// Invalid enum parameter passed to an OpenAL function. @@ -55,7 +55,7 @@ pub const listener = struct { /// Set a property for the listener. pub fn set(property: Property, value: anytype) Error!void { - const param = @enumToInt(property); + const param = @intFromEnum(property); const T = @TypeOf(value); switch (T) { f32 => c.alListenerf(param, value), @@ -64,7 +64,7 @@ pub const listener = struct { f32 => c.alListenerfv(param, value[0..]), i32 => c.alListeneriv(param, value[0..]), else => unreachable, - } + }, } switch (c.alGetError()) { @@ -116,23 +116,15 @@ pub const buffer = struct { /// Fill a buffer with audio data. pub fn fill(reference: u32, data: Data, freq: i32) Error!void { - const format = switch (data) { + c.alBufferData(reference, switch (data) { .mono8 => c.AL_FORMAT_MONO8, .mono16 => c.AL_FORMAT_MONO16, .stereo8 => c.AL_FORMAT_STEREO8, .stereo16 => c.AL_FORMAT_STEREO16, - }; - - switch (data) { - .mono8, .stereo8 => |slice| { - const size = @intCast(c_int, slice.len); - c.alBufferData(reference, format, slice.ptr, size, freq); - }, - .mono16, .stereo16 => |slice| { - const size = @intCast(c_int, slice.len) * 2; - c.alBufferData(reference, format, slice.ptr, size, freq); - }, - } + }, switch (data) { else => |slice| slice.ptr }, switch (data) { + .mono8, .stereo8 => |slice| @intCast(slice.len), + .mono16, .stereo16 => |slice| @intCast(slice.len * 2), + }, freq); switch (c.alGetError()) { c.AL_NO_ERROR => {}, @@ -193,13 +185,13 @@ pub const source = struct { /// Set a property for the source. pub fn set(reference: u32, property: Property, value: anytype) Error!void { - const param = @enumToInt(property); + const param = @intFromEnum(property); const T = @TypeOf(value); switch (T) { f32 => c.alSourcef(reference, param, value), i32 => c.alSourcei(reference, param, value), else => switch (@typeInfo(T)) { - .Enum => c.alSourcei(reference, param, @enumToInt(value)), + .Enum => c.alSourcei(reference, param, @intFromEnum(value)), else => switch (Child(T)) { f32 => c.alSourcefv(reference, param, value[0..]), i32 => c.alSourceiv(reference, param, value[0..]), @@ -220,7 +212,7 @@ pub const source = struct { /// Get a scalar property from the source. pub fn get(comptime T: type, reference: u32, property: Property) Error!T { - const param = @enumToInt(property); + const param = @intFromEnum(property); var value: T = undefined; switch (T) { f32 => c.alGetSourcef(reference, param, &value), @@ -229,7 +221,7 @@ pub const source = struct { .Enum => { var raw: i32 = undefined; c.alGetSourcei(reference, param, &raw); - value = @intToEnum(T, raw); + value = @enumFromInt(raw); }, else => unreachable, }, diff --git a/src/alc.zig b/src/alc.zig index 0b6439a..3db5440 100644 --- a/src/alc.zig +++ b/src/alc.zig @@ -1,5 +1,5 @@ // Audio Library Context wrapper -// Copyright (C) 2021-2022 Nguyễn Gia Phong +// Copyright (C) 2021-2023 Nguyễn Gia Phong // // This file is part of zeal. // @@ -32,7 +32,7 @@ pub const Device = c.ALCdevice; /// Opaque context handle. pub const Context = c.ALCcontext; -pub const Error = error { +pub const Error = error{ /// Invalid device handle. InvalidDevice, /// Invalid context handle. @@ -265,11 +265,11 @@ fn getInfo(comptime T: type, device: ?*Device, attr: Enum) !T { else => unreachable, }, i32 => return (try getInfo([1]i32, device, attr))[0], - [:0]const u8 => span(c.alcGetString(device, @enumToInt(attr))), + [:0]const u8 => span(c.alcGetString(device, @intFromEnum(attr))), else => array: { assert(@typeInfo(T).Array.child == i32); var data: T = undefined; - c.alcGetIntegerv(device, @enumToInt(attr), data.len, &data); + c.alcGetIntegerv(device, @intFromEnum(attr), data.len, &data); break :array data; }, }; diff --git a/src/sf.zig b/src/sf.zig index bce6dcc..1d8f1dd 100644 --- a/src/sf.zig +++ b/src/sf.zig @@ -1,5 +1,5 @@ // libsndfile wrapper -// Copyright (C) 2021-2022 Nguyễn Gia Phong +// Copyright (C) 2021-2023 Nguyễn Gia Phong // // This file is part of zeal. // @@ -29,7 +29,7 @@ pub const Mode = enum { read_write, }; -pub const Error = Allocator.Error || error { +pub const Error = Allocator.Error || error{ UnrecognizedFormat, SystemError, MalformedFile, @@ -58,7 +58,7 @@ pub const SndFile = struct { const pimpl = c.sf_open(path.ptr, c_mode, &info); _ = c.sf_command(pimpl, c.SFC_SET_SCALE_FLOAT_INT_READ, null, c.SF_TRUE); - return SndFile { + return SndFile{ .pimpl = pimpl orelse return switch (c.sf_error(null)) { c.SF_ERR_UNRECOGNISED_FORMAT => Error.UnrecognizedFormat, c.SF_ERR_SYSTEM => Error.SystemError, @@ -66,7 +66,7 @@ pub const SndFile = struct { c.SF_ERR_UNSUPPORTED_ENCODING => Error.UnsupportedEncoding, else => unreachable, }, - .frames = @intCast(usize, info.frames), + .frames = @intCast(info.frames), .sample_rate = info.samplerate, .channels = info.channels, .format = info.format, @@ -79,11 +79,11 @@ pub const SndFile = struct { /// The returned memory is managed by the caller. pub fn read(self: SndFile, allocator: Allocator, frames: usize) Error![]const i16 { - const items = frames * @intCast(usize, self.channels); + const items = frames * @as(usize, @intCast(self.channels)); const memory = try allocator.alloc(i16, items); errdefer allocator.free(memory); - const n = c.sf_read_short(self.pimpl, memory.ptr, @intCast(i64, items)); - return try allocator.realloc(memory, @intCast(usize, n)); + const n = c.sf_read_short(self.pimpl, memory.ptr, @intCast(items)); + return try allocator.realloc(memory, @intCast(n)); } /// Read the entire file. The returned memory is managed by the caller. diff --git a/src/zeal.zig b/src/zeal.zig index e5984bf..3be59fb 100644 --- a/src/zeal.zig +++ b/src/zeal.zig @@ -23,7 +23,7 @@ const alc = @import("alc.zig"); const sf = @import("sf.zig"); const std = @import("std"); -pub const Error = al.Error || error { +pub const Error = al.Error || error{ UncurrentContext, }; @@ -74,8 +74,8 @@ pub const Context = struct { stereo_sources: ?i32 = null, /// Maximum number of auxiliary source sends. max_auxiliary_sends: ?i32 = null, - /// Enabling HRTF. + /// Enabling HRTF. hrtf: ?alc.Logical = null, /// The HRTF to be used. hrtf_id: ?i32 = null, @@ -90,11 +90,11 @@ pub const Context = struct { var attr_list = [_]i32{ 0 } ** (fields.len * 2 + 1); var i: u8 = 0; inline for (fields) |f| if (@field(attributes, f.name)) |v| { - attr_list[i] = @enumToInt(@field(alc.Enum, f.name)); - attr_list[i + 1] = if (@TypeOf(v) == i32) v else @enumToInt(v); + attr_list[i] = @intFromEnum(@field(alc.Enum, f.name)); + attr_list[i + 1] = if (@TypeOf(v) == i32) v else @intFromEnum(v); i += 2; }; - return Context { + return Context{ .pimpl = try alc.createContext(device.pimpl, attr_list[0..i:0]), .device = device, }; @@ -111,9 +111,9 @@ pub const Context = struct { pub fn getCurrent() ?alc.Error!Context { if (alc.getCurrentContext()) |pimpl| - return Context { + return Context{ .pimpl = pimpl, - .device = Device { .pimpl = try alc.getContextsDevice(pimpl) }, + .device = Device{ .pimpl = try alc.getContextsDevice(pimpl) }, }; return null; } @@ -153,7 +153,7 @@ pub const Audio = struct { const sound = try SndFile.open(path, sf.Mode.read); defer sound.close(); const data = try sound.readAll(allocator); - return Audio { + return Audio{ .allocator = allocator, .data = switch (sound.channels) { 1 => al.Data{ .mono16 = data }, @@ -201,8 +201,7 @@ pub const Source = struct { } pub fn bind(self: Source, buffer: Buffer) Error!void { - try al.source.set(self.reference, .buffer, - @intCast(i32, buffer.reference)); + try al.source.set(self.reference, .buffer, @intCast(buffer.reference)); } /// Specify if the source always has 3D spatialization features (true), |