audio: add InStream and more functiosn coverage to soundio binding

This commit is contained in:
alichraghi 2022-07-08 00:41:56 +04:30 committed by Stephen Gutekanst
parent 751cceb94a
commit f1845c0f41
4 changed files with 97 additions and 0 deletions

View file

@ -1,6 +1,8 @@
const std = @import("std");
const c = @import("c.zig");
const InStream = @import("InStream.zig");
const OutStream = @import("OutStream.zig");
const Format = @import("enums.zig").Format;
const Device = @This();
@ -10,10 +12,22 @@ pub fn unref(self: Device) void {
c.soundio_device_unref(self.handle);
}
pub fn id(self: Device) [:0]const u8 {
return std.mem.span(self.handle.*.id);
}
pub fn name(self: Device) [:0]const u8 {
return std.mem.span(self.handle.*.name);
}
pub fn createInStream(self: Device) error{OutOfMemory}!InStream {
return InStream{ .handle = c.soundio_instream_create(self.handle) orelse return error.OutOfMemory };
}
pub fn createOutStream(self: Device) error{OutOfMemory}!OutStream {
return OutStream{ .handle = c.soundio_outstream_create(self.handle) orelse return error.OutOfMemory };
}
pub fn supportsFormat(self: Device, format: Format) bool {
return c.soundio_device_supports_format(self.handle, @enumToInt(format));
}

View file

@ -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 InStream = @This();
pub const WriteCallback = fn (stream: ?[*]c.SoundIoInStream, frame_count_min: c_int, frame_count_max: c_int) callconv(.C) void;
handle: *c.SoundIoInStream,
pub fn deinit(self: InStream) void {
c.soundio_instream_destroy(self.handle);
}
pub fn open(self: InStream) Error!void {
try intToError(c.soundio_instream_open(self.handle));
}
pub fn start(self: InStream) Error!void {
try intToError(c.soundio_instream_start(self.handle));
}
pub fn beginWrite(self: InStream, areas: [*]?[*]c.SoundIoChannelArea, frame_count: *i32) Error!void {
try intToError(c.soundio_instream_begin_write(
self.handle,
areas,
frame_count,
));
}
pub fn endWrite(self: InStream) Error!void {
try intToError(c.soundio_instream_end_write(self.handle));
}
pub fn setFormat(self: InStream, format: Format) void {
self.handle.*.format = @enumToInt(format);
}
pub fn setWriteCallback(self: InStream, callback: WriteCallback) void {
self.handle.*.write_callback = callback;
}
pub fn layout(self: InStream) ChannelLayout {
return ChannelLayout{ .handle = self.handle.*.layout };
}
pub fn sampleRate(self: InStream) i32 {
return self.handle.*.sample_rate;
}
pub fn layoutError(self: InStream) Error!void {
try intToError(self.handle.*.layout_error);
}

View file

@ -1,6 +1,7 @@
const c = @import("c.zig");
const intToError = @import("error.zig").intToError;
const Error = @import("error.zig").Error;
const Aim = @import("enums.zig").Aim;
const Backend = @import("enums.zig").Backend;
const Device = @import("Device.zig");
@ -36,6 +37,10 @@ pub fn waitEvents(self: SoundIo) void {
c.soundio_wait_events(self.handle);
}
pub fn wakeup(self: SoundIo) void {
c.soundio_wakeup(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);
@ -46,6 +51,16 @@ pub fn defaultOutputDeviceIndex(self: SoundIo) ?u16 {
return if (index < 0) null else @intCast(u16, index);
}
pub fn inputDeviceCount(self: SoundIo) ?u16 {
const count = c.soundio_input_device_count(self.handle);
return if (count < 0) null else @intCast(u16, count);
}
pub fn outputDeviceCount(self: SoundIo) ?u16 {
const count = c.soundio_output_device_count(self.handle);
return if (count < 0) null else @intCast(u16, count);
}
pub fn getInputDevice(self: SoundIo, index: u16) ?Device {
return Device{
.handle = c.soundio_get_input_device(self.handle, index) orelse return null,
@ -57,3 +72,15 @@ pub fn getOutputDevice(self: SoundIo, index: u16) ?Device {
.handle = c.soundio_get_output_device(self.handle, index) orelse return null,
};
}
pub fn getInputDeviceFromID(self: SoundIo, id: [:0]const u8, is_raw: bool) ?Device {
return Device{
.handle = c.soundio_get_input_device_from_id(self.handle, id.ptr, is_raw) orelse return null,
};
}
pub fn getOutputDeviceFromID(self: SoundIo, id: [:0]const u8, is_raw: bool) ?Device {
return Device{
.handle = c.soundio_get_output_device_from_id(self.handle, id.ptr, is_raw) orelse return null,
};
}

View file

@ -2,6 +2,7 @@ pub usingnamespace @import("enums.zig");
pub const c = @import("c.zig");
pub const SoundIo = @import("SoundIo.zig");
pub const Device = @import("Device.zig");
pub const InStream = @import("InStream.zig");
pub const OutStream = @import("OutStream.zig");
pub const Error = @import("error.zig").Error;