MidiInsertTrackEvents

DWORD WINAPI MidiInsertTrackEvents
(
	HANDLE hSequence, 
	WORD wTrack, 
	DWORD dwEvents, 
	LPBRELS_MIDI_EVENT lpEvents
);

This function inserts one or more MIDI events (encoded in a BRELS_MIDI_EVENT array) into a track of the desired sequence.

Parameters

Return values

Returns the new number of events in the track, or zero if an error occurred. The most likely causes to an error are an invalid sequence handle, an invalid track index or an invalid events array.

Remarks

Be sure that the array of events has the correct size and its structures are correctly filled. Check BRELS_MIDI_EVENT for more information. The events do not need to be ordered. The end-of-track event (0x2F) is not necessary since it is automatically appended to the sequence.

When filling many events at once, you could use a helper function just like the one in the example below:

Example

void FillEvent
(
	LPBRELS_MIDI_EVENT lpEvent, 
	WORD wTag, 
	DWORD dwTicks, 
	BYTE Event, 
	BYTE DataSize, 
	LPBYTE lpData
)
{
	lpEvent->wTag = wTag;
	lpEvent->dwTicks = dwTicks;
	lpEvent->Event = Event;
	lpEvent->DataSize = DataSize;
	if ((Event<0x80) || (Event==0xF0)) // Variable-length events
		lpEvent->lpData = lpData;
	else
		CopyMemory(&lpEvent->Data, lpData, DataSize);
};

void Encode()
{
	HANDLE Sequence;
	BRELS_MIDI_EVENT Events[17];

	MidiCreate(NO_DEVICE, 16, &Sequence);

	// Tempo: 16 ticks mean 500000 (0x07A120) microseconds
	FillEvent(&Events[ 0], 0,   0, 0x51, 3, "\x07\xA1\x20");
	FillEvent(&Events[ 1], 0,   0, 0x90, 2, "\x40\x7F"); // E5 On
	FillEvent(&Events[ 2], 0,  32, 0x80, 2, "\x40\x00"); // E5 Off
	FillEvent(&Events[ 3], 0,  32, 0x90, 2, "\x41\x7F"); // F5 On
	FillEvent(&Events[ 4], 0,  64, 0x80, 2, "\x41\x00"); // F5 Off
	FillEvent(&Events[ 5], 0,  64, 0x90, 2, "\x43\x7F"); // G5 On
	FillEvent(&Events[ 6], 0,  96, 0x80, 2, "\x43\x00"); // G5 Off
	FillEvent(&Events[ 7], 0,  96, 0x90, 2, "\x45\x7F"); // A5 On
	FillEvent(&Events[ 8], 0, 128, 0x80, 2, "\x45\x00"); // A5 Off
	FillEvent(&Events[ 9], 0, 128, 0x90, 2, "\x40\x7F"); // E5 On
	FillEvent(&Events[10], 0, 160, 0x80, 2, "\x40\x00"); // E5 Off
	FillEvent(&Events[11], 0, 160, 0x90, 2, "\x41\x7F"); // F5 On
	FillEvent(&Events[12], 0, 192, 0x80, 2, "\x41\x00"); // F5 Off
	FillEvent(&Events[13], 0, 192, 0x90, 2, "\x43\x7F"); // G5 On
	FillEvent(&Events[14], 0, 224, 0x80, 2, "\x43\x00"); // G5 Off
	FillEvent(&Events[15], 0, 224, 0x90, 2, "\x45\x7F"); // A5 On
	FillEvent(&Events[16], 0, 256, 0x80, 2, "\x45\x00"); // A5 Off

	MidiInsertTrack(Sequence, 0);
	MidiInsertTrackEvents(Sequence, 0, 17, (LPBRELS_MIDI_EVENT) &Events);
	MidiEncode(Sequence, "test1.mid", TRUE);
	MidiClose(Sequence);
};

Further information

Released in November 19th, 2003
Updated in April 29th, 2004
Property of Breno de Lima Sarmento
Home page: http://www27.brinkster.com/brels
E-mail: [email protected]
ICQ: 78977999 ()