Newbie question on Tdef ( tasks, routines)

I usually use Ndef with a Pbind for pattern stuff but wanting to explore/learn more Tdef (Tasks, Routines).

(
Tdef(\d,{
	loop{
		Synth(\kick);
		0.5.wait;
	}
})
)

Tdef(\d).play

this plays fine

(
Tdef(\d,{ 
	~kick = Synth(\kick);
	loop{
		~kick;
		1.wait;
	}
})
)

this plays once but dose not loop. Testing to see if I can make this work and play multiple synths using fork over making each synth its own Ndef. Also would be nice if I could include the wait time in the ~kick

First, do an isolated test:

a = Synth(\kick);  // sound

a  // no sound

Simply referring to a Synth object does not cause it to make its sound again.

Playing a synth is an action. Actions in SC happen only by calling a method – if you haven’t called a method, then there’s no action.

In Synth(\kick), there’s an invisible method call: it’s really Synth.new(\kick). The new method does several things: 1/ make a new Synth instance; 2/ call a method on the instance to produce a /s_new message with the given parameters; 3/ send the message. If you want the synth to play more than once, then you need the whole sequence – the Synth object doesn’t support the reuse of a previous sequence.

If you’d like not to rewrite the Synth instruction, a good way is to package it in a function.

(
Tdef(\d, { 
	~kick = { Synth(\kick) };
	loop {
		~kick.value;
		1.wait;
	}
})
)

“Nothing happens in SC without calling a method” – .value is the method call that retriggers the function.

Or, better for timing (see the Server Timing help file, for some details which patterns handle for you, but which become your responsibility in a Task):

(
Tdef(\d, { 
	~kick = { s.bind { Synth(\kick) } };
	loop {
		~kick.value;
		1.wait;
	}
})
)

(“What does s.bind do?” See Server Timing help first, or search the forum.)

hjh

1 Like

I got it ! thank you for this clarification !