scritcher/src/custom.zig

187 lines
4.8 KiB
Zig
Raw Normal View History

// Custom plugins
const std = @import("std");
const lv2 = @import("lv2_helpers.zig");
const plugins = @import("plugin.zig");
const image = @import("image.zig");
const c = lv2.c;
const RunBuffers = plugins.RunBuffers;
pub const RandomNoise = struct {
r: std.rand.DefaultPrng,
2019-08-14 12:10:09 +00:00
rand_buf: ?[]f32 = null,
allocator: ?*std.mem.Allocator = null,
cnt: usize = 0,
pub fn init(
allocator: *std.mem.Allocator,
params: var,
) ?RandomNoise {
var r = std.rand.DefaultPrng.init(params.seed);
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);
}
return RandomNoise{
.r = r,
2019-08-14 12:10:09 +00:00
.allocator = allocator,
.rand_buf = rand_buf,
};
} else {
return RandomNoise{
.r = r,
};
}
}
2019-08-14 12:10:09 +00:00
pub fn deinit(self: *RandomNoise) void {
if (self.allocator == null) return;
if (self.rand_buf == null) return;
self.allocator.?.free(self.rand_buf.?);
}
pub fn run(self: *RandomNoise, 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] = self.r.random.float(f32);
}
}
};
2019-07-13 22:55:57 +00:00
pub const WildNoise = struct {
r: std.rand.DefaultPrng,
2019-08-14 12:10:09 +00:00
rand_buf: ?[]f32 = null,
allocator: ?*std.mem.Allocator = null,
2019-07-13 22:55:57 +00:00
cnt: usize = 0,
pub fn init(
allocator: *std.mem.Allocator,
params: var,
) ?WildNoise {
var r = std.rand.DefaultPrng.init(params.seed);
2019-07-13 22:55:57 +00:00
if (params.fill_bytes > 0) {
var rand_buf = allocator.alloc(f32, params.fill_bytes) catch return null;
2019-07-13 22:55:57 +00:00
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,
};
}
}
pub fn deinit(self: *WildNoise) void {
2019-08-14 12:10:09 +00:00
if (self.allocator == null) return;
if (self.rand_buf == null) return;
self.allocator.?.free(self.rand_buf.?);
}
2019-07-13 22:55:57 +00:00
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));
}
}
};
2019-08-06 22:25:31 +00:00
/// Write any float to the image.
/// Keep in mind that the bit representation of the float will clash with
/// the format of BMP pixel data, which means writing 0 everywhere won't give
/// you the black color.
pub const Write = struct {
data: f32,
pub fn init(
allocator: *std.mem.Allocator,
params: var,
) Write {
return Write{
.data = params.data,
};
}
pub fn deinit(self: *Write) void {}
pub fn run(self: *Write, bufs: *RunBuffers) void {
bufs.out[0] = self.data;
}
};
pub const Embed = struct {
allocator: *std.mem.Allocator,
filepath: []const u8,
sndfile: *c.SNDFILE = undefined,
2019-10-22 21:23:05 +00:00
buf: []f32 = undefined,
pub fn init(allocator: *std.mem.Allocator, params: var) @This() {
return Embed{
.allocator = allocator,
.filepath = params.path,
};
}
pub fn setup(self: *@This()) !void {
var in_fmt = c.SF_INFO{
2019-11-10 16:37:59 +00:00
.frames = @as(c_int, 0),
.samplerate = @as(c_int, 0),
.channels = @as(c_int, 0),
.format = @as(c_int, 0),
.sections = @as(c_int, 0),
.seekable = @as(c_int, 0),
};
2019-10-22 21:23:05 +00:00
self.sndfile = try image.sopen(
self.allocator,
self.filepath,
c.SFM_READ,
&in_fmt,
);
image.sseek(self.sndfile, 0);
2019-10-22 21:23:05 +00:00
self.buf = try self.allocator.alloc(f32, @intCast(usize, in_fmt.channels));
}
pub fn deinit(self: *@This()) void {}
pub fn run(self: *@This(), bufs: *RunBuffers) void {
2019-10-22 21:23:05 +00:00
const read_bytes = c.sf_readf_float(self.sndfile, self.buf.ptr, 1);
if (read_bytes == 0) {
bufs.out[0] = bufs.in[0];
2019-10-22 21:23:05 +00:00
return;
}
if (read_bytes < 0) {
const st: i32 = c.sf_error(self.sndfile);
2020-01-15 01:31:20 +00:00
std.debug.warn("Failed to read {} ({})\n", .{
2019-10-22 21:23:05 +00:00
self.filepath,
c.sf_error_number(st),
2020-01-15 01:31:20 +00:00
});
2019-10-22 21:23:05 +00:00
return;
}
bufs.out[0] = bufs.in[0] + self.buf[0];
}
};