MIDISetEvent() can insert a new event in a track.

Before calling MIDISetEvent(), you need to set the variable MIDIEvent.!Type to the desired event ID number or name to be inserted. For example, to insert a On Note event, you can set MIDIEvent.!Type to either 'On Note' or 144.

You should also set the other variables that would be set when editing an event.

Finally, you should specify the INS option, as well as the TIME, CHAN, and DATA options.

The new event will be initially inserted into the currently selected track (unless you specify the track into which to insert). The new event is inserted at the end of the track, right before any End of Track event (unless you specify the BEFORE or AFTER options, discussed below). If the event's TIME is such that it needs to be moved elsewhere in the track, you'll need to sort the track. That can be done when you insert the event, by specifying the SORT option. But if you're inserting numerous events (via numerous calls to MIDISetEvent), it is more efficient to sort the track once only, after you're finished inserting all desired events into the track. (Alternately, if you're inserting only 1 event into a track that already contains some events, you may prefer to use the BEFORE or AFTER methods of insertion to place the event where it should ultimately go, and avoid the need to sort the track).

For example, here we insert an On Note event at the end of the currently selected track:

/* Set the various variables */
MIDIEvent.!Type = 'On Note'  /* Event type */
MIDIEvent.!Measure = 1       /* TIME is 1:1:0 */
MIDIEvent.!Beat = 1
MIDIEvent.!Clock = 0
MIDIEvent.!Channel = 1
MIDIEvent.!Data1 = 60        /* Note Number */
MIDIEvent.!Data2 = 64        /* Velocity */

/* Insert it */
err = MIDISetEvent('INS|TIME|CHAN|DATA')

/* An error? */
IF err \== "" THEN SAY err
When you insert a new event, it automatically becomes the currently selected event.


Specifying the track

If you wish to insert the event in some track other than the currently selected track, you pass the desired number of the track into which to insert the new event, where 1 is the first track.

For example, here we insert an On Note event at the end of the track 10:

/* Set the various variables */
MIDIEvent.!Type = 'On Note'
MIDIEvent.!Measure = 1
MIDIEvent.!Beat = 1
MIDIEvent.!Clock = 0
MIDIEvent.!Channel = 1
MIDIEvent.!Data1 = 60
MIDIEvent.!Data2 = 64

/* Insert it in track 10 */
err = MIDISetEvent('INS|TIME|CHAN|DATA', 10)
IF err \== "" THEN SAY err
The track you specify automatically becomes the currently selected track.


Inserting events in a new (empty) track

If the track you specify happens to be an empty track (ie, doesn't already contain any events), then the track is created anew, and the event is added to the end of it. For this reason, it is best to insert the events starting with the event that has the earliest time. The last event to be inserted should be the End of Track event. In this way, you'll be inserting events respective to their TIME, and no sorting will need to be performed upon the track.


Inserting events BEFORE/AFTER

You can insert an event BEFORE or AFTER the currently selected event. If the event to be inserted has a TIME that would place it immediately before or after the currently selected event, then this will prevent having to SORT the track.

Simply specify the BEFORE or AFTER option when inserting the event, in addition to the INS (and other) options:

/* Insert after the currently selected event */
err = MIDISetEvent('AFTER|INS|TIME|CHAN|DATA')

Sorting events inserted out of order

If you do insert events out of order according to their TIME, you can instruct MIDISetEvent() to perform a SORT on the track to reorder the events according to their TIME. It is best to do this once only, after all events are inserted.

For example, here we sort the currently selected track (after events have been inserted into it):

/* SORT the currently selected track */
MIDISetEvent('SORT')
To save an additional call to MIDISetEvent(), you can do the SORT when you insert the last event:
/* Insert the last event and SORT */
err = MIDISetEvent('SORT|INS|TIME|CHAN|DATA')

Errata

To save some typing, you can abbreviate any of the event names to just the first four characters. For example, the following two lines both specify an On Note:

MIDIEvent.!Type = 'On N'
MIDIEvent.!Type = 'On Note'