diff --git a/examples/noise.scri b/examples/noise.scri index 171c934..a202f7e 100644 --- a/examples/noise.scri +++ b/examples/noise.scri @@ -1,3 +1,5 @@ load :0; -noise 3 1; +noise 5 1 3981 10000; +noise 5 2 3982 0; +noise 5 3 3982 5; quicksave; diff --git a/src/custom.zig b/src/custom.zig index 0e2b3e3..bd9d583 100644 --- a/src/custom.zig +++ b/src/custom.zig @@ -8,21 +8,45 @@ const c = lv2.c; const RunBuffers = plugins.RunBuffers; pub const RandomNoise = struct { - allocator: *std.mem.Allocator, r: std.rand.DefaultPrng, + rand_buf: ?[]f32, + cnt: usize = 0, pub fn init( allocator: *std.mem.Allocator, - ) RandomNoise { - var r = std.rand.DefaultPrng.init(std.time.timestamp()); + params: *plugins.ParamMap, + ) !RandomNoise { + const seed = @floatToInt(u64, params.get("seed").?.value); + const fillbytes = @floatToInt(usize, params.get("fill_bytes").?.value); - return RandomNoise{ - .allocator = allocator, - .r = r, - }; + var r = std.rand.DefaultPrng.init(seed); + + if (fillbytes > 0) { + var rand_buf = try allocator.alloc(f32, fillbytes); + + for (rand_buf) |_, idx| { + rand_buf[idx] = r.random.float(f32); + } + + return RandomNoise{ + .r = r, + .rand_buf = rand_buf, + }; + } else { + return RandomNoise{ + .r = r, + .rand_buf = null, + }; + } } pub fn run(self: *RandomNoise, bufs: *RunBuffers) void { - bufs.out[0] = self.r.random.float(f32); + if (self.rand_buf) |rand_buf| { + if (self.cnt >= rand_buf.len) self.cnt = 0; + bufs.out[0] = rand_buf[self.cnt]; + self.cnt += 1; + } else { + bufs.out[0] = self.r.random.float(f32); + } } }; diff --git a/src/image.zig b/src/image.zig index f6a2ae1..020d893 100644 --- a/src/image.zig +++ b/src/image.zig @@ -62,7 +62,7 @@ fn getEndPos(path: []const u8) !usize { fn temporaryName(allocator: *std.mem.Allocator) ![]u8 { const template_start = "/temp/temp_"; - const template = "/tmp/temp_XXXXXX"; + const template = "/tmp/temp_XXXXXXXX"; var nam = try allocator.alloc(u8, template.len); std.mem.copy(u8, nam, template); @@ -333,8 +333,81 @@ pub const Image = struct { pub fn runCustomPlugin( self: *Image, comptime Plugin: type, - pos: plugins.Position, - ) void { - var plugin = Plugin.init(self.allocator); + position: plugins.Position, + params: *plugins.ParamMap, + ) !void { + var plugin = try Plugin.init(self.allocator, params); + + // the code here is a copypaste of runPlugin() without the specific + // lilv things. + var tmpnam = try temporaryName(self.allocator); + std.debug.warn("\trunning CUSTOM plugin from '{}' to '{}'\n", self.curpath, tmpnam); + + var out_fmt = mkSfInfo(); + var out_file = try sopen(self.allocator, tmpnam, c.SFM_WRITE, &out_fmt); + + var bufs = try plugins.RunBuffers.init(self.allocator); + defer bufs.deinit(); + + const file_end = try getEndPos(self.curpath); + const seek_pos = position.seekPos(file_end); + std.debug.warn("\tstart {} end {}\n", seek_pos.start, seek_pos.end); + + // make sure we start from 0 + _ = c.sf_seek(self.sndfile, 0, c.SEEK_SET); + + // there are four main stages: + // - the bmp header copy + // - pre-plugin + // - CUSTOM plugin + // - post-plugin + + var file_copy_buf = try self.allocator.alloc(f32, BufferSize); + defer self.allocator.free(file_copy_buf); + + // pre-plugin copy, merged with bmp header copy + try self.copyBytes( + out_file, + file_copy_buf, + usize(0), + seek_pos.start + @mod(seek_pos.start, BufferSize), + ); + + _ = c.sf_seek(self.sndfile, @intCast(i64, seek_pos.start), c.SEEK_SET); + + var i: usize = seek_pos.start; + std.debug.warn("\tseek pos start: {} end: {}\n", seek_pos.start, seek_pos.end); + + var inbuf = bufs.in; + var outbuf = bufs.out; + + while (i <= seek_pos.end) : (i += 1) { + const read_bytes = c.sf_readf_float(self.sndfile, bufs.in.ptr, 1); + if (read_bytes == 0) { + std.debug.warn("WARN! reached EOF at idx={}\n", i); + break; + } + + plugin.run(&bufs); + try swrite(out_file, bufs.out.ptr, 1); + } + + _ = c.sf_seek(self.sndfile, @intCast(i64, seek_pos.end), c.SEEK_SET); + + // post-plugin copy + try self.copyBytes( + out_file, + file_copy_buf, + seek_pos.end + 1, + file_end + @mod(file_end, BufferSize), + ); + + c.sf_write_sync(out_file); + _ = c.sf_close(out_file); + _ = c.sf_close(self.sndfile); + + // reopen the file as SFM_READ so we can run plugin chains etc + self.sndfile = try sopen(self.allocator, tmpnam, c.SFM_READ, &out_fmt); + self.curpath = tmpnam; } }; diff --git a/src/lang.zig b/src/lang.zig index 20506fa..922e1dc 100644 --- a/src/lang.zig +++ b/src/lang.zig @@ -107,6 +107,16 @@ pub const Command = struct { .value = val, }); } + + pub fn appendParamMap( + self: *Command, + map: *plugin.ParamMap, + symbol: []const u8, + ) !void { + var val = try self.floatArgAt(self.cur_idx); + self.cur_idx += 1; + _ = try map.put(symbol, val); + } }; pub const CommandList = std.ArrayList(*Command); diff --git a/src/plugin.zig b/src/plugin.zig index a3e40d4..7794c9e 100644 --- a/src/plugin.zig +++ b/src/plugin.zig @@ -16,6 +16,7 @@ pub const Param = struct { /// List of parameters to be set to control ports. pub const ParamList = std.ArrayList(Param); +pub const ParamMap = std.AutoHashMap([]const u8, f32); /// Represents an absolute position in the image. pub const SeekPos = struct { diff --git a/src/runner.zig b/src/runner.zig index 4e54aaa..fde23ff 100644 --- a/src/runner.zig +++ b/src/runner.zig @@ -6,6 +6,7 @@ const custom = @import("custom.zig"); const Position = plugin.Position; const ParamList = plugin.ParamList; +const ParamMap = plugin.ParamMap; const Image = images.Image; @@ -246,15 +247,18 @@ pub const Runner = struct { try image.runPlugin("http://plugin.org.uk/swh-plugins/revdelay", pos, params); } - fn noiseCmd(self: *Runner, pos: Position, params: ParamList) !void { + fn noiseCmd(self: *Runner, pos: Position, map: *ParamMap) !void { var image = try self.getImage(); - image.runCustomPlugin(custom.RandomNoise, pos); + try image.runCustomPlugin(custom.RandomNoise, pos, map); } fn runCommand(self: *Runner, cmd: *lang.Command) !void { var params = ParamList.init(self.allocator); defer params.deinit(); + var map = ParamMap.init(self.allocator); + defer map.deinit(); + return switch (cmd.command) { .Noop => {}, .Load => blk: { @@ -392,7 +396,11 @@ pub const Runner = struct { .Noise => blk: { const pos = try cmd.consumePosition(); - try self.noiseCmd(pos, params); + + try cmd.appendParamMap(&map, "seed"); + try cmd.appendParamMap(&map, "fill_bytes"); + + try self.noiseCmd(pos, &map); }, else => blk: {