diff --git a/src/image.zig b/src/image.zig index 00b83b8..2aabd98 100644 --- a/src/image.zig +++ b/src/image.zig @@ -54,6 +54,21 @@ fn swrite(file: *c.SNDFILE, buf: [*]f32, frames: i64) !void { } } +fn sseek(file: *c.SNDFILE, offset: usize) !void { + const frames = c.sf_seek(file, @intCast(i64, offset), c.SEEK_SET); + + if (frames != @intCast(i64, offset)) { + std.debug.warn("failed to seek to {}\n", offset); + return error.SeekFail; + } +} + +fn sf_tell(file: *c.SNDFILE) i64 { + var frames = c.sf_seek(file, 0, c.SEEK_CUR); + std.debug.warn("\t\t{} frames\n", frames); + return -frames; +} + fn temporaryName(allocator: *std.mem.Allocator) ![]u8 { const template_start = "/temp/temp_"; const template = "/tmp/temp_XXXXXXXX"; @@ -168,12 +183,12 @@ pub const Image = struct { var buf = try self.allocator.alloc(f32, BufferSize); defer self.allocator.free(buf); - const total_bytes = end - start; var i: usize = start; // we do sf_seek() calls to make sure we are actually on the start // and actually end at the end position for the file. - _ = c.sf_seek(self.sndfile, @intCast(i64, start), c.SEEK_SET); + try sseek(self.sndfile, start); + try sseek(out_file, start); while (i <= end) : (i += buf.len) { std.debug.warn("i={}, buf.len={}, end={}\n", i, buf.len, end); @@ -197,7 +212,8 @@ pub const Image = struct { try swrite(out_file, view.ptr, @intCast(i64, view.len)); } - _ = c.sf_seek(self.sndfile, @intCast(i64, end), c.SEEK_SET); + try sseek(self.sndfile, end); + try sseek(out_file, end); } fn getSeekPos(self: *Image, position: plugins.Position) plugins.SeekPos { @@ -275,9 +291,6 @@ pub const Image = struct { const seek_pos = self.getSeekPos(position); - // make sure we start from 0 - _ = c.sf_seek(self.sndfile, 0, c.SEEK_SET); - // there are four main stages: // - the bmp header copy // - pre-plugin @@ -291,7 +304,7 @@ pub const Image = struct { seek_pos.start, ); - _ = c.sf_seek(self.sndfile, @intCast(i64, seek_pos.start), c.SEEK_SET); + try sseek(self.sndfile, seek_pos.start); var i: usize = seek_pos.start; std.debug.warn("\tseek pos start: {} end: {}\n", seek_pos.start, seek_pos.end); @@ -310,7 +323,7 @@ pub const Image = struct { try swrite(out_file, outbuf.ptr, 1); } - _ = c.sf_seek(self.sndfile, @intCast(i64, seek_pos.end), c.SEEK_SET); + try sseek(self.sndfile, seek_pos.end); // post-plugin copy try self.copyBytes( @@ -355,7 +368,7 @@ pub const Image = struct { const seek_pos = self.getSeekPos(position); // make sure we start from 0 - _ = c.sf_seek(self.sndfile, 0, c.SEEK_SET); + try sseek(self.sndfile, 0); // there are four main stages: // - the bmp header copy @@ -370,7 +383,7 @@ pub const Image = struct { seek_pos.start, ); - _ = c.sf_seek(self.sndfile, @intCast(i64, seek_pos.start), c.SEEK_SET); + try sseek(self.sndfile, seek_pos.start); var i: usize = seek_pos.start; std.debug.warn("\tseek pos start: {} end: {}\n", seek_pos.start, seek_pos.end); @@ -389,7 +402,7 @@ pub const Image = struct { try swrite(out_file, bufs.out.ptr, 1); } - _ = c.sf_seek(self.sndfile, @intCast(i64, seek_pos.end), c.SEEK_SET); + try sseek(self.sndfile, seek_pos.end); // post-plugin copy try self.copyBytes(