slight intermediary optimization
This commit is contained in:
parent
c3756c4bd9
commit
26b7ed1310
3 changed files with 92 additions and 62 deletions
|
|
@ -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);
|
||||
if (aired_sn.active == 1) {
|
||||
const dist: f64 = self.soundnode.distance(aired_sn);
|
||||
|
||||
var b_sample_tick: u32 = 0;
|
||||
var n_sample_tick: u32 = 0;
|
||||
var b_sample_tick: u32 = 0;
|
||||
var n_sample_tick: u32 = 0;
|
||||
|
||||
const tck = @as(f64, @floatFromInt(current_tick)) - (128.571428 * dist);
|
||||
const tck = @as(f64, @floatFromInt(current_tick)) - (128.571428 * dist);
|
||||
|
||||
if (tck > 0) {
|
||||
if (tck > 0) {
|
||||
|
||||
b_sample_tick = @intFromFloat( @floor(tck) );
|
||||
n_sample_tick = b_sample_tick + 1;
|
||||
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 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 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;
|
||||
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);
|
||||
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 dist: f64 = self.soundnode.distance(aired_sn);
|
||||
|
||||
const tck = @as(f64, @floatFromInt(current_tick)) - (128.571428 * dist);
|
||||
var b_sample_tick: u32 = 0;
|
||||
var n_sample_tick: u32 = 0;
|
||||
|
||||
if (tck > 0) {
|
||||
const tck = @as(f64, @floatFromInt(current_tick)) - (128.571428 * dist);
|
||||
|
||||
b_sample_tick = @intFromFloat( @floor(tck) );
|
||||
n_sample_tick = b_sample_tick + 1;
|
||||
if (tck > 0) {
|
||||
|
||||
const attenuation: f64 = @as(f64, std.math.exp(-(dist / 100.0)));
|
||||
b_sample_tick = @intFromFloat( @floor(tck) );
|
||||
n_sample_tick = b_sample_tick + 1;
|
||||
|
||||
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 attenuation: f64 = @as(f64, std.math.exp(-(dist / 100.0)));
|
||||
|
||||
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;
|
||||
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);
|
||||
|
||||
srcamp += relayed_r_amp;
|
||||
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;
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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,24 +224,31 @@ 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| {
|
||||
var toremove = ArrayList(usize).init(allocator);
|
||||
defer toremove.deinit();
|
||||
|
||||
if (tick <= activity.end_tick and tick >= activity.start_tick) {
|
||||
try activity.do();
|
||||
} else if (tick >= activity.end_tick) {
|
||||
try toremove.append(j);
|
||||
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;
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
while (j > 0) {
|
||||
j -= 1;
|
||||
const activity_ind = toremove.items[j];
|
||||
_ = soundnode.activities.swapRemove(activity_ind);
|
||||
}
|
||||
|
||||
soundnode.fab.increment_tick();
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue