adsr + basicsynths fix

This commit is contained in:
aprilnightk 2025-09-21 09:59:59 +03:00
parent 68a3024cfe
commit 1981f243ed
6 changed files with 151 additions and 90 deletions

View file

@ -8,12 +8,22 @@ sn = s.node(1)
;0 sn.@setfreq(0, 4)
;0-10 sn.sine()
sn2 = s.node(1)
sn2 = s.node(2)
;sn:0 >> sn2:10
;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 sn2.@setfreq(0,400)
;0 sn2.@setfreq(1,800)
;0-10 sn2.triangle()
;0-10 sn2:10:-1/1 ::> fp(0):396/404
;0-10 sn2:10:-1/1 ::> fp(1):795/806
;0 sn2.setpin(40, 0.9)
;0 sn2.setpin(41, 0.4)
;0-10 sn2:10:-1/1 ::> 35:0.1/0.9
;0-10 sn2.adsr(sec(0.1), sec(0.2), sec(0.3), sec(0.4), sec(0.5))
;0-10 sn2.triangle(0.5)
;0-10 sn2.sine(0.5)

View file

@ -65,6 +65,40 @@ class SonnumCompiler:
name_src, name_trg = ln[1:].split('-/>')
ln = f'self.add_activity("unlink", 0, 0, {name_src}, {name_trg}, [0, 2])'
elif '>>' in ln:
name_src, name_trg = ln[1:].split('>>')
name_src, pin_src = name_src.split(':')
name_trg, pin_trg = name_trg.split(':')
ln = f'self.add_activity("link", 0, 0, {name_src}, {name_trg}, [{pin_src}, {pin_trg}])'
elif '::>' in ln:
tickdata, cmd = ln[1:].split(' ',1)
if '-' in tickdata:
starttick, endtick = tickdata.split('-', 1)
else:
starttick = tickdata
endtick = starttick
name_src, name_trg = cmd.split('::>')
name_src, pin_src, rangepair_src = name_src.split(':')
pin_trg, rangepair_trg = name_trg.split(':')
from_src, from_trg = rangepair_src.split('/')
to_src, to_trg = rangepair_trg.split('/')
ln = f'self.add_activity("rerange", sec({starttick}), sec({endtick}), {name_src}, {name_src}, [{pin_src}, {pin_trg}, {from_src}, {from_trg}, {to_src}, {to_trg}])'
elif ':>' in ln:
tickdata, cmd = ln[1:].split(' ',1)
if '-' in tickdata:
starttick, endtick = tickdata.split('-', 1)
else:
starttick = tickdata
endtick = starttick
name_src, pin_trg = cmd.split(':>')
name_src, pin_src = name_src.split(':')
ln = f'self.add_activity("copy", sec({starttick}), sec({endtick}), {name_src}, None, [{pin_src}, {pin_trg}])'
else:
ln = ln[1:]

View file

@ -16,6 +16,15 @@ def gainpin(freq_no):
def shiftpin(freq_no):
return 160+freq_no
def fp(freq_no):
return 64+freq_no
def gp(freq_no):
return 112+freq_no
def sp(freq_no):
return 160+freq_no
def sec(seconds):
return seconds * 44100

View file

