fixed the pin system, workable

This commit is contained in:
aprilnightk 2025-09-17 12:20:37 +03:00
parent 3f30771451
commit f143f98948
4 changed files with 41 additions and 110 deletions

View file

@ -1,4 +1,4 @@
s.setup(s.sec(60))
s.setup(s.sec(10))
n = s.node()
n.setbasefreq(0, s.note("D4"))

View file

@ -119,6 +119,7 @@ pub const Activity = struct {
const amp = gain * math.sin(utility.corrected_tau * self.soundnode.basefreq * current_tick - self.soundnode.basephase * utility.tau);
self.soundnode.add_r_amp(amp);
}
};

View file

@ -1,11 +1,11 @@
const std = @import("std");
const print = std.debug.print;
const SoundNode = @import("soundnode.zig").SoundNode;
const Allocator = std.mem.Allocator;
pub const Pin = struct {
uid: usize = 0,
allocator: Allocator,
src_node: *SoundNode,
@ -17,7 +17,6 @@ pub const Pin = struct {
active: u8 = 0,
pub fn create(allocator: Allocator,
uid: usize,
src_node: *SoundNode,
trg_node: *SoundNode,
src_pin: usize,
@ -27,7 +26,6 @@ pub const Pin = struct {
pin.* = .{
.allocator = allocator,
.uid = uid,
.src_node = src_node,
.trg_node = trg_node,
.src_pin = src_pin,
@ -71,7 +69,7 @@ pub const Pin = struct {
},
6 => {self.trg_node.in6 += self.src_node.r_amp; },
7 => {self.trg_node.in7 *= self.src_node.r_amp; },
7 => {self.trg_node.in7 += self.src_node.r_amp; },
else => {},
}
},
@ -104,7 +102,7 @@ pub const Pin = struct {
},
6 => {self.trg_node.in6 += self.src_node.out1; },
7 => {self.trg_node.in7 *= self.src_node.out1; },
7 => {self.trg_node.in7 += self.src_node.out1; },
else => {},
}
},
@ -137,7 +135,7 @@ pub const Pin = struct {
},
6 => {self.trg_node.in6 += self.src_node.out6; },
7 => {self.trg_node.in7 *= self.src_node.out6; },
7 => {self.trg_node.in7 += self.src_node.out6; },
else => {},
}
},
@ -170,7 +168,7 @@ pub const Pin = struct {
},
6 => {self.trg_node.in6 += self.src_node.out7; },
7 => {self.trg_node.in7 *= self.src_node.out7; },
7 => {self.trg_node.in7 += self.src_node.out7; },
else => {},
}
},
@ -181,7 +179,7 @@ pub const Pin = struct {
3 => {self.trg_node.in_basephase += self.src_node.gain; },
4 => {self.trg_node.in_phase += self.src_node.gain; },
8 => {self.trg_node.in8 += self.src_node.gain; },
9 => {self.trg_node.in9 *= self.src_node.gain; },
9 => {self.trg_node.in9 += self.src_node.gain; },
else => {},
}
},
@ -192,7 +190,7 @@ pub const Pin = struct {
3 => {self.trg_node.in_basephase += self.src_node.basephase; },
4 => {self.trg_node.in_phase += self.src_node.basephase; },
8 => {self.trg_node.in8 += self.src_node.basephase; },
9 => {self.trg_node.in9 *= self.src_node.basephase; },
9 => {self.trg_node.in9 += self.src_node.basephase; },
else => {},
}
},
@ -203,7 +201,7 @@ pub const Pin = struct {
3 => {self.trg_node.in_basephase += self.src_node.phase; },
4 => {self.trg_node.in_phase += self.src_node.phase; },
8 => {self.trg_node.in8 += self.src_node.phase; },
9 => {self.trg_node.in9 *= self.src_node.phase; },
9 => {self.trg_node.in9 += self.src_node.phase; },
else => {},
}
},
@ -214,7 +212,7 @@ pub const Pin = struct {
3 => {self.trg_node.in_basephase += self.src_node.out8; },
4 => {self.trg_node.in_phase += self.src_node.out8; },
8 => {self.trg_node.in8 += self.src_node.out8; },
9 => {self.trg_node.in9 *= self.src_node.out8; },
9 => {self.trg_node.in9 += self.src_node.out8; },
else => {},
}
},
@ -225,7 +223,7 @@ pub const Pin = struct {
3 => {self.trg_node.in_basephase += self.src_node.out9; },
4 => {self.trg_node.in_phase += self.src_node.out9; },
8 => {self.trg_node.in8 += self.src_node.out9; },
9 => {self.trg_node.in9 *= self.src_node.out9; },
9 => {self.trg_node.in9 += self.src_node.out9; },
else => {},
}
},
@ -235,7 +233,7 @@ pub const Pin = struct {
switch (self.trg_pin) {
5 => {self.trg_node.in_basefreq += self.src_node.basefreq; },
10 => {self.trg_node.in10 += self.src_node.basefreq; },
11 => {self.trg_node.in11 *= self.src_node.basefreq; },
11 => {self.trg_node.in11 += self.src_node.basefreq; },
else => {},
}
},
@ -244,7 +242,7 @@ pub const Pin = struct {
switch (self.trg_pin) {
5 => {self.trg_node.in_basefreq += self.src_node.out10; },
10 => {self.trg_node.in10 += self.src_node.out10; },
11 => {self.trg_node.in11 *= self.src_node.out10; },
11 => {self.trg_node.in11 += self.src_node.out10; },
else => {},
}
},
@ -253,7 +251,7 @@ pub const Pin = struct {
switch (self.trg_pin) {
5 => {self.trg_node.in_basefreq += self.src_node.out11; },
10 => {self.trg_node.in10 += self.src_node.out11; },
11 => {self.trg_node.in11 *= self.src_node.out11; },
11 => {self.trg_node.in11 += self.src_node.out11; },
else => {},
}
},

