SynthDef and RecordBuf

Hi everyone,
i need to create a single SynthDef starting from these elements (del_2, del_3 and x). I would create a tool to use in live electronics that is able to loop, use grain and delay starting from the attached script - combining del_2, del_3 and x in a single SynthDef, and adding elements (for example freeze, or other effects) on it.

My question is not so much in the form as in a good ‘aesthetic’ outcome. In fact, I tried to merge the two SynthDef myself but, even if the form may be correct, the result always goes into feedback and I don’t understand the reason. Likewise, the loop formally works but on a practical level it doesn’t define the duration, or if I want to overwrite the loop.

Rhe result will then be managed via OSC or GUI.

Thanks so much!

s.options.inDevice_("Mic");
s.options.outDevice_("Headphones");

s.boot;
Buffer.freeAll;
b = Buffer.alloc(s, s.sampleRate * 2);

(
// Synth input

SynthDef.new(\mic, {arg inBus=0,outBus=0,amp=0;
	                var sig;
	                    sig = SoundIn.ar(inBus) * amp.lag(0.02);
	                    sig = LeakDC.ar(sig);
	                Out.ar(outBus,sig)
             }).add;

// Synth router

SynthDef(\router, {arg inBus=0, outBus=0, fadeT = 1;
		           var sig, init, gate;
		               sig    = In.ar(inBus,1);
		               init   = Impulse.kr(0);
		               outBus = K2A.ar(outBus);
			           gate   = HPZ1.ar(outBus).abs > 0;

	               Out.ar(outBus,
			              sig * EnvGen.ar(Env([0,0,1], [0,fadeT],\cub),gate+init));
			       Out.ar(DelayN.ar(outBus, fadeT, fadeT),
			              sig * EnvGen.ar(Env([0,1,0], [0,fadeT],\cub),gate));
	       }).add;
)

(
~micBus.free;
~delBus.free;

~micBus = Bus.audio(s,1); 
~delBus = Bus.audio(s,1); 

~micGrp = Group.new;
~fxGrp  = Group.after(~micGrp); 

a = Synth(\mic,    [\inBus,0,       \outBus,~micBus,\amp,1],       ~micGrp); 
b = Synth(\router, [\inBus,~micBus, \outBus,~delBus, \fadeT, 0.5], ~micGrp, \addToTail);
)

// First Synthdef

(
SynthDef(\del_2, {arg inBus=0, outBus=0, maxdel=2, del=0.5, fbk=0.0,
	                  amp=0, gate=0, fade=0.2, done=2;
	              var in, local, sig, dly, env;
	                  in    = In.ar(inBus);
	                  local = LocalIn.ar(2) + [in, 0];  // Dual mono
	                  dly   = DelayL.ar(local, maxdel, del,1 );
		              dly   = LeakDC.ar(dly);
	                  env   = Linen.kr(gate,fade,1,fade,done);
		          LocalOut.ar(dly.reverse * fbk); // reverse = ping pong effect
	              Out.ar(outBus, local * env * amp);
                  }).add;
)


c = Synth(\del_2,[\inBus,~delBus,\outBus,0, \amp,1,\fade,0.5, \del, 0.8, \fbk,0.2, \gate,1],~fxGrp);    // Delay

c.set(\del,0.3);
c.set(\fbk,0.6);
c.set(\fbk,0.9);
c.set(\fbk,0.1);
c.set(\fbk,0);
c.set(\gate,0);


// Second Synthdef

(
SynthDef(\del_3, {arg inBus=0, outBus=0, maxdel=10, del=0.5, decayT=1, mix=0,
	                  amp=0, gate=0, fade=0.2, pan=0, done=2;
	              var in, sig, dly, pann, env;
	                  in    = In.ar(inBus);
	                  dly   = CombL.ar(in, maxdel, del, decayT);
	                  env   = Linen.kr(gate,fade,1,fade,done);
	                  sig   = LeakDC.ar(dly); 
		              sig   = XFade2.ar(dly, in, mix) * env * amp;
	                  pann  = Pan2.ar(sig, pan);
	             Out.ar(outBus, pann);
                 }).add;
)

c = Synth(\del_3,[\inBus,~delBus,\outBus,0,\amp,1,\fade,0.5, \decayT,2, \gate,1],~fxGrp);    // Delay

c.set(\decayT,7.6);
c.set(\del,0.5,\mix, -1);
c.set(\del,0.5,\mix, 0.5);
c.set(\decayT,10);
c.set(\decayT,0);
c.set(\gate,0);

// Loop

(
x = {
	var sig;
    sig = SoundIn.ar(0);
	RecordBuf.ar(sig,b,\offset.kr(0),\reclev.kr(1),\prelev.kr(0),\run.kr(1),\loop.kr(1));
	PlayBuf.ar(1!2,b,loop:1);
}.play;
)

May I ask… does it need to be a single SynthDef?

I’d suggest modularizing, for a couple of reasons:

  • Simpler SynthDefs are easier to develop and troubleshoot. (Instead of trying to get 20 features to work all at once, work on one of them first, then another.)
  • If each effect is its own SynthDef (and Synth), you can reorder effects easily just by reordering Synth nodes. If it’s one big SynthDef, the order of operations is fixed, and you can’t change it without replacing the entire big node.

By “looping,” I presume that you want to record a bar or two and play it back repeatedly. The end of the sample looping back to the beginning is very likely to be discontinuous, producing a click. So you don’t want to do this using PlayBuf or BufRd’s loop input. You will need a crossfade. Perhaps try XPlayBuf: Release v0.1.2-xfade · elgiano/XPlayBuf · GitHub – alternately, you could implement a cross fade using built-in SC UGens.

XPlayBuf has a ‘loopDur’ input.

The built-in buffer players don’t have a loop duration input, but they can be reset on demand. You can implement loop duration, and overwrite, in terms of reset.

hjh