add write cmd

- make custom plugins return ?Plugin instead of !Plugin
This commit is contained in:
Luna 2019-07-29 11:51:40 -03:00
parent ff78044587
commit 8172066851
5 changed files with 48 additions and 5 deletions

3
examples/write.scri Normal file
View File

@ -0,0 +1,3 @@
load :0;
write 3 1 1;
quicksave;

View File

@ -15,14 +15,14 @@ pub const RandomNoise = struct {
pub fn init( pub fn init(
allocator: *std.mem.Allocator, allocator: *std.mem.Allocator,
params: *plugins.ParamMap, params: *plugins.ParamMap,
) !RandomNoise { ) ?RandomNoise {
const seed = @floatToInt(u64, params.get("seed").?.value); const seed = @floatToInt(u64, params.get("seed").?.value);
const fillbytes = @floatToInt(usize, params.get("fill_bytes").?.value); const fillbytes = @floatToInt(usize, params.get("fill_bytes").?.value);
var r = std.rand.DefaultPrng.init(seed); var r = std.rand.DefaultPrng.init(seed);
if (fillbytes > 0) { 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| { for (rand_buf) |_, idx| {
rand_buf[idx] = r.random.float(f32); rand_buf[idx] = r.random.float(f32);
@ -59,14 +59,14 @@ pub const WildNoise = struct {
pub fn init( pub fn init(
allocator: *std.mem.Allocator, allocator: *std.mem.Allocator,
params: *plugins.ParamMap, params: *plugins.ParamMap,
) !WildNoise { ) ?WildNoise {
const seed = @floatToInt(u64, params.get("seed").?.value); const seed = @floatToInt(u64, params.get("seed").?.value);
const fillbytes = @floatToInt(usize, params.get("fill_bytes").?.value); const fillbytes = @floatToInt(usize, params.get("fill_bytes").?.value);
var r = std.rand.DefaultPrng.init(seed); var r = std.rand.DefaultPrng.init(seed);
if (fillbytes > 0) { 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| { for (rand_buf) |_, idx| {
rand_buf[idx] = @intToFloat(f32, r.random.int(u1)); 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;
}
};

View File

@ -1,6 +1,7 @@
const std = @import("std"); const std = @import("std");
const lv2 = @import("lv2_helpers.zig"); const lv2 = @import("lv2_helpers.zig");
const c = lv2.c; const c = lv2.c;
const custom = @import("custom.zig");
const plugins = @import("plugin.zig"); const plugins = @import("plugin.zig");
@ -17,6 +18,7 @@ pub const ImageError = error{
InvalidSymbol, InvalidSymbol,
InstantiateFail, InstantiateFail,
WriteFail, WriteFail,
PluginLoadFail,
}; };
/// Low level integration function with libsndfile. /// Low level integration function with libsndfile.
@ -358,7 +360,12 @@ pub const Image = struct {
position: plugins.Position, position: plugins.Position,
params: *plugins.ParamMap, params: *plugins.ParamMap,
) !void { ) !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 // the code here is a copypaste of runPlugin() without the specific
// lilv things. // lilv things.

View File

@ -26,8 +26,10 @@ pub const CommandType = enum {
Delay, Delay,
Vinyl, Vinyl,
RevDelay, RevDelay,
Noise, Noise,
WildNoise, WildNoise,
Write,
Rotate, Rotate,
}; };
@ -161,6 +163,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); _ = try self.keywords.put("wildnoise", .WildNoise);
_ = try self.keywords.put("write", .Write);
// even more custom // even more custom
_ = try self.keywords.put("rotate", .Rotate); _ = try self.keywords.put("rotate", .Rotate);

View File

@ -258,6 +258,11 @@ pub const Runner = struct {
try image.runCustomPlugin(custom.WildNoise, pos, map); 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( fn rotateCmd(
self: *Runner, self: *Runner,
deg: f32, deg: f32,
@ -431,6 +436,12 @@ pub const Runner = struct {
try self.wildNoiseCmd(pos, &map); try self.wildNoiseCmd(pos, &map);
}, },
.Write => blk: {
const pos = try cmd.consumePosition();
try cmd.appendParamMap(&map, "data");
try self.writeCmd(pos, &map);
},
.Rotate => blk: { .Rotate => blk: {
const deg = try cmd.floatArgAt(0); const deg = try cmd.floatArgAt(0);
const bgfill = try cmd.argAt(1); const bgfill = try cmd.argAt(1);