Combining MIDI notes and controls

Hi,
I’d like to control several patterns alternately, according to a midi note number, with a single midi controller. For example, when I play midi note 60, the values of controller 10 control a parameter of pattern A, and when I stop playing this note, my pattern A continues in automatic mode. When I play midi note 61, my cc 10 controls a Pattern B parameter, and so on.

I’m looking for the best way to code this kind of interaction. In fact, it’s a question of reconstructing a kind of aftertouch, for an interface (the Sylphyo) that doesn’t have one.

Here’s a simplified proposal, I have the feeling that it’s not a good way to realize this interaction, especially if I multiply the number of notes and make the pattern more complex. What’s more, I think it would be better to avoid the last loop (routine).

I’d be very grateful for your opinion and suggestions.

MIDIIn.connectAll;
(
//Get note on and off
(
MIDIdef.noteOn(\note60on, { |val, num, chan, src|
	~note60 = true;
}, noteNum: 60);

MIDIdef.noteOff(\note60off, { |val, num, chan, src|
	~note60 = false;
}, noteNum: 60);
);

////Get controller value
MIDIdef.cc(\ctlin, { |val|
    ~valCC10 = val.linlin(0, 127, 0, 1);  // Conversion de la valeur CC
}, ccNum: 10);

// A Flow
 Pbindef(\autoFlow,
    //\instrument, \synthA,  // Synthétiseur par défaut
    \freq, Pwhite(440, 460, inf),  // Exemple de séquence de fréquence
    \dur, 0.1  // Durée de chaque note
).play;

// Conditionnal action
~controlWithCC = {
    if (~note60,
		//Note On, the pattern is controlled by cc 10
		{Pbindef(\autoFlow, \freq, Pfunc{~valCC10 * 1000 + 50});},
		//Note Off, the pattern works automatically
	    {Pbindef(\autoFlow, \freq, Pwhite(440, 460, inf))}
	);
};

//
~checkControl = Routine({
    inf.do {
        ~controlWithCC.value;
        0.02.wait;
    }
}).play;
)

To avoid the last routine, call controlWithCC inside MIDIdef:

MIDIdef.noteOn(\note60on, { |val, num, chan, src|
	~note60 = true;
    ~controlWithCC.value;
}, noteNum: 60);

I don’t see problems with your approach, I think the best way is to multiply the number of notes and make the pattern more complex, and then you will see that lot of code are duplicated. Then it will be clear for you what code you should put in a function so you don’t have to type it again and again

For exemple you can create a data structure like this:
~setup = [ (noteNum: 60, valfunc: { { Pfunc{~valCC10 * 1000 + 50} } }) ];
then process the list to generate automatically MIDIdef and ~controlWithCC

An idea that you may find useful : instead of replacing the key in Pdef(\autoFlow), play a chained pattern Pdef(\modpat) <> Pdef(\autoFlow) then ~controlWithCC use Pbindef(\modpat, …) or Pdef(\modpat).set(…) to override a parameter. With noteOff you just have to erase it and the overridden pattern appear again.

Thank you very much for your reply, which helped me to think of another, more concise solution:

(
MIDIdef.cc(\ctlin, { |val| ~valCC10 = val.linlin(0, 127, 0, 1)}, ccNum: 10);

~mappingOn = (
	60: {Pbindef(\autoFlow, \freq, Pfunc{~valCC10 * 1000 + 50})},
	62: {Pbindef(\autoFlow2, \freq, Pfunc{~valCC10.range(1000, 2000)})},
	64: {Pbindef(\autoFlow3, \freq, Pfunc{~valCC10.range(100, 200)})}
);

~mappingOff = (
	60: {Pbindef(\autoFlow, \freq, Pwhite(440, 460, inf))},
	62: {Pbindef(\autoFlow2, \freq, Pwhite(220, 230, inf))},
	64: {Pbindef(\autoFlow3, \freq, Pwhite(110, 115, inf))}
);

Pbindef(\autoFlow, \freq, Pwhite(440, 460, inf), \dur, 0.1).play;
Pbindef(\autoFlow2, \freq, Pwhite(220, 230, inf), \dur, 0.1).play;
Pbindef(\autoFlow3, \freq, Pwhite(110, 115, inf), \dur, 0.1).play;

(
MIDIdef.noteOn(\noteOnTest, { |val, num, chan, src|
	~mappingOn[num].value;
});

MIDIdef.noteOff(\note60off, { |val, num, chan, src|
	~mappingOff[num].value;
});
)
)

I haven’t yet studied this way of composing with the <> symbol, but as these are different patterns for each note, each following its own automatic mode, I’m not sure it’s useful here ?