Producing count-in beats seems to be a rather simple problem: put the MIDI events (normally event_on event_off events on channel 10) in front of the MIDI file and play that file. This was my first naive approach. You will find that this is not a satisfying approach with many MIDI files.
For scores starting with an anacrusis some usual MIDI converters (I use the MIDI converter in Capella V4.0) generate rests in front of a MIDI score - even if you don't enter rests in the score:
This is normally not a problem - both notations and MIDI renditions are valid. It may however be a problem if you add count-in beats in front of the MIDI tune: should the count-in beats replace the rests or leave the rests intact? I guess that every normal musician agrees that the count-in beats replace (overwrite) the rests. Things may even be more complex: the sum of rests may be so small that a single count-in beat cannot fill this measure!.
When adding count-in beats a designer for a MIDI editor must proceed as follows:
The bitter consequence is thus: it is not possible to design an algorithm that produces correct count-in events for all MIDI files.
Even if you succeed to place the count-in beats correctly in the MIDI file some problems may still remain. The musician cannot be forced to specify the "usual" number of count-in beats. In our notation sample above this would be 3 or 7 count-in beats - he can also specify 5 or 6 or.... if he likes this. You (being the designer of the MIDI editor) will now have the problem that the bar/beat count may be incorrect - which may lead to an even more complex algorithm just to produce count-in beats.
Let me remember that - even if you have solved all these problems - some questions remain open: