diff --git a/src/image.zig b/src/image.zig index f165896..785ae91 100644 --- a/src/image.zig +++ b/src/image.zig @@ -1,12 +1,6 @@ const std = @import("std"); const lv2 = @import("lv2_helpers.zig"); - -const c = @cImport({ - @cInclude("sndfile.h"); - - @cInclude("lilv/lilv.h"); - @cInclude("lv2/core/lv2.h"); -}); +const c = lv2.c; const plugins = @import("plugin.zig"); @@ -14,6 +8,7 @@ pub const ImageError = error{ OpenFail, InvalidPlugin, UnknownPlugin, + InvalidSymbol, }; /// Low level integration function with libsndfile. @@ -103,13 +98,41 @@ pub const Image = struct { pos: plugins.Position, params: plugins.ParamList, ) !void { - var context = try plugins.makeContext(self.allocator, plugin_uri); - std.debug.warn("world: {}\n", context.world); - std.debug.warn("plugin: {}\n", context.plugin); + var ctx = try plugins.makeContext(self.allocator, plugin_uri); + std.debug.warn("\tworld: {}\n", ctx.world); + std.debug.warn("\tplugin: {}\n", ctx.plugin); - var ports = try lv2.setupPorts(&context); - for (ports) |port| { - std.debug.warn("port: {}\n", port.*); + var ports = try lv2.setupPorts(&ctx); + + if (ctx.n_audio_in != 1) { + std.debug.warn("plugin <{}> does not accept mono input.\n", plugin_uri); + return ImageError.InvalidPlugin; + } + + // now, for each param for the plugin, we find its port, and set + // the value for the port there. + var it = params.iterator(); + + while (it.next()) |param| { + var sym_cstr = try std.cstr.addNullByte(self.allocator, param.sym); + defer self.allocator.free(sym_cstr); + + var sym = c.lilv_new_string(ctx.world, sym_cstr.ptr); + const port = c.lilv_plugin_get_port_by_symbol(ctx.plugin, sym) orelse blk: { + std.debug.warn("assert fail: symbol not found on port"); + return ImageError.InvalidSymbol; + }; + + c.lilv_node_free(sym); + + var idx = c.lilv_port_get_index(ctx.plugin, port); + std.debug.warn( + "sym={}, idx={} to val={}\n", + param.sym, + idx, + param.value, + ); + ports[idx].value = param.value; } } }; diff --git a/src/lv2_helpers.zig b/src/lv2_helpers.zig index 8a6dc57..834a813 100644 --- a/src/lv2_helpers.zig +++ b/src/lv2_helpers.zig @@ -2,6 +2,7 @@ const std = @import("std"); const plugin = @import("plugin.zig"); pub const c = @cImport({ + @cInclude("sndfile.h"); @cInclude("lilv/lilv.h"); @cInclude("lv2/core/lv2.h"); }); diff --git a/src/runner.zig b/src/runner.zig index 0be0451..4b47dbd 100644 --- a/src/runner.zig +++ b/src/runner.zig @@ -143,12 +143,9 @@ pub const Runner = struct { /// Run the http://lv2plug.in/plugins/eg-amp plugin over the file. fn ampCmd(self: *Runner, split: usize, index: usize, gain: f32) !void { var image = try self.getImage(); - var param = try self.allocator.create(plugin.Param); - defer self.allocator.destroy(param); - - param.* = plugin.Param{ .sym = "gain", .value = gain }; var params = plugin.ParamList.init(self.allocator); + try params.append(plugin.Param{ .sym = "gain", .value = gain }); defer params.deinit(); try image.runPlugin(