From 3ec8cd4704d3387a78df891278de4f0466dbbb03 Mon Sep 17 00:00:00 2001 From: aprilnightk Date: Wed, 17 Sep 2025 22:14:41 +0300 Subject: [PATCH] all basicsynths so far --- mysynths/sintest.py | 8 +++++--- pysonnum/activity.py | 8 ++++---- zigsonnum/activities/basicsynths.zig | 23 ++++++++++++++++++++++- zigsonnum/activity.zig | 8 ++++++++ zigsonnum/sonnum.zig | 15 +++++++++++++-- 5 files changed, 52 insertions(+), 10 deletions(-) diff --git a/mysynths/sintest.py b/mysynths/sintest.py index 9c19490..1c0f262 100644 --- a/mysynths/sintest.py +++ b/mysynths/sintest.py @@ -8,10 +8,12 @@ s.pin(m, BASEFREQ, n, IN_BASEFREQ) 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, 330) +n.setpin(0, OUT8, 0.3) +n.pulse(0, s.sec(10)) -n.printstate(1, 1) -n.printstate(20000, 20000) +n.printstate(30000, 30000) +n.printstate(30001, 30001) 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 9463356..b6fe4dc 100644 --- a/pysonnum/activity.py +++ b/pysonnum/activity.py @@ -14,19 +14,19 @@ OPCODES = { 'sine': 50, 'triangle': 51, + 'square': 52, + 'sawtooth': 53, + 'skewsine': 54, + 'pulse': 55, 'singenN': 100, - 'square': 10, - 'sawtooth': 11, 'setskew': 12, - 'skewsine': 14, 'slidebasefreq': 15, 'slidegain': 16, 'slidephase': 17, 'slideskew': 18, 'slidepos': 19, - 'pulse': 20, 'fmsetup': 21, 'fm': 22, 'am': 23, diff --git a/zigsonnum/activities/basicsynths.zig b/zigsonnum/activities/basicsynths.zig index ee2c767..23166d7 100644 --- a/zigsonnum/activities/basicsynths.zig +++ b/zigsonnum/activities/basicsynths.zig @@ -104,4 +104,25 @@ pub fn skewsine(self: *Activity) void { self.soundnode.add_r_amp(final_amp); -} \ No newline at end of file +} + +// Produces a pulse wave to r_amp pin +// Uses OUT8 as pulse phase +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 amp = 2 * (tp - @floor(0.5 + tp)); + const amp_detract = 2 * (tp_detract - @floor(0.5+tp_detract)); + + const final_amp = gain * (amp - amp_detract); + + self.soundnode.add_r_amp(final_amp); + +} diff --git a/zigsonnum/activity.zig b/zigsonnum/activity.zig index 04c4ae9..b7e8e59 100644 --- a/zigsonnum/activity.zig +++ b/zigsonnum/activity.zig @@ -34,7 +34,9 @@ pub const Activity = struct { } pub fn do(self: *Activity) !void { + switch (self.opcode) { + 4 => { self.relay(); }, 5 => { self.bridge(); }, @@ -42,11 +44,17 @@ pub const Activity = struct { 50 => { basicsynths.sine(self); }, 51 => { basicsynths.triangle(self); }, + 52 => { basicsynths.square(self); }, + 53 => { basicsynths.sawtooth(self); }, + 54 => { basicsynths.skewsine(self); }, + 55 => { basicsynths.pulse(self); }, 100 => { generators.singenN(self); }, else => {}, + } + } pub fn relay(self: *Activity) void { diff --git a/zigsonnum/sonnum.zig b/zigsonnum/sonnum.zig index 4ee9c97..76ead44 100644 --- a/zigsonnum/sonnum.zig +++ b/zigsonnum/sonnum.zig @@ -282,7 +282,6 @@ pub fn main() !void { const sn = soundnodes.items[src_node]; sn.wantprint = true; - sn.printState(); }, @@ -416,9 +415,21 @@ 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; + } + 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; + } + sample = @intFromFloat(right.r_amp * settings.max_amp_multiplier); try stdout.writeInt(i24, sample, Endian.little);