SimpleMIDIFile fromPattern track order

If I have a pattern with multiple tracks (defined by multiple instruments), how can I force them to be in the order I want?

Currently I can get SimpleMIDIFile only to order the tracks based on the time of the first note (earlier first note, lower track number). Then I import into MuseScore and a bass part may be in the top staff – no thanks.

I tried putting dummy events at the beginning of my pattern, but SimpleMIDIFile ignores them because they are rests.

So… is this genuinely a fully unsupported requirement?

EDIT: Answering my own question… seems no alternative but to add a trackNames parameter. I can pull-request that later.

hjh

Hi James,

good point, indeed that is indeed a missing feature. A trackNames parameter could be a nice one. I could also imagine adding a \track key to your pattern and make SimpleMIDIFile:fromPattern use that if not nil and otherwise default to the current behavior. Meanwhile, it is also possible to post-process your SimpleMIDIFile. Example:

m = SimpleMIDIFile.fromPattern(
	Pbind( \instrument, Pseq([\a,\b],inf), \freq, Pseq( [ 330, Pwhite( 220, 440, 1 ) ], inf ), \dur, 0.1 );
);

// switch first and second track (very crude but it works ;-) )
m.midiEvents.do({ |item|
	item[0] = switch( item[0], 1, 2, 2, 1 );
});

Correction, this already works. Simply add a ‘track’ key to your Pattern and it will override the automatic track assignment. Example below puts things in track 10 and 20 (just to prove the point):

m = SimpleMIDIFile.fromPattern(
	Pbind( \instrument, Pseq([\a,\b],inf), \freq, Pseq( [ 330, Pwhite( 220, 440, 1 ) ], inf ), \dur, 0.1, \track, Pseq([10,20],inf) );
);