diff --git a/audio/soundio/ChannelLayout.zig b/audio/soundio/ChannelLayout.zig new file mode 100644 index 00000000..e31c8aea --- /dev/null +++ b/audio/soundio/ChannelLayout.zig @@ -0,0 +1,11 @@ +const c = @import("c.zig"); +const intToError = @import("error.zig").intToError; +const Error = @import("error.zig").Error; + +const ChannelLayout = @This(); + +handle: c.SoundIoChannelLayout, + +pub fn channelCount(self: ChannelLayout) i32 { + return self.handle.channel_count; +} diff --git a/audio/soundio/Device.zig b/audio/soundio/Device.zig index b340dc77..eab96502 100644 --- a/audio/soundio/Device.zig +++ b/audio/soundio/Device.zig @@ -1,5 +1,19 @@ +const std = @import("std"); const c = @import("c.zig"); +const OutStream = @import("OutStream.zig"); -const SoundIoDevice = @This(); +const Device = @This(); handle: *c.SoundIoDevice, + +pub fn unref(self: Device) void { + c.soundio_device_unref(self.handle); +} + +pub fn name(self: Device) [:0]const u8 { + return std.mem.span(self.handle.*.name); +} + +pub fn createOutStream(self: Device) error{OutOfMemory}!OutStream { + return OutStream{ .handle = c.soundio_outstream_create(self.handle) orelse return error.OutOfMemory }; +} diff --git a/audio/soundio/OutStream.zig b/audio/soundio/OutStream.zig new file mode 100644 index 00000000..e47c94d5 --- /dev/null +++ b/audio/soundio/OutStream.zig @@ -0,0 +1,55 @@ +const c = @import("c.zig"); +const intToError = @import("error.zig").intToError; +const Error = @import("error.zig").Error; +const Format = @import("enums.zig").Format; +const ChannelLayout = @import("ChannelLayout.zig"); + +const OutStream = @This(); + +pub const WriteCallback = fn (stream: ?[*]c.SoundIoOutStream, frame_count_min: c_int, frame_count_max: c_int) callconv(.C) void; + +handle: *c.SoundIoOutStream, + +pub fn deinit(self: OutStream) void { + c.soundio_outstream_destroy(self.handle); +} + +pub fn open(self: OutStream) Error!void { + try intToError(c.soundio_outstream_open(self.handle)); +} + +pub fn start(self: OutStream) Error!void { + try intToError(c.soundio_outstream_start(self.handle)); +} + +pub fn beginWrite(self: OutStream, areas: [*]?[*]c.SoundIoChannelArea, frame_count: *i32) Error!void { + try intToError(c.soundio_outstream_begin_write( + self.handle, + areas, + frame_count, + )); +} + +pub fn endWrite(self: OutStream) Error!void { + try intToError(c.soundio_outstream_end_write(self.handle)); +} + +pub fn setFormat(self: OutStream, format: Format) void { + self.handle.*.format = @enumToInt(format); +} + +pub fn setWriteCallback(self: OutStream, callback: WriteCallback) void { + self.handle.*.write_callback = callback; +} + +pub fn layout(self: OutStream) ChannelLayout { + return ChannelLayout{ .handle = self.handle.*.layout }; +} + +pub fn sampleRate(self: OutStream) i32 { + return self.handle.*.sample_rate; +} + +pub fn layoutError(self: OutStream) Error!void { + try intToError(self.handle.*.layout_error); +} diff --git a/audio/soundio/SoundIo.zig b/audio/soundio/SoundIo.zig index f7c5ccb0..4ab144e0 100644 --- a/audio/soundio/SoundIo.zig +++ b/audio/soundio/SoundIo.zig @@ -8,8 +8,8 @@ const SoundIo = @This(); handle: *c.SoundIo, -pub fn init() Error!SoundIo { - return SoundIo{ .handle = c.soundio_create() orelse return Error.OutOfMemory }; +pub fn init() error{OutOfMemory}!SoundIo { + return SoundIo{ .handle = c.soundio_create() orelse return error.OutOfMemory }; } pub fn deinit(self: SoundIo) void { @@ -32,6 +32,10 @@ pub fn flushEvents(self: SoundIo) void { c.soundio_flush_events(self.handle); } +pub fn waitEvents(self: SoundIo) void { + c.soundio_wait_events(self.handle); +} + pub fn defaultInputDeviceIndex(self: SoundIo) ?u16 { const index = c.soundio_default_input_device_index(self.handle); return if (index < 0) null else @intCast(u16, index); diff --git a/audio/soundio/enums.zig b/audio/soundio/enums.zig index 48466403..20504459 100644 --- a/audio/soundio/enums.zig +++ b/audio/soundio/enums.zig @@ -1,4 +1,4 @@ -pub const c = @import("c.zig"); +const c = @import("c.zig"); pub const ChannelId = enum(u7) { invalid = c.SoundIoChannelIdInvalid, diff --git a/audio/soundio/main.zig b/audio/soundio/main.zig index 2a573001..1c1d660a 100644 --- a/audio/soundio/main.zig +++ b/audio/soundio/main.zig @@ -1,4 +1,28 @@ pub usingnamespace @import("enums.zig"); -pub usingnamespace @import("SoundIo.zig"); -pub usingnamespace @import("Device.zig"); +pub const c = @import("c.zig"); +pub const SoundIo = @import("SoundIo.zig"); +pub const Device = @import("Device.zig"); +pub const OutStream = @import("OutStream.zig"); pub const Error = @import("error.zig").Error; + +test { + refAllDecls(@import("SoundIo.zig")); + refAllDecls(@import("Device.zig")); + refAllDecls(@import("OutStream.zig")); + refAllDecls(@import("ChannelLayout.zig")); +} + +fn refAllDecls(comptime T: type) void { + @setEvalBranchQuota(10000); + inline for (comptime @import("std").meta.declarations(T)) |decl| { + if (decl.is_pub) { + if (@TypeOf(@field(T, decl.name)) == type) { + switch (@typeInfo(@field(T, decl.name))) { + .Struct, .Enum, .Union, .Opaque => refAllDecls(@field(T, decl.name)), + else => {}, + } + } + _ = @field(T, decl.name); + } + } +}