View file

@ -12,11 +12,6 @@ const SoundSettings = @import("settings.zig").SoundSettings;
const utility = @import("utility.zig");
pub fn singleSineTick(st: *SoundSettings, freq: f64, t: u32, phase: f64) f64 {
const ft: f64 = @floatFromInt(t);
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| {
@ -55,43 +50,6 @@ pub fn removeSoundNodeFromList(snlist: *ArrayList(*SoundNode), sn: *SoundNode) u
}
pub fn returnPinToList(pinlist: *ArrayList(*Pin), pn: *Pin) !void {
for (pinlist.items, 0..) |pin, ind| {
if (pn.uid < pin.uid) {
try pinlist.insert(ind, pn);
return;
}
}
try pinlist.append(pn);
return;
}
pub fn removePinFromList(pinlist: *ArrayList(*Pin), pn: *Pin) !void {
var found: u8 = 0;
var toremove: usize = 0;
for (pinlist.items, 0..) |pin, ind| {
if (pn.uid == pin.uid) {
toremove = ind;
found = 1;
}
}
if (found == 1) {
_ = pinlist.orderedRemove(toremove);
}
return found;
}
pub fn reactivateSoundNode(alist: *ArrayList(*SoundNode), plist: *ArrayList(*SoundNode), sn: *SoundNode) !void {
@ -109,21 +67,6 @@ pub fn deactivateSoundNode(alist: *ArrayList(*SoundNode), plist: *ArrayList(*Sou
}
pub fn reactivatePin(alist: *ArrayList(*Pin), plist: *ArrayList(*Pin), pn: *Pin) !void {
const remove_success = removePinFromList(plist, pn);
_ = remove_success;
try returnSoundNodeToList(alist, pn);
}
pub fn deactivatePin(alist: *ArrayList(*Pin), plist: *ArrayList(*Pin), pn: *Pin) !void {
const remove_success = removePinFromList(alist, pn);
_ = remove_success;
try plist.append(pn);
}
pub fn main() !void {
@ -142,11 +85,7 @@ pub fn main() !void {
defer a_soundnodes.deinit();
var p_soundnodes = ArrayList(*SoundNode).init(allocator);
defer p_soundnodes.deinit();
// Creating a list of soundnodes
var pins = ArrayList(*Pin).init(allocator);
defer pins.deinit();
// Determining length of resulting audio in ticks
const start_tick: u32 = 0;
var end_tick: u32 = 44100 * 10;
@ -277,6 +216,7 @@ pub fn main() !void {
switch (opcode) {
0 => {
const uid: usize = @as(usize, src_node);
const sn = try SoundNode.create(allocator, uid);
//print("Added node {s} at tick {d}\n", .{nodename, tick});
@ -290,14 +230,11 @@ pub fn main() !void {
const trg = soundnodes.items[trg_node];
//print("Wired nodes at tick {d}\n", .{tick});
const uid: usize = pins.items.len;
const src_pin: usize = @intFromFloat(op1);
const trg_pin: usize = @intFromFloat(op2);
const pin = try Pin.create(allocator, uid, src, trg, src_pin, trg_pin);
try pins.append(pin);
try src.pins.append(pin);
const pin = try Pin.create(allocator, src, trg, src_pin, trg_pin);
try trg.pins.append(pin);
},
3 => {
@ -313,21 +250,16 @@ pub fn main() !void {
else => {
const src = soundnodes.items[src_node];
const a = try Activity.create(allocator, tick_start, tick_end, opcode, src, [6]f64{op1, op2, op3, op4, op5, op6});
const soundnode = soundnodes.items[src_node];
const a = try Activity.create(allocator, tick_start, tick_end, opcode, soundnode, [6]f64{op1, op2, op3, op4, op5, op6});
//print("Set activity {d} for node {d} s at tick {d}\n", .{opcode, src_node, tick});
try src.activities.append(a);
try soundnode.activities.append(a);
if (src.activities.items.len == 1 and src.active == 0) {
src.active = 1;
try reactivateSoundNode(&a_soundnodes, &p_soundnodes, src);
if (soundnode.activities.items.len == 1 and soundnode.active == 0) {
soundnode.active = 1;
try reactivateSoundNode(&a_soundnodes, &p_soundnodes, soundnode);
for (src.pins.items, 0..) |pin, zj| {
pin.active = 1;
_ = zj;
}
}
},
@ -351,6 +283,12 @@ pub fn main() !void {
if (a_soundnodes.items.len > 2) {
for (a_soundnodes.items[2..], 0..) |soundnode, i| {
for (soundnode.pins.items, 0..) |pin, zj| {
if (pin.src_node.active == 1 or pin.trg_pin == 1) {
pin.propagate();
}
_ = zj;
}
var toremove = ArrayList(usize).init(allocator);
defer toremove.deinit();
@ -363,10 +301,6 @@ pub fn main() !void {
}
}
for (soundnode.pins.items, 0..) |pin, zj| {
pin.propagate();
_ = zj;
}
var j: usize = toremove.items.len;
@ -380,11 +314,7 @@ pub fn main() !void {
soundnode.active = 0;
try deactivateSoundNode(&a_soundnodes, &p_soundnodes, soundnode);
for (soundnode.pins.items, 0..) |pin, zj| {
pin.active = 0;
_ = zj;
}
}
}
@ -404,6 +334,13 @@ pub fn main() !void {
for (soundnodes.items[0..2], 0..) |soundnode, i| {
for (soundnode.pins.items, 0..) |pin, jz| {
if (pin.src_node.active == 1 or pin.trg_pin == 1) {
pin.propagate();
}
_ = jz;
}
var toremove = ArrayList(usize).init(allocator);
defer toremove.deinit();
@ -417,11 +354,6 @@ pub fn main() !void {
}
for (soundnode.pins.items, 0..) |pin, jz| {
pin.propagate();
_ = jz;
}
var j: usize = toremove.items.len;
while (j > 0) {
@ -444,8 +376,8 @@ pub fn main() !void {
sample = @intFromFloat(right.r_amp * settings.max_amp_multiplier);
try stdout.writeInt(i24, sample, Endian.little);
left.fab.increment_tick();
right.fab.increment_tick();
left.fab.current_tick += 1;
right.fab.current_tick += 1;
tick += 1;
}