Example #1
0
//
// I_ReadMidiTrack()
//
// Reads an entire midi track from memory and creates a list of MidiEvents
// pointers where the event's start time is the time since the beginning of the
// track.  It is the caller's responsibility to delete the list returned
// by this function.
//
static std::list<MidiEvent*> *I_ReadMidiTrack(MEMFILE *mf)
{
	if (!mf)
		return NULL;
		
	midi_chunk_header_t chunkheader;
	unsigned int track_time = 0;
	
    size_t res = mem_fread(&chunkheader, cTrackHeaderSize, 1, mf); 
	if (!res)
		return NULL;
		
	chunkheader.chunk_id = ntohl(chunkheader.chunk_id);
	chunkheader.chunk_size = ntohl(chunkheader.chunk_size);
		
	if (chunkheader.chunk_id != cTrackChunkId)
	{
		Printf(PRINT_HIGH, "I_ReadMidiTrack: Unexpected chunk header ID\n");
		return NULL;
	}
	
	std::list<MidiEvent*> *eventlist = new std::list<MidiEvent*>;
	
	size_t trackend = mem_ftell(mf) + chunkheader.chunk_size;
	while (mem_ftell(mf) < int(trackend))
	{
		MidiEvent *newevent = I_ReadMidiEvent(mf, track_time);
		
		if (!newevent)
		{
			Printf(PRINT_HIGH, "I_ReadMidiTrack: Unable to read MIDI event\n");
			
			I_ClearMidiEventList(eventlist);
			delete eventlist;
			
			return NULL;
		}
		
		eventlist->push_back(newevent);
		track_time = newevent->getMidiClockTime();
	}
	
	return eventlist;
}