diff --git a/mysynths/test.py b/mysynths/test.py index cb17c80..417c5af 100644 --- a/mysynths/test.py +++ b/mysynths/test.py @@ -3,24 +3,17 @@ s.setup(10) ;0 s.left.@setgain(0.7) ;0 s.right.@setgain(0.7) -ns = [] -for k in range(0, 9): - n = s.node(20) - ns.append(n) - ;n->s.left - ;n->s.right - - ;0 n.@setgain(0.04) - - ;0 n.setpos(1,1,1) - ;0 n.setpin(52, 0.9) - - for i in range(0,40): - ;0 n.@setfreq(i, s.note("C3")*(i+1)) - ;0 n.@setfreqgain(i, 1/float(i+1)) - ;n-/>s.left - ;n-/>s.right +sn = s.node(1) -;ns[4]->s.left -;ns[4]->s.right -;0-90 ns[4].sine() \ No newline at end of file +;0 sn.@setfreq(0, 4) +;0-10 sn.sine() + +sn2 = s.node(1) + + +;0 sn>sn2.link(0, 10) +;0 sn2.@wire_lr() +;0 sn2.@setfreq(0,440) +;0-10 sn2.rerange(10, 64, -1, 1, 400, 500) + +;0-10 sn2.triangle() \ No newline at end of file diff --git a/pysonnum/soundnode.py b/pysonnum/soundnode.py index 2dbbffa..b0102f2 100644 --- a/pysonnum/soundnode.py +++ b/pysonnum/soundnode.py @@ -42,6 +42,14 @@ class SoundNode: def act(self, *args): self.c.add_activity(*args) + def wire_lr(self, *args): + self.act("link", 0, 0, self, self.s.left, [0, 1]) + self.act("link", 0, 0, self, self.s.right, [0, 1]) + + def air_lr(self, *args): + self.act("link", 0, 0, self, self.s.left, [0, 2]) + self.act("link", 0, 0, self, self.s.right, [0, 2]) + def setfreq(self, starttick, endtick, args): freq_no = args[0] freq = args[1] diff --git a/zigsonnum/activities/basicsynths.zig b/zigsonnum/activities/basicsynths.zig index 774f4bb..21466a4 100644 --- a/zigsonnum/activities/basicsynths.zig +++ b/zigsonnum/activities/basicsynths.zig @@ -21,7 +21,8 @@ pub fn sine(self: *Activity) void { 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 phase = self.soundnode.pins[freq_pin + 144]; + 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); @@ -46,7 +47,7 @@ pub fn triangle(self: *Activity) void { 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 phase = self.soundnode.pins[freq_pin + 144]; const totalphase = self.soundnode.pins[33] + shift + phase; @@ -75,7 +76,7 @@ pub fn square(self: *Activity) void { 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 phase = self.soundnode.pins[freq_pin + 144]; const totalphase = self.soundnode.pins[33] + shift + phase; @@ -106,7 +107,7 @@ pub fn sawtooth(self: *Activity) void { 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 phase = self.soundnode.pins[freq_pin + 144]; const totalphase = self.soundnode.pins[33] + shift + phase; @@ -138,7 +139,7 @@ pub fn skewsine(self: *Activity) void { 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 phase = self.soundnode.pins[freq_pin + 144]; const totalphase = self.soundnode.pins[33] + shift + phase; @@ -172,9 +173,8 @@ pub fn pulse(self: *Activity) void { const current_tick: f64 = @floatFromInt(self.soundnode.fab.current_tick); - const maingain = self.soundnode.corrGain(self.operands[0]); - - const pulse_shift = self.soundnode.pins[@intFromFloat(self.operands[1])]; + const pulse_shift = self.soundnode.pins[@intFromFloat(self.operands[0])]; + const maingain = self.soundnode.corrGain(self.operands[1]); var final_amp: f64 = 0; @@ -209,8 +209,8 @@ pub fn whitenoise(self: *Activity) void { const current_tick: f64 = @floatFromInt(self.soundnode.fab.current_tick); - const seed = self.soundnode.pins[@intFromFloat(self.operands[1])]; - const maingain = self.soundnode.corrGain(self.operands[0]); + const seed = self.soundnode.pins[@intFromFloat(self.operands[0])]; + const maingain = self.soundnode.corrGain(self.operands[1]); const w = (seed*current_tick) / math.sin(current_tick); const amp = (2 * (w - @floor(w))) - 1; diff --git a/zigsonnum/activity.zig b/zigsonnum/activity.zig index eee8e75..2867105 100644 --- a/zigsonnum/activity.zig +++ b/zigsonnum/activity.zig @@ -40,6 +40,7 @@ pub const Activity = struct { 4 => { self.relay(); }, 5 => { self.copy(); }, 6 => { self.mute(); }, + 7 => { self.rerange(); }, 10 => { spatial.setpos(self); }, @@ -96,6 +97,48 @@ pub const Activity = struct { } + pub fn rerange(self: *Activity) void { + + const from_pin: usize = @intFromFloat(self.operands[0]); + const to_pin: usize = @intFromFloat(self.operands[1]); + + const from_lo = self.operands[2]; + const from_hi = self.operands[3]; + const to_lo = self.operands[4]; + const to_hi = self.operands[5]; + + // Special case: setting freq results in setting calculating new phase too + if (to_pin >= 64 and to_pin < 112) { + + const freq_no = to_pin - 64; + + 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]; + + const share = (self.soundnode.pins[from_pin] - from_lo) / (from_hi - from_lo); + const newfreq = to_lo + ((to_hi - to_lo) * share); + //print("ORIG {d} SHARE {d} NEWFREQ {d} ", .{self.soundnode.pins[from_pin], share, newfreq}); + + self.soundnode.pins[to_pin] = newfreq; + + const c = fcurrent_tick / 44100; + const pp = prevphase + c * (newfreq - prevfreq) + @floor(c * prevfreq - prevphase); + + //print("OLDPHASE {d} NEWPHASE {d} :: ", .{self.soundnode.pins[208 + freq_no], pp - @floor(pp)}); + self.soundnode.pins[208 + freq_no] = pp - @floor(pp); + + } else { + + const share = (self.soundnode.pins[from_pin] - from_lo) / (from_hi - from_lo); + self.soundnode.pins[to_pin] = to_lo + ((to_hi - to_lo) * share); + + } + + } + pub fn mute(self: *Activity) void { self.soundnode.fab.set_r_amp(0); diff --git a/zigsonnum/sonnum.zig b/zigsonnum/sonnum.zig index fc1c273..930f811 100644 --- a/zigsonnum/sonnum.zig +++ b/zigsonnum/sonnum.zig @@ -229,6 +229,7 @@ pub fn main() !void { const freq_q: u8 = @truncate(trg_node); const sn = try SoundNode.create(allocator, uid, freq_q); + sn.pins[32]=1; //print("Added node {s} at tick {d}\n", .{nodename, tick}); try soundnodes.append(sn); diff --git a/zigsonnum/soundnode.zig b/zigsonnum/soundnode.zig index 107e4f9..4e6fa63 100644 --- a/zigsonnum/soundnode.zig +++ b/zigsonnum/soundnode.zig @@ -67,7 +67,7 @@ pub const SoundNode = struct { }; //Gain and per-freq gains must be initialized as 1 - + sn.pins[32] = 1; @memset(sn.pins[112..160], 1); return sn;