From c3756c4bd9c43b7e9fd87a64724204381764a0fd Mon Sep 17 00:00:00 2001 From: aprilnightk Date: Mon, 15 Sep 2025 19:23:03 +0300 Subject: [PATCH] adsr fix --- mysynths/epiano.py | 74 ++++++++++++++++++++++++++++-------------- pysonnum/compiler.py | 3 +- pysonnum/soundnode.py | 2 +- zigsonnum/activity.zig | 13 ++++---- zigsonnum/sonnum.zig | 2 +- 5 files changed, 60 insertions(+), 34 deletions(-) diff --git a/mysynths/epiano.py b/mysynths/epiano.py index ddd811a..404c5c5 100644 --- a/mysynths/epiano.py +++ b/mysynths/epiano.py @@ -1,24 +1,30 @@ +import random + class PianoString: - def __init__(self, s, note): + def __init__(self, s, note, i): self.s = s + self.basefreq = self.s.note(note) self.primary_gain = 0.4 self.mod = self.s.node() self.node = self.s.node() + + self.node.setpos(0, -10.0+i, 0, 0) self.node.setbasefreq(0, self.basefreq) self.node.setgain(0, self.primary_gain) self.mod.setbasefreq(0, 10) self.mod.setgain(0, 0.8) - self.mod.sine(0, self.s.end) self.node.fmsetup(0, self.basefreq, 0.009) - self.node.fm(0, self.s.end) self.node.setskew(0, 0.3) - + + self.node.setadsrgain(0, 1) + self.node.setadsrsustain(0, 0.2) + self.s.wire(self.mod, self.node) self.s.air(self.node, [self.s.left, self.s.right]) @@ -28,34 +34,52 @@ class PianoString: def play(self, starttick, endtick): - self.node.slidegain(starttick, endtick, self.primary_gain, 0) - self.node.sine(starttick, endtick, 0.5) - self.node.skewsine(starttick, endtick) - self.node.square(starttick, endtick, 0.2) - self.node.triangle(starttick, endtick, 0.4) + keyhold_endtick = endtick + endtick += self.s.sec(0.3) + + self.node.adsr(starttick, endtick, + self.s.sec(0.03), + self.s.sec(0.03), + self.s.sec(0.36), + keyhold_endtick - starttick, + keyhold_endtick - starttick + self.s.sec(0.3)) + + #self.mod.sine(starttick, endtick) + #self.node.fm(starttick, endtick) + self.node.sine(starttick, endtick, 0.2) + self.node.skewsine(starttick, endtick, 0.3) + self.node.square(starttick, endtick, 0.04) + + self.node.adsr(starttick, endtick, + self.s.sec(0.067), + self.s.sec(0.07), + self.s.sec(0.09), + self.s.sec(0.1), + self.s.sec(0.2)) + + self.node.whitenoise(starttick, endtick, 0.01) + self.node.triangle(starttick, endtick, 0.2) def playsec(self, start, end): self.play(self.s.sec(start), self.s.sec(end)) -s.setup(s.sec(4)) -""" -S = PianoString(s, "E4") -S.playsec(0,1) +notes = 'CDEFGAB' +octaves = '345' -S2 = PianoString(s, "C#4") -S2.playsec(1,2) +gamma = [] -""" -n = s.node() -n.setgain(0, 0.4) -#n.whitenoise(0, s.sec(2), 0.8924587, 0.3) +for o in octaves: + for n in notes: + gamma.append(n+o) +s.setup(s.sec(len(gamma)+2)) + +for i in range(0, len(gamma)): + note = gamma[i] + + S = PianoString(s, note, i) + + S.playsec(i, i+1.4) -n.setadsrgain(0, 1) -n.setadsrsustain(0, 0.4) -n.adsr(0, s.sec(2), s.sec(0.03), s.sec(0.04), s.sec(0.3), s.sec(1.3), s.sec(2)) -n.sine(0, s.sec(2), 0.3) -n.triangle(0, s.sec(2)) -s.wire(n, [s.left, s.right]) diff --git a/pysonnum/compiler.py b/pysonnum/compiler.py index 27f3d51..bdbfe65 100644 --- a/pysonnum/compiler.py +++ b/pysonnum/compiler.py @@ -1,7 +1,8 @@ from .sonnum import Sonnum from .activity import Activity - +import random + class SonnumCompiler: def __init__(self): diff --git a/pysonnum/soundnode.py b/pysonnum/soundnode.py index ce707a8..19b8926 100644 --- a/pysonnum/soundnode.py +++ b/pysonnum/soundnode.py @@ -81,7 +81,7 @@ class SoundNode: def mute(self, start_tick, end_tick): self.act('mute', start_tick, end_tick, self, None, []) - def whitenoise(self, start_tick, end_tick, seed = 0.259624856928, gainmult = 0.0): + def whitenoise(self, start_tick, end_tick, gainmult = 0.0, seed = 0.259624856928): self.act('whitenoise', start_tick, end_tick, self, None, [seed, gainmult]) def setadsrgain(self, start_tick, adsrgain): diff --git a/zigsonnum/activity.zig b/zigsonnum/activity.zig index 7c988cb..7db65d7 100644 --- a/zigsonnum/activity.zig +++ b/zigsonnum/activity.zig @@ -584,8 +584,9 @@ pub const Activity = struct { pub fn adsr(self: *Activity) !void { - const current_tick: f64 = @floatFromInt(self.soundnode.fab.current_tick); + const real_current_tick: f64 = @floatFromInt(self.soundnode.fab.current_tick); const start_tick: f64 = @floatFromInt(self.start_tick); + const current_tick = real_current_tick - start_tick; var adsrgain = self.soundnode.g("adsrgain"); const adsrsustain = self.soundnode.g("adsrsustain"); @@ -603,15 +604,15 @@ pub const Activity = struct { var gain: f64 = 0; - if (current_tick <= start_tick + attack_tick) { + if (current_tick <= attack_tick) { gain = adsrgain * current_tick / attack_tick; - } else if (current_tick <= start_tick + hold_tick) { + } else if (current_tick <= hold_tick) { gain = adsrgain; - } else if (current_tick <= start_tick + decay_tick) { + } else if (current_tick <= decay_tick) { gain = ((current_tick-hold_tick)*(adsrsustain-adsrgain)/(decay_tick - hold_tick)) + adsrgain; - } else if (current_tick <= start_tick + sustain_tick) { + } else if (current_tick <= sustain_tick) { gain = adsrsustain; - } else if (current_tick <= start_tick + release_tick) { + } else if (current_tick <= release_tick) { gain = (adsrsustain * (current_tick - sustain_tick) / (sustain_tick - release_tick)) + adsrsustain; } diff --git a/zigsonnum/sonnum.zig b/zigsonnum/sonnum.zig index 3388134..6933c14 100644 --- a/zigsonnum/sonnum.zig +++ b/zigsonnum/sonnum.zig @@ -26,7 +26,7 @@ pub fn main() !void { // Creating a list of soundnodes var soundnodes = ArrayList(*SoundNode).init(allocator); defer soundnodes.deinit(); - + // Determining length of resulting audio in ticks const start_tick: u32 = 0; var end_tick: u32 = 44100 * 10;