pin bridging

This commit is contained in:
aprilnightk 2025-09-17 16:19:02 +03:00
parent a32d3a5da1
commit 1e2f8d78e8
8 changed files with 113 additions and 22 deletions

View file

@ -1,9 +1,19 @@
s.setup(s.sec(10)) s.setup(s.sec(10))
m = s.node()
n = 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)) n.sine(0, s.sec(10))
s.wire(n, s.left) s.wire(n, s.left)
s.wire(n, s.right) s.wire(n, s.right)

View file

@ -7,6 +7,7 @@ OPCODES = {
'endtick': 2, 'endtick': 2,
'setpin': 3, 'setpin': 3,
'relay': 4, 'relay': 4,
'bridge': 4,
'setpos': 10, 'setpos': 10,

View file

@ -3,6 +3,33 @@ from .activity import Activity
import random 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: class SonnumCompiler:
def __init__(self): def __init__(self):

View file

@ -2,20 +2,7 @@ from .soundnode import SoundNode
from .notes import * from .notes import *
class Sonnum: 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): def __init__(self, compiler):
self.c = compiler self.c = compiler
@ -106,7 +93,7 @@ class Sonnum:
for trg_node in trg_nodes: for trg_node in trg_nodes:
self.act('pin', 0, 0, src_node, trg_node, [0, 1]) 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): if not isinstance(src_nodes, list):
src_nodes = [src_nodes] src_nodes = [src_nodes]

View file

@ -27,6 +27,9 @@ class SoundNode:
def setpin(self, start_tick, pin_no, value): def setpin(self, start_tick, pin_no, value):
self.act('setpin', start_tick, start_tick, self, None, [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): def sine(self, start_tick, end_tick, gainmult = 0.0):
self.act('sine', start_tick, end_tick, self, None, [gainmult]) self.act('sine', start_tick, end_tick, self, None, [gainmult])

View file

@ -14,7 +14,7 @@ pub fn sine(self: *Activity) void {
const current_tick: f64 = @floatFromInt(self.soundnode.fab.current_tick); const current_tick: f64 = @floatFromInt(self.soundnode.fab.current_tick);
const gain = self.soundnode.corrGain(self.operands[0]); 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; const final_amp = gain * amp;
self.soundnode.add_r_amp(final_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 gain = self.soundnode.corrGain(self.operands[0]);
const period = 44100 / self.soundnode.basefreq; 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 amp = @abs(2 * (2 * ( tp - @floor(tp + 0.5) ) )) - 1;
const final_amp = gain * amp; const final_amp = gain * amp;
@ -45,7 +45,7 @@ pub fn square(self: *Activity) void {
const gain = self.soundnode.corrGain(self.operands[0]); 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) { if (sin > 0) {
self.soundnode.add_r_amp(gain); 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 gain = self.soundnode.corrGain(self.operands[0]);
const period = 44100 / self.soundnode.basefreq; 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 amp = 2 * (tp - @floor(0.5+tp));
const final_amp = gain * amp; const final_amp = gain * amp;
@ -84,11 +84,11 @@ pub fn skewsine(self: *Activity) void {
if (skew == 0) { 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) { } 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)); const sincos = skew * math.sin(m) / (1 - skew*math.cos(m));
amp = (1/skew) * math.atan(sincos); amp = (1/skew) * math.atan(sincos);

View file

@ -35,6 +35,7 @@ pub const Activity = struct {
pub fn do(self: *Activity) !void { pub fn do(self: *Activity) !void {
switch (self.opcode) { switch (self.opcode) {
4 => { self.relay(); }, 4 => { self.relay(); },
5 => { self.bridge(); },
10 => { spatial.setpos(self); }, 10 => { spatial.setpos(self); },
50 => { basicsynths.sine(self); }, 50 => { basicsynths.sine(self); },
51 => { basicsynths.triangle(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 => {},
}
}
}; };

View file

@ -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 { pub fn create(allocator: Allocator, uid: usize) !*SoundNode {
const activities = ArrayList(*Activity).init(allocator); const activities = ArrayList(*Activity).init(allocator);