ensure sf_seek is called on the out file in copyBytes

This commit is contained in:
Luna 2019-07-15 10:53:23 -03:00
parent 8c2bbee372
commit d12268f028
1 changed files with 24 additions and 11 deletions

View File

@ -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(