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| {
|
for (self.soundnode.wire_in.items, 0..) |wired_sn, i| {
|
||||||
|
|
||||||
const relayed_r_amp = wired_sn.fab.get_r_amp(current_tick);
|
if (wired_sn.active == 1) {
|
||||||
self.soundnode.fab.add_r_amp(relayed_r_amp);
|
const relayed_r_amp = wired_sn.fab.get_r_amp(current_tick);
|
||||||
|
self.soundnode.fab.add_r_amp(relayed_r_amp);
|
||||||
|
}
|
||||||
_ = i;
|
_ = i;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for (self.soundnode.air_in.items, 0..) |aired_sn, 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;
|
|
||||||
|
|
||||||
const tck = @as(f64, @floatFromInt(current_tick)) - (128.571428 * dist);
|
|
||||||
|
|
||||||
if (tck > 0) {
|
var b_sample_tick: u32 = 0;
|
||||||
|
var n_sample_tick: u32 = 0;
|
||||||
|
|
||||||
b_sample_tick = @intFromFloat( @floor(tck) );
|
const tck = @as(f64, @floatFromInt(current_tick)) - (128.571428 * dist);
|
||||||
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);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
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;
|
_ = i;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
@ -439,38 +442,42 @@ pub const Activity = struct {
|
||||||
|
|
||||||
for (self.soundnode.wire_in.items, 0..) |wired_sn, i| {
|
for (self.soundnode.wire_in.items, 0..) |wired_sn, i| {
|
||||||
|
|
||||||
const relayed_r_amp = wired_sn.fab.get_r_amp(current_tick);
|
if (wired_sn.active == 1) {
|
||||||
srcamp += relayed_r_amp;
|
const relayed_r_amp = wired_sn.fab.get_r_amp(current_tick);
|
||||||
|
srcamp += relayed_r_amp;
|
||||||
|
}
|
||||||
_ = i;
|
_ = i;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for (self.soundnode.air_in.items, 0..) |aired_sn, 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;
|
const dist: f64 = self.soundnode.distance(aired_sn);
|
||||||
var n_sample_tick: u32 = 0;
|
|
||||||
|
|
||||||
const tck = @as(f64, @floatFromInt(current_tick)) - (128.571428 * dist);
|
|
||||||
|
|
||||||
if (tck > 0) {
|
var b_sample_tick: u32 = 0;
|
||||||
|
var n_sample_tick: u32 = 0;
|
||||||
|
|
||||||
b_sample_tick = @intFromFloat( @floor(tck) );
|
const tck = @as(f64, @floatFromInt(current_tick)) - (128.571428 * dist);
|
||||||
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;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
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;
|
_ = i;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -26,7 +26,7 @@ pub fn main() !void {
|
||||||
// Creating a list of soundnodes
|
// Creating a list of soundnodes
|
||||||
var soundnodes = ArrayList(*SoundNode).init(allocator);
|
var soundnodes = ArrayList(*SoundNode).init(allocator);
|
||||||
defer soundnodes.deinit();
|
defer soundnodes.deinit();
|
||||||
|
|
||||||
// Determining length of resulting audio in ticks
|
// Determining length of resulting audio in ticks
|
||||||
const start_tick: u32 = 0;
|
const start_tick: u32 = 0;
|
||||||
var end_tick: u32 = 44100 * 10;
|
var end_tick: u32 = 44100 * 10;
|
||||||
|
|
@ -175,6 +175,7 @@ pub fn main() !void {
|
||||||
//print("Wired nodes at tick {d}\n", .{tick});
|
//print("Wired nodes at tick {d}\n", .{tick});
|
||||||
|
|
||||||
try trg.wire_in.append(src);
|
try trg.wire_in.append(src);
|
||||||
|
try src.wire_targets.append(trg);
|
||||||
},
|
},
|
||||||
|
|
||||||
2 => {
|
2 => {
|
||||||
|
|
@ -183,6 +184,7 @@ pub fn main() !void {
|
||||||
|
|
||||||
//print("Aired nodes at tick {d}\n", .{tick});
|
//print("Aired nodes at tick {d}\n", .{tick});
|
||||||
try trg.air_in.append(src);
|
try trg.air_in.append(src);
|
||||||
|
try src.air_targets.append(trg);
|
||||||
},
|
},
|
||||||
|
|
||||||
3 => {
|
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});
|
//print("Set activity {d} for node {d} s at tick {d}\n", .{opcode, src_node, tick});
|
||||||
try src.activities.append(a);
|
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| {
|
for (soundnodes.items[2..], 0..) |soundnode, i| {
|
||||||
|
|
||||||
var toremove = ArrayList(usize).init(allocator);
|
if (soundnode.active == 1) {
|
||||||
defer toremove.deinit();
|
|
||||||
|
|
||||||
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();
|
for (soundnode.activities.items, 0..) |activity, j| {
|
||||||
} else if (tick >= activity.end_tick) {
|
|
||||||
try toremove.append(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();
|
soundnode.fab.increment_tick();
|
||||||
_ = i;
|
_ = i;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -26,6 +26,10 @@ pub const SoundNode = struct {
|
||||||
|
|
||||||
air_in: ArrayList(*SoundNode),
|
air_in: ArrayList(*SoundNode),
|
||||||
wire_in: ArrayList(*SoundNode),
|
wire_in: ArrayList(*SoundNode),
|
||||||
|
|
||||||
|
air_targets: ArrayList(*SoundNode),
|
||||||
|
wire_targets: ArrayList(*SoundNode),
|
||||||
|
|
||||||
activities: ArrayList(*Activity),
|
activities: ArrayList(*Activity),
|
||||||
props: StringHashMap(f64),
|
props: StringHashMap(f64),
|
||||||
|
|
||||||
|
|
@ -34,11 +38,14 @@ pub const SoundNode = struct {
|
||||||
z: f32 = 0,
|
z: f32 = 0,
|
||||||
|
|
||||||
fab: FreqAmpBuffer,
|
fab: FreqAmpBuffer,
|
||||||
|
active: u8 = 1,
|
||||||
|
|
||||||
pub fn create(allocator: Allocator, name: []const u8) !*SoundNode {
|
pub fn create(allocator: Allocator, name: []const u8) !*SoundNode {
|
||||||
|
|
||||||
const air_in = ArrayList(*SoundNode).init(allocator);
|
const air_in = ArrayList(*SoundNode).init(allocator);
|
||||||
const wire_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 activities = ArrayList(*Activity).init(allocator);
|
||||||
const fab = FreqAmpBuffer{};
|
const fab = FreqAmpBuffer{};
|
||||||
const props = StringHashMap(f64).init(allocator);
|
const props = StringHashMap(f64).init(allocator);
|
||||||
|
|
@ -50,12 +57,15 @@ pub const SoundNode = struct {
|
||||||
.name = name,
|
.name = name,
|
||||||
.air_in = air_in,
|
.air_in = air_in,
|
||||||
.wire_in = wire_in,
|
.wire_in = wire_in,
|
||||||
|
.air_targets = air_targets,
|
||||||
|
.wire_targets = wire_targets,
|
||||||
.activities = activities,
|
.activities = activities,
|
||||||
.fab = fab,
|
.fab = fab,
|
||||||
.props = props,
|
.props = props,
|
||||||
.x = 0,
|
.x = 0,
|
||||||
.y = 0,
|
.y = 0,
|
||||||
.z = 0,
|
.z = 0,
|
||||||
|
.active = 0,
|
||||||
};
|
};
|
||||||
|
|
||||||
return sn;
|
return sn;
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue