That is very easy, just create a synth input (you could name it ‘time’) and plug that into ‘endSynth’ instead of ‘atk’.
I just reread your original question.
If you want to have synthdefs which are ‘general’ and not customized to very specific use-case (which I think is recommendable for a variety of reasons), consider these examples:
( // The simple version without the autorelease synth input. I added a few extra inputs for flexibility
SynthDef(\simpleSynth, {|freq = 220, atk = 0.1, rel = 0.3, gate = 1, amp = 0.5, out = 0|
var env = Env.asr(atk, 1, rel).kr(2, gate);
var sig = SinOsc.ar(freq) * 0.2;
sig = sig * env * amp ! 2;
Out.ar(out, sig)
}).add
)
(
/// PLAY THE SYNTH FROM A MIDI KEYBOARD
MIDIClient.init;
MIDIIn.connectAll;
/// Create an array of size 128, initially all indices = nil. Each index corresponds to a midi note number in the range 0-127
~synths = {nil}!128;
MIDIdef.noteOn(\noteOn, {|vel, nn, chan|
~synths[nn] = Synth(\simpleSynth, [freq: nn.midicps])
});
MIDIdef.noteOff(\noteOff, {|vel, nn, chan|
~synths[nn].set(\gate, 0);
~synths[nn] = nil
});
)
/// PLAY THE SYNTH WITH A PATTERN WITH DIFFERENT SUSTAIN TIMES USING EVENTS
/// See the help file for Event
(instrument: \simpleSynth, sustain: 0.1, amp: 0.5).play
(instrument: \simpleSynth, atk: 2, rel: 2, sustain: 4, freq: 52.midicps, amp: 0.5).play // 2 secs atk, 2 sec hold, 2 sec rel
/// USING A ROUTINE
(
{
var x = Synth(\simpleSynth, [atk: 2, rel: 2, freq: 52.midicps]);
4.wait;
x.set(\gate, 0)
}.fork
)
/// USING A PATTERN
/// I highly recommend reading the Pattern Guide (search for it in the help browser)
(
Pbind(
\instrument, \simpleSynth,
\dur, Pseq([1]),
\freq, 52.midicps,
\atk, 2,
\rel, 2,
\amp, 0.5,
\sustain, 4 // try changing the sustain value
).play
)
// OR
(instrument: \simpleSynth, atk: 2, rel: 2, dur: Pseq([1]), sustain: 4, freq: 52.midicps, amp: 0.5).asPattern.play