Hey all,
I am trying to create multiple instances of a SynthDef at once. To do this, I am using Dictionaries.
This is my SynthDef below. I am using it to access a Buffer that I have saved locally.
SynthDef(\mainbuf, {
arg atk = 0.01, rel = 0.01, sust = 1, rate = 1, patch = 22, amp = 1;
var sig, buffer;
buffer = \buffer.kr(0);
sig = PlayBuf.ar(1, buffer, BufRateScale.kr(buffer) * rate, * BufFrames.ir(buffer));
// Added Impulse here to make sure the gate trigger is closed first, then opens to prevent zombie synths from spawning
sig = sig * Env.asr(atk, sust, rel).ar(Done.freeSelf, \gate.kr(1) + Impulse.kr(0));
sig = Out.ar(patch, sig * amp);
}).add;
This is a Tdef I am using with Dictionaries below. The problem I am running into is that the keys in the dictionary have to be unique, so I can’t reference the same SynthDef multiple times. Ideally, I am trying to get these two arrays to play in sync, each being it’s own Synth instance. Additionally, I would like to add Dictionaries to also control parameters for atk, rel, rate, bank:
(
Tdef(\test,
Routine {
var bpm, beat, tatum;
var bufAmp;
bpm = 170;
beat = 60 / bpm;
tatum = beat / 4;
bufAmp = Dictionary [
\mainbuf -> [1, 1, 1, 0],
\mainbuf -> [0, 0, 0, 1],
];
loop {
16.do { |i|
bufAmp.keysValueDo { |synthDef, ampValues|
s.makeBundle(s.latency, {
synth = Synth(synthDef, [
patch: ~b1,
amp: ampValues[i % ampValues.size], // Apply amp value from dictionary based on current loop index
//atk:
//rel:
//rate:
//bank:
], ~sources);
});
s.makeBundle(s.latency, {
synth.set(\gate, 0);
});
tatum.wait;
};
};
};
};
).play(quant: 4);
)
When I was testing this earlier, I was able to get this to work if I used multiple SynthDefs (i.e mainbuf1, mainbuf2. This made it so the keys are unique and I was able to trigger both, but I am trying to keep the code clean and just use the one SynthDef that I am needing.
Thank you