components/prboom/mmus2mid.c
//
// MIDIToMidi()
//
// This routine converts an Allegro MIDI structure to a midi 1 format file
// in memory. It is used to support memory MUS -> MIDI conversion
//
// Passed a pointer to an Allegro MIDI structure, a pointer to a pointer to
// a buffer containing midi data, and a pointer to a length return.
// Returns 0 if successful, MEMALLOC if a memory allocation error occurs
//
MIDIToMidi()
This routine converts an Allegro MIDI structure to a midi 1 format file
in memory. It is used to support memory MUS -> MIDI conversion

Passed a pointer to an Allegro MIDI structure, a pointer to a pointer to
a buffer containing midi data, and a pointer to a length return.
Returns 0 if successful, MEMALLOC if a memory allocation error occurs
MIDIToMidi () This routine converts an Allegro MIDI structure to a midi 1 format file in memory . It is used to support memory MUS - > MIDI conversion Passed a pointer to an Allegro MIDI structure a pointer to a pointer to a buffer containing midi data and a pointer to a length return . Returns 0 if successful MEMALLOC if a memory allocation error occurs
int MIDIToMidi(MIDI *mididata,UBYTE **mid,int *midlen)
{
  size_t total;
  int i,ntrks;
  UBYTE *midiptr;
  total = sizeof(midihdr);
  for (i=0,ntrks=0;i<MIDI_TRACKS;i++)
    if (mididata->track[i].len)
    {
      total += 8 + mididata->track[i].len; 
      ntrks++;
    }
  if ((*mid = malloc(total))==NULL)
    return MEMALLOC;
  midihdr[10] = 0;
  midihdr[11] = (UBYTE)ntrks;   
  midihdr[12] = (mididata->divisions>>8) & 0x7f;
  midihdr[13] = (mididata->divisions) & 0xff;
  midiptr = *mid;
  memcpy(midiptr,midihdr,sizeof(midihdr));
  midiptr += sizeof(midihdr);
  for (i=0;i<MIDI_TRACKS;i++)
  {
    if (mididata->track[i].len)
    {
      memcpy(midiptr,trackhdr,sizeof(trackhdr));    
      midiptr += sizeof(trackhdr);
      TWriteLength(&midiptr,mididata->track[i].len);  
      memcpy(midiptr,mididata->track[i].data,mididata->track[i].len);
      midiptr += mididata->track[i].len;
    }
  }
  *midlen = midiptr - *mid;
  return 0;
}
int MIDIToMidi ( MIDI * mididata , UBYTE * * mid , int * midlen ) { size_t total ; int i , ntrks ; UBYTE * midiptr ; total = sizeof ( midihdr ) ; for ( i = 0 , ntrks = 0 ; i < MIDI_TRACKS ; i ++ ) if ( mididata -> track [ i ] . len ) { total += 8 + mididata -> track [ i ] . len ; ntrks ++ ; } if ( ( * mid = malloc ( total ) ) == NULL ) return MEMALLOC ; midihdr [ 10 ] = 0 ; midihdr [ 11 ] = ( UBYTE ) ntrks ; midihdr [ 12 ] = ( mididata -> divisions >> 8 ) & 0x7f ; midihdr [ 13 ] = ( mididata -> divisions ) & 0xff ; midiptr = * mid ; memcpy ( midiptr , midihdr , sizeof ( midihdr ) ) ; midiptr += sizeof ( midihdr ) ; for ( i = 0 ; i < MIDI_TRACKS ; i ++ ) { if ( mididata -> track [ i ] . len ) { memcpy ( midiptr , trackhdr , sizeof ( trackhdr ) ) ; midiptr += sizeof ( trackhdr ) ; TWriteLength ( & midiptr , mididata -> track [ i ] . len ) ; memcpy ( midiptr , mididata -> track [ i ] . data , mididata -> track [ i ] . len ) ; midiptr += mididata -> track [ i ] . len ; } } * midlen = midiptr - * mid ; return 0 ; }
MIDIToMidi()
This routine converts an Allegro MIDI structure to a midi 1 format file
in memory.
MIDIToMidi () This routine converts an Allegro MIDI structure to a midi 1 format file in memory .
// calculate how long the mid buffer must be, and allocate // Track hdr + track length // fill in number of tracks and bigendian divisions (ticks/qnote) // set number of tracks in header // write the midi header // write the tracks // header // track length // data // return length information