components/prboom/mmus2mid.c
//
// MidiToMIDI()
//
// Convert an in-memory copy of a MIDI format 0 or 1 file to
// an Allegro MIDI structure, that is valid or has been zeroed
//
// Passed a pointer to a memory buffer with MIDI format music in it and a
// pointer to an Allegro MIDI structure.
//
// Returns 0 if successful, BADMIDHDR if the buffer is not MIDI format
//
MidiToMIDI()
Convert an in-memory copy of a MIDI format 0 or 1 file to
an Allegro MIDI structure, that is valid or has been zeroed

Passed a pointer to a memory buffer with MIDI format music in it and a
pointer to an Allegro MIDI structure.

Returns 0 if successful, BADMIDHDR if the buffer is not MIDI format
MidiToMIDI () Convert an in - memory copy of a MIDI format 0 or 1 file to an Allegro MIDI structure that is valid or has been zeroed Passed a pointer to a memory buffer with MIDI format music in it and a pointer to an Allegro MIDI structure . Returns 0 if successful BADMIDHDR if the buffer is not MIDI format
int MidiToMIDI(UBYTE *mid,MIDI *mididata)
{
  int i;
  int ntracks;
  if (memcmp(mid,midihdr,4))
    return BADMIDHDR;
  mididata->divisions = (mid[12]<<8)+mid[13];
  ntracks = (mid[10]<<8)+mid[11];
  if (ntracks>=MIDI_TRACKS)
    return BADMIDHDR;
  mid += 4;
  {  
    size_t t = ReadLength(&mid);            
    mid += t;
  }
  for (i=0;i<ntracks;i++)
  {
    while (memcmp(mid,trackhdr,4))    
    {
      mid += 4;
      {
        size_t t = ReadLength(&mid);            
        mid += t;  
      }
    }
    mid += 4;
    mididata->track[i].len = ReadLength(&mid);  
    mididata->track[i].data = realloc(mididata->track[i].data,mididata->track[i].len);
    memcpy(mididata->track[i].data,mid,mididata->track[i].len);
    mid += mididata->track[i].len;
  }
  for (;i<MIDI_TRACKS;i++)
    if (mididata->track[i].len)
    {
      free(mididata->track[i].data);
      mididata->track[i].data = NULL;
      mididata->track[i].len = 0;
    }
  return 0;
}
int MidiToMIDI ( UBYTE * mid , MIDI * mididata ) { int i ; int ntracks ; if ( memcmp ( mid , midihdr , 4 ) ) return BADMIDHDR ; mididata -> divisions = ( mid [ 12 ] << 8 ) + mid [ 13 ] ; ntracks = ( mid [ 10 ] << 8 ) + mid [ 11 ] ; if ( ntracks >= MIDI_TRACKS ) return BADMIDHDR ; mid += 4 ; { size_t t = ReadLength ( & mid ) ; mid += t ; } for ( i = 0 ; i < ntracks ; i ++ ) { while ( memcmp ( mid , trackhdr , 4 ) ) { mid += 4 ; { size_t t = ReadLength ( & mid ) ; mid += t ; } } mid += 4 ; mididata -> track [ i ] . len = ReadLength ( & mid ) ; mididata -> track [ i ] . data = realloc ( mididata -> track [ i ] . data , mididata -> track [ i ] . len ) ; memcpy ( mididata -> track [ i ] . data , mid , mididata -> track [ i ] . len ) ; mid += mididata -> track [ i ] . len ; } for ( ; i < MIDI_TRACKS ; i ++ ) if ( mididata -> track [ i ] . len ) { free ( mididata -> track [ i ] . data ) ; mididata -> track [ i ] . data = NULL ; mididata -> track [ i ] . len = 0 ; } return 0 ; }
MidiToMIDI()
Convert an in-memory copy of a MIDI format 0 or 1 file to
an Allegro MIDI structure, that is valid or has been zeroed
MidiToMIDI () Convert an in - memory copy of a MIDI format 0 or 1 file to an Allegro MIDI structure that is valid or has been zeroed
// read the midi header // killough 10/7/98: fix mid from being modified twice before sequence pt. // seek past header // now read each track // simply skip non-track data // seek past header // killough 10/7/98: prevent mid undefined behavior // get length, move mid past it // read a track