From 1e2f8d78e89ea5a32c0dd8ca77746b6eb32601e1 Mon Sep 17 00:00:00 2001 From: aprilnightk Date: Wed, 17 Sep 2025 16:19:02 +0300 Subject: [PATCH] pin bridging --- mysynths/sintest.py | 12 +++++- pysonnum/activity.py | 1 + pysonnum/compiler.py | 27 +++++++++++++ pysonnum/sonnum.py | 17 +------- pysonnum/soundnode.py | 3 ++ zigsonnum/activities/basicsynths.zig | 12 +++--- zigsonnum/activity.zig | 59 ++++++++++++++++++++++++++++ zigsonnum/soundnode.zig | 4 ++ 8 files changed, 113 insertions(+), 22 deletions(-) diff --git a/mysynths/sintest.py b/mysynths/sintest.py index fca8bf6..4bb84a4 100644 --- a/mysynths/sintest.py +++ b/mysynths/sintest.py @@ -1,9 +1,19 @@ s.setup(s.sec(10)) +m = s.node() n = s.node() -n.setpin(0, s.BASEFREQ, s.note("D4")) +s.pin(m, R_AMP, n, IN_GAIN) + +m.setpin(0, BASEFREQ, 6) + + +n.setpin(0, BASEFREQ, s.note("D4")) +n.bridge(0, s.sec(10), IN_GAIN, GAIN) + +m.sine(0, s.sec(10)) n.sine(0, s.sec(10)) + s.wire(n, s.left) s.wire(n, s.right) \ No newline at end of file diff --git a/pysonnum/activity.py b/pysonnum/activity.py index d48ab00..539d582 100644 --- a/pysonnum/activity.py +++ b/pysonnum/activity.py @@ -7,6 +7,7 @@ OPCODES = { 'endtick': 2, 'setpin': 3, 'relay': 4, + 'bridge': 4, 'setpos': 10, diff --git a/pysonnum/compiler.py b/pysonnum/compiler.py index c3051a9..19a648c 100644 --- a/pysonnum/compiler.py +++ b/pysonnum/compiler.py @@ -3,6 +3,33 @@ from .activity import Activity 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: def __init__(self): diff --git a/pysonnum/sonnum.py b/pysonnum/sonnum.py index 93b2990..e795166 100644 --- a/pysonnum/sonnum.py +++ b/pysonnum/sonnum.py @@ -2,20 +2,7 @@ from .soundnode import SoundNode from .notes import * class Sonnum: - - 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 - + def __init__(self, compiler): self.c = compiler @@ -106,7 +93,7 @@ class Sonnum: for trg_node in trg_nodes: self.act('pin', 0, 0, src_node, trg_node, [0, 1]) - def pin(self, src_nodes, trg_nodes, src_pin, trg_pin): + def pin(self, src_nodes, src_pin, trg_nodes, trg_pin): if not isinstance(src_nodes, list): src_nodes = [src_nodes] diff --git a/pysonnum/soundnode.py b/pysonnum/soundnode.py index 295bf01..1f5bb62 100644 --- a/pysonnum/soundnode.py +++ b/pysonnum/soundnode.py @@ -27,6 +27,9 @@ class SoundNode: def setpin(self, start_tick, pin_no, value): self.act('setpin', start_tick, start_tick, self, None, [pin_no, value]) + def bridge(self, start_tick, end_tick, in_pin, out_pin): + self.act('bridge', 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]) diff --git a/zigsonnum/activities/basicsynths.zig b/zigsonnum/activities/basicsynths.zig index 5be0643..ee2c767 100644 --- a/zigsonnum/activities/basicsynths.zig +++ b/zigsonnum/activities/basicsynths.zig @@ -14,7 +14,7 @@ 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.basephase * utility.tau); + const amp = math.sin(utility.corrected_tau * self.soundnode.basefreq * current_tick - self.soundnode.totalPhase() * utility.tau); const final_amp = gain * amp; self.soundnode.add_r_amp(final_amp); @@ -29,7 +29,7 @@ pub fn triangle(self: *Activity) void { const gain = self.soundnode.corrGain(self.operands[0]); const period = 44100 / self.soundnode.basefreq; - const tp = (current_tick - (period * self.soundnode.basephase)) / period; + const tp = (current_tick - (period * self.soundnode.totalPhase())) / period; const amp = @abs(2 * (2 * ( tp - @floor(tp + 0.5) ) )) - 1; const final_amp = gain * amp; @@ -45,7 +45,7 @@ pub fn square(self: *Activity) void { const gain = self.soundnode.corrGain(self.operands[0]); - const sin = math.sin(utility.corrected_tau * self.soundnode.basefreq * current_tick - self.soundnode.basephase * utility.tau); + const sin = math.sin(utility.corrected_tau * self.soundnode.basefreq * current_tick - self.soundnode.totalPhase() * utility.tau); if (sin > 0) { self.soundnode.add_r_amp(gain); @@ -63,7 +63,7 @@ pub fn sawtooth(self: *Activity) void { const gain = self.soundnode.corrGain(self.operands[0]); const period = 44100 / self.soundnode.basefreq; - const tp = (current_tick - (period * self.soundnode.basephase)) / period; + const tp = (current_tick - (period * self.soundnode.totalPhase())) / period; const amp = 2 * (tp - @floor(0.5+tp)); const final_amp = gain * amp; @@ -84,11 +84,11 @@ pub fn skewsine(self: *Activity) void { if (skew == 0) { - amp = math.sin(utility.corrected_tau * self.soundnode.basefreq * current_tick - self.soundnode.basephase * utility.tau); + amp = math.sin(utility.corrected_tau * self.soundnode.basefreq * current_tick - self.soundnode.totalPhase() * utility.tau); } else if (skew > 0) { - const m = (utility.corrected_tau * self.soundnode.basefreq * current_tick - self.soundnode.basephase * utility.tau); + 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); diff --git a/zigsonnum/activity.zig b/zigsonnum/activity.zig index 9349bda..ac9187b 100644 --- a/zigsonnum/activity.zig +++ b/zigsonnum/activity.zig @@ -35,6 +35,7 @@ pub const Activity = struct { pub fn do(self: *Activity) !void { switch (self.opcode) { 4 => { self.relay(); }, + 5 => { self.bridge(); }, 10 => { spatial.setpos(self); }, 50 => { basicsynths.sine(self); }, 51 => { basicsynths.triangle(self); }, @@ -49,4 +50,62 @@ pub const Activity = struct { } + pub fn bridge(self: *Activity) void { + + const in_pin: usize = @intFromFloat(self.operands[0]); + const out_pin: usize = @intFromFloat(self.operands[1]); + + var in_value: f64 = 0; + + switch (in_pin) { + + 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) { + + 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.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 => {}, + + } + + + } + }; \ No newline at end of file diff --git a/zigsonnum/soundnode.zig b/zigsonnum/soundnode.zig index 8ca809e..9125380 100644 --- a/zigsonnum/soundnode.zig +++ b/zigsonnum/soundnode.zig @@ -93,6 +93,10 @@ pub const SoundNode = struct { } } + 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);