(
{
    var freq = 220;
    var trig = Impulse.kr(1); // re-pluck every second
    var burst = SinOsc.ar(freq) * EnvGen.kr(Env.perc(0.001, 0.05), trig); // short sine burst
    Pluck.ar(
        burst,
        trig,
        0.2,          // max delay time
        1/freq,       // delay time = 1 / frequency
        3,            // decay
        0.5           // damping
    ) * 0.4;
}.play;
)



(
SynthDef(\envPluckSine, {
    |out=0, freq=220, amp=0.4, sustain=1.5|
    var trig, env, sig;

    trig = Impulse.kr(1); // triggers every second

    env = EnvGen.kr(Env.perc(0.001, sustain), trig);

    sig = SinOsc.ar(freq) * env * amp;

    Out.ar(out, sig.dup); // stereo
}).add;
)



(
SynthDef(\pluckedSine, {
    |out=0, freq=220, amp=0.4, decay=3, coef=0.5|
    var trig, burst, pluck;

    trig = Impulse.kr(0); // trigger manually

    burst = SinOsc.ar(freq) * EnvGen.kr(Env.perc(0.001, 0.05), trig);

    pluck = Pluck.ar(
        burst,
        trig,
        0.2,        // max delay time
        1 / freq,   // delay time = 1 / freq
        decay,
        coef
    );

    Out.ar(out, pluck.dup * amp);
}).add;
)



(
~notes = Array.newClear(128);

MIDIdef.noteOn(\noteOnTest, {
	arg vel, nn, chan, src;
	[vel, nn].postln;

	if (chan == 0, {
		~notes[nn] = Synth.new(
			\prophet5style,
			[
				\freq, nn.midicps,
				\amp, vel.linexp(1,127,0.01,0.3),
				// \gate, 1,

			]
		);
	});
});

MIDIdef.noteOff(\noteOffTest, {
	arg vel, nn, chan;
	[vel, nn].postln;

	if (chan == 0, {
		~notes[nn].set(\gate, 0);
		~notes[nn] = nil;
	});
});
)

