From a71ff830ac9e2aef51dee465e815bfb9b60ee577 Mon Sep 17 00:00:00 2001 From: Luna Date: Sat, 13 Jul 2019 19:55:57 -0300 Subject: [PATCH] add wildnoise cmd --- doc/README.md | 4 +++- examples/noise.scri | 2 +- src/custom.zig | 44 ++++++++++++++++++++++++++++++++++++++++++++ src/lang.zig | 2 ++ src/runner.zig | 14 ++++++++++++++ 5 files changed, 64 insertions(+), 2 deletions(-) diff --git a/doc/README.md b/doc/README.md index 4678a48..95995be 100644 --- a/doc/README.md +++ b/doc/README.md @@ -138,10 +138,12 @@ Parameters: - `feedback`: Feedback, 0..1, default 0 - `xfade_samp`: Crossfade samples (int), 0..5000, default 1250 -## `noise split index seed repeat_bytes` +## `wildnoise/noise split index seed repeat_bytes` Inject random noise on the image. +the `wildnoise` version gives a harder version than `noise`. + Parameters: - `seed`, Random seed - `repeat_bytes`, Amount of bytes to preload with random data and repeat diff --git a/examples/noise.scri b/examples/noise.scri index a202f7e..57ae984 100644 --- a/examples/noise.scri +++ b/examples/noise.scri @@ -1,5 +1,5 @@ load :0; noise 5 1 3981 10000; -noise 5 2 3982 0; +wildnoise 5 2 3982 0; noise 5 3 3982 5; quicksave; diff --git a/src/custom.zig b/src/custom.zig index bd9d583..d1e7269 100644 --- a/src/custom.zig +++ b/src/custom.zig @@ -50,3 +50,47 @@ pub const RandomNoise = struct { } } }; + +pub const WildNoise = struct { + r: std.rand.DefaultPrng, + rand_buf: ?[]f32, + cnt: usize = 0, + + pub fn init( + allocator: *std.mem.Allocator, + params: *plugins.ParamMap, + ) !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); + + for (rand_buf) |_, idx| { + rand_buf[idx] = @intToFloat(f32, r.random.int(u1)); + } + + return WildNoise{ + .r = r, + .rand_buf = rand_buf, + }; + } else { + return WildNoise{ + .r = r, + .rand_buf = null, + }; + } + } + + pub fn run(self: *WildNoise, bufs: *RunBuffers) void { + 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] = @intToFloat(f32, self.r.random.int(u1)); + } + } +}; diff --git a/src/lang.zig b/src/lang.zig index 922e1dc..6b6b980 100644 --- a/src/lang.zig +++ b/src/lang.zig @@ -27,6 +27,7 @@ pub const CommandType = enum { Vinyl, RevDelay, Noise, + WildNoise, }; pub const Command = struct { @@ -157,6 +158,7 @@ pub const Lang = struct { // custom implementations (not lv2) _ = try self.keywords.put("noise", .Noise); + _ = try self.keywords.put("wildnoise", .WildNoise); } pub fn parse(self: *Lang, data: []const u8) !CommandList { diff --git a/src/runner.zig b/src/runner.zig index fde23ff..c835ba9 100644 --- a/src/runner.zig +++ b/src/runner.zig @@ -252,6 +252,11 @@ pub const Runner = struct { try image.runCustomPlugin(custom.RandomNoise, pos, map); } + fn wildNoiseCmd(self: *Runner, pos: Position, map: *ParamMap) !void { + var image = try self.getImage(); + try image.runCustomPlugin(custom.WildNoise, pos, map); + } + fn runCommand(self: *Runner, cmd: *lang.Command) !void { var params = ParamList.init(self.allocator); defer params.deinit(); @@ -403,6 +408,15 @@ pub const Runner = struct { try self.noiseCmd(pos, &map); }, + .WildNoise => blk: { + const pos = try cmd.consumePosition(); + + try cmd.appendParamMap(&map, "seed"); + try cmd.appendParamMap(&map, "fill_bytes"); + + try self.wildNoiseCmd(pos, &map); + }, + else => blk: { std.debug.warn("Unsupported command: {}\n", cmd.command); break :blk RunError.UnknownCommand;