From b59f937d0040f628fba1fd07094d4346aed6d495 Mon Sep 17 00:00:00 2001 From: Luna Date: Sun, 14 Jul 2019 18:54:48 -0300 Subject: [PATCH 1/2] decomission getEndPos() in favor of SF_INFO.frames --- examples/middle_amp.scri | 7 +------ src/image.zig | 31 ++++++++++++++++++++----------- 2 files changed, 21 insertions(+), 17 deletions(-) diff --git a/examples/middle_amp.scri b/examples/middle_amp.scri index a1ed655..f159c8e 100644 --- a/examples/middle_amp.scri +++ b/examples/middle_amp.scri @@ -1,8 +1,3 @@ load :0; -amp 80 70 10; -amp 80 73 20; -amp 80 75 20; -amp 80 76 20; -amp 7 4 2; -amp 5 1 8; +amp 3 1 10; quicksave; diff --git a/src/image.zig b/src/image.zig index 020d893..7ba72b1 100644 --- a/src/image.zig +++ b/src/image.zig @@ -108,24 +108,30 @@ pub const Image = struct { /// Pointer to the underlying libsndfile's SNDFILE struct. sndfile: *c.SNDFILE, + /// Current sound file's framecount. + frames: usize, + /// The original image file path. path: []const u8, /// Represents the current path being worked on. curpath: []const u8, - /// Open a BMP file. + /// Open a BMP image for later. pub fn open(allocator: *std.mem.Allocator, path: []const u8) !*Image { var in_fmt = mkSfInfo(); var sndfile = try sopen(allocator, path, c.SFM_READ, &in_fmt); var image = try allocator.create(Image); + std.debug.assert(in_fmt.frames > i64(0)); + image.* = Image{ .allocator = allocator, .sndfile = sndfile, .path = path, .curpath = path, + .frames = @intCast(usize, in_fmt.frames), }; return image; @@ -197,6 +203,13 @@ pub const Image = struct { _ = c.sf_seek(self.sndfile, @intCast(i64, end), c.SEEK_SET); } + fn getSeekPos(self: *Image, position: plugins.Position) plugins.SeekPos { + const file_end = self.frames; + var seek_pos = position.seekPos(file_end); + std.debug.warn("\tstart {} end {}\n", seek_pos.start, seek_pos.end); + return seek_pos; + } + /// Run a plugin over the image. /// This setups a new lilv world/plugin among other things. /// The internal SNDFILE pointer is modified to point to the output of the @@ -263,9 +276,7 @@ pub const Image = struct { // just copy the original image and the part where we run the plugin // over the image. - const file_end = try getEndPos(self.curpath); - const seek_pos = position.seekPos(file_end); - std.debug.warn("\tstart {} end {}\n", seek_pos.start, seek_pos.end); + const seek_pos = self.getSeekPos(position); // make sure we start from 0 _ = c.sf_seek(self.sndfile, 0, c.SEEK_SET); @@ -284,7 +295,7 @@ pub const Image = struct { out_file, file_copy_buf, usize(0), - seek_pos.start + @mod(seek_pos.start, BufferSize), + seek_pos.start, ); _ = c.sf_seek(self.sndfile, @intCast(i64, seek_pos.start), c.SEEK_SET); @@ -313,7 +324,7 @@ pub const Image = struct { out_file, file_copy_buf, seek_pos.end + 1, - file_end + @mod(file_end, BufferSize), + self.frames, ); c.sf_write_sync(out_file); @@ -349,9 +360,7 @@ pub const Image = struct { var bufs = try plugins.RunBuffers.init(self.allocator); defer bufs.deinit(); - const file_end = try getEndPos(self.curpath); - const seek_pos = position.seekPos(file_end); - std.debug.warn("\tstart {} end {}\n", seek_pos.start, seek_pos.end); + const seek_pos = self.getSeekPos(position); // make sure we start from 0 _ = c.sf_seek(self.sndfile, 0, c.SEEK_SET); @@ -370,7 +379,7 @@ pub const Image = struct { out_file, file_copy_buf, usize(0), - seek_pos.start + @mod(seek_pos.start, BufferSize), + seek_pos.start, ); _ = c.sf_seek(self.sndfile, @intCast(i64, seek_pos.start), c.SEEK_SET); @@ -399,7 +408,7 @@ pub const Image = struct { out_file, file_copy_buf, seek_pos.end + 1, - file_end + @mod(file_end, BufferSize), + self.frames, ); c.sf_write_sync(out_file); From 8c2bbee372c5ae33716ea728ba65d0d2cb04a361 Mon Sep 17 00:00:00 2001 From: Luna Date: Sun, 14 Jul 2019 19:01:50 -0300 Subject: [PATCH 2/2] make copyBytes allocate its own buffer --- src/image.zig | 21 ++++----------------- 1 file changed, 4 insertions(+), 17 deletions(-) diff --git a/src/image.zig b/src/image.zig index 7ba72b1..00b83b8 100644 --- a/src/image.zig +++ b/src/image.zig @@ -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, );