add wildnoise cmd

This commit is contained in:
Luna 2019-07-13 19:55:57 -03:00
parent a993c1dc0e
commit a71ff830ac
5 changed files with 64 additions and 2 deletions

View File

@ -138,10 +138,12 @@ Parameters:
- `feedback`: Feedback, 0..1, default 0 - `feedback`: Feedback, 0..1, default 0
- `xfade_samp`: Crossfade samples (int), 0..5000, default 1250 - `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. Inject random noise on the image.
the `wildnoise` version gives a harder version than `noise`.
Parameters: Parameters:
- `seed`, Random seed - `seed`, Random seed
- `repeat_bytes`, Amount of bytes to preload with random data and repeat - `repeat_bytes`, Amount of bytes to preload with random data and repeat

View File

@ -1,5 +1,5 @@
load :0; load :0;
noise 5 1 3981 10000; noise 5 1 3981 10000;
noise 5 2 3982 0; wildnoise 5 2 3982 0;
noise 5 3 3982 5; noise 5 3 3982 5;
quicksave; quicksave;

View File

@ -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));
}
}
};

View File

@ -27,6 +27,7 @@ pub const CommandType = enum {
Vinyl, Vinyl,
RevDelay, RevDelay,
Noise, Noise,
WildNoise,
}; };
pub const Command = struct { pub const Command = struct {
@ -157,6 +158,7 @@ pub const Lang = struct {
// custom implementations (not lv2) // custom implementations (not lv2)
_ = try self.keywords.put("noise", .Noise); _ = try self.keywords.put("noise", .Noise);
_ = try self.keywords.put("wildnoise", .WildNoise);
} }
pub fn parse(self: *Lang, data: []const u8) !CommandList { pub fn parse(self: *Lang, data: []const u8) !CommandList {

View File

@ -252,6 +252,11 @@ pub const Runner = struct {
try image.runCustomPlugin(custom.RandomNoise, pos, map); 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 { fn runCommand(self: *Runner, cmd: *lang.Command) !void {
var params = ParamList.init(self.allocator); var params = ParamList.init(self.allocator);
defer params.deinit(); defer params.deinit();
@ -403,6 +408,15 @@ pub const Runner = struct {
try self.noiseCmd(pos, &map); 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: { else => blk: {
std.debug.warn("Unsupported command: {}\n", cmd.command); std.debug.warn("Unsupported command: {}\n", cmd.command);
break :blk RunError.UnknownCommand; break :blk RunError.UnknownCommand;