Skip to content

Commit

Permalink
Use RWops also for mac and win32
Browse files Browse the repository at this point in the history
  • Loading branch information
pmandin committed Jul 16, 2005
1 parent 8526202 commit 38c5f27
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 38 deletions.
30 changes: 13 additions & 17 deletions native_midi/native_midi_common.c
Expand Up @@ -277,7 +277,7 @@ static MIDIEvent *MIDItoStream(MIDIFile *mididata)
return currentEvent;
}

static int ReadMIDIFile(MIDIFile *mididata, FILE *fp)
static int ReadMIDIFile(MIDIFile *mididata, SDL_RWops *rw)
{
int i = 0;
Uint32 ID;
Expand All @@ -288,27 +288,27 @@ static int ReadMIDIFile(MIDIFile *mididata, FILE *fp)

if (!mididata)
return 0;
if (!fp)
if (!rw)
return 0;

/* Make sure this is really a MIDI file */
fread(&ID, 1, 4, fp);
SDL_RWread(rw, &ID, 1, 4);
if (BE_LONG(ID) != 'MThd')
return 0;

/* Header size must be 6 */
fread(&size, 1, 4, fp);
SDL_RWread(rw, &size, 1, 4);
size = BE_LONG(size);
if (size != 6)
return 0;

/* We only support format 0 and 1, but not 2 */
fread(&format, 1, 2, fp);
SDL_RWread(rw, &format, 1, 2);
format = BE_SHORT(format);
if (format != 0 && format != 1)
return 0;

fread(&tracks, 1, 2, fp);
SDL_RWread(rw, &tracks, 1, 2);
tracks = BE_SHORT(tracks);
mididata->nTracks = tracks;

Expand All @@ -321,14 +321,14 @@ static int ReadMIDIFile(MIDIFile *mididata, FILE *fp)
}

/* Retrieve the PPQN value, needed for playback */
fread(&division, 1, 2, fp);
SDL_RWread(rw, &division, 1, 2);
mididata->division = BE_SHORT(division);


for (i=0; i<tracks; i++)
{
fread(&ID, 1, 4, fp); /* We might want to verify this is MTrk... */
fread(&size, 1, 4, fp);
SDL_RWread(rw, &ID, 1, 4); /* We might want to verify this is MTrk... */
SDL_RWread(rw, &size, 1, 4);
size = BE_LONG(size);
mididata->track[i].len = size;
mididata->track[i].data = malloc(size);
Expand All @@ -337,7 +337,7 @@ static int ReadMIDIFile(MIDIFile *mididata, FILE *fp)
Mix_SetError("Out of memory");
goto bail;
}
fread(mididata->track[i].data, 1, size, fp);
SDL_RWread(rw, mididata->track[i].data, 1, size);
}
return 1;

Expand All @@ -351,9 +351,8 @@ static int ReadMIDIFile(MIDIFile *mididata, FILE *fp)
return 0;
}

