From a trigger buffer to a synthDef

Hi everyone! I’m studying Buffers and granulator synthesis possibilities, like TGrains, for an university audio project.
I’m trying to create a synthDef from this variables and codes but I don’t know how to manage the 5 Mix.
Anyone can help?
thanks a lot.

(
var trate = 10, amp = 0.2, stretch = 2;
var trigger;
{
	trigger = Impulse.kr(trate);
     Mix.fill(5,{ TGrains.ar(2,trigger, t.bufnum, 1,
				Line.kr(0.3.rand,BufDur.kr(t.bufnum) + 0.3.rand, BufDur.kr(t.bufnum) * stretch, doneAction: 2)
				+ TRand.kr(0,0.01,trigger),
				0.1,TRand.kr(-1,1,trigger),amp,1);
})
}.play
)

A question:

Is there a reason you used Line.kr? You should almost always use an audio rate Ugen to index into a Buffer.

Beyond that, you should make one signal that does what you want, then iterate over it with 5.collect{} and call .sum to mix the signals together.

Then, I like to multiply the result of the collect by, in this case, 5.reciprocal. You might even consider making the 5 a var so you don’t forget how many iterations you’re calling when you scale the amplitude. Then you can just change the integer value that that var is referencing in your SynthDef.

what kind of ar Ugen you think I should use?

ok, so 5.collect {} must be outside my synthdef var arguments?

thanks a lot

You can use Line.kr here as the trigger is also control rate. This is fine. However, if you increase the trigger rate you will want to switch to audio rate at some point.

I think you have the structure of your code a little bit wrong (vars declared outside the curlies imply they will be used elsewhere - you will want to limit the scope of vars as much as possible to keep things simple). Also, it might be helpful to name the arguments when there are many, that way its easier to see whats going on at a glance.

Below also shows the correct way to do the .collect{...}.sum.
I’ve also made everything audio rate, but you could put them all back to kr if you know you don’t want to increase the trigger rate too far.

Now there are actually a few errors here. When you calculate the end of the centre position line you are actually going past the end of the buffer by adding a value to it. Also, this value will only be randomised once, when the function is defined, so you might as well write a number there yourself.
Similarly you will have a problem inside TGrains when you add to the center_pos again.

~my_buf = Buffer.read(s, Platform.resourceDir +/+ "sounds/a11wlk01.wav");

(
{
	var t_rate = 10;
	var amp = 0.2;
	var stretch = 2;
	var trigger = Impulse.ar(t_rate);
	var center_pos = Line.ar(
		start: 0.3.rand, 
		end: BufDur.kr(~my_buf) + 0.3.rand,   // this is wrong, as it will go past end of buffer
		dur: BufDur.kr(~my_buf) * stretch, 
		doneAction: 2
	);
	5.collect{
		TGrains.ar(
			numChannels: 2,
			trigger: trigger,
			bufnum: ~my_buf,
			rate: 1,
			centerPos: center_pos + TRand.ar(0, 0.01, trigger),  // sim. this is wrong
			dur: 0.1,
			pan: TRand.ar(-1, 1, trigger),
			amp: 1
		)
	}.sum;
}.play
)

Now there is another way of doing this using more multichannel expansion…
Get rid of the collect, and make the trigger an array of 5 impulses.
Now, when this is pasted to TGrains, its output will be an array with 5 elements, where each element is itself an array of 2 elements. You can add all these elements by using Mix.
Theres a comment showing what goes into mix, and what comes out.

~my_buf = Buffer.read(s, Platform.resourceDir +/+ "sounds/a11wlk01.wav")
(
{
	var t_rate = 10;
	var amp = 0.2;
	var stretch = 2;
	var trigger = Impulse.ar(t_rate!5);
	
	var center_pos = Line.ar(
		start: 0.3.rand, 
		end: BufDur.kr(~my_buf) + 0.3.rand,   // this is wrong, as it will go past end of buffer
		dur: BufDur.kr(~my_buf) * stretch, 
		doneAction: 2
	);
	
	var grain_multi = TGrains.ar(
		numChannels: 2,
		trigger: trigger,
		bufnum: ~my_buf,
		rate: 1,
		centerPos: center_pos + TRand.ar(0, 0.01, trigger),
		dur: 0.1,
		pan: TRand.ar(-1, 1, trigger).poll,
		amp: 1
	);
	// [ [1L, 1R], [2L, 2R], ... [5L, 5R] ] -> [ (1L + 2L + ... 5L), (1R + 2R + ... 5R)]; 
	Mix.ar(grain_multi);
	
}.play
)