diff --git a/src/image.zig b/src/image.zig index 0634d2b..0b53d09 100644 --- a/src/image.zig +++ b/src/image.zig @@ -41,6 +41,24 @@ fn sopen( return file.?; } +///Read a single frame from a file into an interleaved buffer. +///If more channels are required than are available in the file, the remaining +///channels are distributed in a round-robin fashion (LRLRL). +fn sread(file_opt: ?*c.SNDFILE, file_chans: c_int, buf: []f32) bool { + var file = file_opt.?; + + const n_read: c.sf_count_t = c.sf_readf_float(file, buf.ptr, 1); + const buf_chans = @intCast(c_int, buf.len); + + var i = file_chans - 1; + while (i < buf_chans) : (i += 1) { + //buf[@intCast(usize, i)] = buf[i % file_chans]; + buf[@intCast(usize, i)] = buf[@intCast(usize, @mod(i, file_chans))]; + } + + return n_read == 1; +} + fn getEndPos(path: []const u8) !usize { var file = try std.fs.File.openRead(path); defer file.close(); @@ -204,5 +222,35 @@ pub const Image = struct { std.debug.warn("file end: {}\n", end_pos); const seek_pos = position.seekPos(end_pos); std.debug.warn("start {} end {}\n", seek_pos.start, seek_pos.end); + + var i: usize = 0; + + while (i < end_pos) : (i += 1) { + // if we're still on the bmp header phase, copy bytes + // if we aren't in the range from seek_pos, copy bytes + // if we are in the range, run lilv plugin + + // TODO speed this up by not reading byte-by-byte. + // TODO check value of this + _ = c.sf_readf_float(self.sndfile, rctx.in_buf.ptr, 1); + + if (i < BMPHeaderSize or !seek_pos.contains(i)) { + _ = c.sf_writef_float(out_file, rctx.in_buf.ptr, 1); + } else { + lv2.lilv_instance_run(rctx.instance, 1); + + var count = c.sf_writef_float(out_file, rctx.out_buf.ptr, 1); + if (count != 1) { + std.debug.warn( + "Failed to write to output file at idx {}.\n", + i, + ); + + // maybe we return an error? + } + } + } + + _ = c.sf_close(out_file); } }; diff --git a/src/plugin.zig b/src/plugin.zig index 661fdec..45ea8d2 100644 --- a/src/plugin.zig +++ b/src/plugin.zig @@ -21,6 +21,10 @@ pub const ParamList = std.ArrayList(Param); pub const SeekPos = struct { start: usize, end: usize, + + pub fn contains(self: SeekPos, idx: usize) bool { + return (self.start <= idx) and (idx <= self.end); + } }; /// Represents a relative position in the image