finalized optimization of inactive nodes

This commit is contained in:
aprilnightk 2025-09-15 22:43:15 +03:00
parent 26b7ed1310
commit 8362e1643c
4 changed files with 94 additions and 28 deletions

View file

@ -74,7 +74,7 @@ gamma = []
for o in octaves: for o in octaves:
for n in notes: for n in notes:
gamma.append(n+o) gamma.append(n+o)
s.setup(s.sec(len(gamma)+2)) s.setup(s.sec(60))
for i in range(0, len(gamma)): for i in range(0, len(gamma)):
note = gamma[i] note = gamma[i]

View file

@ -16,6 +16,60 @@ pub fn singleSineTick(st: *SoundSettings, freq: f64, t: u32, phase: f64) f64 {
return math.sin(st.sine_multiplier * freq * ft - phase * utility.tau); return math.sin(st.sine_multiplier * freq * ft - phase * utility.tau);
} }
pub fn returnSoundNodeToList(snlist: *ArrayList(*SoundNode), sn: *SoundNode) !void {
for (snlist.items, 0..) |soundnode, ind| {
if (sn.uid < soundnode.uid) {
try snlist.insert(ind, sn);
return;
}
}
try snlist.append(sn);
return;
}
pub fn removeSoundNodeFromList(snlist: *ArrayList(*SoundNode), sn: *SoundNode) u8 {
var found: u8 = 0;
var toremove: usize = 0;
for (snlist.items, 0..) |soundnode, ind| {
if (sn.uid == soundnode.uid) {
toremove = ind;
found = 1;
}
}
if (found == 1) {
_ = snlist.orderedRemove(toremove);
}
return found;
}
pub fn reactivateSoundNode(alist: *ArrayList(*SoundNode), plist: *ArrayList(*SoundNode), sn: *SoundNode) !void {
const remove_success = removeSoundNodeFromList(plist, sn);
_ = remove_success;
try returnSoundNodeToList(alist, sn);
}
pub fn deactivateSoundNode(alist: *ArrayList(*SoundNode), plist: *ArrayList(*SoundNode), sn: *SoundNode) !void {
const remove_success = removeSoundNodeFromList(alist, sn);
_ = remove_success;
try plist.append(sn);
}
pub fn main() !void { pub fn main() !void {
const allocator = std.heap.c_allocator; const allocator = std.heap.c_allocator;
@ -27,6 +81,11 @@ pub fn main() !void {
var soundnodes = ArrayList(*SoundNode).init(allocator); var soundnodes = ArrayList(*SoundNode).init(allocator);
defer soundnodes.deinit(); defer soundnodes.deinit();
var a_soundnodes = ArrayList(*SoundNode).init(allocator);
defer a_soundnodes.deinit();
var p_soundnodes = ArrayList(*SoundNode).init(allocator);
defer p_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;
@ -159,9 +218,11 @@ pub fn main() !void {
0 => { 0 => {
const nodename = try std.fmt.allocPrint(allocator, "{d}", .{src_node}); const nodename = try std.fmt.allocPrint(allocator, "{d}", .{src_node});
const sn = try SoundNode.create(allocator, nodename); const uid: usize = @as(usize, src_node);
const sn = try SoundNode.create(allocator, nodename, uid);
//print("Added node {s} at tick {d}\n", .{nodename, tick}); //print("Added node {s} at tick {d}\n", .{nodename, tick});
try soundnodes.append(sn); try soundnodes.append(sn);
try p_soundnodes.append(sn);
try sn.s("basefreq", @as(f64, 440)); try sn.s("basefreq", @as(f64, 440));
try sn.s("gain", @as(f64, 1)); try sn.s("gain", @as(f64, 1));
@ -206,8 +267,9 @@ 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) { if (src.activities.items.len == 1 and src.active == 0) {
src.active = 1; src.active = 1;
try reactivateSoundNode(&a_soundnodes, &p_soundnodes, src);
} }
}, },
@ -224,31 +286,32 @@ pub fn main() !void {
for (soundnodes.items[2..], 0..) |soundnode, i| { for (soundnodes.items[2..], 0..) |soundnode, i| {
if (soundnode.active == 1) { var toremove = ArrayList(usize).init(allocator);
defer toremove.deinit();
var toremove = ArrayList(usize).init(allocator); for (soundnode.activities.items, 0..) |activity, j| {
defer toremove.deinit();
for (soundnode.activities.items, 0..) |activity, j| { if (tick <= activity.end_tick and tick >= activity.start_tick) {
try activity.do();
if (tick <= activity.end_tick and tick >= activity.start_tick) { } else if (tick >= activity.end_tick) {
try activity.do(); try toremove.append(j);
} 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 and soundnode.active == 1) {
soundnode.active = 0;
try deactivateSoundNode(&a_soundnodes, &p_soundnodes, soundnode);
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;
}
} }
} }
soundnode.fab.increment_tick(); soundnode.fab.increment_tick();

View file

@ -23,6 +23,7 @@ pub const SoundNode = struct {
allocator: Allocator, allocator: Allocator,
name: []const u8 = "soundnode", name: []const u8 = "soundnode",
uid: usize = 0,
air_in: ArrayList(*SoundNode), air_in: ArrayList(*SoundNode),
wire_in: ArrayList(*SoundNode), wire_in: ArrayList(*SoundNode),
@ -40,7 +41,7 @@ pub const SoundNode = struct {
fab: FreqAmpBuffer, fab: FreqAmpBuffer,
active: u8 = 1, active: u8 = 1,
pub fn create(allocator: Allocator, name: []const u8) !*SoundNode { pub fn create(allocator: Allocator, name: []const u8, uid: usize) !*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);
@ -53,6 +54,7 @@ pub const SoundNode = struct {
const sn = try allocator.create(SoundNode); const sn = try allocator.create(SoundNode);
sn.* = .{ sn.* = .{
.uid = uid,
.allocator = allocator, .allocator = allocator,
.name = name, .name = name,
.air_in = air_in, .air_in = air_in,

View file

@ -11,3 +11,4 @@ pub fn prnt(s: []const u8) void {
pub fn interpolate(x: f64, x0: f64, x1: f64, y0: f64, y1: f64) f64 { pub fn interpolate(x: f64, x0: f64, x1: f64, y0: f64, y1: f64) f64 {
return y0 + ((y1-y0)*(x-x0) / (x1-x0)); return y0 + ((y1-y0)*(x-x0) / (x1-x0));
} }