Stateless multi segment envelope function

hey,

how can i create a stateless multi segment function beeing used as a grain window and beeing able to modulate its curves and its relative segment durations when its scaled to formantFreq per grain? thanks

(
{
    var snd, freq, trig, formantFreq, phase, window;

    freq = 100;
	formantFreq = 400;

	trig = Impulse.ar(freq);
	phase = Sweep.ar(trig, formantFreq);
	window = phase.lincurve(0, 1, 1, 0, -4); // perc shape
	//window = (phase * 2).fold2; // triangular shape

	snd = SinOsc.ar(formantFreq);

	snd = snd * window * (phase < 0.99);

	snd !2 * 0.1;
	
}.play;
)