From c5a2e317c451b46cdb974e69b54f8eee2c686d39 Mon Sep 17 00:00:00 2001 From: Luna Date: Mon, 15 Jul 2019 10:58:45 -0300 Subject: [PATCH 1/2] remove error part of sseek() --- src/image.zig | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/src/image.zig b/src/image.zig index 2aabd98..491e994 100644 --- a/src/image.zig +++ b/src/image.zig @@ -54,12 +54,10 @@ fn swrite(file: *c.SNDFILE, buf: [*]f32, frames: i64) !void { } } -fn sseek(file: *c.SNDFILE, offset: usize) !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; } } @@ -187,8 +185,8 @@ pub const Image = struct { // we do sf_seek() calls to make sure we are actually on the start // and actually end at the end position for the file. - try sseek(self.sndfile, start); - try sseek(out_file, start); + sseek(self.sndfile, start); + sseek(out_file, start); while (i <= end) : (i += buf.len) { std.debug.warn("i={}, buf.len={}, end={}\n", i, buf.len, end); @@ -212,8 +210,8 @@ pub const Image = struct { try swrite(out_file, view.ptr, @intCast(i64, view.len)); } - try sseek(self.sndfile, end); - try sseek(out_file, end); + sseek(self.sndfile, end); + sseek(out_file, end); } fn getSeekPos(self: *Image, position: plugins.Position) plugins.SeekPos { @@ -304,7 +302,7 @@ pub const Image = struct { seek_pos.start, ); - try sseek(self.sndfile, seek_pos.start); + 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); @@ -323,7 +321,7 @@ pub const Image = struct { try swrite(out_file, outbuf.ptr, 1); } - try sseek(self.sndfile, seek_pos.end); + sseek(self.sndfile, seek_pos.end); // post-plugin copy try self.copyBytes( @@ -368,7 +366,7 @@ pub const Image = struct { const seek_pos = self.getSeekPos(position); // make sure we start from 0 - try sseek(self.sndfile, 0); + sseek(self.sndfile, 0); // there are four main stages: // - the bmp header copy @@ -383,7 +381,7 @@ pub const Image = struct { seek_pos.start, ); - try sseek(self.sndfile, seek_pos.start); + 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); @@ -402,7 +400,7 @@ pub const Image = struct { try swrite(out_file, bufs.out.ptr, 1); } - try sseek(self.sndfile, seek_pos.end); + sseek(self.sndfile, seek_pos.end); // post-plugin copy try self.copyBytes( From 87556c5b7eed85035cc5a7b3f964f4796749d17e Mon Sep 17 00:00:00 2001 From: Luna Date: Mon, 15 Jul 2019 11:26:41 -0300 Subject: [PATCH 2/2] simplify copyBytes excess calc and do sseek() --- examples/middle_amp.scri | 2 +- src/image.zig | 16 +++++++--------- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/examples/middle_amp.scri b/examples/middle_amp.scri index f159c8e..a5928fd 100644 --- a/examples/middle_amp.scri +++ b/examples/middle_amp.scri @@ -1,3 +1,3 @@ load :0; -amp 3 1 10; +amp 3 1 20; quicksave; diff --git a/src/image.zig b/src/image.zig index 491e994..1523133 100644 --- a/src/image.zig +++ b/src/image.zig @@ -190,19 +190,17 @@ pub const Image = struct { while (i <= end) : (i += buf.len) { std.debug.warn("i={}, buf.len={}, end={}\n", i, buf.len, end); - const excess = @intCast(i64, i + buf.len) - @intCast(i64, end); + sseek(self.sndfile, i); + sseek(out_file, i); + + const bytes_until_end = end - i; var read_bytes: i64 = undefined; var view: []f32 = buf[0..buf.len]; - if (excess > 0) { - std.debug.warn( - "excess of {} bytes, reading {} instead\n", - excess, - @intCast(i64, buf.len) - excess, - ); - read_bytes = c.sf_readf_float(self.sndfile, buf.ptr, @intCast(i64, buf.len) - excess); - view = buf[0..@intCast(usize, excess)]; + if (bytes_until_end < buf.len) { + read_bytes = c.sf_readf_float(self.sndfile, buf.ptr, @intCast(i64, bytes_until_end)); + view = buf[0..bytes_until_end]; } else { read_bytes = c.sf_readf_float(self.sndfile, buf.ptr, @intCast(i64, buf.len)); }