MIDIEvent *CreateMIDIEventList(char *midifile, Uint16 *division)
MIDIEvent *CreateMIDIEventList(SDL_RWops *rw, Uint16 *division)
{
FILE *fp = NULL;
MIDIFile *mididata = NULL;
MIDIEvent *eventList;
int trackID;
Expand All @@ -363,17 +362,14 @@ MIDIEvent *CreateMIDIEventList(char *midifile, Uint16 *division)
return NULL;

/* Open the file */
fp = fopen(midifile, "rb");
if ( fp != NULL )
if ( rw != NULL )
{
/* Read in the data */
if ( ! ReadMIDIFile(mididata, fp))
if ( ! ReadMIDIFile(mididata, rw))
{
free(mididata);
fclose(fp);
return NULL;
}
fclose(fp);
}
else
{
Expand Down
2 changes: 1 addition & 1 deletion native_midi/native_midi_common.h
Expand Up @@ -58,7 +58,7 @@ typedef struct MIDIEvent
/* Load a midifile to memory, converting it to a list of MIDIEvents.
This function returns a linked lists of MIDIEvents, 0 if an error occured.
*/
MIDIEvent *CreateMIDIEventList(char *midifile, Uint16 *division);
MIDIEvent *CreateMIDIEventList(SDL_RWops *rw, Uint16 *division);

/* Release a MIDIEvent list after usage. */
void FreeMIDIEventList(MIDIEvent *head);
Expand Down
69 changes: 66 additions & 3 deletions native_midi/native_midi_mac.c
Expand Up @@ -95,15 +95,20 @@ NativeMidiSong *native_midi_loadsong(char *midifile)
int part_poly_max[32];
int numParts = 0;
Uint16 ppqn;
SDL_RWops *rw;

/* Init the arrays */
memset(part_poly_max,0,sizeof(part_poly_max));
memset(part_to_inst,-1,sizeof(part_to_inst));

/* Attempt to load the midi file */
evntlist = CreateMIDIEventList(midifile, &ppqn);
if (!evntlist)
goto bail;
rw = SDL_RWFromFile(midifile, "rb");
if (rw) {
evntlist = CreateMIDIEventList(rw, &ppqn);
SDL_RWclose(rw);
if (!evntlist)
goto bail;
}

/* Allocate memory for the song struct */
song = malloc(sizeof(NativeMidiSong));
Expand Down Expand Up @@ -152,6 +157,64 @@ NativeMidiSong *native_midi_loadsong(char *midifile)

NativeMidiSong *native_midi_loadsong_RW(SDL_RWops *rw)
{
NativeMidiSong *song = NULL;
MIDIEvent *evntlist = NULL;
int part_to_inst[32];
int part_poly_max[32];
int numParts = 0;
Uint16 ppqn;

/* Init the arrays */
memset(part_poly_max,0,sizeof(part_poly_max));
memset(part_to_inst,-1,sizeof(part_to_inst));

/* Attempt to load the midi file */
evntlist = CreateMIDIEventList(rw, &ppqn);
if (!evntlist)
goto bail;

/* Allocate memory for the song struct */
song = malloc(sizeof(NativeMidiSong));
if (!song)
goto bail;

/* Build a tune sequence from the event list */
song->tuneSequence = BuildTuneSequence(evntlist, ppqn, part_poly_max, part_to_inst, &numParts);
if(!song->tuneSequence)
goto bail;

/* Now build a tune header from the data we collect above, create
all parts as needed and assign them the correct instrument.
*/
song->tuneHeader = BuildTuneHeader(part_poly_max, part_to_inst, numParts);
if(!song->tuneHeader)
goto bail;

/* Increment the instance count */
gInstaceCount++;
if (gTunePlayer == NULL)
gTunePlayer = OpenDefaultComponent(kTunePlayerComponentType, 0);

/* Finally, free the event list */
FreeMIDIEventList(evntlist);

return song;

bail:
if (evntlist)
FreeMIDIEventList(evntlist);

if (song)
{
if(song->tuneSequence)
free(song->tuneSequence);

if(song->tuneHeader)
DisposePtr((Ptr)song->tuneHeader);

free(song);
}

return NULL;
}

Expand Down
59 changes: 42 additions & 17 deletions native_midi/native_midi_win32.c
Expand Up @@ -192,32 +192,57 @@ int native_midi_detect()

NativeMidiSong *native_midi_loadsong(char *midifile)
{
NativeMidiSong *newsong;
NativeMidiSong *newsong;
MIDIEvent *evntlist = NULL;
SDL_RWops *rw;

newsong=malloc(sizeof(NativeMidiSong));
if (!newsong)
return NULL;
memset(newsong,0,sizeof(NativeMidiSong));

/* Attempt to load the midi file */
rw = SDL_RWFromFile(midifile, "rb");
if (rw) {
evntlist = CreateMIDIEventList(rw, &newsong->ppqn);
SDL_RWclose(rw);
if (!evntlist)
{
free(newsong);
return NULL;
}
}

newsong=malloc(sizeof(NativeMidiSong));
if (!newsong)
return NULL;
memset(newsong,0,sizeof(NativeMidiSong));

/* Attempt to load the midi file */
evntlist = CreateMIDIEventList(midifile, &newsong->ppqn);
if (!evntlist)
{
free(newsong);
return NULL;
}

MIDItoStream(newsong, evntlist);
MIDItoStream(newsong, evntlist);

FreeMIDIEventList(evntlist);

return newsong;
return newsong;
}

NativeMidiSong *native_midi_loadsong_RW(SDL_RWops *rw)
{
return NULL;
NativeMidiSong *newsong;
MIDIEvent *evntlist = NULL;

newsong=malloc(sizeof(NativeMidiSong));
if (!newsong)
return NULL;
memset(newsong,0,sizeof(NativeMidiSong));

/* Attempt to load the midi file */
evntlist = CreateMIDIEventList(rw, &newsong->ppqn);
if (!evntlist)
{
free(newsong);
return NULL;
}

MIDItoStream(newsong, evntlist);

FreeMIDIEventList(evntlist);

return newsong;
}

void native_midi_freesong(NativeMidiSong *song)
Expand Down

0 comments on commit 38c5f27

Please sign in to comment.