EDIT: I got some stuff wrong as you can see from James’ post below, I deleted that part.
I changed the env of the synthdef to what I think you want - gate 1: samples loops around, gate 0: sample fades out. Remember to supply the bufnum below.
(
SynthDef(\sampler,{
arg bufnum, rel, start, end, ffreq1, ffreq2, amp, gate = 0, atkBus; // added atkBus to args
var env, ptr, sig, filter1, filter2, filter3, mainsig, noise;
env = Env.asr(In.kr(atkBus), 1, rel).kr(0, gate);
ptr = Phasor.ar(2, BufRateScale.kr(bufnum), start, end);
sig = BufRd.ar(2, bufnum, ptr);
filter1 = BHiPass.ar(sig, ffreq1);
filter2 = BLowPass.ar(filter1, ffreq2);
Out.ar(0, env * filter2 * amp);
}).add;
)
~sampler = Synth(\sampler, [atkBus: ~cc2, sus: 1, rel: 0.1, start: 0, end: 34000, ffreq1: 50, ffreq2: 700, amp: 0.5]);
// NB Remember to supply the bufnum
// ~sampler.set(\bufnum, x)
~cc2.set(1);
~sampler.set(\gate, 1)
~sampler.set(\gate, 0)
For some stuff you might need, like a one-shot sample, it is better to use Named Control style arguments because you can specify a trigger argument, like \gate.tr(0) - this is what you need for one-shot samples. Here is how I would write it. You can comment out the sustaining env and uncomment the perc env to test one-shot samples. It is highly advisable to use default values for most arguments especially filter frequencies.
(
SynthDef(\sampler,{
var env, ptr, sig, filter1, filter2;
var buf = \bufnum.kr;
env = Env.asr(In.kr(\atkBus.kr), 1, \rel.kr(0.1)).kr(0, \gate.kr(0));
// env = Env.perc(In.kr(\atkBus.kr), \rel.kr(0.5)).kr(0, \gate.tr(0));
ptr = Phasor.ar(2, BufRateScale.kr(\bufnum.kr), \start.kr(0), BufFrames.kr(buf));
sig = BufRd.ar(2, buf, ptr);
filter1 = BHiPass.ar(sig, \ffreq1.kr(50));
filter2 = BLowPass.ar(filter1, \ffreq2.kr(700));
Out.ar(0, env * filter2 * \amp.kr(1));
}).add;
)
There are at least two other ways of achieving the same result, which one to pick is mostly a stylistic choice.
Use a bus outside the synthdef and asMap to map the bus to the arg:
(
SynthDef(\sampler,{
var env, ptr, sig, filter1, filter2;
var buf = \bufnum.kr;
env = Env.asr(\atk.kr(0.1), 1, \rel.kr(0.1)).kr(0, \gate.kr(0));
// env = Env.perc(\atk.kr(0.1), \rel.kr(0.5)).kr(0, \gate.tr(0));
ptr = Phasor.ar(2, BufRateScale.kr(\bufnum.kr), \start.kr(0), BufFrames.kr(buf));
sig = BufRd.ar(2, buf, ptr);
filter1 = BHiPass.ar(sig, \ffreq1.kr(50));
filter2 = BLowPass.ar(filter1, \ffreq2.kr(700));
Out.ar(0, env * filter2 * \amp.kr(1));
}).add;
)
~sampler = Synth(\sampler, [atk: ~cc2.asMap, sus: 1, rel: 0.1, start: 0, end: 34000, ffreq1: 50, ffreq2: 700, amp: 0.5]);
// NB Remember to supply the bufnum
// ~sampler.set(\bufnum, x)
~cc2.set(0.1);
~sampler.set(\gate, 1)
~sampler.set(\gate, 0)
Or what I would probably do, not use a bus at all, use the same synthDef as above and set the value directly in the mididef:
MIDIdef.cc(\cc2, { arg val; ~sampler.set(\atk, val.linexp(0, 127, 0.1, 1)) }, 1, 0);
The last two examples has the advantage that you don’t have to decide wether you are using a bus or not when you write the synthdef - some instances of the synthdef can use busses, others not.