From f5137de7bbe71f5db10abd3a30f8659e5b88bc45 Mon Sep 17 00:00:00 2001 From: aprilnightk Date: Wed, 17 Sep 2025 17:10:01 +0300 Subject: [PATCH] working bridging, starting generators --- mysynths/sintest.py | 14 +++++----- pysonnum/activity.py | 5 +++- pysonnum/soundnode.py | 6 +++++ zigsonnum/activities/generators.zig | 26 +++++++++++++++++++ zigsonnum/activity.zig | 8 +++++- zigsonnum/sonnum.zig | 17 ++++++++++++ zigsonnum/soundnode.zig | 40 +++++++++++++++++++++++++++++ 7 files changed, 106 insertions(+), 10 deletions(-) create mode 100644 zigsonnum/activities/generators.zig diff --git a/mysynths/sintest.py b/mysynths/sintest.py index 4bb84a4..9c19490 100644 --- a/mysynths/sintest.py +++ b/mysynths/sintest.py @@ -3,17 +3,15 @@ s.setup(s.sec(10)) m = s.node() n = s.node() -s.pin(m, R_AMP, n, IN_GAIN) +s.pin(m, BASEFREQ, n, IN_BASEFREQ) -m.setpin(0, BASEFREQ, 6) +m.singenN(0, s.sec(10), 50.4, 0, 0.003, 440) +n.bridge(0, s.sec(10), IN_BASEFREQ, BASEFREQ) +n.triangle(0, s.sec(10)) -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)) - +n.printstate(1, 1) +n.printstate(20000, 20000) 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 539d582..9463356 100644 --- a/pysonnum/activity.py +++ b/pysonnum/activity.py @@ -7,12 +7,15 @@ OPCODES = { 'endtick': 2, 'setpin': 3, 'relay': 4, - 'bridge': 4, + 'bridge': 5, + 'printstate': 9, 'setpos': 10, 'sine': 50, 'triangle': 51, + + 'singenN': 100, 'square': 10, 'sawtooth': 11, diff --git a/pysonnum/soundnode.py b/pysonnum/soundnode.py index 1f5bb62..2cf2bd0 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 printstate(self, start_tick, end_tick): + self.act('printstate', start_tick, end_tick, self, None, []) + def bridge(self, start_tick, end_tick, in_pin, out_pin): self.act('bridge', start_tick, end_tick, self, None, [in_pin, out_pin]) @@ -41,6 +44,9 @@ class SoundNode: 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]) diff --git a/zigsonnum/activities/generators.zig b/zigsonnum/activities/generators.zig new file mode 100644 index 0000000..e5f7088 --- /dev/null +++ b/zigsonnum/activities/generators.zig @@ -0,0 +1,26 @@ +const std = @import("std"); +const math = std.math; +const print = std.debug.print; +const Activity = @import ("../activity.zig").Activity; +const SoundNode = @import("../soundnode.zig").SoundNode; +const utility = @import("../utility.zig"); +const dbg = utility.dbg; +const idbg = utility.idbg; + + +// Produces a sine wave to r_amp pin +pub fn singenN(self: *Activity) void { + + const current_tick: f64 = @floatFromInt(self.soundnode.fab.current_tick); + const magnitude = self.operands[0]; + const phase = self.operands[1]; + const freq = self.operands[2]; + const ylevel = self.operands[3]; + + const y = magnitude * math.sin(current_tick * freq + phase) + ylevel; + + self.soundnode.basefreq = y; + self.soundnode.out10 = y; + self.soundnode.out11 = y; + +} \ No newline at end of file diff --git a/zigsonnum/activity.zig b/zigsonnum/activity.zig index ac9187b..04c4ae9 100644 --- a/zigsonnum/activity.zig +++ b/zigsonnum/activity.zig @@ -8,6 +8,7 @@ const utility = @import("utility.zig"); const basicsynths = @import ("activities/basicsynths.zig"); const spatial = @import ("activities/spatial.zig"); +const generators = @import ("activities/generators.zig"); pub const Activity = struct { @@ -36,9 +37,14 @@ pub const Activity = struct { switch (self.opcode) { 4 => { self.relay(); }, 5 => { self.bridge(); }, + 10 => { spatial.setpos(self); }, + 50 => { basicsynths.sine(self); }, 51 => { basicsynths.triangle(self); }, + + 100 => { generators.singenN(self); }, + else => {}, } } @@ -90,7 +96,7 @@ pub const Activity = struct { self.soundnode.basefreq = in_value; const c = fcurrent_tick / 44100; - const pp = prevphase + c*(self.soundnode.basefreq - prevfreq) + @floor(c * prevfreq - prevphase); + const pp = prevphase + c * (self.soundnode.basefreq - prevfreq) + @floor(c * prevfreq - prevphase); self.soundnode.phase = pp - @floor(pp); diff --git a/zigsonnum/sonnum.zig b/zigsonnum/sonnum.zig index 9116c50..4ee9c97 100644 --- a/zigsonnum/sonnum.zig +++ b/zigsonnum/sonnum.zig @@ -278,6 +278,14 @@ pub fn main() !void { }, + 9 => { + + const sn = soundnodes.items[src_node]; + sn.wantprint = true; + sn.printState(); + + }, + else => { const soundnode = soundnodes.items[src_node]; @@ -329,6 +337,10 @@ pub fn main() !void { } } + if (soundnode.wantprint == true) { + soundnode.wantprint = false; + soundnode.printState(); + } var j: usize = toremove.items.len; @@ -383,6 +395,11 @@ pub fn main() !void { } + if (soundnode.wantprint == true) { + soundnode.wantprint = false; + soundnode.printState(); + } + var j: usize = toremove.items.len; while (j > 0) { diff --git a/zigsonnum/soundnode.zig b/zigsonnum/soundnode.zig index 9125380..4c16c6b 100644 --- a/zigsonnum/soundnode.zig +++ b/zigsonnum/soundnode.zig @@ -62,9 +62,48 @@ pub const SoundNode = struct { y: f32 = 0, z: f32 = 0, + wantprint: bool = false, + fab: FreqAmpBuffer, active: u8 = 1, + 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; @@ -143,6 +182,7 @@ pub const SoundNode = struct { .x = 0, .y = 0, .z = 0, + .wantprint = false, .active = 0, };