diff --git a/examples/write.scri b/examples/write.scri new file mode 100644 index 0000000..f7906c3 --- /dev/null +++ b/examples/write.scri @@ -0,0 +1,3 @@ +load :0; +write 3 1 1; +quicksave; diff --git a/src/custom.zig b/src/custom.zig index d1e7269..df9f331 100644 --- a/src/custom.zig +++ b/src/custom.zig @@ -15,14 +15,14 @@ pub const RandomNoise = struct { pub fn init( allocator: *std.mem.Allocator, params: *plugins.ParamMap, - ) !RandomNoise { + ) ?RandomNoise { const seed = @floatToInt(u64, params.get("seed").?.value); const fillbytes = @floatToInt(usize, params.get("fill_bytes").?.value); var r = std.rand.DefaultPrng.init(seed); if (fillbytes > 0) { - var rand_buf = try allocator.alloc(f32, fillbytes); + var rand_buf = allocator.alloc(f32, fillbytes) catch return null; for (rand_buf) |_, idx| { rand_buf[idx] = r.random.float(f32); @@ -59,14 +59,14 @@ pub const WildNoise = struct { pub fn init( allocator: *std.mem.Allocator, params: *plugins.ParamMap, - ) !WildNoise { + ) ?WildNoise { const seed = @floatToInt(u64, params.get("seed").?.value); const fillbytes = @floatToInt(usize, params.get("fill_bytes").?.value); var r = std.rand.DefaultPrng.init(seed); if (fillbytes > 0) { - var rand_buf = try allocator.alloc(f32, fillbytes); + var rand_buf = allocator.alloc(f32, fillbytes) catch return null; for (rand_buf) |_, idx| { rand_buf[idx] = @intToFloat(f32, r.random.int(u1)); @@ -94,3 +94,22 @@ pub const WildNoise = struct { } } }; + +pub const Write = struct { + data: f32, + + pub fn init( + allocator: *std.mem.Allocator, + params: *plugins.ParamMap, + ) Write { + const data = params.get("data").?; + + return Write{ + .data = data.value, + }; + } + + pub fn run(self: *Write, bufs: *RunBuffers) void { + bufs.out[0] = self.data; + } +}; diff --git a/src/image.zig b/src/image.zig index 1155501..8686e12 100644 --- a/src/image.zig +++ b/src/image.zig @@ -1,6 +1,7 @@ const std = @import("std"); const lv2 = @import("lv2_helpers.zig"); const c = lv2.c; +const custom = @import("custom.zig"); const plugins = @import("plugin.zig"); @@ -17,6 +18,7 @@ pub const ImageError = error{ InvalidSymbol, InstantiateFail, WriteFail, + PluginLoadFail, }; /// Low level integration function with libsndfile. @@ -358,7 +360,12 @@ pub const Image = struct { position: plugins.Position, params: *plugins.ParamMap, ) !void { - var plugin = try Plugin.init(self.allocator, params); + var plugin_opt: ?Plugin = Plugin.init(self.allocator, params); + if (plugin_opt == null) { + return ImageError.PluginLoadFail; + } + + var plugin = plugin_opt.?; // the code here is a copypaste of runPlugin() without the specific // lilv things. diff --git a/src/lang.zig b/src/lang.zig index f0de684..55dd9b2 100644 --- a/src/lang.zig +++ b/src/lang.zig @@ -26,8 +26,10 @@ pub const CommandType = enum { Delay, Vinyl, RevDelay, + Noise, WildNoise, + Write, Rotate, }; @@ -161,6 +163,7 @@ pub const Lang = struct { // custom implementations (not lv2) _ = try self.keywords.put("noise", .Noise); _ = try self.keywords.put("wildnoise", .WildNoise); + _ = try self.keywords.put("write", .Write); // even more custom _ = try self.keywords.put("rotate", .Rotate); diff --git a/src/runner.zig b/src/runner.zig index f669e9e..316d515 100644 --- a/src/runner.zig +++ b/src/runner.zig @@ -258,6 +258,11 @@ pub const Runner = struct { try image.runCustomPlugin(custom.WildNoise, pos, map); } + fn writeCmd(self: *Runner, pos: Position, map: *ParamMap) !void { + var image = try self.getImage(); + try image.runCustomPlugin(custom.Write, pos, map); + } + fn rotateCmd( self: *Runner, deg: f32, @@ -431,6 +436,12 @@ pub const Runner = struct { try self.wildNoiseCmd(pos, &map); }, + .Write => blk: { + const pos = try cmd.consumePosition(); + try cmd.appendParamMap(&map, "data"); + try self.writeCmd(pos, &map); + }, + .Rotate => blk: { const deg = try cmd.floatArgAt(0); const bgfill = try cmd.argAt(1);