Buffer Looping with fadeIn/fadeOut Envelope


I want to do a fadein/fadeout with a buffer that is running in a loop (see code below). But everytime the buffer has played once, the SynthDef stops, even with doneAction:0 in the envelope.
Is there a workaround? I think I could use a Tdef, but I would like to solve the problem within the SynthDef. Any Suggestions? Thank you :slight_smile:

SynthDef(\pback, { 
	arg out = 0, bufnum = 0;
        var playbuf, env, e;
	e = Env([0, 1, 1, 0],[1, 2, 1 ]);
	env = EnvGen.ar(e,1, doneAction:0);
	playbuf = PlayBuf.ar(2, b, rate:1, loop:1);
	playbuf = playbuf * env;
    Out.ar(out, playbuf);

The envelope env ends after 4 seconds, and nothing retriggers it. So PlayBuf is looping but the envelope has gone to 0, suppressing the output.

If it’s a crossfade you’re after, you’ll need 2 envelopes and 2 PlayBufs. It’s certainly possible but the triggering logic usually ties my head in knots.