From 87556c5b7eed85035cc5a7b3f964f4796749d17e Mon Sep 17 00:00:00 2001 From: Luna Date: Mon, 15 Jul 2019 11:26:41 -0300 Subject: [PATCH] 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)); }