Skip to content

Commit

Permalink
Remove track indexing step
Browse files Browse the repository at this point in the history
  • Loading branch information
EmK530 committed Jul 17, 2023
1 parent 524f7ef commit 8402bf0
Showing 1 changed file with 33 additions and 49 deletions.
82 changes: 33 additions & 49 deletions MIDI/LoadMIDI.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,15 @@
#include "DataStorage.c"
#include "../Playback/MainPlayer.c"

unsigned long int *trackPositions;
unsigned long int *trackSizes;
long long int lastPos = 0;
unsigned long int lastSize = 0;

int TextSearch(char text[]){
unsigned char* str = ReadRange(strlen(text));
int res = strcmp(str,text);
free(str);
return 1-res;
}

int IndexTrack(){
if(TextSearch("MTrk")==1){
unsigned long int size = (ReadFast()*16777216)+(ReadFast()*65536)+(ReadFast()*256)+ReadFast();
trackPositions[realTracks]=filePos+bufPos;
trackSizes[realTracks]=size;
//printf("\nTrack %u | Size %lu",realTracks+1,size);
bufPos+=size;
return 1;
} else {
printf("\nTextSearch failed at %lu",filePos+bufPos);
return 0;
}
}

long long ReadVariableLen(){
byte temp;
long long val = 0;
Expand All @@ -43,20 +28,28 @@ long long ReadVariableLen(){
}
return val;
}
void ParseTrack(unsigned long int id, int thres, unsigned long int size){
char ParseTrack(unsigned long int id, int thres){
unsigned long int skippedNotes[16][256];
memset(skippedNotes,0,sizeof(skippedNotes));
byte prevEvent = 0;
Seek(trackPositions[id]);
SynthEvents[id] = malloc(size/3 * sizeof(struct SynthEvent));
Tempos[id] = malloc(size/6 * sizeof(struct SynthEvent));
if(lastPos!=0){
Seek(lastPos+lastSize+8);
}
lastPos=filePos+bufPos;
if(TextSearch("MTrk")!=1){
printf("\nTextSearch failed at %lu",filePos+bufPos);
return 1;
}
lastSize = (ReadFast()*16777216)+(ReadFast()*65536)+(ReadFast()*256)+ReadFast();
printf("\nTrack %hu / %hu | Size %lu",id+1,fakeTracks,lastSize);
SynthEvents[id] = malloc(lastSize/3 * sizeof(struct SynthEvent));
Tempos[id] = malloc(lastSize/6 * sizeof(struct SynthEvent));
eventCounts[id] = 0;
tempoCounts[id] = 0;
float trackTime = 0;
unsigned long int idx = 0;
unsigned long int idx2 = 0;
unsigned long int start = filePos+bufPos;
while((filePos+bufPos)-start<size){
while((filePos+bufPos)-lastPos<lastSize){
trackTime += ReadVariableLen();
byte readEvent = ReadFast();
if(readEvent < 0x80){
Expand Down Expand Up @@ -178,12 +171,10 @@ void ParseTrack(unsigned long int id, int thres, unsigned long int size){
byte temp = Read();
tempo = (tempo<<8)|temp;
}
//printf("\nAdding tempo %lu",tempo);
struct SynthEvent eventToAdd2 = { trackTime, tempo };
Tempos[id][idx2]=eventToAdd2;
idx2++;
tempoCounts[id]++;
//printf("\nAdded tempo %lu",tempo);
} else if(readEvent == 0x2F){
break;
} else {
Expand All @@ -198,12 +189,11 @@ void ParseTrack(unsigned long int id, int thres, unsigned long int size){
}
realloc(SynthEvents[id], idx * sizeof(struct SynthEvent));
realloc(Tempos[id], idx2 * sizeof(struct SynthEvent));
return 0;
}

void LoadMIDI(char path[], int thres, unsigned int bs){
BufferInit(path, 0, bs);
trackPositions = malloc(65535 * sizeof(unsigned long int));
trackSizes = malloc(65535 * sizeof(unsigned long int));
if(TextSearch("MThd")==0){
error("MIDI header not found");
}
Expand All @@ -214,32 +204,26 @@ void LoadMIDI(char path[], int thres, unsigned int bs){
printf("\nFormat: %d",format);
printf("\nExpected Track Count: %d",fakeTracks);
printf("\nPPQ: %d",ppq);
printf("\nIndexing tracks...");
while(realTracks<fakeTracks){
if(IndexTrack()==1){
realTracks++;
} else {
printf("\nIndexing done");
break;
}
}
realloc(trackPositions, realTracks * sizeof(unsigned long int));
realloc(trackSizes, realTracks * sizeof(unsigned long int));
eventCounts = malloc(realTracks * sizeof(unsigned long int));
tempoCounts = malloc(realTracks * sizeof(unsigned long int));
eventCounts = malloc(fakeTracks * sizeof(unsigned long int));
tempoCounts = malloc(fakeTracks * sizeof(unsigned long int));
ResizeBuffer(100000000);
printf("\nIndexed %d tracks",realTracks);
printf("\nBegin parsing...");
SynthEvents = malloc(realTracks * sizeof(struct SynthEvent *));
Tempos = malloc(realTracks * sizeof(struct SynthEvent *));
for(int i = 0; i < realTracks; i++){
printf("\nTrack %hu / %hu | Size %lu",i+1,realTracks,trackSizes[i]);
ParseTrack(i,thres,trackSizes[i]);
SynthEvents = malloc(fakeTracks * sizeof(struct SynthEvent *));
Tempos = malloc(fakeTracks * sizeof(struct SynthEvent *));
int i = 0;
for(i = 0; i < fakeTracks; i++){
if(ParseTrack(i,thres)==1){
break;
}
}
realTracks=i;
printf("\nLoaded %d tracks",realTracks);
SynthEvents = realloc(SynthEvents, realTracks * sizeof(struct SynthEvent));
Tempos = realloc(Tempos, realTracks * sizeof(struct SynthEvent));
eventCounts = realloc(eventCounts, realTracks * sizeof(unsigned long int));
tempoCounts = realloc(tempoCounts, realTracks * sizeof(unsigned long int));
printf("\nLoaded %lu notes.",notes);
free(trackPositions);
free(trackSizes);
free(buffer);
printf("\nBeginning playback...");
StartPlayback();
}
}

0 comments on commit 8402bf0

Please sign in to comment.