From 761e3cfbf7b5202e41354ed9ac6a80b99626edf2 Mon Sep 17 00:00:00 2001 From: aprilnightk Date: Sat, 20 Sep 2025 20:53:27 +0300 Subject: [PATCH] highly revamped pin system --- mysynths/test.py | 17 ++ pysonnum/activity.py | 48 ++-- pysonnum/compiler.py | 105 ++++----- pysonnum/sonnum.py | 16 +- pysonnum/soundnode.py | 94 +++----- zigsonnum/activities/basicsynths.zig | 183 ++++++++++----- zigsonnum/activities/spatial.zig | 6 +- zigsonnum/activity.zig | 82 +++---- zigsonnum/link.zig | 92 ++++++++ zigsonnum/pin.zig | 265 ---------------------- zigsonnum/sonnum.zig | 68 +++--- zigsonnum/soundnode.zig | 326 +++++---------------------- 12 files changed, 455 insertions(+), 847 deletions(-) create mode 100644 mysynths/test.py create mode 100644 zigsonnum/link.zig delete mode 100644 zigsonnum/pin.zig diff --git a/mysynths/test.py b/mysynths/test.py new file mode 100644 index 0000000..f52be7b --- /dev/null +++ b/mysynths/test.py @@ -0,0 +1,17 @@ +s.setup(sec(100)) + +n = s.node(3) + +;n->s.left +;n->s.right + +;0 s.left.@setgain(0.7) +;0 s.right.@setgain(0.7) + +;0 n.setpos(1,1,1) +;sec(1) n.setpos(-100,1,1) + +for i in range(0,40): + ;0 n.@setfreq(i, s.note("C3")*(i+1)) + +;0-sec(90) n.triangle() diff --git a/pysonnum/activity.py b/pysonnum/activity.py index ca60ffd..946af5b 100644 --- a/pysonnum/activity.py +++ b/pysonnum/activity.py @@ -1,30 +1,32 @@ import struct -OPCODES = { +OPCODES = dict() +OPCODES['create'] = 0 +OPCODES['endtick'] = 2 +OPCODES['link'] = 1 +OPCODES['setpin'] = 3 +OPCODES['printstate'] = 9 + +with open("zigsonnum/activity.zig", 'r') as zigfl: - 'create': 0, - 'feed': 1, - 'endtick': 2, - 'setpin': 3, - 'relay': 4, - 'copy': 5, - 'mute': 6, - 'printstate': 9, - - 'setpos': 10, - - 'sine': 50, - 'triangle': 51, - 'square': 52, - 'sawtooth': 53, - 'skewsine': 54, - 'pulse': 55, - 'whitenoise': 55, - - 'singenN': 100, - 'adsr': 150, + zone = False + for ln in zigfl.read().split('\n'): + + if 'STARTOPCODES' in ln: + zone = True + + elif 'ENDOPCODES' in ln: + zone = False + + elif zone and '=>' in ln: + parts = ln.split('=>') + num = parts[0].strip() + name = parts[1].split('(')[0].split('.')[1] + try: + OPCODES[name] = int(num) + except: + pass - } class Activity: diff --git a/pysonnum/compiler.py b/pysonnum/compiler.py index 19a648c..ab535f4 100644 --- a/pysonnum/compiler.py +++ b/pysonnum/compiler.py @@ -1,33 +1,9 @@ from .sonnum import Sonnum from .activity import Activity +from .soundnode import * import random -R_AMP = 0 -OUT1 = 1 -GAIN = 2 -BASEPHASE = 3 -PHASE = 4 -BASEFREQ = 5 -OUT6 = 6 -OUT7 = 7 -OUT8 = 8 -OUT9 = 9 -OUT10 = 10 -OUT11 = 11 - -IN_WIRE = 0 -IN_AIR = 1 -IN_GAIN = 2 -IN_BASEPHASE = 3 -IN_PHASE = 4 -IN_BASEFREQ = 5 -IN6 = 6 -IN7 = 7 -IN8 = 8 -IN9 = 9 -IN10 = 10 -IN11 = 11 class SonnumCompiler: @@ -55,8 +31,7 @@ class SonnumCompiler: self.activities.append(a) def transpile_snm_to_py(self, snm_src): - return snm_src - + py_src = [] for ln in snm_src.split('\n'): @@ -70,59 +45,55 @@ class SonnumCompiler: if ln.startswith(';'): - if ln.endswith('*'): - - name = self.sanitize_operand(ln[1:-1]) - ln = f"i_create_simple(self, self.sonnum, '{name}')" + if '=>' in ln: - elif ln.endswith('!'): - - ticklen = self.sanitize_operand(ln[1:-1]) - ln = f"i_end_tick(self, self.sonnum, {ticklen})" - - elif ln.endswith('@'): - - name = self.sanitize_operand(ln[1:-1]) - ln = f"i_create_relay(self, self.sonnum, '{name}')" - - elif '=>' in ln: name_src, name_trg = ln[1:].split('=>') - ln = f"i_wire(self, self.sonnum, '{name_src}', '{name_trg}')" - + ln = f'self.add_activity("link", 0, 0, {name_src}, {name_trg}, [0, 1])' + elif '->' in ln: - name_src, name_trg = ln[1:].split('->') - ln = f"i_air(self, self.sonnum, '{name_src}', '{name_trg}')" + name_src, name_trg = ln[1:].split('->') + ln = f'self.add_activity("link", 0, 0, {name_src}, {name_trg}, [0, 2])' + else: - if '; ' in ln: - - tickdata, ln = ln[1:].split('; ',1) - - if '-' in tickdata: - starttick, endtick = tickdata.split('-', 1) - else: - starttick = tickdata - endtick = starttick - - starttick = self.sanitize_operand(starttick) - endtick = self.sanitize_operand(endtick) - - lst = ln.split(' ') - src_node_name = lst.pop(0) - instr = lst.pop(0) - operands = [self.sanitize_operand(op) for op in lst] - ln = f"i_{instr}(self, self.sonnum, {starttick}, {endtick}, '{src_node_name}', "+', '.join(operands)+")" + ln = ln[1:] + tickdata, cmd = ln.split(' ',1) + + if '-' in tickdata: + starttick, endtick = tickdata.split('-', 1) else: - pass - + starttick = tickdata + endtick = starttick + + cmdcore, cmdargs = cmd.split('(', 1) + cmdargs = cmdargs[:-1] + + cmdparts = cmdcore.split('.') + operand = cmdparts.pop(-1) + nodenames = '.'.join(cmdparts) + + if '>' in nodenames: + src_nodename, trg_nodename = nodenames.split('>') + else: + src_nodename = nodenames + trg_nodename = nodenames + + if operand.startswith('@'): + operand = operand[1:] + ln = f'{src_nodename}.{operand}({starttick}, {endtick}, [{cmdargs}])' + + else: + ln = f'self.add_activity("{operand}", {starttick}, {endtick}, {src_nodename}, {trg_nodename}, [{cmdargs}])' + ln = tablevel*'\t' + ln py_src.append(ln) else: ln = tablevel*'\t' + ln py_src.append(ln) - + + print('\n'.join(py_src)) return '\n'.join(py_src) def run_transpiled_code(self, py_src): diff --git a/pysonnum/sonnum.py b/pysonnum/sonnum.py index 08f91bc..2e23a7f 100644 --- a/pysonnum/sonnum.py +++ b/pysonnum/sonnum.py @@ -17,13 +17,14 @@ class Sonnum: self.left = self.relay() self.right = self.relay() - self.left.setpos(0, -0.3, 0, 0) - self.right.setpos(0, 0.3, 0, 0) + self.left.act("setpos", 0, 0, self.left, None, [0.3, 0, 0]) + self.right.act("setpos", 0, 0, self.right, None, [-0.3, 0, 0]) - def add_node(self, name): + def add_node(self, name, freq_q): order = len(self.nodes) node = SoundNode(self, order, name) + node.freq_q = freq_q self.nodes.append(node) return node @@ -43,10 +44,11 @@ class Sonnum: def act(self, *args): self.c.add_activity(*args) - def node(self): + def node(self, freq_q = 1): - node = self.add_node("") - self.act('create', 0, 0, node, None, []) + node = self.add_node("", freq_q) + fakenode = SoundNode(self, freq_q, '') + self.act('create', 0, 0, node, fakenode, []) return node @@ -58,7 +60,7 @@ class Sonnum: def relay(self): - node = self.add_node("") + node = self.add_node("", 0) self.act('create', 0, 0, node, None, []) self.act('relay', 0, self.g('endtick'), node, None, []) diff --git a/pysonnum/soundnode.py b/pysonnum/soundnode.py index a351a08..664b4d8 100644 --- a/pysonnum/soundnode.py +++ b/pysonnum/soundnode.py @@ -1,3 +1,21 @@ +R_AMP = 0 +WIRE_IN = 1 +AIR_IN = 2 +GAIN = 32 +PHASE = 33 +X = 61 +Y = 62 +Z = 63 + +def freqpin(freq_no): + return 64+freq_no + +def gainpin(freq_no): + return 112+freq_no + +def sec(seconds): + return seconds * 44100 + class SoundNode: def __init__(self, sonnum, order, name): @@ -21,68 +39,20 @@ class SoundNode: def act(self, *args): self.c.add_activity(*args) - def feed(self, trg_nodes, src_pin, trg_pin): + def setfreq(self, starttick, endtick, args): + freq_no = args[0] + freq = args[1] + self.act("setpin", starttick, endtick, self, None, [freqpin(freq_no), freq]) - if not isinstance(trg_nodes, list): - trg_nodes = [trg_nodes] - - for trg_node in trg_nodes: - self.act('feed', 0, 0, self, trg_node, [src_pin, trg_pin]) + def setfreqgain(self, starttick, endtick, args): + freq_no = args[0] + gain = args[1] + self.act("setpin", starttick, endtick, self, None, [gainpin(freq_no), gain]) - def wire(self, trg_nodes): + def setgain(self, starttick, endtick, args): + gain = args[0] + self.act("setpin", starttick, endtick, self, None, [GAIN, gain]) - if not isinstance(trg_nodes, list): - trg_nodes = [trg_nodes] - - for trg_node in trg_nodes: - self.act('feed', 0, 0, self, trg_node, [0, 0]) - - def air(self, trg_nodes): - - if not isinstance(trg_nodes, list): - trg_nodes = [trg_nodes] - - for trg_node in trg_nodes: - self.act('feed', 0, 0, self, trg_node, [0, 1]) - - def setpos(self, start_tick, x, y, z): - self.act('setpos', start_tick, start_tick, self, None, [x, y, z]) - - def setpin(self, start_tick, pin_no, value): - self.act('setpin', start_tick, start_tick, self, None, [pin_no, value]) - - def printstate(self, start_tick, end_tick): - self.act('printstate', start_tick, end_tick, self, None, []) - - def copy(self, start_tick, end_tick, in_pin, out_pin): - self.act('copy', start_tick, end_tick, self, None, [in_pin, out_pin]) - - def sine(self, start_tick, end_tick, gainmult = 0.0): - self.act('sine', start_tick, end_tick, self, None, [gainmult]) - - def triangle(self, start_tick, end_tick, gainmult = 0.0): - self.act('triangle', start_tick, end_tick, self, None, [gainmult]) - - def square(self, start_tick, end_tick, gainmult = 0.0): - self.act('square', start_tick, end_tick, self, None, [gainmult]) - - def sawtooth(self, start_tick, end_tick, gainmult = 0.0): - self.act('sawtooth', start_tick, end_tick, self, None, [gainmult]) - - def singenN(self, start_tick, end_tick, magnitude, phase, freq, ylevel): - self.act('singenN', start_tick, end_tick, self, None, [magnitude, phase, freq, ylevel]) - - def skewsine(self, start_tick, end_tick, gainmult = 0.0): - self.act('skewsine', start_tick, end_tick, self, None, [gainmult]) - - def pulse(self, start_tick, end_tick, gainmult = 0.0): - self.act('pulse', start_tick, end_tick, self, None, [gainmult]) - - def mute(self, start_tick, end_tick): - self.act('mute', start_tick, end_tick, self, None, []) - - def whitenoise(self, start_tick, end_tick, gainmult = 0.0, seed = 0.259624856928): - self.act('whitenoise', start_tick, end_tick, self, None, [seed, gainmult]) - - def adsr(self, start_tick, end_tick, attack_tick, hold_tick, decay_tick, sustain_tick, release_tick, gainmult = 0.0): - self.act('adsr', start_tick, end_tick, self, None, [attack_tick, hold_tick, decay_tick, sustain_tick, release_tick, gainmult]) \ No newline at end of file + def setphase(self, starttick, endtick, phase): + phase = args[0] + self.act("setpin", starttick, endtick, self, None, [PHASE, gain]) \ No newline at end of file diff --git a/zigsonnum/activities/basicsynths.zig b/zigsonnum/activities/basicsynths.zig index d7532ef..972f97a 100644 --- a/zigsonnum/activities/basicsynths.zig +++ b/zigsonnum/activities/basicsynths.zig @@ -12,12 +12,24 @@ const idbg = utility.idbg; pub fn sine(self: *Activity) void { const current_tick: f64 = @floatFromInt(self.soundnode.fab.current_tick); - const gain = self.soundnode.corrGain(self.operands[0]); - - const amp = math.sin(utility.corrected_tau * self.soundnode.basefreq * current_tick - self.soundnode.totalPhase() * utility.tau); - const final_amp = gain * amp; + const maingain = self.soundnode.corrGain(self.operands[0]); - self.soundnode.add_r_amp(final_amp); + var final_amp: f64 = 0; + + const max_freq_pin = 64 + self.soundnode.freq_q; + for (64..max_freq_pin) |freq_pin| { + + const gain = self.soundnode.pins[freq_pin + 48]; + const shift = self.soundnode.pins[freq_pin + 96]; + const phase = self.soundnode.pins[freq_pin + 114]; + const totalphase = self.soundnode.pins[33] + shift + phase; + + const amp = math.sin(utility.corrected_tau * self.soundnode.pins[freq_pin] * current_tick - totalphase * utility.tau); + final_amp += maingain * gain * amp; + + } + + self.soundnode.add_r_amp(final_amp); } @@ -25,15 +37,27 @@ pub fn sine(self: *Activity) void { pub fn triangle(self: *Activity) void { const current_tick: f64 = @floatFromInt(self.soundnode.fab.current_tick); - - const gain = self.soundnode.corrGain(self.operands[0]); + const maingain = self.soundnode.corrGain(self.operands[0]); - const period = 44100 / self.soundnode.basefreq; - const tp = (current_tick - (period * self.soundnode.totalPhase())) / period; + var final_amp: f64 = 0; - const amp = @abs(2 * (2 * ( tp - @floor(tp + 0.5) ) )) - 1; - const final_amp = gain * amp; + const max_freq_pin = 64 + self.soundnode.freq_q; + for (64..max_freq_pin) |freq_pin| { + + const gain = self.soundnode.pins[freq_pin + 48]; + const shift = self.soundnode.pins[freq_pin + 96]; + const phase = self.soundnode.pins[freq_pin + 114]; + + const totalphase = self.soundnode.pins[33] + shift + phase; + + const period = 44100 / self.soundnode.pins[freq_pin]; + const tp = (current_tick - (period * totalphase)) / period; + + const amp = @abs(2 * (2 * ( tp - @floor(tp + 0.5) ) )) - 1; + final_amp += maingain * gain * amp; + } + self.soundnode.add_r_amp(final_amp); } @@ -42,15 +66,29 @@ pub fn triangle(self: *Activity) void { pub fn square(self: *Activity) void { const current_tick: f64 = @floatFromInt(self.soundnode.fab.current_tick); + const maingain = self.soundnode.corrGain(self.operands[0]); - const gain = self.soundnode.corrGain(self.operands[0]); + var final_amp: f64 = 0; - const sin = math.sin(utility.corrected_tau * self.soundnode.basefreq * current_tick - self.soundnode.totalPhase() * utility.tau); + const max_freq_pin = 64 + self.soundnode.freq_q; + for (64..max_freq_pin) |freq_pin| { + + const gain = self.soundnode.pins[freq_pin + 48]; + const shift = self.soundnode.pins[freq_pin + 96]; + const phase = self.soundnode.pins[freq_pin + 114]; + + const totalphase = self.soundnode.pins[33] + shift + phase; + + const sin = math.sin(utility.corrected_tau * self.soundnode.pins[freq_pin] * current_tick - totalphase * utility.tau); + + if (sin > 0) { + final_amp += maingain * gain; + } else { + final_amp += -(maingain * gain); + } + + self.soundnode.add_r_amp(final_amp); - if (sin > 0) { - self.soundnode.add_r_amp(gain); - } else { - self.soundnode.add_r_amp(-gain); } } @@ -59,70 +97,107 @@ pub fn square(self: *Activity) void { pub fn sawtooth(self: *Activity) void { const current_tick: f64 = @floatFromInt(self.soundnode.fab.current_tick); - - const gain = self.soundnode.corrGain(self.operands[0]); + const maingain = self.soundnode.corrGain(self.operands[0]); - const period = 44100 / self.soundnode.basefreq; - const tp = (current_tick - (period * self.soundnode.totalPhase())) / period; + var final_amp: f64 = 0; - const amp = 2 * (tp - @floor(0.5+tp)); - const final_amp = gain * amp; + const max_freq_pin = 64 + self.soundnode.freq_q; + for (64..max_freq_pin) |freq_pin| { + + const gain = self.soundnode.pins[freq_pin + 48]; + const shift = self.soundnode.pins[freq_pin + 96]; + const phase = self.soundnode.pins[freq_pin + 114]; + + const totalphase = self.soundnode.pins[33] + shift + phase; + + const period = 44100 / self.soundnode.pins[freq_pin]; + const tp = (current_tick - (period * totalphase)) / period; + + const amp = 2 * (tp - @floor(0.5+tp)); + final_amp += maingain * gain * amp; + } + self.soundnode.add_r_amp(final_amp); } // Produces a skewed sine wave to R_AMP pin -// Skewness factor is taken from OUT8 pin +// Operand 1 is pin number containing skewness factor pub fn skewsine(self: *Activity) void { const current_tick: f64 = @floatFromInt(self.soundnode.fab.current_tick); - const gain = self.soundnode.corrGain(self.operands[0]); - const skew = self.soundnode.out8; - var amp: f64 = 0; - - if (skew == 0) { - - amp = math.sin(utility.corrected_tau * self.soundnode.basefreq * current_tick - self.soundnode.totalPhase() * utility.tau); + const skew = self.soundnode.pins[@intFromFloat(self.operands[1])]; + const maingain = self.soundnode.corrGain(self.operands[0]); - } else if (skew > 0) { + var final_amp: f64 = 0; + + const max_freq_pin = 64 + self.soundnode.freq_q; + for (64..max_freq_pin) |freq_pin| { - const m = (utility.corrected_tau * self.soundnode.basefreq * current_tick - self.soundnode.totalPhase() * utility.tau); - const sincos = skew * math.sin(m) / (1 - skew*math.cos(m)); - amp = (1/skew) * math.atan(sincos); + const gain = self.soundnode.pins[freq_pin + 48]; + const shift = self.soundnode.pins[freq_pin + 96]; + const phase = self.soundnode.pins[freq_pin + 114]; - } else if (skew < 0) { + const totalphase = self.soundnode.pins[33] + shift + phase; - const m = (utility.corrected_tau * self.soundnode.basefreq * current_tick - self.soundnode.basephase * utility.tau); - const sincos = skew * math.cos(m) / (1 + skew*math.sin(m)); - amp = (1/skew) * math.atan(sincos); + if (skew == 0) { + + final_amp += maingain * gain * (math.sin(utility.corrected_tau * self.soundnode.pins[freq_pin] * current_tick - totalphase * utility.tau)); + } else if (skew > 0) { + + const m = (utility.corrected_tau * self.soundnode.pins[freq_pin] * current_tick - totalphase * utility.tau); + const sincos = skew * math.sin(m) / (1 - skew*math.cos(m)); + final_amp += maingain * gain * (1/skew) * math.atan(sincos); + + } else if (skew < 0) { + + const m = (utility.corrected_tau * self.soundnode.pins[freq_pin] * current_tick - totalphase * utility.tau); + const sincos = skew * math.cos(m) / (1 + skew*math.sin(m)); + final_amp += maingain * gain * (1/skew) * math.atan(sincos); + + } + } - const final_amp = gain * amp; - self.soundnode.add_r_amp(final_amp); } // Produces a pulse wave to r_amp pin -// Uses OUT8 as pulse phase +// Operand 1 is pin number containing pulse shift pub fn pulse(self: *Activity) void { const current_tick: f64 = @floatFromInt(self.soundnode.fab.current_tick); - const gain = self.soundnode.corrGain(self.operands[0]); - - const period = 44100 / self.soundnode.basefreq; - const tp = (current_tick - (period * self.soundnode.totalPhase())) / period; - const tp_detract = (current_tick - (period * (self.soundnode.out8 + self.soundnode.totalPhase()))) / period; + const maingain = self.soundnode.corrGain(self.operands[0]); - const amp = 2 * (tp - @floor(0.5 + tp)); - const amp_detract = 2 * (tp_detract - @floor(0.5+tp_detract)); + const pulse_shift = self.soundnode.pins[@intFromFloat(self.operands[1])]; + + var final_amp: f64 = 0; + + const max_freq_pin = 64 + self.soundnode.freq_q; + for (64..max_freq_pin) |freq_pin| { - const final_amp = gain * (amp - amp_detract); - + const gain = self.soundnode.pins[freq_pin + 48]; + const shift = self.soundnode.pins[freq_pin + 96]; + const phase = self.soundnode.pins[freq_pin + 114]; + + const totalphase = self.soundnode.pins[33] + shift + phase; + + const period = 44100 / self.soundnode.pins[freq_pin]; + const tp = (current_tick - (period * totalphase)) / period; + const tp_detract = (current_tick - (period * (pulse_shift + totalphase))) / period; + + const amp = 2 * (tp - @floor(0.5 + tp)); + const amp_detract = 2 * (tp_detract - @floor(0.5+tp_detract)); + + final_amp += maingain * gain * (amp - amp_detract); + + } + self.soundnode.add_r_amp(final_amp); } @@ -134,13 +209,13 @@ pub fn whitenoise(self: *Activity) void { const current_tick: f64 = @floatFromInt(self.soundnode.fab.current_tick); - const seed = self.soundnode.out8; - const gain = self.soundnode.corrGain(self.operands[0]); + const seed = self.soundnode.pins[@intFromFloat(self.operands[1])]; + const maingain = self.soundnode.corrGain(self.operands[0]); const w = (seed*current_tick) / math.sin(current_tick); const amp = (2 * (w - @floor(w))) - 1; - const final_amp = gain * amp; + const final_amp = maingain * amp; self.soundnode.fab.add_r_amp(final_amp); diff --git a/zigsonnum/activities/spatial.zig b/zigsonnum/activities/spatial.zig index 41ed80d..f4fb665 100644 --- a/zigsonnum/activities/spatial.zig +++ b/zigsonnum/activities/spatial.zig @@ -9,9 +9,9 @@ const idbg = utility.idbg; pub fn setpos(self: *Activity) void { - self.soundnode.x = @as(f32, @floatCast(self.operands[0])); - self.soundnode.y = @as(f32, @floatCast(self.operands[1])); - self.soundnode.z = @as(f32, @floatCast(self.operands[2])); + self.soundnode.pins[61] = @as(f32, @floatCast(self.operands[0])); + self.soundnode.pins[62] = @as(f32, @floatCast(self.operands[1])); + self.soundnode.pins[63] = @as(f32, @floatCast(self.operands[2])); } diff --git a/zigsonnum/activity.zig b/zigsonnum/activity.zig index dab12e3..eee8e75 100644 --- a/zigsonnum/activity.zig +++ b/zigsonnum/activity.zig @@ -36,7 +36,7 @@ pub const Activity = struct { pub fn do(self: *Activity) !void { switch (self.opcode) { - + //STARTOPCODES 4 => { self.relay(); }, 5 => { self.copy(); }, 6 => { self.mute(); }, @@ -51,9 +51,9 @@ pub const Activity = struct { 55 => { basicsynths.pulse(self); }, 56 => { basicsynths.whitenoise(self); }, - 100 => { generators.singenN(self); }, - 150 => { generators.adsr(self); }, - + //100 => { generators.singenN(self); }, + //150 => { generators.adsr(self); }, + //ENDOPCODES else => {}, } @@ -62,64 +62,36 @@ pub const Activity = struct { pub fn relay(self: *Activity) void { - self.soundnode.add_r_amp(self.soundnode.in_wire * self.soundnode.gain); - self.soundnode.add_r_amp(self.soundnode.in_air * self.soundnode.gain); + self.soundnode.add_r_amp(self.soundnode.pins[1] * self.soundnode.pins[32]); + self.soundnode.add_r_amp(self.soundnode.pins[2] * self.soundnode.pins[32]); } pub fn copy(self: *Activity) void { - - const in_pin: usize = @intFromFloat(self.operands[0]); - const out_pin: usize = @intFromFloat(self.operands[1]); - - var in_value: f64 = 0; + + const from_pin: usize = @intFromFloat(self.operands[0]); + const to_pin: usize = @intFromFloat(self.operands[1]); - switch (in_pin) { + // Special case: setting freq results in setting calculating new phase too + if (to_pin >= 64 and to_pin < 112) { - 0 => { in_value = self.soundnode.in_wire; }, - 1 => { in_value = self.soundnode.in_air; }, - 2 => { in_value = self.soundnode.in_gain; }, - 3 => { in_value = self.soundnode.in_basephase; }, - 4 => { in_value = self.soundnode.in_phase; }, - 5 => { in_value = self.soundnode.in_basefreq; }, - 6 => { in_value = self.soundnode.in6; }, - 7 => { in_value = self.soundnode.in7; }, - 8 => { in_value = self.soundnode.in8; }, - 9 => { in_value = self.soundnode.in9; }, - 10 => { in_value = self.soundnode.in10; }, - 11 => { in_value = self.soundnode.in11; }, - else => {}, - } - - switch (out_pin) { + const freq_no = to_pin - 64; - 0 => { self.soundnode.r_amp = in_value; }, - 1 => { self.soundnode.out1 = in_value; }, - 2 => { self.soundnode.gain = in_value; }, - 3 => { self.soundnode.basephase = in_value; }, - 4 => { self.soundnode.phase = in_value; }, - 5 => { + const fcurrent_tick: f64 = @floatFromInt(self.soundnode.fab.current_tick); + const prevphase = self.soundnode.pins[208 + freq_no]; + const prevfreq = self.soundnode.pins[to_pin]; + + const newfreq = self.soundnode.pins[from_pin]; + self.soundnode.pins[to_pin] = newfreq; + + const c = fcurrent_tick / 44100; + + const pp = prevphase + c * (newfreq - prevfreq) + @floor(c * prevfreq - prevphase); + + self.soundnode.pins[208 + freq_no] = pp - @floor(pp); - const fcurrent_tick: f64 = @floatFromInt(self.soundnode.fab.current_tick); - const prevphase = self.soundnode.phase; - const prevfreq = self.soundnode.basefreq; - - self.soundnode.basefreq = in_value; - const c = fcurrent_tick / 44100; - - const pp = prevphase + c * (self.soundnode.basefreq - prevfreq) + @floor(c * prevfreq - prevphase); - - self.soundnode.phase = pp - @floor(pp); - - }, - 6 => { self.soundnode.out6 = in_value; }, - 7 => { self.soundnode.out7 = in_value; }, - 8 => { self.soundnode.out8 = in_value; }, - 9 => { self.soundnode.out9 = in_value; }, - 10 => { self.soundnode.out10 = in_value; }, - 11 => { self.soundnode.out11 = in_value; }, - else => {}, - + } else { + self.soundnode.pins[to_pin] = self.soundnode.pins[from_pin]; } } @@ -127,7 +99,7 @@ pub const Activity = struct { pub fn mute(self: *Activity) void { self.soundnode.fab.set_r_amp(0); - self.soundnode.r_amp = 0; + self.soundnode.pins[0] = 0; } diff --git a/zigsonnum/link.zig b/zigsonnum/link.zig new file mode 100644 index 0000000..535e071 --- /dev/null +++ b/zigsonnum/link.zig @@ -0,0 +1,92 @@ +const std = @import("std"); +const print = std.debug.print; +const SoundNode = @import("soundnode.zig").SoundNode; +const Allocator = std.mem.Allocator; + + +pub const Link = struct { + + allocator: Allocator, + + src_node: *SoundNode, + trg_node: *SoundNode, + + src_pin: usize = 0, + trg_pin: usize = 0, + + active: u8 = 0, + + pub fn create(allocator: Allocator, + src_node: *SoundNode, + trg_node: *SoundNode, + src_pin: usize, + trg_pin: usize) !*Link { + + const link = try allocator.create(Link); + + link.* = .{ + .allocator = allocator, + .src_node = src_node, + .trg_node = trg_node, + .src_pin = src_pin, + .trg_pin = trg_pin, + .active = 0, + }; + + return link; + } + + pub fn propagate(self: *Link) void { + + // Special case for air_in (pin 2) + if (self.trg_pin == 2) { + + const current_tick = self.src_node.fab.current_tick; + + const dist: f64 = self.trg_node.distance(self.src_node); + 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) { + + 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 = self.src_node.fab.get_r_amp(b_sample_tick); + const n_r_amp = self.src_node.fab.get_r_amp(n_sample_tick); + + const tick_diff = tck - @as(f64, @floatFromInt(b_sample_tick)); + self.trg_node.pins[2] += (b_r_amp + (tick_diff * (n_r_amp - b_r_amp))) * attenuation; + + } + + // Special case: setting freq results in setting calculating new phase too + } else if (self.trg_pin >= 64 and self.trg_pin < 112) { + + const freq_no = self.trg_pin - 64; + + const fcurrent_tick: f64 = @floatFromInt(self.src_node.fab.current_tick); + const prevphase = self.trg_node.pins[208 + freq_no]; + const prevfreq = self.trg_node.pins[self.trg_pin]; + + const newfreq = self.src_node.pins[self.src_pin]; + self.trg_node.pins[self.trg_pin] = newfreq; + + const c = fcurrent_tick / 44100; + + const pp = prevphase + c * (newfreq - prevfreq) + @floor(c * prevfreq - prevphase); + + self.trg_node.pins[208 + freq_no] = pp - @floor(pp); + + } else { + + self.trg_node.pins[self.trg_pin] += self.src_node.pins[self.src_pin]; + + } + + } + +}; \ No newline at end of file diff --git a/zigsonnum/pin.zig b/zigsonnum/pin.zig deleted file mode 100644 index 391003a..0000000 --- a/zigsonnum/pin.zig +++ /dev/null @@ -1,265 +0,0 @@ -const std = @import("std"); -const print = std.debug.print; -const SoundNode = @import("soundnode.zig").SoundNode; -const Allocator = std.mem.Allocator; - - -pub const Pin = struct { - - allocator: Allocator, - - src_node: *SoundNode, - trg_node: *SoundNode, - - src_pin: usize = 0, - trg_pin: usize = 0, - - active: u8 = 0, - - pub fn create(allocator: Allocator, - src_node: *SoundNode, - trg_node: *SoundNode, - src_pin: usize, - trg_pin: usize) !*Pin { - - const pin = try allocator.create(Pin); - - pin.* = .{ - .allocator = allocator, - .src_node = src_node, - .trg_node = trg_node, - .src_pin = src_pin, - .trg_pin = trg_pin, - .active = 0, - }; - - return pin; - } - - pub fn propagate(self: *Pin) void { - - switch (self.src_pin) { - - 0 => { - switch (self.trg_pin) { - 0 => {self.trg_node.in_wire += self.src_node.r_amp; }, - 1 => { - const current_tick = self.src_node.fab.current_tick; - - const dist: f64 = self.trg_node.distance(self.src_node); - 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) { - - 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 = self.src_node.fab.get_r_amp(b_sample_tick); - const n_r_amp = self.src_node.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.trg_node.in_air += relayed_r_amp; - } - - }, - 6 => {self.trg_node.in6 += self.src_node.r_amp; }, - 7 => {self.trg_node.in7 += self.src_node.r_amp; }, - else => {}, - } - }, - - 1 => { - switch (self.trg_pin) { - 0 => {self.trg_node.in_wire += self.src_node.out1; }, - 1 => { - const current_tick = self.src_node.fab.current_tick; - const dist: f64 = self.trg_node.distance(self.src_node); - 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) { - - 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 = self.src_node.fab.get_r_amp(b_sample_tick); - const n_r_amp = self.src_node.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.trg_node.in_air += relayed_r_amp; - } - - }, - 6 => {self.trg_node.in6 += self.src_node.out1; }, - 7 => {self.trg_node.in7 += self.src_node.out1; }, - else => {}, - } - }, - - 6 => { - switch (self.trg_pin) { - 0 => {self.trg_node.in_wire += self.src_node.out6; }, - 1 => { - const current_tick = self.src_node.fab.current_tick; - const dist: f64 = self.trg_node.distance(self.src_node); - 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) { - - 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 = self.src_node.fab.get_r_amp(b_sample_tick); - const n_r_amp = self.src_node.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.trg_node.in_air += relayed_r_amp; - } - - }, - 6 => {self.trg_node.in6 += self.src_node.out6; }, - 7 => {self.trg_node.in7 += self.src_node.out6; }, - else => {}, - } - }, - - 7 => { - switch (self.trg_pin) { - 0 => {self.trg_node.in_wire += self.src_node.out7; }, - 1 => { - const current_tick = self.src_node.fab.current_tick; - const dist: f64 = self.trg_node.distance(self.src_node); - 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) { - - 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 = self.src_node.fab.get_r_amp(b_sample_tick); - const n_r_amp = self.src_node.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.trg_node.in_air += relayed_r_amp; - } - - }, - 6 => {self.trg_node.in6 += self.src_node.out7; }, - 7 => {self.trg_node.in7 += self.src_node.out7; }, - else => {}, - } - }, - - 2 => { - switch (self.trg_pin) { - 2 => {self.trg_node.in_gain += self.src_node.gain; }, - 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; }, - else => {}, - } - }, - - 3 => { - switch (self.trg_pin) { - 2 => {self.trg_node.in_gain += self.src_node.basephase; }, - 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; }, - else => {}, - } - }, - - 4 => { - switch (self.trg_pin) { - 2 => {self.trg_node.in_gain += self.src_node.phase; }, - 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; }, - else => {}, - } - }, - - 8 => { - switch (self.trg_pin) { - 2 => {self.trg_node.in_gain += self.src_node.out8; }, - 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; }, - else => {}, - } - }, - - 9 => { - switch (self.trg_pin) { - 2 => {self.trg_node.in_gain += self.src_node.out9; }, - 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; }, - else => {}, - } - }, - - - 5 => { - 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; }, - else => {}, - } - }, - - 10 => { - 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; }, - else => {}, - } - }, - - 11 => { - 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; }, - else => {}, - } - }, - - else => {}, - - } - - } - -}; \ No newline at end of file diff --git a/zigsonnum/sonnum.zig b/zigsonnum/sonnum.zig index 76ead44..4a121c2 100644 --- a/zigsonnum/sonnum.zig +++ b/zigsonnum/sonnum.zig @@ -6,7 +6,7 @@ const ArrayList = std.ArrayList; const Endian = std.builtin.Endian; const SoundNode = @import("soundnode.zig").SoundNode; -const Pin = @import ("pin.zig").Pin; +const Link = @import ("link.zig").Link; const Activity = @import("activity.zig").Activity; const SoundSettings = @import("settings.zig").SoundSettings; const utility = @import("utility.zig"); @@ -223,7 +223,10 @@ pub fn main() !void { 0 => { const uid: usize = @as(usize, src_node); - const sn = try SoundNode.create(allocator, uid); + const freq_q: u8 = @truncate(trg_node); + + const sn = try SoundNode.create(allocator, uid, freq_q); + //print("Added node {s} at tick {d}\n", .{nodename, tick}); try soundnodes.append(sn); try p_soundnodes.append(sn); @@ -238,8 +241,8 @@ pub fn main() !void { const src_pin: usize = @intFromFloat(op1); const trg_pin: usize = @intFromFloat(op2); - const pin = try Pin.create(allocator, src, trg, src_pin, trg_pin); - try trg.pins.append(pin); + const link = try Link.create(allocator, src, trg, src_pin, trg_pin); + try trg.links.append(link); }, 2 => { @@ -258,24 +261,8 @@ pub fn main() !void { const sn = soundnodes.items[src_node]; const sn_pin: usize = @intFromFloat(op1); - switch (sn_pin) { - - 0 => { sn.r_amp = op2; }, - 1 => { sn.out1 = op2; }, - 2 => { sn.gain = op2; }, - 3 => { sn.basephase = op2; }, - 4 => { sn.phase = op2; }, - 5 => { sn.basefreq = op2; }, - 6 => { sn.out6 = op2; }, - 7 => { sn.out7 = op2; }, - 8 => { sn.out8 = op2; }, - 9 => { sn.out9 = op2; }, - 10 => { sn.out10 = op2; }, - 11 => { sn.out11 = op2; }, - else => {}, - - } - + sn.pins[sn_pin] = op2; + }, 9 => { @@ -316,10 +303,10 @@ pub fn main() !void { for (a_soundnodes.items[2..], 0..) |soundnode, i| { - for (soundnode.pins.items, 0..) |pin, j| { + for (soundnode.links.items, 0..) |link, j| { - if (pin.src_node.active == 1 or pin.trg_pin == 1) { - pin.propagate(); + if (link.src_node.active == 1 or link.trg_pin == 1) { + link.propagate(); } _ = j; } @@ -374,9 +361,9 @@ 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(); + for (soundnode.links.items, 0..) |link, jz| { + if (link.src_node.active == 1 or link.trg_pin == 1) { + link.propagate(); } _ = jz; } @@ -415,22 +402,26 @@ pub fn main() !void { left = soundnodes.items[0]; right = soundnodes.items[1]; - if (left.r_amp > 1) { - left.r_amp = 1; - } else if (left.r_amp < -1) { - left.r_amp = -1; + var left_r_amp = left.pins[0]; + + if (left_r_amp > 1) { + left_r_amp = 1; + } else if (left_r_amp < -1) { + left_r_amp = -1; } - sample = @intFromFloat(left.r_amp * settings.max_amp_multiplier); + sample = @intFromFloat(left_r_amp * settings.max_amp_multiplier); try stdout.writeInt(i24, sample, Endian.little); - if (right.r_amp > 1) { - right.r_amp = 1; - } else if (right.r_amp < -1) { - right.r_amp = -1; + var right_r_amp = right.pins[0]; + + if (right_r_amp > 1) { + right_r_amp = 1; + } else if (right_r_amp < -1) { + right_r_amp = -1; } - sample = @intFromFloat(right.r_amp * settings.max_amp_multiplier); + sample = @intFromFloat(right_r_amp * settings.max_amp_multiplier); try stdout.writeInt(i24, sample, Endian.little); left.fab.current_tick += 1; @@ -440,5 +431,6 @@ pub fn main() !void { } try bw.flush(); + print("\n", .{}); } \ No newline at end of file diff --git a/zigsonnum/soundnode.zig b/zigsonnum/soundnode.zig index 4c16c6b..dcd4096 100644 --- a/zigsonnum/soundnode.zig +++ b/zigsonnum/soundnode.zig @@ -10,7 +10,7 @@ const StringHashMap = std.StringHashMap; const pnt = @import("point.zig"); const Pnt = pnt.Pnt; -const Pin = @import("pin.zig").Pin; +const Link = @import("link.zig").Link; const Activity = @import("activity.zig").Activity; const SoundSettings = @import("settings.zig").SoundSettings; const FreqAmpBuffer = @import("freqamp.zig").FreqAmpBuffer; @@ -24,171 +24,81 @@ pub const SoundNode = struct { allocator: Allocator, uid: usize = 0, + freq_q: u8, - // Input pins - in_wire: f64, - in_air: f64, - in_gain: f64, - in_basephase: f64, - in_phase: f64, - in_basefreq: f64, - in6: f64, - in7: f64, - in8: f64, - in9: f64, - in10: f64, - in11: f64, - - // Output pins - - r_amp: f64, - out1: f64, - gain: f64, - basephase: f64, - phase: f64, - basefreq: f64, - out6: f64, - out7: f64, - out8: f64, - out9: f64, - out10: f64, - out11: f64, + pins: [256]f64, activities: ArrayList(*Activity), - pins: ArrayList(*Pin), + links: ArrayList(*Link), + props: StringHashMap(f64), - - x: f32 = 0, - y: f32 = 0, - z: f32 = 0, - + wantprint: bool = false, fab: FreqAmpBuffer, active: u8 = 1, + + pub fn create(allocator: Allocator, uid: usize, freq_q: u8) !*SoundNode { + + const activities = ArrayList(*Activity).init(allocator); + const links = ArrayList(*Link).init(allocator); + const fab = FreqAmpBuffer{}; + const props = StringHashMap(f64).init(allocator); + + const sn = try allocator.create(SoundNode); + + + sn.* = .{ + .uid = uid, + .freq_q = freq_q, + + .pins = std.mem.zeroes([256]f64), + + .allocator = allocator, + + .activities = activities, + .links = links, + .fab = fab, + .props = props, + + .wantprint = false, + .active = 0, + }; + + //Gain and per-freq gains must be initialized as 1 + + sn.pins[32] = 1; + @memset(sn.pins[112..160], 1); + + return sn; + + } + pub fn printState(self: *SoundNode) void { const fcurrent_tick: f64 = @floatFromInt(self.current_tick()); print("\n===SOUNDNODE {d} TICK {d} SECOND {d}===\n", .{self.uid, fcurrent_tick, fcurrent_tick/44100}); - - print(">> IN_WIRE = {d}\n", .{self.in_wire}); - print(">> IN_AIR = {d}\n", .{self.in_air}); - print(">> IN_GAIN = {d}\n", .{self.in_gain}); - print(">> IN_BASEPHASE = {d}\n", .{self.in_basephase}); - print(">> IN_PHASE = {d}\n", .{self.in_phase}); - print(">> IN_BASEFREQ = {d}\n", .{self.in_basefreq}); - print(">> IN6 = {d}\n", .{self.in6}); - print(">> IN7 = {d}\n", .{self.in7}); - print(">> IN8 = {d}\n", .{self.in8}); - print(">> IN9 = {d}\n", .{self.in9}); - print(">> IN10 = {d}\n", .{self.in10}); - print(">> IN11 = {d}\n", .{self.in11}); - - print("-----------------------\n", .{}); - - print(">> R_AMP = {d}\n", .{self.r_amp}); - print(">> OUT1 = {d}\n", .{self.out1}); - print(">> GAIN = {d}\n", .{self.gain}); - print(">> BASEPHASE = {d}\n", .{self.basephase}); - print(">> PHASE = {d}\n", .{self.phase}); - print(">> BASEFREQ = {d}\n", .{self.basefreq}); - print(">> OUT6 = {d}\n", .{self.out6}); - print(">> OUT7 = {d}\n", .{self.out7}); - print(">> OUT8 = {d}\n", .{self.out8}); - print(">> OUT9 = {d}\n", .{self.out9}); - print(">> OUT10 = {d}\n", .{self.out10}); - print(">> OUT11 = {d}\n", .{self.out11}); - + print("======================\n", .{}); } pub fn nullizeStartTick(self: *SoundNode) void { - self.in_wire = 0; - self.in_air = 0; - self.in_gain = 0; - self.in_basephase = 0; - self.in_phase = 0; - self.in_basefreq = 0; - self.in6 = 0; - self.in7 = 0; - self.in8 = 0; - self.in9 = 0; - self.in10 = 0; - self.in11 = 0; - - self.r_amp = 0; + // Zeroing the first 32 pins (dynamic pins) + @memset(self.pins[0..32], 0); } pub fn corrGain(self: *SoundNode, gainmult: f64) f64 { if (gainmult > 0) { - return self.gain * gainmult; + return self.pins[32] * gainmult; } else { - return self.gain; + return self.pins[32]; } } - - pub fn totalPhase(self: *SoundNode) f64 { - return self.phase + self.basephase; - } - - pub fn create(allocator: Allocator, uid: usize) !*SoundNode { - - const activities = ArrayList(*Activity).init(allocator); - const pins = ArrayList(*Pin).init(allocator); - const fab = FreqAmpBuffer{}; - const props = StringHashMap(f64).init(allocator); - - const sn = try allocator.create(SoundNode); - - sn.* = .{ - .uid = uid, - .allocator = allocator, - - .in_wire = 0, - .in_air = 0, - .in_gain = 0, - .in_basephase = 0, - .in_phase = 0, - .in_basefreq = 0, - .in6 = 0, - .in7 = 0, - .in8 = 0, - .in9 = 0, - .in10 = 0, - .in11 = 0, - - .r_amp = 0, - .out1 = 0, - .gain = 1, - .basephase = 0, - .phase = 0, - .basefreq = 0, - .out6 = 0, - .out7 = 0, - .out8 = 0, - .out9 = 0, - .out10 = 0, - .out11 = 0, - - .activities = activities, - .pins = pins, - .fab = fab, - .props = props, - .x = 0, - .y = 0, - .z = 0, - .wantprint = false, - .active = 0, - }; - - return sn; - - } pub fn deinit(self: *SoundNode) void { @@ -207,7 +117,7 @@ pub const SoundNode = struct { pub fn add_r_amp(self: *SoundNode, r_amp: f64) void { self.fab.add_r_amp(r_amp); - self.r_amp += r_amp; + self.pins[0] += r_amp; } @@ -230,143 +140,13 @@ pub const SoundNode = struct { pub fn distance(self: *SoundNode, other: *SoundNode) f64 { - if ((self.x == other.x) and (self.y == other.y) and (self.z == other.z)) { + if ((self.pins[61] == other.pins[61]) and (self.pins[62] == other.pins[62]) and (self.pins[63] == other.pins[63])) { return 0; } - const dx: f32 = self.x - other.x; - const dy: f32 = self.y - other.y; - const dz: f32 = self.z - other.z; - - return math.sqrt(dx*dx + dy*dy + dz*dz); - - } - -}; - - - -pub const SoundNodeOld = struct { - - allocator: Allocator, - - name: []const u8 = "soundnode", - uid: usize = 0, - - air_in: ArrayList(*SoundNode), - wire_in: ArrayList(*SoundNode), - - air_targets: ArrayList(*SoundNode), - wire_targets: ArrayList(*SoundNode), - - activities: ArrayList(*Activity), - props: StringHashMap(f64), - - x: f32 = 0, - y: f32 = 0, - z: f32 = 0, - - fab: FreqAmpBuffer, - active: u8 = 1, - - pub fn create(allocator: Allocator, name: []const u8, uid: usize) !*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); - - const sn = try allocator.create(SoundNode); - - sn.* = .{ - .uid = uid, - .allocator = allocator, - .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; - - } - - pub fn init(allocator: Allocator, name: []const u8) !SoundNode { - - const air_in = ArrayList(*SoundNode).init(allocator); - const wire_in = ArrayList(*SoundNode).init(allocator); - const activities = ArrayList(*Activity).init(allocator); - const fab = try FreqAmpBuffer.init(allocator); - const props = StringHashMap(f64).init(allocator); - - return .{ - .allocator = allocator, - .name = name, - .air_in = air_in, - .wire_in = wire_in, - .activities = activities, - .fab = fab, - .props = props, - .x = 0, - .y = 0, - .z = 0, - }; - - } - - pub fn deinit(self: *SoundNode) void { - - self.air_in.deinit(); - self.wire_in.deinit(); - self.activities.deinit(); - self.fab.deinit(); - self.props.deinit(); - - } - - pub fn current_tick(self: *SoundNode) u32 { - - return self.fab.current_tick; - - } - - pub fn g(self: *SoundNode, key: []const u8) f64 { - - const value = self.props.get(key); - - if (value) |v| { - return v; - } else { - return @as(f64, 0.0); - } - } - - pub fn s(self: *SoundNode, key: []const u8, value: f64) !void { - - try self.props.put(key, value); - - } - - pub fn distance(self: *SoundNode, other: *SoundNode) f64 { - - if ((self.x == other.x) and (self.y == other.y) and (self.z == other.z)) { - return 0; - } - - const dx: f32 = self.x - other.x; - const dy: f32 = self.y - other.y; - const dz: f32 = self.z - other.z; + const dx = self.pins[61] - other.pins[61]; + const dy = self.pins[62] - other.pins[62]; + const dz = self.pins[63] - other.pins[63]; return math.sqrt(dx*dx + dy*dy + dz*dz);