Max SDK t-timeobject – creating a callback function that includes a voice number
I’m trying write an external that has multiple voices, each voice driven by an ITM time object. I want each voice, 0 to NVOICES-1, to run independently of the others. I’m working from the info on ITM in Chapter 18 of the Max API doc, and adapting the delay2 code example that’s shipped with the Max sdk.
I’m able to create an array of time objects, one per voice, declaring it in the master structure of the external (called reA) as:
t_object *d_timeObj[NVOICES];
…and I can declare the callback function:
void reA_doOne(t_reA *x);
I’m then able to instantiate these giving a single callback function reA_doOne() as follows:
for (voiceNo=NVOICES-1; voiceNo>=0; voiceNo--) {
x->d_timeObj[voiceNo] = (t_object *) time_new((t_object *)x, gensym("delaytime"), (method)reA_doOne, TIME_FLAGS_TICKSONLY | TIME_FLAGS_USECLOCK);
}
That all compiles and runs, but it’s not quite enough. When reA_doOne() gets called, it doesn’t know which time object in the array (ie which voice ) is calling it. Hmm.
So I tried the following...
Declaration:
void reA_doOne(t_reA *x, long voiceNo);
Instantiation of the time object:
for (voiceNo=NVOICES-1; voiceNo>=0; voiceNo--) {
x->d_timeObj[voiceNo] = (t_object *) time_new((t_object *)x, gensym("delaytime"), (method)reA_doOne(*x, voiceNo), TIME_FLAGS_TICKSONLY | TIME_FLAGS_USECLOCK);
}
But that fails. It seems, not too surprisingly, that the time_new() function will only accept a method name reA-doOne; it won’t accept adding arguments to that method in the call. The compiler error I get is:
Semantic Issue Group
Passing 't_reA' (aka 'struct reA') to parameter of incompatible type 't_reA *' (aka 'struct reA *'); remove *
Passing argument to parameter 'x' here
I’ve tried lots of variants of this, but haven’t found a way yet.
Again, all ll I need is that when the reA_doOne() function gets called, I know which timeObject in the array of time objects (ie which voice) called it.
My fallback is to create a slew of explicitly-named functions, one for each voice, reA_doOne00(), reA_doOne01(), reA_doOne02() etc, each set up as the callback function for one timeObject. But that's ugly and doesn't allow the number of voices to be changed easily.
Any thoughts?