make copyBytes allocate its own buffer

This commit is contained in:
Luna 2019-07-14 19:01:50 -03:00
parent b59f937d00
commit 8c2bbee372

View file

@ -54,12 +54,6 @@ fn swrite(file: *c.SNDFILE, buf: [*]f32, frames: i64) !void {
}
}
fn getEndPos(path: []const u8) !usize {
var file = try std.fs.File.openRead(path);
defer file.close();
return file.getEndPos();
}
fn temporaryName(allocator: *std.mem.Allocator) ![]u8 {
const template_start = "/temp/temp_";
const template = "/tmp/temp_XXXXXXXX";
@ -125,6 +119,7 @@ pub const Image = struct {
var image = try allocator.create(Image);
std.debug.assert(in_fmt.frames > i64(0));
std.debug.assert(in_fmt.seekable == i32(1));
image.* = Image{
.allocator = allocator,
@ -167,10 +162,12 @@ pub const Image = struct {
fn copyBytes(
self: *Image,
out_file: *c.SNDFILE,
buf: []f32,
start: usize,
end: usize,
) !void {
var buf = try self.allocator.alloc(f32, BufferSize);
defer self.allocator.free(buf);
const total_bytes = end - start;
var i: usize = start;
@ -287,13 +284,9 @@ pub const Image = struct {
// - plugin
// - post-plugin
var file_copy_buf = try self.allocator.alloc(f32, BufferSize);
defer self.allocator.free(file_copy_buf);
// pre-plugin copy, merged with bmp header copy
try self.copyBytes(
out_file,
file_copy_buf,
usize(0),
seek_pos.start,
);
@ -322,7 +315,6 @@ pub const Image = struct {
// post-plugin copy
try self.copyBytes(
out_file,
file_copy_buf,
seek_pos.end + 1,
self.frames,
);
@ -371,13 +363,9 @@ pub const Image = struct {
// - CUSTOM plugin
// - post-plugin
var file_copy_buf = try self.allocator.alloc(f32, BufferSize);
defer self.allocator.free(file_copy_buf);
// pre-plugin copy, merged with bmp header copy
try self.copyBytes(
out_file,
file_copy_buf,
usize(0),
seek_pos.start,
);
@ -406,7 +394,6 @@ pub const Image = struct {
// post-plugin copy
try self.copyBytes(
out_file,
file_copy_buf,
seek_pos.end + 1,
self.frames,
);