diff --git a/examples/middle_amp.scri b/examples/middle_amp.scri index a5928fd..f159c8e 100644 --- a/examples/middle_amp.scri +++ b/examples/middle_amp.scri @@ -1,3 +1,3 @@ load :0; -amp 3 1 20; +amp 3 1 10; quicksave; diff --git a/src/image.zig b/src/image.zig index 1523133..2aabd98 100644 --- a/src/image.zig +++ b/src/image.zig @@ -54,10 +54,12 @@ 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; } } @@ -185,22 +187,24 @@ 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. - sseek(self.sndfile, start); - sseek(out_file, start); + 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); - sseek(self.sndfile, i); - sseek(out_file, i); - - const bytes_until_end = end - i; + const excess = @intCast(i64, i + buf.len) - @intCast(i64, end); var read_bytes: i64 = undefined; var view: []f32 = buf[0..buf.len]; - 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]; + 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)]; } else { read_bytes = c.sf_readf_float(self.sndfile, buf.ptr, @intCast(i64, buf.len)); } @@ -208,8 +212,8 @@ pub const Image = struct { try swrite(out_file, view.ptr, @intCast(i64, view.len)); } - sseek(self.sndfile, end); - sseek(out_file, end); + try sseek(self.sndfile, end); + try sseek(out_file, end); } fn getSeekPos(self: *Image, position: plugins.Position) plugins.SeekPos { @@ -300,7 +304,7 @@ pub const Image = struct { seek_pos.start, ); - sseek(self.sndfile, seek_pos.start); + 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); @@ -319,7 +323,7 @@ pub const Image = struct { try swrite(out_file, outbuf.ptr, 1); } - sseek(self.sndfile, seek_pos.end); + try sseek(self.sndfile, seek_pos.end); // post-plugin copy try self.copyBytes( @@ -364,7 +368,7 @@ pub const Image = struct { const seek_pos = self.getSeekPos(position); // make sure we start from 0 - sseek(self.sndfile, 0); + try sseek(self.sndfile, 0); // there are four main stages: // - the bmp header copy @@ -379,7 +383,7 @@ pub const Image = struct { seek_pos.start, ); - sseek(self.sndfile, seek_pos.start); + 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); @@ -398,7 +402,7 @@ pub const Image = struct { try swrite(out_file, bufs.out.ptr, 1); } - sseek(self.sndfile, seek_pos.end); + try sseek(self.sndfile, seek_pos.end); // post-plugin copy try self.copyBytes(