Ndef call function in several period / bars

Hi, I have a question about making a function call in Ndef.

I write the function to generate random array, with specific length.

(
~funcD = { arg len = 16;
	var n = Array.fill(len, {arg i ; i});
	var m = Array.fill(rrand(4, 12),  { n.take(n.choose) } );
	m.sort;
	m.postln;
};
)

~funcD.();

This array return array with 4~ 16 elements with rrand.

and I would like to use in Ndef,
for example, after 8 bar, I would like to change the pattern of array.

and for that I made another function to make pattern

g = {|div, len, lane|
	Mix((c.(div) % len) eq: lane) * t.(div);
};

this is just simple impulse trigger to call the pattern formed as
g.(4, 8, [1,2, 5, 8]);
4 means impulse freq, 8 is the bar length, array is a patterns in the 8 bar

my question is this is simlple drum, with Ndef,
I would like to call a array function after 8 bar or 16 bar loop play
what I Imagine is it act like random drum sequencer.
but In the Ndef, Idk how to call a different pattern after a specific loop

(
x = Ndef(\drumKit, {
	var gt = g.(4,8,[0,3,5,6,7]);
	var kickGate = Select.kr(Mix(c.(4) % 8 eq: [3,6]) ,[gt, TChoose.kr(gt, [gt,0,0,0,t.(8),t.(16),t.(32)])]);
	var snareGate = g.(4,8,[4]);
	Pan2.ar(RLPF.ar(~samplePlay.(~kick, kickGate) * 0.6, 6000)) +
	Pan2.ar(~samplePlay.(~snare, snareGate)*0.5);
}).fadeTime_(1).playN(outs:[0,1,16,17], amps:[0.0,0.0, 0.4, 0.4]);
)

I can’t quite get your code to run, and can’t quite tell exactly what you’re intending, but here’s a few pointers that might help…

In general, sequencing by using Select is not ideal, this is really meant to switch between multiple continuously-running signal chains rather than step through values.

You might look at the Demand ugens - these provide ways to generate sequences of values inside of a Synth or Ndef. Here’s a quick example that might spark some ideas…

Start with simple Impulse driven kick:

(
Ndef(\kick, {
	var trig, env, kick;
	
	trig = Impulse.ar(2);
	
	env = Env.perc(0.01, 0.6).ar(gate:trig);
	
	kick = LPF.ar(Saw.ar(
		freq: env.lincurve(0, 1, 30, 90, 20),
		mul: 16  * env
	), 90).tanh;
	kick = kick + (-3.dbamp * Amplitude.ar(kick, 0.01, 0.4) * FreeVerb.ar(LPF.ar(kick, 200), 1, 3.1, 0.1));
	
	-16.dbamp * kick * [1, 1]
}).play;
)

This is the same thing but with a Dseq describing a sequence of timings (in this case, just repeating 1/2 second durations forever)

(
Ndef(\kick, {
	var pattern, trig, env, kick;
	
	pattern = Dseq([1/2], inf);
	trig = TDuty.ar(pattern);
	
	env = Env.perc(0.01, 0.6).ar(gate:trig);
	
	kick = LPF.ar(Saw.ar(
		freq: env.lincurve(0, 1, 30, 90, 20),
		mul: 16  * env
	), 90).tanh;
	kick = kick + (-3.dbamp * Amplitude.ar(kick, 0.01, 0.4) * FreeVerb.ar(LPF.ar(kick, 200), 1, 3.1, 0.1));
	
	-16.dbamp * kick * [1, 1]
}).play;
)

With a more complex pattern:

(
Ndef(\kick, {
	var pattern, trig, env, kick;
	
	pattern = Dseq([2, 2, 1.5, 1.5, 1], inf);
	pattern = (1/4) * pattern;
	trig = TDuty.ar(pattern);
	
	env = Env.perc(0.01, 0.5).ar(gate:trig);
	
	kick = LPF.ar(Saw.ar(
		freq: env.lincurve(0, 1, 30, 90, 20),
		mul: 16  * env
	), 90).tanh;
	kick = kick + (-3.dbamp * Amplitude.ar(kick, 0.01, 0.4) * FreeVerb.ar(LPF.ar(kick, 200), 1, 3.1, 0.1));
	
	-16.dbamp * kick * [1, 1]
}).play;
)

And randomizing the order of the durations in the sequence:

(
Ndef(\kick, {
	var pattern, trig, env, kick;
	var durations;
	
	durations = Drand([1, 1, 1, 1, 1.5, 1.5, 2], inf);
	
	pattern = Dseq([durations], inf);
	pattern = (1/4) * pattern;
	trig = TDuty.ar(pattern);
	
	env = Env.perc(0.01, 0.5).ar(gate:trig);
	
	kick = LPF.ar(Saw.ar(
		freq: env.lincurve(0, 1, 30, 90, 20),
		mul: 16  * env
	), 90).tanh;
	kick = kick + (-3.dbamp * Amplitude.ar(kick, 0.01, 0.4) * FreeVerb.ar(LPF.ar(kick, 200), 1, 3.1, 0.1));
	
	-16.dbamp * kick * [1, 1]
}).play;
)

Hopefully this gives you some ideas about how to implement random sequencing things? In the long term, the better way to do this is probably with Patterns, since sequencing things are somewhat limited if you’re doing it from inside a SynthDef - but, anything you can get working with Demand UGens you can also build with Patterns, so this is a reasonable place to start experimenting, and you can decide if you want to learn the Pattern stuff later on.

1 Like

thanks for the kind reply!
I will try!
I also think pbind or pattern is much easier to implement the idea,
but I use this technique a lot for later,
so I explore some way to implement!

thank you so much!

Do you think Ndef’s argument can be an array?