Routine Trigger synth

This is a Two type of synth
A

(
SynthDef(\x, {
	arg freq=440, modFreq=20;
	var snd = LFNoise1.ar(freq, mul: 0.5);
	var mod = SinOsc.ar(modFreq, mul: 0.5);
	var env = EnvGen.kr(Env.perc(0.03, 2.0), doneAction:2);
	snd = snd * mod * env;
	Out.ar(0, snd!2);
}).add;
)

B

(
SynthDef(\x, {
	arg freq = 440, modFreq= 20;
	var sig,snd,env;
	sig = SinOsc.ar(100);
	sig = ((sig - Delay1.ar(sig))/2pi) % 1;
	sig = sig * SinOsc.ar(200).range(freq, modFreq/2);
	sig = (Phasor.ar(DC.ar(0), sig, 0.0, 1.0)* 2pi).sin;
	(sig * 10.dbamp) ! 2;
	env = EnvGen.kr(Env.adsr(0.2,1.0,0,1.0), doneAction: 2);
	// env = EnvGen.kr(Env.perc(0.01, 0.125), doneAction: 2); //percussion? 
	snd = SinOsc.ar(200) * sig * env;
	Out.ar(0, snd!2);
}).add;
)

I would like to trigger in Rotuine

(

b = ModernLife.new.setRule("245678/3").fps_(2).fadeAmount_(1).play;

r = Routine.run({
    inf.do({
		var sig = b.getFreq[0]; // sum of array of simple number 
		var mod = b.getFreq[1]; // sum of array of simple number
		// [sig, mod].postln;

		Synth(\x).set(\freq, sig, \modFreq, mod);
		// Synth(\x).set(\modFreq, mod);
		1.0.rand.wait;
		
		// "done".postln;
    }).play;
});
)

Run code A in the Routine Cpu meter is OK,
but Run code B in the Routine, the CPU meter keeps accumulating so fast
at some point reaches 100 percent.
Is there any way to release the used Synth?! or is it not going to peak?
b is simple array and b.getFreg is number from the sum of array.

What’s the available information in an ADSR envelope?

Attack time, decay time, sustain level (NOT time), release time.

And the stages of an ADSR envelope?

First it takes “attack-time” to rise to full scale. Then it falls, over “decay-time,” to reach the sustain level. Then it holds there… and then it releases.

Now. How long does it hold at the sustain level?

The envelope definition doesn’t say. There is a sustain level but no sustain time. The time is unknown. So how does it know?

The only way is to get the release time info from somewhere else.

In a VST, what determines the moment of release? A MIDI note-off message. The release must be signaled.

In modular, what determines the moment of release? A gate signal falls from positive to 0 (again, signaling the release).

So in SC, you will need a gate to be positive when the synth starts, and change to 0 when you want it to start releasing.

There are many examples in the help files about this. Probably best to start with Env and EnvGen help.

hjh

Looking again:

This is using only the Attack and Decay segments… so your use case isn’t really attack - decay - sustain - release.

So one way is: don’t write in unnecessary segments: EnvGen.kr(Env([0, 1, 0], [0.2, 1.0], -4), doneAction: 2);

If you really want ADSR, but timed, the synth could generate a gate:

EnvGen.kr(
	Env.adsr(0.2,1.0,0,1.0),
	// 0.2 is the time before *starting* to release
	gate: Trig1.kr(Impulse.kr(0), 0.2),
	doneAction: 2
);

Or the routine could schedule the release:

(
SynthDef(\x, {
	arg freq = 440, modFreq = 20, gate = 1;
	var sig,snd,env;
	sig = SinOsc.ar(100);
	sig = ((sig - Delay1.ar(sig))/2pi) % 1;
	sig = sig * SinOsc.ar(200).range(freq, modFreq/2);
	sig = (Phasor.ar(DC.ar(0), sig, 0.0, 1.0)* 2pi).sin;
	(sig * 10.dbamp) ! 2;
	env = EnvGen.kr(Env.adsr(0.2,1.0,0,1.0), gate, doneAction: 2);
	snd = SinOsc.ar(200) * sig * env;
	Out.ar(0, snd!2);
}).add;
)

r = Routine.run({
    inf.do({
		var node;
		var sig = b.getFreq[0]; // sum of array of simple number 
		var mod = b.getFreq[1]; // sum of array of simple number
		// [sig, mod].postln;

		node = Synth(\x, [\freq, sig, \modFreq, mod]);
		thisThread.clock.sched(0.2, { node.release });
		1.0.rand.wait;
		
		// "done".postln;
    }).play;
});

hjh