From 26b7ed131028ddd4909dbc5d87b5c37f459159aa Mon Sep 17 00:00:00 2001 From: aprilnightk Date: Mon, 15 Sep 2025 21:58:21 +0300 Subject: [PATCH] slight intermediary optimization --- zigsonnum/activity.zig | 97 ++++++++++++++++++++++------------------- zigsonnum/sonnum.zig | 47 ++++++++++++-------- zigsonnum/soundnode.zig | 10 +++++ 3 files changed, 92 insertions(+), 62 deletions(-) diff --git a/zigsonnum/activity.zig b/zigsonnum/activity.zig index 7db65d7..36546be 100644 --- a/zigsonnum/activity.zig +++ b/zigsonnum/activity.zig @@ -104,38 +104,41 @@ pub const Activity = struct { for (self.soundnode.wire_in.items, 0..) |wired_sn, i| { - const relayed_r_amp = wired_sn.fab.get_r_amp(current_tick); - self.soundnode.fab.add_r_amp(relayed_r_amp); + if (wired_sn.active == 1) { + const relayed_r_amp = wired_sn.fab.get_r_amp(current_tick); + self.soundnode.fab.add_r_amp(relayed_r_amp); + } _ = i; } for (self.soundnode.air_in.items, 0..) |aired_sn, i| { - const dist: f64 = self.soundnode.distance(aired_sn); - - var b_sample_tick: u32 = 0; - var n_sample_tick: u32 = 0; - - const tck = @as(f64, @floatFromInt(current_tick)) - (128.571428 * dist); + if (aired_sn.active == 1) { + const dist: f64 = self.soundnode.distance(aired_sn); - if (tck > 0) { + var b_sample_tick: u32 = 0; + var n_sample_tick: u32 = 0; - b_sample_tick = @intFromFloat( @floor(tck) ); - n_sample_tick = b_sample_tick + 1; - - const attenuation: f64 = @as(f64, std.math.exp(-(dist / 100.0))); - - const b_r_amp = aired_sn.fab.get_r_amp(b_sample_tick); - const n_r_amp = aired_sn.fab.get_r_amp(n_sample_tick); - - const tick_diff = tck - @as(f64, @floatFromInt(b_sample_tick)); - const relayed_r_amp = (b_r_amp + (tick_diff * (n_r_amp - b_r_amp))) * attenuation; - - self.soundnode.fab.add_r_amp(relayed_r_amp); - - } + const tck = @as(f64, @floatFromInt(current_tick)) - (128.571428 * dist); + if (tck > 0) { + + b_sample_tick = @intFromFloat( @floor(tck) ); + n_sample_tick = b_sample_tick + 1; + + const attenuation: f64 = @as(f64, std.math.exp(-(dist / 100.0))); + + const b_r_amp = aired_sn.fab.get_r_amp(b_sample_tick); + const n_r_amp = aired_sn.fab.get_r_amp(n_sample_tick); + + const tick_diff = tck - @as(f64, @floatFromInt(b_sample_tick)); + const relayed_r_amp = (b_r_amp + (tick_diff * (n_r_amp - b_r_amp))) * attenuation; + + self.soundnode.fab.add_r_amp(relayed_r_amp); + + } + } _ = i; } @@ -439,38 +442,42 @@ pub const Activity = struct { for (self.soundnode.wire_in.items, 0..) |wired_sn, i| { - const relayed_r_amp = wired_sn.fab.get_r_amp(current_tick); - srcamp += relayed_r_amp; + if (wired_sn.active == 1) { + const relayed_r_amp = wired_sn.fab.get_r_amp(current_tick); + srcamp += relayed_r_amp; + } _ = i; } for (self.soundnode.air_in.items, 0..) |aired_sn, i| { - const dist: f64 = self.soundnode.distance(aired_sn); + if (aired_sn.active == 1) { - var b_sample_tick: u32 = 0; - var n_sample_tick: u32 = 0; - - const tck = @as(f64, @floatFromInt(current_tick)) - (128.571428 * dist); + const dist: f64 = self.soundnode.distance(aired_sn); - if (tck > 0) { + var b_sample_tick: u32 = 0; + var n_sample_tick: u32 = 0; - b_sample_tick = @intFromFloat( @floor(tck) ); - n_sample_tick = b_sample_tick + 1; - - const attenuation: f64 = @as(f64, std.math.exp(-(dist / 100.0))); - - const b_r_amp = aired_sn.fab.get_r_amp(b_sample_tick); - const n_r_amp = aired_sn.fab.get_r_amp(n_sample_tick); - - const tick_diff = tck - @as(f64, @floatFromInt(b_sample_tick)); - const relayed_r_amp = (b_r_amp + (tick_diff * (n_r_amp - b_r_amp))) * attenuation; - - srcamp += relayed_r_amp; - - } + const tck = @as(f64, @floatFromInt(current_tick)) - (128.571428 * dist); + if (tck > 0) { + + b_sample_tick = @intFromFloat( @floor(tck) ); + n_sample_tick = b_sample_tick + 1; + + const attenuation: f64 = @as(f64, std.math.exp(-(dist / 100.0))); + + const b_r_amp = aired_sn.fab.get_r_amp(b_sample_tick); + const n_r_amp = aired_sn.fab.get_r_amp(n_sample_tick); + + const tick_diff = tck - @as(f64, @floatFromInt(b_sample_tick)); + const relayed_r_amp = (b_r_amp + (tick_diff * (n_r_amp - b_r_amp))) * attenuation; + + srcamp += relayed_r_amp; + + } + } _ = i; } diff --git a/zigsonnum/sonnum.zig b/zigsonnum/sonnum.zig index 6933c14..b35f251 100644 --- a/zigsonnum/sonnum.zig +++ b/zigsonnum/sonnum.zig @@ -26,7 +26,7 @@ pub fn main() !void { // Creating a list of soundnodes var soundnodes = ArrayList(*SoundNode).init(allocator); defer soundnodes.deinit(); - + // Determining length of resulting audio in ticks const start_tick: u32 = 0; var end_tick: u32 = 44100 * 10; @@ -175,6 +175,7 @@ pub fn main() !void { //print("Wired nodes at tick {d}\n", .{tick}); try trg.wire_in.append(src); + try src.wire_targets.append(trg); }, 2 => { @@ -183,6 +184,7 @@ pub fn main() !void { //print("Aired nodes at tick {d}\n", .{tick}); try trg.air_in.append(src); + try src.air_targets.append(trg); }, 3 => { @@ -204,6 +206,10 @@ pub fn main() !void { //print("Set activity {d} for node {d} s at tick {d}\n", .{opcode, src_node, tick}); try src.activities.append(a); + if (src.activities.items.len == 1) { + src.active = 1; + } + }, } @@ -218,26 +224,33 @@ pub fn main() !void { for (soundnodes.items[2..], 0..) |soundnode, i| { - var toremove = ArrayList(usize).init(allocator); - defer toremove.deinit(); + if (soundnode.active == 1) { - for (soundnode.activities.items, 0..) |activity, j| { - - if (tick <= activity.end_tick and tick >= activity.start_tick) { - try activity.do(); - } else if (tick >= activity.end_tick) { - try toremove.append(j); + var toremove = ArrayList(usize).init(allocator); + defer toremove.deinit(); + + for (soundnode.activities.items, 0..) |activity, j| { + + if (tick <= activity.end_tick and tick >= activity.start_tick) { + try activity.do(); + } else if (tick >= activity.end_tick) { + try toremove.append(j); + } } + + var j: usize = toremove.items.len; + + while (j > 0) { + j -= 1; + const activity_ind = toremove.items[j]; + _ = soundnode.activities.swapRemove(activity_ind); + if (soundnode.activities.items.len == 0) { + soundnode.active = 0; + } + } + } - var j: usize = toremove.items.len; - - while (j > 0) { - j -= 1; - const activity_ind = toremove.items[j]; - _ = soundnode.activities.swapRemove(activity_ind); - } - soundnode.fab.increment_tick(); _ = i; } diff --git a/zigsonnum/soundnode.zig b/zigsonnum/soundnode.zig index da2ddd2..cbd5de7 100644 --- a/zigsonnum/soundnode.zig +++ b/zigsonnum/soundnode.zig @@ -26,6 +26,10 @@ pub const SoundNode = struct { air_in: ArrayList(*SoundNode), wire_in: ArrayList(*SoundNode), + + air_targets: ArrayList(*SoundNode), + wire_targets: ArrayList(*SoundNode), + activities: ArrayList(*Activity), props: StringHashMap(f64), @@ -34,11 +38,14 @@ pub const SoundNode = struct { z: f32 = 0, fab: FreqAmpBuffer, + active: u8 = 1, pub fn create(allocator: Allocator, name: []const u8) !*SoundNode { const air_in = ArrayList(*SoundNode).init(allocator); const wire_in = ArrayList(*SoundNode).init(allocator); + const air_targets = ArrayList(*SoundNode).init(allocator); + const wire_targets = ArrayList(*SoundNode).init(allocator); const activities = ArrayList(*Activity).init(allocator); const fab = FreqAmpBuffer{}; const props = StringHashMap(f64).init(allocator); @@ -50,12 +57,15 @@ pub const SoundNode = struct { .name = name, .air_in = air_in, .wire_in = wire_in, + .air_targets = air_targets, + .wire_targets = wire_targets, .activities = activities, .fab = fab, .props = props, .x = 0, .y = 0, .z = 0, + .active = 0, }; return sn;