diff --git a/src/custom.zig b/src/custom.zig index 0420bd3..a91bdd2 100644 --- a/src/custom.zig +++ b/src/custom.zig @@ -16,12 +16,15 @@ pub const RandomNoise = struct { pub fn init( allocator: *std.mem.Allocator, - params: var, + params: *plugins.ParamMap, ) ?RandomNoise { - var r = std.rand.DefaultPrng.init(params.seed); + const seed = @floatToInt(u64, params.get("seed").?.value); + const fillbytes = @floatToInt(usize, params.get("fill_bytes").?.value); - if (params.fill_bytes > 0) { - var rand_buf = allocator.alloc(f32, params.fill_bytes) catch return null; + var r = std.rand.DefaultPrng.init(seed); + + if (fillbytes > 0) { + var rand_buf = allocator.alloc(f32, fillbytes) catch return null; for (rand_buf) |_, idx| { rand_buf[idx] = r.random.float(f32); @@ -64,12 +67,15 @@ pub const WildNoise = struct { pub fn init( allocator: *std.mem.Allocator, - params: var, + params: *plugins.ParamMap, ) ?WildNoise { - var r = std.rand.DefaultPrng.init(params.seed); + const seed = @floatToInt(u64, params.get("seed").?.value); + const fillbytes = @floatToInt(usize, params.get("fill_bytes").?.value); - if (params.fill_bytes > 0) { - var rand_buf = allocator.alloc(f32, params.fill_bytes) catch return null; + var r = std.rand.DefaultPrng.init(seed); + + if (fillbytes > 0) { + var rand_buf = allocator.alloc(f32, fillbytes) catch return null; for (rand_buf) |_, idx| { rand_buf[idx] = @intToFloat(f32, r.random.int(u1)); @@ -112,10 +118,10 @@ pub const Write = struct { pub fn init( allocator: *std.mem.Allocator, - params: var, + params: *plugins.ParamMap, ) Write { return Write{ - .data = params.data, + .data = params.get("data").?.value, }; } @@ -133,10 +139,10 @@ pub const Embed = struct { sndfile: *c.SNDFILE = undefined, buf: []f32 = undefined, - pub fn init(allocator: *std.mem.Allocator, params: var) @This() { + pub fn init(allocator: *std.mem.Allocator, params: *plugins.ParmaMap) @This() { return Embed{ .allocator = allocator, - .filepath = params.path, + .filepath = params.get("path").?.value, }; } diff --git a/src/image.zig b/src/image.zig index b9202ef..d677afa 100644 --- a/src/image.zig +++ b/src/image.zig @@ -422,7 +422,7 @@ pub const Image = struct { self: *Image, comptime Plugin: type, position: plugins.Position, - extra: var, + extra: *ParamMap, ) !void { var plugin_opt: ?Plugin = Plugin.init(self.allocator, extra); if (plugin_opt == null) { diff --git a/src/lang.zig b/src/lang.zig index 3b7a542..cc8d58a 100644 --- a/src/lang.zig +++ b/src/lang.zig @@ -178,13 +178,13 @@ pub const Command = struct { }; pub const Noise = CustomCommand(Tag.noise, custom.RandomNoise, struct { - seed: u64, - fill_bytes: usize, + seed: f32, + fill_bytes: f32, }); pub const Wildnoise = CustomCommand(Tag.wildnoise, custom.WildNoise, struct { - seed: u64, - fill_bytes: usize, + seed: f32, + fill_bytes: f32, }); pub const Write = CustomCommand(Tag.write, custom.Write, struct { @@ -521,7 +521,7 @@ pub const Lang = struct { var cmd = try self.allocator.create(command_struct); const is_lv2_command = switch (command_struct.base_tag) { .noop, .load, .quicksave, .runqs, .rotate => false, - else => true, + else => command_struct.command_type == .lv2_command, }; // TODO: crash when no arguments are left but we still need @@ -543,6 +543,7 @@ pub const Lang = struct { cmd.split = try std.fmt.parseInt(usize, split.?, 10); cmd.index = try std.fmt.parseInt(usize, index.?, 10); + // All parameters for LV2 plugins are f32. inline for (@typeInfo(@TypeOf(cmd.parameters)).Struct.fields) |cmd_field| { const maybe_arg = tok_it.next(); if (maybe_arg == null) { @@ -551,15 +552,10 @@ pub const Lang = struct { } const arg = maybe_arg.?; - const arg_value = switch (cmd_field.field_type) { - f32 => try std.fmt.parseFloat(f32, arg), - u64 => try std.fmt.parseInt(u64, arg, 10), - usize => try std.fmt.parseInt(usize, arg, 10), - []const u8 => try self.allocator.dupe(u8, arg), - else => @compileError("parameter struct has unsupported type " ++ @typeName(cmd_field.field_type)), - }; + if (cmd_field.field_type != f32) + @compileError("LV2 parameter struct can only have f32 fields"); - @field(cmd.parameters, cmd_field.name) = arg_value; + @field(cmd.parameters, cmd_field.name) = try std.fmt.parseFloat(f32, arg); } } else { inline for (@typeInfo(command_struct).Struct.fields) |cmd_field| { @@ -574,7 +570,7 @@ pub const Lang = struct { i32 => try std.fmt.parseInt(i32, arg, 10), f32 => try std.fmt.parseFloat(f32, arg), []const u8 => try self.allocator.dupe(u8, arg), - else => @compileError("Invalid parameter type (" ++ @typeName(cmd_field.field_type) ++ ") left on command struct " ++ @typeName(command_struct) ++ "."), + else => @panic("Invalid parameter type (" ++ @typeName(cmd_field.field_type) ++ ") left on command struct " ++ @typeName(command_struct) ++ "."), }; std.debug.warn("parsing {}, arg of type {} => {}\n", .{ diff --git a/src/runner.zig b/src/runner.zig index dd5fcd2..db40493 100644 --- a/src/runner.zig +++ b/src/runner.zig @@ -230,8 +230,18 @@ pub const Runner = struct { .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(@TypeOf(command).plugin_type, pos, command.parameters); + try image.runCustomPlugin(@TypeOf(command).plugin_type, pos, ¶ms); } fn newRunCommandSingle(