diff --git a/src/custom.zig b/src/custom.zig index 7d3eb23..4c8c611 100644 --- a/src/custom.zig +++ b/src/custom.zig @@ -138,8 +138,7 @@ pub const Embed = struct { filepath: []const u8, sndfile: *c.SNDFILE = undefined, - - // TODO add its own read buffers from snd file + buf: []f32 = undefined, pub fn init(allocator: *std.mem.Allocator, filepath: []const u8) @This() { return Embed{ @@ -157,6 +156,7 @@ pub const Embed = struct { .sections = c_int(0), .seekable = c_int(0), }; + self.sndfile = try image.sopen( self.allocator, self.filepath, @@ -164,12 +164,29 @@ pub const Embed = struct { &in_fmt, ); - // TODO allocate 1 or 2 in read buffer, but only use 1 + self.buf = try self.allocator.alloc(f32, @intCast(usize, in_fmt.channels)); } pub fn deinit(self: *@This()) void {} pub fn run(self: *@This(), bufs: *RunBuffers) void { - bufs.out[0] = 0; + const read_bytes = c.sf_readf_float(self.sndfile, self.buf.ptr, 1); + + if (read_bytes == 0) { + std.debug.warn("WARN! reached EOF for embed\n"); + return; + } + + if (read_bytes < 0) { + const st: i32 = c.sf_error(self.sndfile); + std.debug.warn( + "Failed to read {} ({})\n", + self.filepath, + c.sf_error_number(st), + ); + return; + } + + bufs.out[0] = bufs.in[0] + self.buf[0]; } };