MIDIdef.cc to Routine doesen't work for me

I can’t understand why is not working …

(
~a = Routine {
	arg inval=0.0;

	loop{

		inval.debug("routine inval: " );

		0.1.wait;

	};
}.play;
)

MIDIClient.init;
MIDIIn.connectAll;

(
// cc
MIDIdef.cc(\cc16,{
	arg value, ccNum, chan, src;
	//[value,ccNum,chan,src].postln;
	switch(ccNum, 16,{~a.(value)}
	);

inval should be written like this:

(
~a = Routine { arg inval = 0.0;
	loop{
		inval.debug("routine inval: " );
		inval = 0.1.wait;
	};
}.play;
)

inval = 0.1.wait; looks weird, but that’s only because of a programming language convention which was probably a mistake from the beginning (going back decades, long before SC).

a = 1 + 1 performs the right-hand side first, then performs the assignment into a. It would be more accurately written 1 + 1 ==> a.

wait is a synonym for yield. So if assignments were written in the order of execution, it would be 0.1.wait ==> inval. Then it would be more clear that the routine’s execution pauses upon wait, and then after wait returns, a new inval is available.

This is documented in the pattern internals helpfile.

After fixing that, then you will discover that, when you’re playing a Routine, inval is reserved for the current clock time. When playing a routine, you cannot use inval to inject values from outside.

So you’ll have to delete the .play to make it work with MIDIdef. Or, in MIDIdef, stash the value in a separate variable, and access the variable in the routine’s loop.

hjh

1 Like

Thank you very much indeed!