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