I have written the following code, which takes a micro sound length section of a sample and then gradually augments it by doubling until it is the full length of the sample. The code only invokes a single instance of the SynthDef.
When the sample length changes it is very abrupt, so I had the idea that at the point of changing from one sample length to another, the older - and therefore shorter - sample could gradually fade out its volume. I’m happy with my current code but worry I might have locked myself in with it, since the synth never uses the release from the Env, as there is only one instance of the SynthDef.
Is there an elegant way to achieve this without rewriting all the code? Something I have tried unsuccessfully to do using Task and do.
Many thanks for any tips. Here is the code.
p = Platform.resourceDir +/+ "sounds/a11wlk01.wav";
~b0 = Buffer.read(s, p);
~b0.play;
(
SynthDef.new (\voice, {
arg amp=1, out=0, gate=1, buf, start, end, rate=1, dur;
var eg = EnvGen.kr(Env.asr(0.001, 1, 0.001), gate, doneAction: 2);
var sig, ptr;
ptr = Phasor.ar(0, BufRateScale.kr(buf)*rate, start, end);
sig = BufRd.ar(2, buf, ptr);
sig = sig * amp;
Out.ar(out, (sig*eg));
}).add;
)
r = 12; // number of repeats
a = Pgeom(1, 2, r).reciprocal;
a = a.asStream.nextN(r);
b = a.reverse;
d = ~b0.numFrames / ~b0.sampleRate;
(
Pmono( \voice,
\buf, ~b0.bufnum,
\dur, Pseq([d], r),
\amp, 0.3,
\start, 0,
\end, Pseq(b*~b0.numFrames, 1),
\rate, 1).play;
);