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
)