remove heap allocation for RunBuffers
This commit is contained in:
parent
5a0e6934e7
commit
519eb51206
2 changed files with 32 additions and 41 deletions
|
@ -370,18 +370,25 @@ pub const Image = struct {
|
||||||
var i: usize = seek_pos.start;
|
var i: usize = seek_pos.start;
|
||||||
std.debug.warn("\tseek pos start: {} end: {}\n", .{ seek_pos.start, seek_pos.end });
|
std.debug.warn("\tseek pos start: {} end: {}\n", .{ seek_pos.start, seek_pos.end });
|
||||||
|
|
||||||
var inbuf = rctx.buffers.in;
|
var inbuf = &rctx.buffers.in;
|
||||||
var outbuf = rctx.buffers.out;
|
var outbuf = &rctx.buffers.out;
|
||||||
|
|
||||||
while (i <= seek_pos.end) : (i += 1) {
|
while (i <= seek_pos.end) : (i += 1) {
|
||||||
const read_bytes = c.sf_readf_float(self.sndfile, inbuf.ptr, 1);
|
inbuf[0] = 0;
|
||||||
|
inbuf[1] = 0;
|
||||||
|
|
||||||
|
const read_bytes = c.sf_readf_float(self.sndfile, inbuf, 1);
|
||||||
if (read_bytes == 0) {
|
if (read_bytes == 0) {
|
||||||
std.debug.warn("WARN! reached EOF at idx={}\n", .{i});
|
std.debug.warn("WARN! reached EOF at idx={}\n", .{i});
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// trick plugins into having correct stereo signal from
|
||||||
|
// my mono input
|
||||||
|
inbuf[1] = inbuf[0];
|
||||||
|
|
||||||
lv2.lilv_instance_run(rctx.instance, 1);
|
lv2.lilv_instance_run(rctx.instance, 1);
|
||||||
try swrite(out_file, outbuf.ptr, 1);
|
try swrite(out_file, outbuf, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
sseek(self.sndfile, seek_pos.end);
|
sseek(self.sndfile, seek_pos.end);
|
||||||
|
@ -439,9 +446,7 @@ pub const Image = struct {
|
||||||
var out_fmt = mkSfInfo();
|
var out_fmt = mkSfInfo();
|
||||||
var out_file = try sopen(self.allocator, tmpnam, c.SFM_WRITE, &out_fmt);
|
var out_file = try sopen(self.allocator, tmpnam, c.SFM_WRITE, &out_fmt);
|
||||||
|
|
||||||
var bufs = try plugins.RunBuffers.init(self.allocator);
|
var bufs = plugins.RunBuffers{};
|
||||||
defer bufs.deinit();
|
|
||||||
|
|
||||||
const seek_pos = self.getSeekPos(position);
|
const seek_pos = self.getSeekPos(position);
|
||||||
|
|
||||||
// make sure we start from 0
|
// make sure we start from 0
|
||||||
|
@ -465,18 +470,18 @@ pub const Image = struct {
|
||||||
var i: usize = seek_pos.start;
|
var i: usize = seek_pos.start;
|
||||||
std.debug.warn("\tseek pos start: {} end: {}\n", .{ seek_pos.start, seek_pos.end });
|
std.debug.warn("\tseek pos start: {} end: {}\n", .{ seek_pos.start, seek_pos.end });
|
||||||
|
|
||||||
var inbuf = bufs.in;
|
var inbuf = &bufs.in;
|
||||||
var outbuf = bufs.out;
|
var outbuf = &bufs.out;
|
||||||
|
|
||||||
while (i <= seek_pos.end) : (i += 1) {
|
while (i <= seek_pos.end) : (i += 1) {
|
||||||
const read_bytes = c.sf_readf_float(self.sndfile, bufs.in.ptr, 1);
|
const read_bytes = c.sf_readf_float(self.sndfile, inbuf, 1);
|
||||||
if (read_bytes == 0) {
|
if (read_bytes == 0) {
|
||||||
std.debug.warn("WARN! reached EOF at idx={}\n", .{i});
|
std.debug.warn("WARN! reached EOF at idx={}\n", .{i});
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
plugin.run(&bufs);
|
plugin.run(&bufs);
|
||||||
try swrite(out_file, bufs.out.ptr, 1);
|
try swrite(out_file, outbuf, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
sseek(self.sndfile, seek_pos.end);
|
sseek(self.sndfile, seek_pos.end);
|
||||||
|
|
|
@ -61,29 +61,11 @@ pub const Context = struct {
|
||||||
|
|
||||||
/// Specific run context for non-plugins.
|
/// Specific run context for non-plugins.
|
||||||
pub const RunBuffers = struct {
|
pub const RunBuffers = struct {
|
||||||
allocator: *std.mem.Allocator,
|
// we use [2]f32 to account for stereo plugins, however
|
||||||
|
// we only use in_buf[0] and out_buf[0], and don't use the
|
||||||
in: []f32,
|
// (supposedly) right side of neither input or output.
|
||||||
out: []f32,
|
in: [2]f32 = [_]f32{0} ** 2,
|
||||||
|
out: [2]f32 = [_]f32{0} ** 2,
|
||||||
pub fn init(allocator: *std.mem.Allocator) !RunBuffers {
|
|
||||||
// we allocate []f32 with size 2 to account for stereo plugins, however
|
|
||||||
// we only use &in_buf[0] and &out_buf[0], and don't use the
|
|
||||||
// (supposedly) right side of neither input or output.
|
|
||||||
var in_buf = try allocator.alloc(f32, 2);
|
|
||||||
std.mem.secureZero(f32, in_buf);
|
|
||||||
|
|
||||||
return RunBuffers{
|
|
||||||
.allocator = allocator,
|
|
||||||
.in = in_buf,
|
|
||||||
.out = try allocator.alloc(f32, 2),
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn deinit(self: *RunBuffers) void {
|
|
||||||
self.allocator.free(self.in);
|
|
||||||
self.allocator.free(self.out);
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/// Represents the specific run context of plugin instantation.
|
/// Represents the specific run context of plugin instantation.
|
||||||
|
@ -103,23 +85,19 @@ pub const RunContext = struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
return RunContext{
|
return RunContext{
|
||||||
.buffers = try RunBuffers.init(allocator),
|
.buffers = RunBuffers{},
|
||||||
.instance = instance.?,
|
.instance = instance.?,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn deinit(self: *RunContext) void {
|
pub fn deinit(self: *RunContext) void {
|
||||||
c.lilv_instance_free(self.instance);
|
c.lilv_instance_free(self.instance);
|
||||||
self.buffers.deinit();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn connectPorts(self: *RunContext, ports: []lv2.Port) void {
|
pub fn connectPorts(self: *RunContext, ports: []lv2.Port) void {
|
||||||
var i: usize = 0;
|
var i: usize = 0;
|
||||||
var o: usize = 0;
|
var o: usize = 0;
|
||||||
|
|
||||||
var in_buf = self.buffers.in;
|
|
||||||
var out_buf = self.buffers.out;
|
|
||||||
|
|
||||||
for (ports) |_, p_idx| {
|
for (ports) |_, p_idx| {
|
||||||
var p = @intCast(u32, p_idx);
|
var p = @intCast(u32, p_idx);
|
||||||
var port: *lv2.Port = &ports[p_idx];
|
var port: *lv2.Port = &ports[p_idx];
|
||||||
|
@ -128,10 +106,18 @@ pub const RunContext = struct {
|
||||||
.Control => lv2.lilv_instance_connect_port(self.instance, p, &port.value),
|
.Control => lv2.lilv_instance_connect_port(self.instance, p, &port.value),
|
||||||
.Audio => blk: {
|
.Audio => blk: {
|
||||||
if (port.is_input) {
|
if (port.is_input) {
|
||||||
lv2.lilv_instance_connect_port(self.instance, p, &in_buf[i]);
|
lv2.lilv_instance_connect_port(
|
||||||
|
self.instance,
|
||||||
|
p,
|
||||||
|
&self.buffers.in[i],
|
||||||
|
);
|
||||||
i += 1;
|
i += 1;
|
||||||
} else {
|
} else {
|
||||||
lv2.lilv_instance_connect_port(self.instance, p, &out_buf[o]);
|
lv2.lilv_instance_connect_port(
|
||||||
|
self.instance,
|
||||||
|
p,
|
||||||
|
&self.buffers.out[o],
|
||||||
|
);
|
||||||
o += 1;
|
o += 1;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue