improvements

This commit is contained in:
aprilnightk 2025-09-20 23:53:16 +03:00
parent 58ac740143
commit 9f4fad0707
6 changed files with 120 additions and 70 deletions

View file

@ -1,10 +1,10 @@
s.setup(100)
s.setup(10)
;0 s.left.@setgain(0.7)
;0 s.right.@setgain(0.7)
ns = []
for k in range(0, 100):
for k in range(0, 9):
n = s.node(20)
ns.append(n)
;n->s.left
@ -15,8 +15,12 @@ for k in range(0, 100):
;0 n.setpos(1,1,1)
;0 n.setpin(52, 0.9)
for i in range(0,20):
for i in range(0,40):
;0 n.@setfreq(i, s.note("C3")*(i+1))
;0 n.@setfreqgain(i, 1/float(i+1))
;0-9 ns[4].sine()
;n-/>s.left
;n-/>s.right
;ns[4]->s.left
;ns[4]->s.right
;0-90 ns[4].sine()

View file

@ -5,6 +5,7 @@ OPCODES['create'] = 0
OPCODES['endtick'] = 2
OPCODES['link'] = 1
OPCODES['setpin'] = 3
OPCODES['unlink'] = 8
OPCODES['printstate'] = 9
with open("zigsonnum/activity.zig", 'r') as zigfl:

View file

@ -55,6 +55,16 @@ class SonnumCompiler:
name_src, name_trg = ln[1:].split('->')
ln = f'self.add_activity("link", 0, 0, {name_src}, {name_trg}, [0, 2])'
elif '=/>' in ln:
name_src, name_trg = ln[1:].split('=/>')
ln = f'self.add_activity("unlink", 0, 0, {name_src}, {name_trg}, [0, 1])'
elif '-/>' in ln:
name_src, name_trg = ln[1:].split('-/>')
ln = f'self.add_activity("unlink", 0, 0, {name_src}, {name_trg}, [0, 2])'
else:
ln = ln[1:]

View file

@ -37,7 +37,7 @@ pub const Link = struct {
}
pub fn propagate(self: *Link) void {
// Special case for air_in (pin 2)
if (self.trg_pin == 2) {

View file

@ -155,10 +155,13 @@ pub fn main() !void {
if (tick%44100 == 0) {
print("{d}-- ", .{tick/44100});
}
for (soundnodes.items, 0..) |soundnode, i| {
soundnode.nullizeStartTick();
for (a_soundnodes.items, 0..) |soundnode, i| {
//print("TICK {d} I {d} ", .{tick, i});
@memset(soundnode.pins[0..32], 0);
_ = i;
}
if (tick == nextopcodetick) {
@ -272,6 +275,31 @@ pub fn main() !void {
},
8 => {
const src = soundnodes.items[src_node];
const trg = soundnodes.items[trg_node];
//print("Wired nodes at tick {d}\n", .{tick});
const src_pin: usize = @intFromFloat(op1);
const trg_pin: usize = @intFromFloat(op2);
var found: bool = false;
var toremove: u64 = 0;
for (trg.links.items, 0..) |link, j| {
if (link.src_node == src and link.trg_node == trg and link.src_pin == src_pin and link.trg_pin == trg_pin) {
toremove = j;
found = true;
break;
}
}
if (found) {
_ = trg.links.orderedRemove(toremove);
}
},
else => {
const soundnode = soundnodes.items[src_node];
@ -281,9 +309,10 @@ pub fn main() !void {
try soundnode.activities.append(a);
if (soundnode.activities.items.len == 1 and soundnode.active == 0) {
soundnode.active = 1;
try reactivateSoundNode(&a_soundnodes, &p_soundnodes, soundnode);
}
},
@ -295,59 +324,65 @@ pub fn main() !void {
}
}
//All but left and right
if (a_soundnodes.items.len > 2) {
var iter: usize = 1;
//while (iter < 0) {
while (iter < a_soundnodes.items.len - 1) {
for (a_soundnodes.items[2..], 0..) |soundnode, i| {
for (soundnode.links.items, 0..) |link, j| {
if (link.src_node.active == 1 or link.trg_pin == 1) {
link.propagate();
}
_ = 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);
}
}
if (soundnode.wantprint == true) {
soundnode.wantprint = false;
soundnode.printState();
}
var j: usize = toremove.items.len;
while (j > 0) {
j -= 1;
const activity_ind = toremove.items[j];
iter += 1;
const soundnode = a_soundnodes.items[iter];
_ = 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);
}
for (soundnode.links.items, 0..) |link, j| {
if (link.src_node.active == 1 or link.trg_pin == 1) {
link.propagate();
}
soundnode.fab.increment_tick();
_ = i;
_ = 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);
}
}
if (soundnode.wantprint == true) {
soundnode.wantprint = false;
soundnode.printState();
}
var j: usize = toremove.items.len;
if (j == 0) {
soundnode.fab.increment_tick();
}
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);
//print("DEACTIVATING {d} AT TICK {d} ", .{soundnode.uid, tick});
}
}
}
// Incrementing passive ones too
@ -355,11 +390,18 @@ pub fn main() !void {
for (p_soundnodes.items, 0..) |soundnode, i| {
soundnode.fab.increment_tick();
_ = i;
}
//Left and right
for (soundnodes.items[0..2], 0..) |soundnode, i| {
iter = 0;
//while (iter < 0) {
while (iter < 2) {
const soundnode = soundnodes.items[iter];
iter += 1;
for (soundnode.links.items, 0..) |link, jz| {
if (link.src_node.active == 1 or link.trg_pin == 1) {
@ -394,11 +436,10 @@ pub fn main() !void {
_ = soundnode.activities.swapRemove(activity_ind);
}
_ = i;
}
//Calculating and writing output amps
left = soundnodes.items[0];
right = soundnodes.items[1];
@ -426,8 +467,10 @@ pub fn main() !void {
left.fab.current_tick += 1;
right.fab.current_tick += 1;
tick += 1;
tick += 1;
}
try bw.flush();

View file

@ -36,7 +36,7 @@ pub const SoundNode = struct {
wantprint: bool = false,
fab: FreqAmpBuffer,
active: u8 = 1,
active: u8 = 0,
pub fn create(allocator: Allocator, uid: usize, freq_q: u8) !*SoundNode {
@ -68,7 +68,6 @@ pub const SoundNode = struct {
//Gain and per-freq gains must be initialized as 1
sn.pins[32] = 1;
@memset(sn.pins[112..160], 1);
return sn;
@ -84,13 +83,6 @@ pub const SoundNode = struct {
}
pub fn nullizeStartTick(self: *SoundNode) void {
// Zeroing the first 32 pins (dynamic pins)
@memset(self.pins[0..32], 0);
}
pub fn corrGain(self: *SoundNode, gainmult: f64) f64 {
if (gainmult > 0) {