//weak musically but just exploring some options

//envelope loop and lag

//should also explore:
//gate to retrigger
//buffer and BufRd BufWr
//DID THIS ONE env loop points, Control 
//Dseq, Demand

(
SynthDef(\looplag, {arg freq=440,amp=0.1;
var env, sound;

env=Env([1,0.9,1.1,0.87,1.13],[0.4,0.6,0.7, 0.3],0,3,1);

sound=Resonz.ar(Saw.ar(freq*EnvGen.ar(env)),freq*2,0.1);

Out.ar(0,Pan2.ar(sound,0.0))

},[0.5,0.1]).send(s);
)

a=();

(
a.synth=Synth(\looplag);

SystemClock.sched(0.5,{a.at(\synth).set(\freq, rrand(100,900)); 0.1+rrand(0.0,0.01)});
)

(
a.synth2=Synth(\looplag,[\freq,600]);

SystemClock.sched(0.5,{a.at(\synth2).set(\freq, rrand(500,700)); 0.1+rrand(0.0,0.01)});
j)

//doesn't work
(
SynthDef(\envcontrol, {arg freq=440,amp=0.1;
var env, sound, lvls;

lvls= Control.names([\env]).ir([1,0.9,1.1,0.87,1.13]);

env=Env(lvls,[0.4,0.6,0.7, 0.3],0,3,1);

sound=Resonz.ar(Saw.ar(freq*EnvGen.ar(env)),freq*2,0.1);

Out.ar(0,Pan2.ar(sound,0.0))

},[0.5,0.1]).send(s);
)

a=Synth(\envcontrol)

a.setn(\env,[2,1.9,1.1,1.87,3.13]); //won't work because ir

b=Synth(\envcontrol, [\env,2,1.9,1.1,1.87,3.13]) //nah

a.setn(\env,[2,1.9,1.1,1.87,3.13]);

(
SynthDef(\envcontrol, {arg freq=440,amp=0.1;
var env, sound, lvls;

lvls= Control.names([\env]).kr([1,0.9,1.1,0.87,1.13]);

env=Env(lvls,[0.4,0.6,0.7, 0.3],0,3,1);

sound=Resonz.ar(Saw.ar(freq*EnvGen.ar(env)),freq*2,0.1);

Out.ar(0,Pan2.ar(sound,0.0))

},[0.5,0.1]).send(s);
)

a=Synth(\envcontrol)

a.setn(\env,[2,1.9,3.1,4.87,3.13]); //won't work because ir

(
b=1.0;
SystemClock.sched(0.0,{
a.setn(\env, Array.rand(5,0.5,14)); //won't work because ir

b});
)

b=nil
a.free

//shouldn't have used a twice!