scritcher/src/runner.zig

450 lines
15 KiB
Zig
Raw Normal View History

2019-07-08 15:38:16 +00:00
const std = @import("std");
const lang = @import("lang.zig");
const images = @import("image.zig");
2019-07-09 01:40:52 +00:00
const plugin = @import("plugin.zig");
const custom = @import("custom.zig");
2019-07-22 22:28:55 +00:00
const magick = @import("magick.zig");
2019-07-08 15:38:16 +00:00
2019-07-10 20:15:57 +00:00
const Position = plugin.Position;
const ParamList = plugin.ParamList;
const ParamMap = plugin.ParamMap;
2019-07-10 20:15:57 +00:00
const Image = images.Image;
pub const RunError = error{
UnknownCommand,
NoBMP,
ImageRequired,
};
2019-07-08 15:38:16 +00:00
pub const Runner = struct {
allocator: *std.mem.Allocator,
image: ?*Image = null,
2019-07-08 15:38:16 +00:00
pub fn init(allocator: *std.mem.Allocator) Runner {
2019-07-09 03:04:01 +00:00
return Runner{
.allocator = allocator,
};
2019-07-08 15:38:16 +00:00
}
pub fn deinit(self: *Runner) void {
if (self.image) |image| {
image.close();
}
}
2019-07-08 16:55:54 +00:00
fn resolveArg(self: *Runner, load_path: []const u8) ![]const u8 {
if (load_path[0] == ':') {
// parse the index from 1 to end
const index = try std.fmt.parseInt(usize, load_path[1..], 10);
var args_it = std.process.args();
_ = args_it.skip();
var i: usize = 0;
while (i <= index) : (i += 1) {
_ = args_it.skip();
}
const arg = try (args_it.next(self.allocator) orelse @panic("expected argument"));
return arg;
} else {
return load_path;
}
}
fn resolveArgPath(self: *Runner, path_or_argidx: []const u8) ![]const u8 {
const path = try self.resolveArg(path_or_argidx);
const resolved_path = try std.fs.path.resolve(
self.allocator,
[_][]const u8{path},
);
2019-07-08 16:55:54 +00:00
return resolved_path;
}
fn loadCmd(self: *Runner, path_or_argidx: []const u8) !void {
2019-07-08 16:55:54 +00:00
var load_path = try self.resolveArgPath(path_or_argidx);
std.debug.warn("load path: {}\n", load_path);
// we could use ImageMagick to convert from X to BMP
// but i can't find an easy way to do things in memory.
// the upside is that this allows some pre-processing by the user
// before loading the file into scritcher. for example, you can start
// krita/gimp and make it export a bmp and while in the program you can
// apply filters, etc.
if (!std.mem.endsWith(u8, load_path, ".bmp")) {
std.debug.warn("Only BMP files are allowed to be loaded.\n");
return RunError.NoBMP;
}
2019-07-10 15:00:43 +00:00
// we don't copy load_path into a temporary file because we're already
// loading it under the SFM_READ mode, which won't cause any destructive
// operations on the file.
self.image = try Image.open(self.allocator, load_path);
2019-07-08 16:55:54 +00:00
}
fn getImage(self: *Runner) !*Image {
if (self.image) |image| {
return image;
} else {
std.debug.warn("image is required!\n");
return RunError.ImageRequired;
}
}
2019-07-11 00:43:23 +00:00
fn makeGlitchedPath(self: *Runner) ![]const u8 {
// we want to transform basename, if it is "x.bmp" to "x_gN.bmp", where
// N is the maximum non-used integer.
var image = try self.getImage();
const basename = std.fs.path.basename(image.path);
const dirname = std.fs.path.dirname(image.path).?;
var dir = try std.fs.Dir.open(self.allocator, dirname);
defer dir.close();
const period_idx = std.mem.lastIndexOf(u8, basename, ".").?;
const extension = basename[period_idx..basename.len];
// starts_with would be "x_g", we want to find all files in the directory
// that start with that name.
const starts_with = try std.fmt.allocPrint(
self.allocator,
"{}_g",
basename[0..period_idx],
);
var max: usize = 0;
while (try dir.next()) |entry| {
switch (entry.kind) {
.File => blk: {
if (!std.mem.startsWith(u8, entry.name, starts_with)) break :blk {};
// we want to get the N in x_gN.ext
const entry_gidx = std.mem.lastIndexOf(u8, entry.name, "_g").?;
const entry_pidx_opt = std.mem.lastIndexOf(u8, entry.name, ".");
if (entry_pidx_opt == null) break :blk {};
const entry_pidx = entry_pidx_opt.?;
// if N isn't a number, we just ignore that file
const idx_str = entry.name[entry_gidx + 2 .. entry_pidx];
const idx = std.fmt.parseInt(usize, idx_str, 10) catch |err| {
break :blk {};
};
if (idx > max) max = idx;
},
else => {},
}
}
2019-07-10 02:04:05 +00:00
const out_path = try std.fmt.allocPrint(
self.allocator,
"{}/{}{}{}",
dirname,
starts_with,
max + 1,
extension,
);
2019-07-11 00:43:23 +00:00
return out_path;
}
fn quicksaveCmd(self: *Runner) !void {
var image = try self.getImage();
const out_path = try self.makeGlitchedPath();
2019-07-10 02:04:05 +00:00
try image.saveTo(out_path);
}
2019-07-11 00:43:23 +00:00
fn runQSCmd(self: *Runner, program: []const u8) !void {
var image = try self.getImage();
const out_path = try self.makeGlitchedPath();
try image.saveTo(out_path);
var proc = try std.ChildProcess.init(
[_][]const u8{ program, out_path },
self.allocator,
);
defer proc.deinit();
std.debug.warn("running '{} {}'", program, out_path);
_ = try proc.spawnAndWait();
}
2019-07-09 01:40:52 +00:00
/// Run the http://lv2plug.in/plugins/eg-amp plugin over the file.
2019-07-10 20:15:57 +00:00
fn ampCmd(self: *Runner, pos: Position, params: ParamList) !void {
2019-07-09 03:04:01 +00:00
var image = try self.getImage();
2019-07-10 20:15:57 +00:00
try image.runPlugin("http://lv2plug.in/plugins/eg-amp", pos, params);
2019-07-09 01:40:52 +00:00
}
2019-07-10 20:15:57 +00:00
fn rFlangerCmd(self: *Runner, pos: Position, params: ParamList) !void {
2019-07-10 16:24:03 +00:00
var image = try self.getImage();
2019-07-10 20:15:57 +00:00
try image.runPlugin("http://plugin.org.uk/swh-plugins/retroFlange", pos, params);
2019-07-10 16:24:03 +00:00
}
2019-07-10 20:15:57 +00:00
fn eqCmd(self: *Runner, position: Position, params: ParamList) !void {
2019-07-10 19:06:44 +00:00
var image = try self.getImage();
try image.runPlugin("http://plugin.org.uk/swh-plugins/dj_eq_mono", position, params);
}
2019-07-10 20:15:57 +00:00
fn phaserCmd(self: *Runner, position: Position, params: ParamList) !void {
2019-07-10 19:24:43 +00:00
var image = try self.getImage();
try image.runPlugin("http://plugin.org.uk/swh-plugins/lfoPhaser", position, params);
}
2019-07-10 20:31:18 +00:00
fn mbeqCmd(self: *Runner, position: Position, bands: []const f32) !void {
2019-07-10 19:43:30 +00:00
var image = try self.getImage();
2019-07-10 20:15:57 +00:00
var params = ParamList.init(self.allocator);
2019-07-10 19:43:30 +00:00
defer params.deinit();
for (bands) |band_value, idx| {
var sym = try std.fmt.allocPrint(self.allocator, "band_{}", idx + 1);
2019-07-10 20:15:57 +00:00
try params.append(plugin.Param{
.sym = sym,
.value = band_value,
});
2019-07-10 19:43:30 +00:00
}
try image.runPlugin("http://plugin.org.uk/swh-plugins/mbeq", position, params);
}
2019-07-10 20:31:18 +00:00
fn chorusCmd(self: *Runner, pos: Position, params: ParamList) !void {
2019-07-10 19:59:15 +00:00
var image = try self.getImage();
try image.runPlugin("http://plugin.org.uk/swh-plugins/multivoiceChorus", pos, params);
}
2019-07-10 20:31:18 +00:00
fn pitchScalerCmd(self: *Runner, pos: Position, params: ParamList) !void {
var image = try self.getImage();
try image.runPlugin("http://plugin.org.uk/swh-plugins/pitchScaleHQ", pos, params);
}
2019-07-10 23:19:40 +00:00
fn reverbCmd(self: *Runner, pos: Position, params: ParamList) !void {
var image = try self.getImage();
try image.runPlugin("http://invadarecords.com/plugins/lv2/erreverb/mono", pos, params);
}
fn highpassCmd(self: *Runner, pos: Position, params: ParamList) !void {
var image = try self.getImage();
try image.runPlugin("http://invadarecords.com/plugins/lv2/filter/hpf/mono", pos, params);
}
2019-07-11 13:20:09 +00:00
fn delayCmd(self: *Runner, pos: Position, params: ParamList) !void {
var image = try self.getImage();
try image.runPlugin("http://plugin.org.uk/swh-plugins/delayorama", pos, params);
}
2019-07-13 19:46:26 +00:00
fn vinylCmd(self: *Runner, pos: Position, params: ParamList) !void {
var image = try self.getImage();
try image.runPlugin("http://plugin.org.uk/swh-plugins/vynil", pos, params);
}
2019-07-13 20:20:14 +00:00
fn revDelayCmd(self: *Runner, pos: Position, params: ParamList) !void {
var image = try self.getImage();
try image.runPlugin("http://plugin.org.uk/swh-plugins/revdelay", pos, params);
}
fn noiseCmd(self: *Runner, pos: Position, map: *ParamMap) !void {
var image = try self.getImage();
try image.runCustomPlugin(custom.RandomNoise, pos, map);
}
2019-07-13 22:55:57 +00:00
fn wildNoiseCmd(self: *Runner, pos: Position, map: *ParamMap) !void {
var image = try self.getImage();
try image.runCustomPlugin(custom.WildNoise, pos, map);
}
2019-07-22 22:28:55 +00:00
fn rotateCmd(self: *Runner) !void {
var image = try self.getImage();
try magick.runRotate(image);
}
2019-07-08 15:38:16 +00:00
fn runCommand(self: *Runner, cmd: *lang.Command) !void {
2019-07-10 20:15:57 +00:00
var params = ParamList.init(self.allocator);
2019-07-10 19:59:15 +00:00
defer params.deinit();
var map = ParamMap.init(self.allocator);
defer map.deinit();
2019-07-08 15:38:16 +00:00
return switch (cmd.command) {
.Noop => {},
2019-07-08 16:55:54 +00:00
.Load => blk: {
var path = cmd.args.at(0);
try self.loadCmd(path);
break :blk;
},
.Quicksave => try self.quicksaveCmd(),
2019-07-11 00:43:23 +00:00
.RunQS => try self.runQSCmd(cmd.args.at(0)),
2019-07-09 01:40:52 +00:00
2019-07-09 03:04:01 +00:00
.Amp => blk: {
2019-07-10 20:15:57 +00:00
const pos = try cmd.consumePosition();
try cmd.appendParam(&params, "gain");
2019-07-10 20:15:57 +00:00
try self.ampCmd(pos, params);
2019-07-09 03:04:01 +00:00
},
2019-07-09 01:40:52 +00:00
2019-07-10 16:24:03 +00:00
.RFlanger => blk: {
2019-07-10 20:15:57 +00:00
const pos = try cmd.consumePosition();
try cmd.appendParam(&params, "delay_depth_avg");
try cmd.appendParam(&params, "law_freq");
2019-07-10 20:15:57 +00:00
try self.rFlangerCmd(pos, params);
2019-07-10 16:24:03 +00:00
},
2019-07-10 19:06:44 +00:00
.Eq => blk: {
const pos = try cmd.consumePosition();
try cmd.appendParam(&params, "lo");
try cmd.appendParam(&params, "mid");
try cmd.appendParam(&params, "hi");
2019-07-10 19:06:44 +00:00
2019-07-10 20:15:57 +00:00
try self.eqCmd(pos, params);
2019-07-10 19:06:44 +00:00
},
2019-07-10 19:24:43 +00:00
.Phaser => blk: {
const pos = try cmd.consumePosition();
try cmd.appendParam(&params, "lfo_rate");
try cmd.appendParam(&params, "lfo_depth");
try cmd.appendParam(&params, "fb");
try cmd.appendParam(&params, "spread");
2019-07-10 19:24:43 +00:00
2019-07-10 20:15:57 +00:00
try self.phaserCmd(pos, params);
2019-07-10 19:24:43 +00:00
},
2019-07-10 19:43:30 +00:00
.Mbeq => blk: {
const pos = try cmd.consumePosition();
const bands = try cmd.floatArgMany(self.allocator, 2, 15, f32(0));
try self.mbeqCmd(pos, bands);
},
2019-07-10 19:59:15 +00:00
.Chorus => blk: {
const pos = try cmd.consumePosition();
try cmd.appendParam(&params, "voices");
try cmd.appendParam(&params, "delay_base");
try cmd.appendParam(&params, "voice_spread");
try cmd.appendParam(&params, "detune");
try cmd.appendParam(&params, "law_freq");
try cmd.appendParam(&params, "attendb");
2019-07-10 19:59:15 +00:00
try self.chorusCmd(pos, params);
},
2019-07-10 20:31:18 +00:00
.PitchScaler => blk: {
const pos = try cmd.consumePosition();
try cmd.appendParam(&params, "mult");
2019-07-10 20:31:18 +00:00
try self.pitchScalerCmd(pos, params);
},
2019-07-10 23:19:40 +00:00
.Reverb => blk: {
const pos = try cmd.consumePosition();
try cmd.appendParam(&params, "roomLength");
try cmd.appendParam(&params, "roomWidth");
try cmd.appendParam(&params, "roomHeight");
try cmd.appendParam(&params, "sourceLR");
try cmd.appendParam(&params, "sourceFB");
try cmd.appendParam(&params, "listLR");
try cmd.appendParam(&params, "listFB");
try cmd.appendParam(&params, "hpf");
try cmd.appendParam(&params, "warmth");
try cmd.appendParam(&params, "diffusion");
2019-07-10 23:19:40 +00:00
try self.reverbCmd(pos, params);
},
.Highpass => blk: {
const pos = try cmd.consumePosition();
try cmd.appendParam(&params, "freq");
try cmd.appendParam(&params, "gain");
try cmd.appendParam(&params, "noClip");
try self.highpassCmd(pos, params);
},
2019-07-11 13:20:09 +00:00
.Delay => blk: {
const pos = try cmd.consumePosition();
try cmd.appendParam(&params, "seed");
try cmd.appendParam(&params, "gain");
try cmd.appendParam(&params, "feedback_pc");
try cmd.appendParam(&params, "tap_count");
try cmd.appendParam(&params, "first_delay");
try cmd.appendParam(&params, "delay_range");
try cmd.appendParam(&params, "delay_scale");
try cmd.appendParam(&params, "delay_rand_pc");
try cmd.appendParam(&params, "gain_scale");
try cmd.appendParam(&params, "wet");
try self.delayCmd(pos, params);
},
2019-07-13 19:46:26 +00:00
.Vinyl => blk: {
const pos = try cmd.consumePosition();
try cmd.appendParam(&params, "year");
try cmd.appendParam(&params, "rpm");
try cmd.appendParam(&params, "warp");
try cmd.appendParam(&params, "click");
try cmd.appendParam(&params, "wear");
try self.vinylCmd(pos, params);
},
2019-07-13 20:20:14 +00:00
.RevDelay => blk: {
const pos = try cmd.consumePosition();
try cmd.appendParam(&params, "delay_time");
try cmd.appendParam(&params, "dry_level");
try cmd.appendParam(&params, "wet_level");
try cmd.appendParam(&params, "feedback");
try cmd.appendParam(&params, "xfade_samp");
try self.revDelayCmd(pos, params);
},
.Noise => blk: {
const pos = try cmd.consumePosition();
try cmd.appendParamMap(&map, "seed");
try cmd.appendParamMap(&map, "fill_bytes");
try self.noiseCmd(pos, &map);
},
2019-07-13 22:55:57 +00:00
.WildNoise => blk: {
const pos = try cmd.consumePosition();
try cmd.appendParamMap(&map, "seed");
try cmd.appendParamMap(&map, "fill_bytes");
try self.wildNoiseCmd(pos, &map);
},
2019-07-22 22:28:55 +00:00
.Rotate => try self.rotateCmd(),
2019-07-08 15:38:16 +00:00
else => blk: {
2019-07-10 19:59:15 +00:00
std.debug.warn("Unsupported command: {}\n", cmd.command);
2019-07-08 15:38:16 +00:00
break :blk RunError.UnknownCommand;
},
};
}
/// Run a list of commands.
2019-07-08 16:13:03 +00:00
pub fn runCommands(
self: *Runner,
cmds: lang.CommandList,
debug_flag: bool,
) !void {
2019-07-08 15:38:16 +00:00
var it = cmds.iterator();
while (it.next()) |cmd| {
2019-07-08 16:13:03 +00:00
if (debug_flag) cmd.print();
2019-07-08 15:38:16 +00:00
try self.runCommand(cmd);
}
}
};