From e8808c501bcf1136f98f2b777e03ffde4ef49ff8 Mon Sep 17 00:00:00 2001 From: Luna Date: Sun, 31 May 2020 21:38:18 -0300 Subject: [PATCH 1/6] make Embed follow existing custom plugin structure --- src/custom.zig | 4 ++-- src/lang.zig | 9 +++------ 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/src/custom.zig b/src/custom.zig index f346fa0..4f9bbc2 100644 --- a/src/custom.zig +++ b/src/custom.zig @@ -140,10 +140,10 @@ pub const Embed = struct { sndfile: *c.SNDFILE = undefined, buf: []f32 = undefined, - pub fn init(allocator: *std.mem.Allocator, filepath: []const u8) @This() { + pub fn init(allocator: *std.mem.Allocator, params: *plugins.ParmaMap) @This() { return Embed{ .allocator = allocator, - .filepath = filepath, + .filepath = params.get("path").?, }; } diff --git a/src/lang.zig b/src/lang.zig index 03a6c52..ef886b9 100644 --- a/src/lang.zig +++ b/src/lang.zig @@ -1,6 +1,7 @@ const std = @import("std"); const plugin = @import("plugin.zig"); +const custom = @import("custom.zig"); pub const ParseError = error{ OutOfMemory, @@ -188,13 +189,9 @@ pub const Command = struct { data: f32, }); - pub const Embed = struct { - pub const base_tag = Tag.embed; - base: Command, - split: usize, - index: usize, + pub const Embed = CustomCommand(Tag.write, custom.Embed, struct { path: []const u8, - }; + }); pub const Rotate = struct { pub const base_tag = Tag.rotate; From 1fac8c73128821630dad4c3c5e911e254a51c157 Mon Sep 17 00:00:00 2001 From: Luna Date: Sun, 31 May 2020 21:40:31 -0300 Subject: [PATCH 2/6] fix putting KV on a string --- src/custom.zig | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/custom.zig b/src/custom.zig index 4f9bbc2..a91bdd2 100644 --- a/src/custom.zig +++ b/src/custom.zig @@ -120,9 +120,8 @@ pub const Write = struct { allocator: *std.mem.Allocator, params: *plugins.ParamMap, ) Write { - const data = params.get("data").?; return Write{ - .data = data.value, + .data = params.get("data").?.value, }; } @@ -143,7 +142,7 @@ pub const Embed = struct { pub fn init(allocator: *std.mem.Allocator, params: *plugins.ParmaMap) @This() { return Embed{ .allocator = allocator, - .filepath = params.get("path").?, + .filepath = params.get("path").?.value, }; } From 1c1e525b1daf6cc32d5c62901bca4eb59414908f Mon Sep 17 00:00:00 2001 From: Luna Date: Sun, 31 May 2020 21:47:31 -0300 Subject: [PATCH 3/6] add support for plugin command types --- src/runner.zig | 43 +++++++++++++++++++++++-------------------- 1 file changed, 23 insertions(+), 20 deletions(-) diff --git a/src/runner.zig b/src/runner.zig index e65bac0..a1bed65 100644 --- a/src/runner.zig +++ b/src/runner.zig @@ -190,26 +190,6 @@ pub const Runner = struct { _ = try proc.spawnAndWait(); } - fn noiseCmd(self: *Runner, pos: Position, map: *ParamMap) !void { - var image = try self.getImage(); - try image.runCustomPlugin(custom.RandomNoise, pos, *ParamMap, map); - } - - fn wildNoiseCmd(self: *Runner, pos: Position, map: *ParamMap) !void { - var image = try self.getImage(); - try image.runCustomPlugin(custom.WildNoise, pos, *ParamMap, map); - } - - fn writeCmd(self: *Runner, pos: Position, map: *ParamMap) !void { - var image = try self.getImage(); - try image.runCustomPlugin(custom.Write, pos, *ParamMap, map); - } - - fn embedCmd(self: *Runner, pos: Position, path: []const u8) !void { - var image = try self.getImage(); - try image.runCustomPlugin(custom.Embed, pos, []const u8, path); - } - fn rotateCmd( self: *Runner, deg: f32, @@ -244,6 +224,26 @@ pub const Runner = struct { try image.runPlugin(typ.lv2_url, pos, params); } + fn executePlugin(self: *@This(), command: var) !void { + const pos = plugin.Position{ + .split = command.split, + .index = command.index, + }; + + var params = ParamMap.init(self.allocator); + defer params.deinit(); + + inline for (@typeInfo(@TypeOf(command.parameters)).Struct.fields) |cmd_field| { + try params.put( + cmd_field.name, + @field(command.parameters, cmd_field.name), + ); + } + + var image = try self.getImage(); + try image.runCustomPlugin(typ.plugin_type, pos, map); + } + fn newRunCommandSingle( self: *@This(), cmd: lang.Command, @@ -261,6 +261,7 @@ pub const Runner = struct { const ctype = typ.command_type; switch (ctype) { .lv2_command => try self.executeLV2Command(command.*), + .plugin_command => try self.executePlugin(command.*), else => @panic("TODO support command type"), } } @@ -305,6 +306,8 @@ pub const Runner = struct { .saturator => try self.newRunCommandSingle(cmd, .saturator), .vintagedelay => try self.newRunCommandSingle(cmd, .vintagedelay), + .noise, .wildnoise, .write, .embed => |tag| try self.newRunCommandSingle(cmd, tag), + else => { std.debug.warn("TODO support {}\n", .{@tagName(cmd.tag)}); @panic("TODO support tag"); From 303a40758df71dfda00c37808ac5c4c613f60b6c Mon Sep 17 00:00:00 2001 From: Luna Date: Sun, 31 May 2020 21:55:03 -0300 Subject: [PATCH 4/6] make custom plugins always receive ParamMap --- src/image.zig | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/image.zig b/src/image.zig index 7f8deb4..d677afa 100644 --- a/src/image.zig +++ b/src/image.zig @@ -422,8 +422,7 @@ pub const Image = struct { self: *Image, comptime Plugin: type, position: plugins.Position, - comptime ExtraType: type, - extra: ExtraType, + extra: *ParamMap, ) !void { var plugin_opt: ?Plugin = Plugin.init(self.allocator, extra); if (plugin_opt == null) { From 10b2c69605bf3be961579ee5fa8cfe24ca4d80c7 Mon Sep 17 00:00:00 2001 From: Luna Date: Sun, 31 May 2020 21:55:22 -0300 Subject: [PATCH 5/6] fix typos --- src/lang.zig | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/lang.zig b/src/lang.zig index ef886b9..04195f5 100644 --- a/src/lang.zig +++ b/src/lang.zig @@ -13,6 +13,8 @@ pub const CommandType = enum { /// "LV2 Commands" are commands that receive split, index, and then receive /// any f64 arguments. lv2_command, + + custom_command, }; fn LV2Command( @@ -33,19 +35,19 @@ fn LV2Command( } fn CustomCommand( - comptime tag: Command.tag, - comptime plugin: type, - comptime parameters: type, + comptime tag: Command.Tag, + comptime Plugin: type, + comptime PluginParameters: type, ) type { return struct { pub const base_tag = tag; - pub const command_type = CommandType.plugin_command; - pub const plugin_type = plugin; + pub const command_type = CommandType.custom_command; + pub const plugin_type = Plugin; base: Command, split: usize, index: usize, - parameters: LV2Parameters, + parameters: PluginParameters, }; } From 83996b889fba82dc24f716203f2450b59fa622dc Mon Sep 17 00:00:00 2001 From: Luna Date: Sun, 31 May 2020 21:55:27 -0300 Subject: [PATCH 6/6] make rotate not be a typed command --- src/lang.zig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lang.zig b/src/lang.zig index 04195f5..cc8d58a 100644 --- a/src/lang.zig +++ b/src/lang.zig @@ -520,7 +520,7 @@ pub const Lang = struct { // Based on the command struct fields, we can parse the arguments. var cmd = try self.allocator.create(command_struct); const is_lv2_command = switch (command_struct.base_tag) { - .noop, .load, .quicksave, .runqs => false, + .noop, .load, .quicksave, .runqs, .rotate => false, else => command_struct.command_type == .lv2_command, };