bool processTracks(MIDI_PLAYER* pMp) { int32_t deltaTick = pMp->currentTick - pMp->lastTick; // Must NEVER be negative!!! if (deltaTick < 0) { hal_printfWarning("Warning: deltaTick is negative! Fast forward? deltaTick=%d", deltaTick); // TODO: correct time here, to prevent skips on following tempo changes! deltaTick = 0; } bool eventsNeedToBeFetched = false; pMp->allTracksAreFinished = true; for (int iTrack = 0; iTrack < midiReadGetNumTracks(pMp->pMidiFile); iTrack++) { pMp->pMidiFile->Track[iTrack].deltaTime -= deltaTick; pMp->trackIsFinished = pMp->pMidiFile->Track[iTrack].ptrNew == pMp->pMidiFile->Track[iTrack].pEndNew; if (!pMp->trackIsFinished) { pMp->allTracksAreFinished = false; eventsNeedToBeFetched = isItTimeToFireThisEvent(pMp, iTrack); } } pMp->lastTick = pMp->currentTick; return eventsNeedToBeFetched; }
void TestEventList(const char *pFilename) { MIDI_FILE *mf = midiFileOpen(pFilename); if (mf) { MIDI_MSG msg; int i, iNum; unsigned int j; midiReadInitMessage(&msg); iNum = midiReadGetNumTracks(mf); for(i=0;i<iNum;i++) { printf("# Track %d\n", i); while(midiReadGetNextMessage(mf, i, &msg)) { printf("\t"); for(j=0;j<msg.iMsgSize;j++) printf("%.2x ", msg.data[j]); printf("\n"); } } midiReadFreeMessage(&msg); midiFileClose(mf); } }
bool playMidiFile(MIDI_PLAYER* pMidiPlayer, const char *pFilename) { if (!midiPlayerOpenFile(pMidiPlayer, pFilename)) return false; hal_printfInfo("Midi Format: %d", pMidiPlayer->pMidiFile->Header.iVersion); hal_printfInfo("Number of tracks: %d", midiReadGetNumTracks(pMidiPlayer->pMidiFile)); hal_printfSuccess("Start playing..."); return true; }
bool midiPlayerOpenFile(MIDI_PLAYER* pMidiPlayer, const char* pFileName) { pMidiPlayer->pMidiFile = midiFileOpen(pFileName); if (!pMidiPlayer->pMidiFile) return false; // Load initial midi events for (int iTrack = 0; iTrack < midiReadGetNumTracks(pMidiPlayer->pMidiFile); iTrack++) { midiReadGetNextMessage(pMidiPlayer->pMidiFile, iTrack, &pMidiPlayer->msg[iTrack]); pMidiPlayer->pMidiFile->Track[iTrack].deltaTime = pMidiPlayer->msg[iTrack].dt; } pMidiPlayer->startTime = hal_clock() * 1000; pMidiPlayer->currentTick = 0; pMidiPlayer->lastTick = 0; pMidiPlayer->trackIsFinished = true; pMidiPlayer->allTracksAreFinished = false; pMidiPlayer->lastUsPerTick = pMidiPlayer->pMidiFile->usPerTick; return true; }
void LoadMIDIEventList(const char *pFilename) { printf("--- MIDISYS ENGINE: LoadMIDIEventList(\"%s\")\n", pFilename); MIDI_FILE *mf = midiFileOpen(pFilename); char str[128]; int ev; int timeline_index = 0; int track_index = 0; MIDI_MSG msg; if (mf) { int i, iNum; unsigned int j; midiReadInitMessage(&msg); iNum = midiReadGetNumTracks(mf); for(i=0;i<iNum;i++) { #ifdef SUPERVERBOSE printf("# Track %d\n", i); #endif while(midiReadGetNextMessage(mf, i, &msg)) { #ifdef SUPERVERBOSE printf(" %.6ld ", msg.dwAbsPos); #endif if (msg.bImpliedMsg) { ev = msg.iImpliedMsg; } else { ev = msg.iType; } memcpy(&timeline[track_index][timeline_index], &msg, sizeof(MIDI_MSG)); if (ev == msgMetaEvent && msg.MsgData.MetaEvent.iType == metaTrackName) { strncpy(timeline_trackname[track_index], msg.MsgData.MetaEvent.Data.Text.pData, 8); timeline_trackname[track_index][8] == '\0'; printf("track #%d, name = \"%s\"\n", track_index, timeline_trackname[track_index]); } #ifdef SUPERVERBOSE DebugPrintEvent(ev,msg); #endif timeline_index++; } printf("track length: %d\n", timeline_index); timeline_tracklength[track_index] = timeline_index; track_index++; timeline_index = 0; } timeline_trackcount = track_index; midiReadFreeMessage(&msg); midiFileClose(mf); } //timeline_length = timeline_index+1; printf("--- MIDISYS ENGINE: LoadMIDIEventList() success\n"); }