@ -19,15 +19,16 @@ pub fn sine(self: *Activity) void {
const max_freq_pin = 64 + self.soundnode.freq_q;
for (64..max_freq_pin) |freq_pin| {
const gain = self.soundnode.pins[freq_pin + 48];
const shift = self.soundnode.pins[freq_pin + 96];
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);
final_amp += maingain * gain * amp;
if (self.soundnode.pins[freq_pin] > 0) {
const gain = self.soundnode.pins[freq_pin + 48];
const shift = self.soundnode.pins[freq_pin + 96];
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);
final_amp += maingain * gain * amp;
}
}
self.soundnode.add_r_amp(final_amp);
@ -45,18 +46,19 @@ pub fn triangle(self: *Activity) void {
const max_freq_pin = 64 + self.soundnode.freq_q;
for (64..max_freq_pin) |freq_pin| {
const gain = self.soundnode.pins[freq_pin + 48];
const shift = self.soundnode.pins[freq_pin + 96];
const phase = self.soundnode.pins[freq_pin + 144];
const totalphase = self.soundnode.pins[33] + shift + phase;
const period = 44100 / self.soundnode.pins[freq_pin];
const tp = (current_tick - (period * totalphase)) / period;
const amp = @abs(2 * (2 * ( tp - @floor(tp + 0.5) ) )) - 1;
final_amp += maingain * gain * amp;
if (self.soundnode.pins[freq_pin] > 0) {
const gain = self.soundnode.pins[freq_pin + 48];
const shift = self.soundnode.pins[freq_pin + 96];
const phase = self.soundnode.pins[freq_pin + 144];
const totalphase = self.soundnode.pins[33] + shift + phase;
const period = 44100 / self.soundnode.pins[freq_pin];
const tp = (current_tick - (period * totalphase)) / period;
const amp = @abs(2 * (2 * ( tp - @floor(tp + 0.5) ) )) - 1;
final_amp += maingain * gain * amp;
}
}
self.soundnode.add_r_amp(final_amp);
@ -74,18 +76,20 @@ pub fn square(self: *Activity) void {
const max_freq_pin = 64 + self.soundnode.freq_q;
for (64..max_freq_pin) |freq_pin| {
const gain = self.soundnode.pins[freq_pin + 48];
const shift = self.soundnode.pins[freq_pin + 96];
const phase = self.soundnode.pins[freq_pin + 144];
const totalphase = self.soundnode.pins[33] + shift + phase;
const sin = math.sin(utility.corrected_tau * self.soundnode.pins[freq_pin] * current_tick - totalphase * utility.tau);
if (sin > 0) {
final_amp += maingain * gain;
} else {
final_amp += -(maingain * gain);
if (self.soundnode.pins[freq_pin] > 0) {
const gain = self.soundnode.pins[freq_pin + 48];
const shift = self.soundnode.pins[freq_pin + 96];
const phase = self.soundnode.pins[freq_pin + 144];
const totalphase = self.soundnode.pins[33] + shift + phase;
const sin = math.sin(utility.corrected_tau * self.soundnode.pins[freq_pin] * current_tick - totalphase * utility.tau);
if (sin > 0) {
final_amp += maingain * gain;
} else {
final_amp += -(maingain * gain);
}
}
self.soundnode.add_r_amp(final_amp);
@ -105,18 +109,19 @@ pub fn sawtooth(self: *Activity) void {
const max_freq_pin = 64 + self.soundnode.freq_q;
for (64..max_freq_pin) |freq_pin| {
const gain = self.soundnode.pins[freq_pin + 48];
const shift = self.soundnode.pins[freq_pin + 96];
const phase = self.soundnode.pins[freq_pin + 144];
const totalphase = self.soundnode.pins[33] + shift + phase;
const period = 44100 / self.soundnode.pins[freq_pin];
const tp = (current_tick - (period * totalphase)) / period;
const amp = 2 * (tp - @floor(0.5+tp));
final_amp += maingain * gain * amp;
if (self.soundnode.pins[freq_pin] > 0) {
const gain = self.soundnode.pins[freq_pin + 48];
const shift = self.soundnode.pins[freq_pin + 96];
const phase = self.soundnode.pins[freq_pin + 144];
const totalphase = self.soundnode.pins[33] + shift + phase;
const period = 44100 / self.soundnode.pins[freq_pin];
const tp = (current_tick - (period * totalphase)) / period;
const amp = 2 * (tp - @floor(0.5+tp));
final_amp += maingain * gain * amp;
}
}
self.soundnode.add_r_amp(final_amp);
@ -137,30 +142,31 @@ pub fn skewsine(self: *Activity) void {
const max_freq_pin = 64 + self.soundnode.freq_q;
for (64..max_freq_pin) |freq_pin| {
const gain = self.soundnode.pins[freq_pin + 48];
const shift = self.soundnode.pins[freq_pin + 96];
const phase = self.soundnode.pins[freq_pin + 144];
const totalphase = self.soundnode.pins[33] + shift + phase;
if (skew == 0) {
if (self.soundnode.pins[freq_pin] > 0) {
const gain = self.soundnode.pins[freq_pin + 48];
const shift = self.soundnode.pins[freq_pin + 96];
const phase = self.soundnode.pins[freq_pin + 144];
final_amp += maingain * gain * (math.sin(utility.corrected_tau * self.soundnode.pins[freq_pin] * current_tick - totalphase * utility.tau));
} else if (skew > 0) {
const totalphase = self.soundnode.pins[33] + shift + phase;
const m = (utility.corrected_tau * self.soundnode.pins[freq_pin] * current_tick - totalphase * utility.tau);
const sincos = skew * math.sin(m) / (1 - skew*math.cos(m));
final_amp += maingain * gain * (1/skew) * math.atan(sincos);
} else if (skew < 0) {
const m = (utility.corrected_tau * self.soundnode.pins[freq_pin] * current_tick - totalphase * utility.tau);
const sincos = skew * math.cos(m) / (1 + skew*math.sin(m));
final_amp += maingain * gain * (1/skew) * math.atan(sincos);
if (skew == 0) {
final_amp += maingain * gain * (math.sin(utility.corrected_tau * self.soundnode.pins[freq_pin] * current_tick - totalphase * utility.tau));
} else if (skew > 0) {
const m = (utility.corrected_tau * self.soundnode.pins[freq_pin] * current_tick - totalphase * utility.tau);
const sincos = skew * math.sin(m) / (1 - skew*math.cos(m));
final_amp += maingain * gain * (1/skew) * math.atan(sincos);
} else if (skew < 0) {
const m = (utility.corrected_tau * self.soundnode.pins[freq_pin] * current_tick - totalphase * utility.tau);
const sincos = skew * math.cos(m) / (1 + skew*math.sin(m));
final_amp += maingain * gain * (1/skew) * math.atan(sincos);
}
}
}
self.soundnode.add_r_amp(final_amp);
@ -181,21 +187,22 @@ pub fn pulse(self: *Activity) void {
const max_freq_pin = 64 + self.soundnode.freq_q;
for (64..max_freq_pin) |freq_pin| {
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 totalphase = self.soundnode.pins[33] + shift + phase;
const period = 44100 / self.soundnode.pins[freq_pin];
const tp = (current_tick - (period * totalphase)) / period;
const tp_detract = (current_tick - (period * (pulse_shift + totalphase))) / period;
const amp = 2 * (tp - @floor(0.5 + tp));
const amp_detract = 2 * (tp_detract - @floor(0.5+tp_detract));
if (self.soundnode.pins[freq_pin] > 0) {
const gain = self.soundnode.pins[freq_pin + 48];
const shift = self.soundnode.pins[freq_pin + 96];
const phase = self.soundnode.pins[freq_pin + 114];
final_amp += maingain * gain * (amp - amp_detract);
const totalphase = self.soundnode.pins[33] + shift + phase;
const period = 44100 / self.soundnode.pins[freq_pin];
const tp = (current_tick - (period * totalphase)) / period;
const tp_detract = (current_tick - (period * (pulse_shift + totalphase))) / period;
const amp = 2 * (tp - @floor(0.5 + tp));
const amp_detract = 2 * (tp_detract - @floor(0.5+tp_detract));
final_amp += maingain * gain * (amp - amp_detract);
}
}
self.soundnode.add_r_amp(final_amp);

View file

@ -31,8 +31,8 @@ pub fn singenN(self: *Activity) void {
}
// Generates a linear ADSR envelope (range 0) to GAIN
// Takes attack gain from OUT8
// Takes sustain gain from OUT9
// Takes attack gain from pin 40
// Takes sustain gain from pin 41
// Tick count starts from 0, as if start_tick was 0
pub fn adsr(self: *Activity) void {
@ -40,8 +40,8 @@ pub fn adsr(self: *Activity) void {
const start_tick: f64 = @floatFromInt(self.start_tick);
const current_tick = real_current_tick - start_tick;
var adsrgain = self.soundnode.out8;
const adsrsustain = self.soundnode.out9;
var adsrgain = self.soundnode.pins[40];
var adsrsustain = self.soundnode.pins[41];
const attack_tick = self.operands[0];
const hold_tick = self.operands[1];
@ -52,6 +52,7 @@ pub fn adsr(self: *Activity) void {
if (gainmult > 0) {
adsrgain *= gainmult;
adsrsustain *= gainmult;
}
var gain: f64 = 0;
@ -68,6 +69,6 @@ pub fn adsr(self: *Activity) void {
gain = (adsrsustain * (current_tick - sustain_tick) / (sustain_tick - release_tick)) + adsrsustain;
}
self.soundnode.gain = gain;
self.soundnode.pins[32] = gain;
}

View file

@ -53,7 +53,7 @@ pub const Activity = struct {
56 => { basicsynths.whitenoise(self); },
//100 => { generators.singenN(self); },
//150 => { generators.adsr(self); },
150 => { generators.adsr(self); },
//ENDOPCODES
else => {},