From f7a16447f47b44468dc443e662752bf2047e8fd1 Mon Sep 17 00:00:00 2001 From: Luna Date: Sun, 7 Jul 2019 14:26:49 -0300 Subject: [PATCH] fix cimport not doing deref --- src/main.zig | 90 ++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 63 insertions(+), 27 deletions(-) diff --git a/src/main.zig b/src/main.zig index 2f5c463..926e59b 100644 --- a/src/main.zig +++ b/src/main.zig @@ -53,6 +53,30 @@ const Param = struct { value: f32, }; +pub fn lilv_instance_connect_port( + instance: [*c]c.LilvInstance, + port_index: u32, + data_location: ?*c_void, +) void { + instance.?.*.lv2_descriptor.?.*.connect_port.?(instance.?.*.lv2_handle, port_index, data_location); +} + +pub fn lilv_instance_activate(instance: [*c]c.LilvInstance) void { + if (instance.?.*.lv2_descriptor.?.*.activate != null) { + instance.?.*.lv2_descriptor.?.*.activate.?(instance.?.*.lv2_handle); + } +} + +pub fn lilv_instance_run(instance: [*c]c.LilvInstance, sample_count: u32) void { + instance.?.*.lv2_descriptor.?.*.run.?(instance.?.*.lv2_handle, sample_count); +} + +pub fn lilv_instance_deactivate(instance: [*c]c.LilvInstance) void { + if (instance.?.*.lv2_descriptor.?.*.deactivate != null) { + instance.?.*.lv2_descriptor.?.*.deactivate.?(instance.?.*.lv2_handle); + } +} + const ParamList = std.ArrayList(Param); const PortType = enum { @@ -74,14 +98,15 @@ const Port = struct { const LV2Apply = struct { allocator: *std.mem.Allocator, - world: *c.LilvWorld = undefined, - plugin: *const c.LilvPlugin = undefined, - instance: *c.LilvInstance = undefined, - in_path: []u8 = undefined, - out_path: []u8 = undefined, + world: ?*c.LilvWorld = null, + plugin: ?*const c.LilvPlugin = null, + instance: ?*c.LilvInstance = null, - in_file: *c.SNDFILE = undefined, - out_file: *c.SNDFILE = undefined, + in_path: ?[]u8 = undefined, + out_path: ?[]u8 = undefined, + + in_file: ?*c.SNDFILE = null, + out_file: ?*c.SNDFILE = null, n_params: u32, @@ -96,8 +121,12 @@ const LV2Apply = struct { sclose(self.in_path, self.in_file); sclose(self.out_path, self.out_file); - c.lilv_instance_free(self.instance); - c.lilv_world_free(self.world); + if (self.instance) |instance| { + c.lilv_instance_free(self.instance); + } + if (self.world) |world| { + c.lilv_world_free(self.world); + } self.allocator.free(self.ports); self.params.deinit(); @@ -172,7 +201,10 @@ const LV2Apply = struct { } }; -fn sopen(path: []const u8, mode: i32, fmt: *c.SF_INFO) ?*c.SNDFILE { +fn sopen(path_opt: ?[]const u8, mode: i32, fmt: *c.SF_INFO) ?*c.SNDFILE { + if (path_opt == null) return null; + + var path = path_opt.?; var file = c.sf_open(path.ptr, mode, fmt); const st: i32 = c.sf_error(file); @@ -184,7 +216,12 @@ fn sopen(path: []const u8, mode: i32, fmt: *c.SF_INFO) ?*c.SNDFILE { return file; } -fn sclose(path: []const u8, file: *c.SNDFILE) void { +fn sclose(path_opt: ?[]const u8, file_opt: ?*c.SNDFILE) void { + if (path_opt == null) return; + if (file_opt == null) return; + var path = path_opt.?; + var file = file_opt.?; + var st: i32 = c.sf_close(file); if (st != 0) { std.debug.warn( @@ -198,7 +235,9 @@ fn sclose(path: []const u8, file: *c.SNDFILE) void { ///Read a single frame from a file into an interleaved buffer. ///If more channels are required than are available in the file, the remaining ///channels are distributed in a round-robin fashion (LRLRL). -fn sread(file: *c.SNDFILE, file_chans: c_int, buf: []f32) bool { +fn sread(file_opt: ?*c.SNDFILE, file_chans: c_int, buf: []f32) bool { + var file = file_opt.?; + const n_read: c.sf_count_t = c.sf_readf_float(file, buf.ptr, 1); const buf_chans = @intCast(c_int, buf.len); @@ -221,17 +260,14 @@ fn print_version() !void { fn print_usage() !void { const stdout_file = try std.io.getStdOut(); try stdout_file.write("Usage: lv2apply [OPTION]... PLUGIN_URI\n"); - try stdout_file.write(" -i IN_FILE Input file\n"); - try stdout_file.write(" -o OUT_FILE Output file\n"); - try stdout_file.write(" -o OUT_FILE Output file\n"); - try stdout_file.write("-c SYM VAL Control value\n"); - try stdout_file.write("--help print help\n"); - try stdout_file.write("--version display version\n"); + try stdout_file.write("\t-i IN_FILE Input file\n"); + try stdout_file.write("\t-o OUT_FILE Output file\n"); + try stdout_file.write("\t-c SYM VAL Control value\n"); + try stdout_file.write("\t--help print help\n"); + try stdout_file.write("\t--version display version\n"); } pub fn main() !void { - std.debug.warn("awoo.\n"); - var arena = std.heap.ArenaAllocator.init(std.heap.c_allocator); const allocator = &arena.allocator; @@ -388,17 +424,17 @@ pub fn main() !void { var p = @intCast(u32, p_idx); if (ptype == .Control) { - c.lilv_instance_connect_port(self.instance, p, &port.value); + lilv_instance_connect_port(self.instance, p, &port.value); } else if (ptype == .Audio) { if (port.is_input) { - c.lilv_instance_connect_port(self.instance, p, &in_buf[i]); + lilv_instance_connect_port(self.instance, p, &in_buf[i]); i += 1; } else { - c.lilv_instance_connect_port(self.instance, p, &in_buf[o]); + lilv_instance_connect_port(self.instance, p, &in_buf[o]); o += 1; } } else { - c.lilv_instance_connect_port(self.instance, p, null); + lilv_instance_connect_port(self.instance, p, null); } } @@ -407,7 +443,7 @@ pub fn main() !void { // read/write from/to sndfile. std.debug.warn("almost there!\n"); - c.lilv_instance_activate(self.instance); + lilv_instance_activate(self.instance); const START = 2 * 44100; // for the first START frames, copy from in to out @@ -423,13 +459,13 @@ pub fn main() !void { std.debug.warn("{} seeked frames\n", seeked); while (sread(self.in_file, in_fmt.channels, in_buf)) { - c.lilv_instance_run(self.instance, 1); + lilv_instance_run(self.instance, 1); if (c.sf_writef_float(self.out_file, out_buf.ptr, 1) != 1) { std.debug.warn("failed to write to output file\n"); return; } } - c.lilv_instance_deactivate(self.instance); + lilv_instance_deactivate(self.instance); return; }