Compare commits

..

2 commits

Author SHA1 Message Date
3af4dc575a fix command ptr alignment 2024-06-01 16:53:01 -03:00
c0da5e68b7 base port to zig 0.12 2024-06-01 15:46:37 -03:00
8 changed files with 82 additions and 67 deletions

View file

@ -1,8 +1,6 @@
const std = @import("std"); const std = @import("std");
const builds = std.build;
const Builder = std.build.Builder;
fn setupLinks(step: *builds.LibExeObjStep) void { fn setupLinks(step: *std.Build.Step.Compile) void {
step.linkSystemLibrary("c"); step.linkSystemLibrary("c");
step.linkSystemLibrary("lilv-0"); step.linkSystemLibrary("lilv-0");
@ -41,13 +39,13 @@ fn setupLinks(step: *builds.LibExeObjStep) void {
} }
} }
pub fn build(b: *Builder) void { pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{}); const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{}); const optimize = b.standardOptimizeOption(.{});
const exe = b.addExecutable(.{ const exe = b.addExecutable(.{
.name = "scritcher", .name = "scritcher",
.root_source_file = .{ .path = "src/main.zig" }, .root_source_file = b.path("src/main.zig"),
.target = target, .target = target,
.optimize = optimize, .optimize = optimize,
}); });
@ -64,7 +62,7 @@ pub fn build(b: *Builder) void {
run_step.dependOn(&run_cmd.step); run_step.dependOn(&run_cmd.step);
const test_step = b.addTest(.{ const test_step = b.addTest(.{
.root_source_file = .{ .path = "src/main.zig" }, .root_source_file = b.path("src/main.zig"),
.target = target, .target = target,
.optimize = optimize, .optimize = optimize,
}); });

View file

@ -26,10 +26,10 @@ pub fn sopen(
mode: i32, mode: i32,
fmt: *c.SF_INFO, fmt: *c.SF_INFO,
) !*c.SNDFILE { ) !*c.SNDFILE {
var cstr_path = try allocator.dupeZ(u8, path); const cstr_path = try allocator.dupeZ(u8, path);
defer allocator.free(cstr_path); defer allocator.free(cstr_path);
var file = c.sf_open(cstr_path.ptr, mode, fmt); const file = c.sf_open(cstr_path.ptr, mode, fmt);
const st: i32 = c.sf_error(file); const st: i32 = c.sf_error(file);
if (st != 0) { if (st != 0) {
@ -78,7 +78,7 @@ pub fn temporaryName(allocator: std.mem.Allocator) ![]u8 {
const template_start = "/temp/temp_"; const template_start = "/temp/temp_";
const template = "/tmp/temp_XXXXXXXXXXXXXXXXXXXXX"; const template = "/tmp/temp_XXXXXXXXXXXXXXXXXXXXX";
var nam = try allocator.alloc(u8, template.len); var nam = try allocator.alloc(u8, template.len);
std.mem.copy(u8, nam, template); std.mem.copyForwards(u8, nam, template);
const seed = @as(u64, @truncate(@as(u128, @bitCast(std.time.nanoTimestamp())))); const seed = @as(u64, @truncate(@as(u128, @bitCast(std.time.nanoTimestamp()))));
var r = std.rand.DefaultPrng.init(seed); var r = std.rand.DefaultPrng.init(seed);
@ -89,8 +89,8 @@ pub fn temporaryName(allocator: std.mem.Allocator) ![]u8 {
while (i < 100) : (i += 1) { while (i < 100) : (i += 1) {
// generate a random uppercase letter, that is, 65 + random number. // generate a random uppercase letter, that is, 65 + random number.
for (fill, 0..) |_, f_idx| { for (fill, 0..) |_, f_idx| {
var idx = @as(u8, @intCast(r.random().uintLessThan(u5, 24))); const idx = @as(u8, @intCast(r.random().uintLessThan(u5, 24)));
var letter = @as(u8, 65) + idx; const letter = @as(u8, 65) + idx;
fill[f_idx] = letter; fill[f_idx] = letter;
} }
@ -139,9 +139,9 @@ pub const Image = struct {
/// Open a BMP image for later. /// Open a BMP image for later.
pub fn open(allocator: std.mem.Allocator, path: []const u8) !*Image { pub fn open(allocator: std.mem.Allocator, path: []const u8) !*Image {
var in_fmt = mkSfInfo(); var in_fmt = mkSfInfo();
var sndfile = try sopen(allocator, path, c.SFM_READ, &in_fmt); const sndfile = try sopen(allocator, path, c.SFM_READ, &in_fmt);
var image = try allocator.create(Image); const image = try allocator.create(Image);
std.debug.assert(in_fmt.frames > @as(i64, 0)); std.debug.assert(in_fmt.frames > @as(i64, 0));
std.debug.assert(in_fmt.seekable == @as(i32, 1)); std.debug.assert(in_fmt.seekable == @as(i32, 1));
@ -160,10 +160,10 @@ pub const Image = struct {
pub fn clone(self: *Image) !*Image { pub fn clone(self: *Image) !*Image {
var in_fmt = mkSfInfo(); var in_fmt = mkSfInfo();
// clone sndfile // clone sndfile
var sndfile = try sopen(self.allocator, self.curpath, c.SFM_READ, &in_fmt); const sndfile = try sopen(self.allocator, self.curpath, c.SFM_READ, &in_fmt);
std.debug.assert(self.frames == @as(usize, @intCast(in_fmt.frames))); std.debug.assert(self.frames == @as(usize, @intCast(in_fmt.frames)));
var image = try self.allocator.create(Image); const image = try self.allocator.create(Image);
std.debug.assert(in_fmt.frames > @as(i64, 0)); std.debug.assert(in_fmt.frames > @as(i64, 0));
std.debug.assert(in_fmt.seekable == @as(i32, 1)); std.debug.assert(in_fmt.seekable == @as(i32, 1));
@ -180,7 +180,7 @@ pub const Image = struct {
} }
pub fn close(self: *Image) void { pub fn close(self: *Image) void {
var st: i32 = c.sf_close(self.sndfile); const st: i32 = c.sf_close(self.sndfile);
if (st != 0) { if (st != 0) {
log.debug("Failed to close {s} ({s})", .{ log.debug("Failed to close {s} ({s})", .{
self.path, self.path,
@ -252,7 +252,7 @@ pub const Image = struct {
fn getSeekPos(self: *Image, position: plugins.Position) plugins.SeekPos { fn getSeekPos(self: *Image, position: plugins.Position) plugins.SeekPos {
const file_end = self.frames; const file_end = self.frames;
var seek_pos = position.seekPos(file_end); const seek_pos = position.seekPos(file_end);
log.debug("\tstart {d} end {d}", .{ seek_pos.start, seek_pos.end }); log.debug("\tstart {d} end {d}", .{ seek_pos.start, seek_pos.end });
return seek_pos; return seek_pos;
} }
@ -321,10 +321,10 @@ pub const Image = struct {
// now, for each param for the plugin, we find its port, and set // now, for each param for the plugin, we find its port, and set
// the value for the port there. // the value for the port there.
for (params.items) |param| { for (params.items) |param| {
var sym_cstr = try self.allocator.dupeZ(u8, param.sym); const sym_cstr = try self.allocator.dupeZ(u8, param.sym);
defer self.allocator.free(sym_cstr); defer self.allocator.free(sym_cstr);
var sym = c.lilv_new_string(ctx.world, sym_cstr.ptr); const sym = c.lilv_new_string(ctx.world, sym_cstr.ptr);
const port = c.lilv_plugin_get_port_by_symbol(ctx.plugin, sym) orelse { const port = c.lilv_plugin_get_port_by_symbol(ctx.plugin, sym) orelse {
log.debug("assert fail: symbol {s} not found on port", .{param.sym}); log.debug("assert fail: symbol {s} not found on port", .{param.sym});
return ImageError.InvalidSymbol; return ImageError.InvalidSymbol;
@ -332,7 +332,7 @@ pub const Image = struct {
c.lilv_node_free(sym); c.lilv_node_free(sym);
var idx = c.lilv_port_get_index(ctx.plugin, port); const idx = c.lilv_port_get_index(ctx.plugin, port);
log.debug("\tset sym={s}, idx={d} to val={}", .{ log.debug("\tset sym={s}, idx={d} to val={}", .{
param.sym, param.sym,
idx, idx,
@ -343,11 +343,11 @@ pub const Image = struct {
// now we need to generate a temporary file and put the output of // now we need to generate a temporary file and put the output of
// running the plugin on that file // running the plugin on that file
var tmpnam = try temporaryName(self.allocator); const tmpnam = try temporaryName(self.allocator);
log.debug("\trunning plugin from '{s}' to '{s}'", .{ self.curpath, tmpnam }); log.debug("\trunning plugin from '{s}' to '{s}'", .{ self.curpath, tmpnam });
var out_fmt = mkSfInfo(); var out_fmt = mkSfInfo();
var out_file = try sopen(self.allocator, tmpnam, c.SFM_WRITE, &out_fmt); const out_file = try sopen(self.allocator, tmpnam, c.SFM_WRITE, &out_fmt);
var rctx = try plugins.RunContext.init(self.allocator, ctx.plugin); var rctx = try plugins.RunContext.init(self.allocator, ctx.plugin);
defer rctx.deinit(); defer rctx.deinit();
@ -380,7 +380,7 @@ pub const Image = struct {
log.debug("\tseek pos start: {d} end: {d}", .{ seek_pos.start, seek_pos.end }); log.debug("\tseek pos start: {d} end: {d}", .{ seek_pos.start, seek_pos.end });
var inbuf = &rctx.buffers.in; var inbuf = &rctx.buffers.in;
var outbuf = &rctx.buffers.out; const outbuf = &rctx.buffers.out;
while (i <= seek_pos.end) : (i += 1) { while (i <= seek_pos.end) : (i += 1) {
inbuf[0] = 0; inbuf[0] = 0;
@ -416,7 +416,7 @@ pub const Image = struct {
try self.reopen(tmpnam); try self.reopen(tmpnam);
try self.checkValid(); try self.checkValid();
var time_taken = timer.read(); const time_taken = timer.read();
log.debug("\ttook {d:.2}ms running plugin", .{time_taken / std.time.us_per_ms}); log.debug("\ttook {d:.2}ms running plugin", .{time_taken / std.time.us_per_ms});
} }
@ -431,7 +431,7 @@ pub const Image = struct {
position: plugins.Position, position: plugins.Position,
extra: anytype, extra: anytype,
) !void { ) !void {
var plugin_opt: ?Plugin = Plugin.init(self.allocator, extra); const plugin_opt: ?Plugin = Plugin.init(self.allocator, extra);
if (plugin_opt == null) { if (plugin_opt == null) {
return ImageError.PluginLoadFail; return ImageError.PluginLoadFail;
} }
@ -448,11 +448,11 @@ pub const Image = struct {
// 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.
var tmpnam = try temporaryName(self.allocator); const tmpnam = try temporaryName(self.allocator);
log.debug("\trunning CUSTOM plugin from '{s}' to '{s}'", .{ self.curpath, tmpnam }); log.debug("\trunning CUSTOM plugin from '{s}' to '{s}'", .{ self.curpath, tmpnam });
var out_fmt = mkSfInfo(); var out_fmt = mkSfInfo();
var out_file = try sopen(self.allocator, tmpnam, c.SFM_WRITE, &out_fmt); const out_file = try sopen(self.allocator, tmpnam, c.SFM_WRITE, &out_fmt);
var bufs = plugins.RunBuffers{}; var bufs = plugins.RunBuffers{};
const seek_pos = self.getSeekPos(position); const seek_pos = self.getSeekPos(position);
@ -478,8 +478,8 @@ pub const Image = struct {
var i: usize = seek_pos.start; var i: usize = seek_pos.start;
log.debug("\tseek pos start: {d} end: {d}", .{ seek_pos.start, seek_pos.end }); log.debug("\tseek pos start: {d} end: {d}", .{ seek_pos.start, seek_pos.end });
var inbuf = &bufs.in; const inbuf = &bufs.in;
var outbuf = &bufs.out; const outbuf = &bufs.out;
while (i <= seek_pos.end) : (i += 1) { while (i <= seek_pos.end) : (i += 1) {
const read_bytes = c.sf_readf_float(self.sndfile, inbuf, 1); const read_bytes = c.sf_readf_float(self.sndfile, inbuf, 1);

View file

@ -143,7 +143,19 @@ pub const Command = struct {
if (base.tag != T.base_tag) if (base.tag != T.base_tag)
return null; return null;
return @fieldParentPtr(T, "base", base); //const baseInt = @intFromPtr(base);
//log.debug("casting from {d}", .{baseInt});
//log.debug("aligns from 8? {d}", .{baseInt % 8});
//log.debug("align T: {d} {s}", .{ @alignOf(*T), @typeName(T) });
//log.debug("align base: {d} {s}", .{ @alignOf(*const @This()), @typeName(@This()) });
const base_aligned: *const @This() = @alignCast(base);
const parented = @as(*const T, @alignCast(@fieldParentPtr("base", base_aligned)));
const ptr: *const T = @alignCast(parented);
//log.debug("align: {d}\n", .{@alignOf(@TypeOf(ptr))});
return ptr;
} }
pub fn print(base: *const @This()) void { pub fn print(base: *const @This()) void {
@ -641,7 +653,7 @@ pub const Lang = struct {
// TODO better tokenizer instead of just tokenize(" ")...maybe???? // TODO better tokenizer instead of just tokenize(" ")...maybe????
var tok_it = std.mem.splitSequence(u8, stmt, " "); var tok_it = std.mem.splitSequence(u8, stmt, " ");
var cmd_opt = tok_it.next(); const cmd_opt = tok_it.next();
if (cmd_opt == null) { if (cmd_opt == null) {
self.doError("No command given", .{}); self.doError("No command given", .{});
continue; continue;
@ -669,11 +681,14 @@ pub const Lang = struct {
} }
comptime var lowered_command_name = [_]u8{0} ** struct_name.len; comptime var lowered_command_name = [_]u8{0} ** struct_name.len;
var runtime_lowered_command_name = [_]u8{0} ** struct_name.len;
comptime { comptime {
for (struct_name, 0..) |c, i| { for (struct_name, 0..) |c, i| {
lowered_command_name[i] = std.ascii.toLower(c); lowered_command_name[i] = std.ascii.toLower(c);
} }
} }
const c_l = lowered_command_name;
std.mem.copyForwards(u8, &runtime_lowered_command_name, &c_l);
// if we have a match, we know the proper struct type // if we have a match, we know the proper struct type
// to use. this actually works compared to storing command_struct // to use. this actually works compared to storing command_struct
@ -685,7 +700,7 @@ pub const Lang = struct {
// Attempting to use ComptimeHashMap hits compiler bugs and I'm // Attempting to use ComptimeHashMap hits compiler bugs and I'm
// not sure if we can map strings to *types* in it. // not sure if we can map strings to *types* in it.
if ((!found) and std.mem.eql(u8, &lowered_command_name, command_string)) { if ((!found) and std.mem.eql(u8, &runtime_lowered_command_name, command_string)) {
found = true; found = true;
try self.parseCommandArguments(cmd_struct_type, &tok_it, &cmds); try self.parseCommandArguments(cmd_struct_type, &tok_it, &cmds);
} }

View file

@ -64,13 +64,13 @@ pub const Port = struct {
/// ///
/// Caller owns returned memory. /// Caller owns returned memory.
pub fn setupPorts(ctx: *plugin.Context) ![]Port { pub fn setupPorts(ctx: *plugin.Context) ![]Port {
var world = ctx.world; const world = ctx.world;
const n_ports: u32 = c.lilv_plugin_get_num_ports(ctx.plugin); const n_ports: u32 = c.lilv_plugin_get_num_ports(ctx.plugin);
var ports = try ctx.allocator.alloc(Port, n_ports); var ports = try ctx.allocator.alloc(Port, n_ports);
for (ports, 0..) |_, idx| { for (ports, 0..) |_, idx| {
var port: *Port = &ports[idx]; const port: *Port = &ports[idx];
port.* = Port{ port.* = Port{
.lilv_port = null, .lilv_port = null,
.ptype = .Control, .ptype = .Control,
@ -81,23 +81,23 @@ pub fn setupPorts(ctx: *plugin.Context) ![]Port {
}; };
} }
var values: []f32 = try ctx.allocator.alloc(f32, n_ports); const values: []f32 = try ctx.allocator.alloc(f32, n_ports);
defer ctx.allocator.free(values); defer ctx.allocator.free(values);
c.lilv_plugin_get_port_ranges_float(ctx.plugin, null, null, values.ptr); c.lilv_plugin_get_port_ranges_float(ctx.plugin, null, null, values.ptr);
var lv2_InputPort = c.lilv_new_uri(world, LV2_CORE__InputPort.ptr).?; const lv2_InputPort = c.lilv_new_uri(world, LV2_CORE__InputPort.ptr).?;
//defer std.heap.c_allocator.destroy(lv2_InputPort); //defer std.heap.c_allocator.destroy(lv2_InputPort);
var lv2_OutputPort = c.lilv_new_uri(world, LV2_CORE__OutputPort.ptr).?; const lv2_OutputPort = c.lilv_new_uri(world, LV2_CORE__OutputPort.ptr).?;
//defer std.heap.c_allocator.destroy(lv2_OutputPort); //defer std.heap.c_allocator.destroy(lv2_OutputPort);
var lv2_AudioPort = c.lilv_new_uri(world, LV2_CORE__AudioPort.ptr).?; const lv2_AudioPort = c.lilv_new_uri(world, LV2_CORE__AudioPort.ptr).?;
//defer std.heap.c_allocator.destroy(lv2_AudioPort); //defer std.heap.c_allocator.destroy(lv2_AudioPort);
var lv2_ControlPort = c.lilv_new_uri(world, LV2_CORE__ControlPort.ptr).?; const lv2_ControlPort = c.lilv_new_uri(world, LV2_CORE__ControlPort.ptr).?;
//defer std.heap.c_allocator.destroy(lv2_ControlPort); //defer std.heap.c_allocator.destroy(lv2_ControlPort);
var lv2_connection_string = c.lilv_new_uri(world, LV2_CORE__connectionOptional.ptr).?; const lv2_connection_string = c.lilv_new_uri(world, LV2_CORE__connectionOptional.ptr).?;
//defer std.heap.c_allocator.destroy(lv2_connection_string); //defer std.heap.c_allocator.destroy(lv2_connection_string);
var i: u32 = 0; var i: u32 = 0;

View file

@ -15,7 +15,7 @@ pub const MagickContext = struct {
pub fn init() !MagickContext { pub fn init() !MagickContext {
mc.InitializeMagick(null); mc.InitializeMagick(null);
var wand = mc.NewMagickWand(); const wand = mc.NewMagickWand();
if (wand == null) return error.WandCreateFail; if (wand == null) return error.WandCreateFail;
return MagickContext{ return MagickContext{
@ -38,7 +38,7 @@ fn magickLoad(image: *Image) !MagickContext {
var mctx = try MagickContext.init(); var mctx = try MagickContext.init();
errdefer mctx.deinit(); errdefer mctx.deinit();
var curpath = try image.allocator.dupeZ(u8, image.curpath); const curpath = try image.allocator.dupeZ(u8, image.curpath);
defer image.allocator.free(curpath); defer image.allocator.free(curpath);
log.debug("loading '{s}'", .{curpath}); log.debug("loading '{s}'", .{curpath});
@ -52,8 +52,8 @@ fn magickLoad(image: *Image) !MagickContext {
fn magickSave(image: *Image, wand: *mc.MagickWand) !void { fn magickSave(image: *Image, wand: *mc.MagickWand) !void {
const allocator = image.allocator; const allocator = image.allocator;
var tmpnam = try images.temporaryName(allocator); const tmpnam = try images.temporaryName(allocator);
var c_tmpnam = try allocator.dupeZ(u8, tmpnam); const c_tmpnam = try allocator.dupeZ(u8, tmpnam);
defer allocator.free(c_tmpnam); defer allocator.free(c_tmpnam);
log.debug("\tmagick: saving to '{s}'..", .{c_tmpnam}); log.debug("\tmagick: saving to '{s}'..", .{c_tmpnam});
@ -72,7 +72,7 @@ pub fn runRotate(image: *Image, deg: f32, bgfill: []const u8) !void {
var mctx = try magickLoad(image); var mctx = try magickLoad(image);
defer mctx.deinit(); defer mctx.deinit();
var bg = mc.NewPixelWand(); const bg = mc.NewPixelWand();
defer mc.DestroyPixelWand(bg); defer mc.DestroyPixelWand(bg);
if (mc.PixelSetColor(bg, bgfill.ptr) != 1) if (mc.PixelSetColor(bg, bgfill.ptr) != 1)

View file

@ -30,6 +30,8 @@ fn copyCommandToHeap(allocator: std.mem.Allocator, command: langs.Command, compt
var heap_cmd = try allocator.create(CommandStruct); var heap_cmd = try allocator.create(CommandStruct);
heap_cmd.* = casted.*; heap_cmd.* = casted.*;
log.debug("casted: {}", .{casted});
log.debug("heap_cmd: {}", .{heap_cmd});
return &heap_cmd.base; return &heap_cmd.base;
} }
@ -54,7 +56,7 @@ pub fn doRepl(allocator: std.mem.Allocator, args_it: anytype) !void {
defer lang.deinit(); defer lang.deinit();
if (total_bytes > 0) { if (total_bytes > 0) {
var scri_existing = try allocator.alloc(u8, total_bytes); const scri_existing = try allocator.alloc(u8, total_bytes);
_ = try file_read_opt.?.read(scri_existing); _ = try file_read_opt.?.read(scri_existing);
defer allocator.free(scri_existing); defer allocator.free(scri_existing);
@ -94,7 +96,7 @@ pub fn doRepl(allocator: std.mem.Allocator, args_it: anytype) !void {
defer file.close(); defer file.close();
var out = file.writer(); var out = file.writer();
var stream = &out; const stream = &out;
// since we opened the file for writing, it becomes empty, so, to ensure // since we opened the file for writing, it becomes empty, so, to ensure
// we don't fuck up later on, we print cmds before starting the repl // we don't fuck up later on, we print cmds before starting the repl
@ -118,7 +120,7 @@ pub fn doRepl(allocator: std.mem.Allocator, args_it: anytype) !void {
// run the load command // run the load command
try runner.runCommands(cmds, true); try runner.runCommands(cmds, true);
const wanted_runner: []const u8 = std.os.getenv("SCRITCHER_RUNNER") orelse "ristretto"; const wanted_runner: []const u8 = std.posix.getenv("SCRITCHER_RUNNER") orelse "ristretto";
var runqs_cmd = langs.Command.RunQS{ var runqs_cmd = langs.Command.RunQS{
.base = langs.Command{ .tag = langs.Command.Tag.runqs }, .base = langs.Command{ .tag = langs.Command.Tag.runqs },
@ -136,7 +138,7 @@ pub fn doRepl(allocator: std.mem.Allocator, args_it: anytype) !void {
readline.add_history(rd_line); readline.add_history(rd_line);
//defer std.heap.c_allocator.destroy(rd_line); //defer std.heap.c_allocator.destroy(rd_line);
var line = rd_line[0..std.mem.len(rd_line)]; const line = rd_line[0..std.mem.len(rd_line)];
if (std.mem.eql(u8, line, "push")) { if (std.mem.eql(u8, line, "push")) {
const heap_cmd = switch (current.tag) { const heap_cmd = switch (current.tag) {
@ -250,7 +252,7 @@ fn doRun(allocator: std.mem.Allocator, args_it: anytype) !void {
// sadly, we read it all into memory. such is life // sadly, we read it all into memory. such is life
const total_bytes = try file.getEndPos(); const total_bytes = try file.getEndPos();
var data = try allocator.alloc(u8, total_bytes); const data = try allocator.alloc(u8, total_bytes);
defer allocator.free(data); defer allocator.free(data);
_ = try file.read(data); _ = try file.read(data);

View file

@ -36,7 +36,7 @@ pub const Position = struct {
pub fn seekPos(self: Position, total_size: usize) SeekPos { pub fn seekPos(self: Position, total_size: usize) SeekPos {
std.debug.assert(self.index <= self.split); std.debug.assert(self.index <= self.split);
var tot = total_size / self.split; const tot = total_size / self.split;
return SeekPos{ return SeekPos{
.start = self.index * tot, .start = self.index * tot,
@ -80,7 +80,7 @@ pub const RunContext = struct {
) !RunContext { ) !RunContext {
_ = allocator; // TODO batch RunBuffers? _ = allocator; // TODO batch RunBuffers?
var instance = c.lilv_plugin_instantiate(plugin, @as(f64, 44100), null); const instance = c.lilv_plugin_instantiate(plugin, @as(f64, 44100), null);
errdefer c.lilv_instance_free(instance); errdefer c.lilv_instance_free(instance);
if (instance == null) { if (instance == null) {
@ -102,7 +102,7 @@ pub const RunContext = struct {
var o: usize = 0; var o: usize = 0;
for (ports, 0..) |_, p_idx| { for (ports, 0..) |_, p_idx| {
var p = @as(u32, @intCast(p_idx)); const p = @as(u32, @intCast(p_idx));
var port: *lv2.Port = &ports[p_idx]; var port: *lv2.Port = &ports[p_idx];
switch (port.ptype) { switch (port.ptype) {
@ -134,12 +134,12 @@ pub fn makeContext(allocator: std.mem.Allocator, plugin_uri: []const u8) !Contex
const cstr_plugin_uri = try allocator.dupeZ(u8, plugin_uri); const cstr_plugin_uri = try allocator.dupeZ(u8, plugin_uri);
defer allocator.free(cstr_plugin_uri); defer allocator.free(cstr_plugin_uri);
var world: *c.LilvWorld = c.lilv_world_new().?; const world: *c.LilvWorld = c.lilv_world_new().?;
errdefer c.lilv_world_free(world); errdefer c.lilv_world_free(world);
c.lilv_world_load_all(world); c.lilv_world_load_all(world);
var uri: *c.LilvNode = c.lilv_new_uri(world, cstr_plugin_uri.ptr) orelse { const uri: *c.LilvNode = c.lilv_new_uri(world, cstr_plugin_uri.ptr) orelse {
log.debug("Invalid plugin URI <{s}>", .{plugin_uri}); log.debug("Invalid plugin URI <{s}>", .{plugin_uri});
return ImageError.InvalidPlugin; return ImageError.InvalidPlugin;
}; };
@ -147,7 +147,7 @@ pub fn makeContext(allocator: std.mem.Allocator, plugin_uri: []const u8) !Contex
const plugins: *const c.LilvPlugins = c.lilv_world_get_all_plugins(world).?; const plugins: *const c.LilvPlugins = c.lilv_world_get_all_plugins(world).?;
var plugin: *const c.LilvPlugin = c.lilv_plugins_get_by_uri(plugins, uri) orelse { const plugin: *const c.LilvPlugin = c.lilv_plugins_get_by_uri(plugins, uri) orelse {
log.debug("Plugin <{s}> not found", .{plugin_uri}); log.debug("Plugin <{s}> not found", .{plugin_uri});
return ImageError.UnknownPlugin; return ImageError.UnknownPlugin;
}; };

View file

@ -47,7 +47,7 @@ pub const Runner = struct {
} }
pub fn clone(self: *Runner) !Runner { pub fn clone(self: *Runner) !Runner {
var cloned_image = if (self.image) |image| try image.clone() else null; const cloned_image = if (self.image) |image| try image.clone() else null;
return Runner{ return Runner{
.allocator = self.allocator, .allocator = self.allocator,
.image = cloned_image, .image = cloned_image,
@ -128,12 +128,12 @@ pub const Runner = struct {
fn makeGlitchedPath(self: *Runner) ![]const u8 { fn makeGlitchedPath(self: *Runner) ![]const u8 {
// we want to transform basename, if it is "x.bmp" to "x_gN.bmp", where // we want to transform basename, if it is "x.bmp" to "x_gN.bmp", where
// N is the maximum non-used integer. // N is the maximum non-used integer.
var image = try self.getImage(); const image = try self.getImage();
const basename = std.fs.path.basename(image.path); const basename = std.fs.path.basename(image.path);
const dirname = std.fs.path.dirname(image.path).?; const dirname = std.fs.path.dirname(image.path).?;
var dir = try std.fs.cwd().openIterableDir(dirname, .{}); var dir = try std.fs.cwd().openDir(dirname, .{ .iterate = true });
defer dir.close(); defer dir.close();
const period_idx = std.mem.lastIndexOf(u8, basename, ".").?; const period_idx = std.mem.lastIndexOf(u8, basename, ".").?;
@ -194,7 +194,7 @@ pub const Runner = struct {
try image.saveTo(out_path); try image.saveTo(out_path);
} }
fn runQSCmd(self: *Runner, cmd: lang.Command) !void { fn runQSCmd(self: *Runner, cmd: *lang.Command) !void {
const runqs = cmd.cast(lang.Command.RunQS).?; const runqs = cmd.cast(lang.Command.RunQS).?;
var image = try self.getImage(); var image = try self.getImage();
const out_path = try self.makeGlitchedPath(); const out_path = try self.makeGlitchedPath();
@ -211,11 +211,11 @@ pub const Runner = struct {
_ = try proc.spawnAndWait(); _ = try proc.spawnAndWait();
} }
fn rotateCmd(self: *Runner, cmd: lang.Command) !void { fn rotateCmd(self: *Runner, cmd: *lang.Command) !void {
const rotate_cmd = cmd.cast(lang.Command.Rotate).?; const rotate_cmd = cmd.cast(lang.Command.Rotate).?;
var image = try self.getImage(); const image = try self.getImage();
var c_bgfill = try self.allocator.dupeZ(u8, rotate_cmd.bgfill); const c_bgfill = try self.allocator.dupeZ(u8, rotate_cmd.bgfill);
defer self.allocator.free(c_bgfill); defer self.allocator.free(c_bgfill);
try magick.runRotate(image, rotate_cmd.deg, c_bgfill); try magick.runRotate(image, rotate_cmd.deg, c_bgfill);
@ -255,7 +255,7 @@ pub const Runner = struct {
fn runSingleCommand( fn runSingleCommand(
self: *@This(), self: *@This(),
cmd: lang.Command, cmd: *lang.Command,
comptime tag: lang.Command.Tag, comptime tag: lang.Command.Tag,
) !void { ) !void {
const typ = lang.Command.tagToType(tag); const typ = lang.Command.tagToType(tag);
@ -267,7 +267,7 @@ pub const Runner = struct {
} }
} }
fn runCommand(self: *@This(), cmd: lang.Command) !void { fn runCommand(self: *@This(), cmd: *lang.Command) !void {
switch (cmd.tag) { switch (cmd.tag) {
.noop => {}, .noop => {},
.load => { .load => {
@ -322,7 +322,7 @@ pub const Runner = struct {
_ = debug_flag; _ = debug_flag;
for (cmds.list.items) |cmd| { for (cmds.list.items) |cmd| {
cmd.print(); cmd.print();
try self.runCommand(cmd.*); try self.runCommand(cmd);
} }
} }
}; };
@ -332,7 +332,7 @@ test "running noop" {
var cmds = lang.CommandList.init(allocator); var cmds = lang.CommandList.init(allocator);
defer cmds.deinit(); defer cmds.deinit();
var command = lang.Command{ .tag = .noop }; const command = lang.Command{ .tag = .noop };
var noop = try allocator.create(lang.Command.Noop); var noop = try allocator.create(lang.Command.Noop);
noop.* = lang.Command.Noop{ .base = command }; noop.* = lang.Command.Noop{ .base = command };