src/codecs/timidity/readmidi.c
author Ozkan Sezer
Mon, 16 Dec 2019 10:33:55 +0300
changeset 1083 7a3b49dbf90f
parent 1069 8d8ebc200fd4
child 1084 c7cedfb5f65f
permissions -rw-r--r--
timidity: minor warning fixes

based on a patch by Wohlstand
admin@999
     1
/*
admin@999
     2
admin@999
     3
    TiMidity -- Experimental MIDI to WAVE converter
admin@999
     4
    Copyright (C) 1995 Tuukka Toivonen <toivonen@clinet.fi>
admin@999
     5
admin@999
     6
    This program is free software; you can redistribute it and/or modify
admin@999
     7
    it under the terms of the Perl Artistic License, available in COPYING.
admin@999
     8
*/
admin@999
     9
admin@999
    10
#if HAVE_CONFIG_H
admin@999
    11
#  include <config.h>
admin@999
    12
#endif
admin@999
    13
admin@999
    14
#include <stdio.h>
admin@999
    15
#include <stdlib.h>
admin@999
    16
#include <string.h>
admin@999
    17
admin@999
    18
#include "SDL.h"
admin@999
    19
admin@999
    20
#include "options.h"
admin@999
    21
#include "timidity.h"
admin@999
    22
#include "common.h"
admin@999
    23
#include "instrum.h"
admin@999
    24
#include "playmidi.h"
admin@999
    25
admin@999
    26
/* Computes how many (fractional) samples one MIDI delta-time unit contains */
admin@999
    27
static void compute_sample_increment(MidiSong *song, Sint32 tempo,
admin@999
    28
				     Sint32 divisions)
admin@999
    29
{
admin@999
    30
  double a;
admin@999
    31
  a = (double) (tempo) * (double) (song->rate) * (65536.0/1000000.0) /
admin@999
    32
    (double)(divisions);
admin@999
    33
admin@999
    34
  song->sample_correction = (Sint32)(a) & 0xFFFF;
admin@999
    35
  song->sample_increment = (Sint32)(a) >> 16;
admin@999
    36
admin@999
    37
  SNDDBG(("Samples per delta-t: %d (correction %d)",
admin@999
    38
	  song->sample_increment, song->sample_correction));
admin@999
    39
}
admin@999
    40
admin@999
    41
/* Read variable-length number (7 bits per byte, MSB first) */
admin@999
    42
static Sint32 getvl(SDL_RWops *rw)
admin@999
    43
{
admin@999
    44
  Sint32 l=0;
admin@999
    45
  Uint8 c;
admin@999
    46
  for (;;)
admin@999
    47
    {
admin@999
    48
      if (!SDL_RWread(rw, &c, 1, 1)) return l;
admin@999
    49
      l += (c & 0x7f);
admin@999
    50
      if (!(c & 0x80)) return l;
admin@999
    51
      l<<=7;
admin@999
    52
    }
admin@999
    53
}
admin@999
    54
sezeroz@1069
    55
#if 0 /* SNDDBG() is just an empty macro */
admin@999
    56
/* Print a string from the file, followed by a newline. Any non-ASCII
admin@999
    57
   or unprintable characters will be converted to periods. */
sezeroz@1069
    58
static int dumpstring(SDL_RWops *rw, Sint32 len, Uint8 type)
admin@999
    59
{
sezeroz@1069
    60
  static char *label[]={
sezeroz@1069
    61
    "Text event: ", "Text: ", "Copyright: ", "Track name: ",
sezeroz@1069
    62
    "Instrument: ", "Lyric: ", "Marker: ", "Cue point: "};
admin@999
    63
  signed char *s=safe_malloc(len+1);
admin@999
    64
  if (len != (Sint32) SDL_RWread(rw, s, 1, len))
admin@999
    65
    {
admin@999
    66
      free(s);
admin@999
    67
      return -1;
admin@999
    68
    }
admin@999
    69
  s[len]='\0';
admin@999
    70
  while (len--)
admin@999
    71
    {
admin@999
    72
      if (s[len]<32)
admin@999
    73
	s[len]='.';
admin@999
    74
    }
sezeroz@1069
    75
  SNDDBG(("%s%s", label[(type>7) ? 0 : type], s));
admin@999
    76
  free(s);
admin@999
    77
  return 0;
admin@999
    78
}
sezeroz@1069
    79
#else
sezeroz@1069
    80
static SDL_INLINE int dumpstring(SDL_RWops *rw, Sint32 len, Uint8 type) {
sezeroz@1069
    81
  return SDL_RWseek(rw, len, RW_SEEK_CUR);
sezeroz@1069
    82
}
sezeroz@1069
    83
#endif
admin@999
    84
admin@999
    85
#define MIDIEVENT(at,t,ch,pa,pb) \
admin@999
    86
  new=safe_malloc(sizeof(MidiEventList)); \
admin@999
    87
  new->event.time=at; new->event.type=t; new->event.channel=ch; \
admin@999
    88
  new->event.a=pa; new->event.b=pb; new->next=0;\
admin@999
    89
  return new;
admin@999
    90
admin@999
    91
#define MAGIC_EOT ((MidiEventList *)(-1))
admin@999
    92
admin@999
    93
/* Read a MIDI event, returning a freshly allocated element that can
admin@999
    94
   be linked to the event list */
admin@999
    95
static MidiEventList *read_midi_event(MidiSong *song)
admin@999
    96
{
admin@999
    97
  static Uint8 laststatus, lastchan;
admin@999
    98
  static Uint8 nrpn=0, rpn_msb[16], rpn_lsb[16]; /* one per channel */
admin@999
    99
  Uint8 me, type, a,b,c;
admin@999
   100
  Sint32 len;
admin@999
   101
  MidiEventList *new;
admin@999
   102
admin@999
   103
  for (;;)
admin@999
   104
    {
admin@999
   105
      song->at += getvl(song->rw);
admin@999
   106
      if (SDL_RWread(song->rw, &me, 1, 1) != 1)
admin@999
   107
	{
admin@999
   108
	  SNDDBG(("read_midi_event: SDL_RWread() failure\n"));
admin@999
   109
	  return NULL;
admin@999
   110
	}
admin@999
   111
      
admin@999
   112
      if(me==0xF0 || me == 0xF7) /* SysEx event */
admin@999
   113
	{
admin@999
   114
	  len=getvl(song->rw);
admin@999
   115
	  SDL_RWseek(song->rw, len, RW_SEEK_CUR);
admin@999
   116
	}
admin@999
   117
      else if(me==0xFF) /* Meta event */
admin@999
   118
	{
admin@999
   119
	  SDL_RWread(song->rw, &type, 1, 1);
admin@999
   120
	  len=getvl(song->rw);
admin@999
   121
	  if (type>0 && type<16)
admin@999
   122
	    {
sezeroz@1069
   123
	      dumpstring(song->rw, len, type);
admin@999
   124
	    }
admin@999
   125
	  else
admin@999
   126
	    switch(type)
admin@999
   127
	      {
admin@999
   128
	      case 0x2F: /* End of Track */
admin@999
   129
		return MAGIC_EOT;
admin@999
   130
admin@999
   131
	      case 0x51: /* Tempo */
admin@999
   132
		SDL_RWread(song->rw, &a, 1, 1);
admin@999
   133
		SDL_RWread(song->rw, &b, 1, 1);
admin@999
   134
		SDL_RWread(song->rw, &c, 1, 1);
admin@999
   135
		MIDIEVENT(song->at, ME_TEMPO, c, a, b);
admin@999
   136
		
admin@999
   137
	      default:
admin@999
   138
		SNDDBG(("(Meta event type 0x%02x, length %d)\n", type, len));
admin@999
   139
		SDL_RWseek(song->rw, len, RW_SEEK_CUR);
admin@999
   140
		break;
admin@999
   141
	      }
admin@999
   142
	}
admin@999
   143
      else
admin@999
   144
	{
admin@999
   145
	  a=me;
admin@999
   146
	  if (a & 0x80) /* status byte */
admin@999
   147
	    {
admin@999
   148
	      lastchan=a & 0x0F;
admin@999
   149
	      laststatus=(a>>4) & 0x07;
admin@999
   150
	      SDL_RWread(song->rw, &a, 1, 1);
admin@999
   151
	      a &= 0x7F;
admin@999
   152
	    }
admin@999
   153
	  switch(laststatus)
admin@999
   154
	    {
admin@999
   155
	    case 0: /* Note off */
admin@999
   156
	      SDL_RWread(song->rw, &b, 1, 1);
admin@999
   157
	      b &= 0x7F;
admin@999
   158
	      MIDIEVENT(song->at, ME_NOTEOFF, lastchan, a,b);
admin@999
   159
admin@999
   160
	    case 1: /* Note on */
admin@999
   161
	      SDL_RWread(song->rw, &b, 1, 1);
admin@999
   162
	      b &= 0x7F;
admin@999
   163
	      MIDIEVENT(song->at, ME_NOTEON, lastchan, a,b);
admin@999
   164
admin@999
   165
	    case 2: /* Key Pressure */
admin@999
   166
	      SDL_RWread(song->rw, &b, 1, 1);
admin@999
   167
	      b &= 0x7F;
admin@999
   168
	      MIDIEVENT(song->at, ME_KEYPRESSURE, lastchan, a, b);
admin@999
   169
admin@999
   170
	    case 3: /* Control change */
admin@999
   171
	      SDL_RWread(song->rw, &b, 1, 1);
admin@999
   172
	      b &= 0x7F;
admin@999
   173
	      {
admin@999
   174
		int control=255;
admin@999
   175
		switch(a)
admin@999
   176
		  {
admin@999
   177
		  case 7: control=ME_MAINVOLUME; break;
admin@999
   178
		  case 10: control=ME_PAN; break;
admin@999
   179
		  case 11: control=ME_EXPRESSION; break;
admin@999
   180
		  case 64: control=ME_SUSTAIN; b = (b >= 64); break;
admin@999
   181
		  case 120: control=ME_ALL_SOUNDS_OFF; break;
admin@999
   182
		  case 121: control=ME_RESET_CONTROLLERS; break;
admin@999
   183
		  case 123: control=ME_ALL_NOTES_OFF; break;
admin@999
   184
admin@999
   185
		    /* These should be the SCC-1 tone bank switch
admin@999
   186
		       commands. I don't know why there are two, or
admin@999
   187
		       why the latter only allows switching to bank 0.
admin@999
   188
		       Also, some MIDI files use 0 as some sort of
admin@999
   189
		       continuous controller. This will cause lots of
admin@999
   190
		       warnings about undefined tone banks. */
admin@999
   191
		  case 0: control=ME_TONE_BANK; break;
admin@999
   192
		  case 32:
admin@999
   193
		    if (b!=0) {
admin@999
   194
		      SNDDBG(("(Strange: tone bank change 0x%02x)\n", b));
admin@999
   195
		    }
admin@999
   196
#if 0	/* `Bank Select LSB' is not worked at GS. Please ignore it. */
admin@999
   197
		    else
admin@999
   198
		      control=ME_TONE_BANK;
admin@999
   199
#endif
admin@999
   200
		    break;
admin@999
   201
admin@999
   202
		  case 100: nrpn=0; rpn_msb[lastchan]=b; break;
admin@999
   203
		  case 101: nrpn=0; rpn_lsb[lastchan]=b; break;
admin@999
   204
		  case 99: nrpn=1; rpn_msb[lastchan]=b; break;
admin@999
   205
		  case 98: nrpn=1; rpn_lsb[lastchan]=b; break;
admin@999
   206
		    
admin@999
   207
		  case 6:
admin@999
   208
		    if (nrpn)
admin@999
   209
		      {
admin@999
   210
			SNDDBG(("(Data entry (MSB) for NRPN %02x,%02x: %d)\n",
admin@999
   211
				rpn_msb[lastchan], rpn_lsb[lastchan], b));
admin@999
   212
			break;
admin@999
   213
		      }
admin@999
   214
		    
admin@999
   215
		    switch((rpn_msb[lastchan]<<8) | rpn_lsb[lastchan])
admin@999
   216
		      {
admin@999
   217
		      case 0x0000: /* Pitch bend sensitivity */
admin@999
   218
			control=ME_PITCH_SENS;
admin@999
   219
			break;
admin@999
   220
admin@999
   221
		      case 0x7F7F: /* RPN reset */
admin@999
   222
			/* reset pitch bend sensitivity to 2 */
admin@999
   223
			MIDIEVENT(song->at, ME_PITCH_SENS, lastchan, 2, 0);
admin@999
   224
admin@999
   225
		      default:
admin@999
   226
			SNDDBG(("(Data entry (MSB) for RPN %02x,%02x: %d)\n",
admin@999
   227
				rpn_msb[lastchan], rpn_lsb[lastchan], b));
admin@999
   228
			break;
admin@999
   229
		      }
admin@999
   230
		    break;
admin@999
   231
		    
admin@999
   232
		  default:
admin@999
   233
		    SNDDBG(("(Control %d: %d)\n", a, b));
admin@999
   234
		    break;
admin@999
   235
		  }
admin@999
   236
		if (control != 255)
admin@999
   237
		  { 
admin@999
   238
		    MIDIEVENT(song->at, control, lastchan, b, 0); 
admin@999
   239
		  }
admin@999
   240
	      }
admin@999
   241
	      break;
admin@999
   242
admin@999
   243
	    case 4: /* Program change */
admin@999
   244
	      a &= 0x7f;
admin@999
   245
	      MIDIEVENT(song->at, ME_PROGRAM, lastchan, a, 0);
admin@999
   246
admin@999
   247
	    case 5: /* Channel pressure - NOT IMPLEMENTED */
admin@999
   248
	      break;
admin@999
   249
admin@999
   250
	    case 6: /* Pitch wheel */
admin@999
   251
	      SDL_RWread(song->rw, &b, 1, 1);
admin@999
   252
	      b &= 0x7F;
admin@999
   253
	      MIDIEVENT(song->at, ME_PITCHWHEEL, lastchan, a, b);
admin@999
   254
admin@999
   255
	    default: 
admin@999
   256
	      SNDDBG(("*** Can't happen: status 0x%02X, channel 0x%02X\n",
admin@999
   257
		      laststatus, lastchan));
admin@999
   258
	      break;
admin@999
   259
	    }
admin@999
   260
	}
admin@999
   261
    }
admin@999
   262
  
admin@999
   263
  return new;
admin@999
   264
}
admin@999
   265
admin@999
   266
#undef MIDIEVENT
admin@999
   267
admin@999
   268
/* Read a midi track into the linked list, either merging with any previous
admin@999
   269
   tracks or appending to them. */
admin@999
   270
static int read_track(MidiSong *song, int append)
admin@999
   271
{
admin@999
   272
  MidiEventList *meep;
admin@999
   273
  MidiEventList *next, *new;
admin@999
   274
  Sint32 len;
admin@999
   275
  Sint64 next_pos, pos;
admin@999
   276
  char tmp[4];
admin@999
   277
admin@999
   278
  meep = song->evlist;
admin@999
   279
  if (append && meep)
admin@999
   280
    {
admin@999
   281
      /* find the last event in the list */
admin@999
   282
      for (; meep->next; meep=meep->next)
admin@999
   283
	;
admin@999
   284
      song->at = meep->event.time;
admin@999
   285
    }
admin@999
   286
  else
admin@999
   287
    song->at=0;
admin@999
   288
admin@999
   289
  /* Check the formalities */
admin@999
   290
  
admin@999
   291
  if (SDL_RWread(song->rw, tmp, 1, 4) != 4 || SDL_RWread(song->rw, &len, 4, 1) != 1)
admin@999
   292
    {
admin@999
   293
      SNDDBG(("Can't read track header.\n"));
admin@999
   294
      return -1;
admin@999
   295
    }
admin@999
   296
  len=SDL_SwapBE32(len);
admin@999
   297
  next_pos = SDL_RWtell(song->rw) + len;
admin@999
   298
  if (memcmp(tmp, "MTrk", 4))
admin@999
   299
    {
admin@999
   300
      SNDDBG(("Corrupt MIDI file.\n"));
admin@999
   301
      return -2;
admin@999
   302
    }
admin@999
   303
admin@999
   304
  for (;;)
admin@999
   305
    {
admin@999
   306
      if (!(new=read_midi_event(song))) /* Some kind of error  */
admin@999
   307
	return -2;
admin@999
   308
admin@999
   309
      if (new==MAGIC_EOT) /* End-of-track Hack. */
admin@999
   310
	{
admin@999
   311
          pos = SDL_RWtell(song->rw);
admin@999
   312
          if (pos < next_pos)
admin@999
   313
            SDL_RWseek(song->rw, next_pos - pos, RW_SEEK_CUR);
admin@999
   314
	  return 0;
admin@999
   315
	}
admin@999
   316
admin@999
   317
      next=meep->next;
admin@999
   318
      while (next && (next->event.time < new->event.time))
admin@999
   319
	{
admin@999
   320
	  meep=next;
admin@999
   321
	  next=meep->next;
admin@999
   322
	}
admin@999
   323
	  
admin@999
   324
      new->next=next;
admin@999
   325
      meep->next=new;
admin@999
   326
admin@999
   327
      song->event_count++; /* Count the event. (About one?) */
admin@999
   328
      meep=new;
admin@999
   329
    }
admin@999
   330
}
admin@999
   331
admin@999
   332
/* Free the linked event list from memory. */
admin@999
   333
static void free_midi_list(MidiSong *song)
admin@999
   334
{
admin@999
   335
  MidiEventList *meep, *next;
admin@999
   336
  if (!(meep = song->evlist)) return;
admin@999
   337
  while (meep)
admin@999
   338
    {
admin@999
   339
      next=meep->next;
admin@999
   340
      free(meep);
admin@999
   341
      meep=next;
admin@999
   342
    }
admin@999
   343
  song->evlist=NULL;
admin@999
   344
}
admin@999
   345
admin@999
   346
/* Allocate an array of MidiEvents and fill it from the linked list of
admin@999
   347
   events, marking used instruments for loading. Convert event times to
admin@999
   348
   samples: handle tempo changes. Strip unnecessary events from the list.
admin@999
   349
   Free the linked list. */
admin@999
   350
static MidiEvent *groom_list(MidiSong *song, Sint32 divisions,Sint32 *eventsp,
admin@999
   351
			     Sint32 *samplesp)
admin@999
   352
{
admin@999
   353
  MidiEvent *groomed_list, *lp;
admin@999
   354
  MidiEventList *meep;
admin@999
   355
  Sint32 i, our_event_count, tempo, skip_this_event, new_value;
admin@999
   356
  Sint32 sample_cum, samples_to_do, at, st, dt, counting_time;
admin@999
   357
admin@999
   358
  int current_bank[MAXCHAN], current_set[MAXCHAN], current_program[MAXCHAN]; 
admin@999
   359
  /* Or should each bank have its own current program? */
admin@999
   360
admin@999
   361
  for (i=0; i<MAXCHAN; i++)
admin@999
   362
    {
admin@999
   363
      current_bank[i]=0;
admin@999
   364
      current_set[i]=0;
admin@999
   365
      current_program[i]=song->default_program;
admin@999
   366
    }
admin@999
   367
admin@999
   368
  tempo=500000;
admin@999
   369
  compute_sample_increment(song, tempo, divisions);
admin@999
   370
admin@999
   371
  /* This may allocate a bit more than we need */
admin@999
   372
  groomed_list=lp=safe_malloc(sizeof(MidiEvent) * (song->event_count+1));
admin@999
   373
  meep=song->evlist;
admin@999
   374
admin@999
   375
  our_event_count=0;
admin@999
   376
  st=at=sample_cum=0;
admin@999
   377
  counting_time=2; /* We strip any silence before the first NOTE ON. */
admin@999
   378
admin@999
   379
  for (i = 0; i < song->event_count; i++)
admin@999
   380
    {
admin@999
   381
      skip_this_event=0;
admin@999
   382
admin@999
   383
      if (meep->event.type==ME_TEMPO)
admin@999
   384
	{
admin@999
   385
	  skip_this_event=1;
admin@999
   386
	}
admin@999
   387
      else if (meep->event.channel >= MAXCHAN)
admin@999
   388
        skip_this_event=1;
admin@999
   389
      else switch (meep->event.type)
admin@999
   390
	{
admin@999
   391
	case ME_PROGRAM:
admin@999
   392
	  if (ISDRUMCHANNEL(song, meep->event.channel))
admin@999
   393
	    {
admin@999
   394
	      if (song->drumset[meep->event.a]) /* Is this a defined drumset? */
admin@999
   395
		new_value=meep->event.a;
admin@999
   396
	      else
admin@999
   397
		{
admin@999
   398
		  SNDDBG(("Drum set %d is undefined\n", meep->event.a));
admin@999
   399
		  new_value=meep->event.a=0;
admin@999
   400
		}
admin@999
   401
	      if (current_set[meep->event.channel] != new_value)
admin@999
   402
		current_set[meep->event.channel]=new_value;
admin@999
   403
	      else 
admin@999
   404
		skip_this_event=1;
admin@999
   405
	    }
admin@999
   406
	  else
admin@999
   407
	    {
admin@999
   408
	      new_value=meep->event.a;
admin@999
   409
	      if ((current_program[meep->event.channel] != SPECIAL_PROGRAM)
admin@999
   410
		  && (current_program[meep->event.channel] != new_value))
admin@999
   411
		current_program[meep->event.channel] = new_value;
admin@999
   412
	      else
admin@999
   413
		skip_this_event=1;
admin@999
   414
	    }
admin@999
   415
	  break;
admin@999
   416
admin@999
   417
	case ME_NOTEON:
admin@999
   418
	  if (counting_time)
admin@999
   419
	    counting_time=1;
admin@999
   420
	  if (ISDRUMCHANNEL(song, meep->event.channel))
admin@999
   421
	    {
admin@999
   422
	      /* Mark this instrument to be loaded */
admin@999
   423
	      if (!(song->drumset[current_set[meep->event.channel]]
admin@999
   424
		    ->instrument[meep->event.a]))
admin@999
   425
		song->drumset[current_set[meep->event.channel]]
admin@999
   426
		  ->instrument[meep->event.a] = MAGIC_LOAD_INSTRUMENT;
admin@999
   427
	    }
admin@999
   428
	  else
admin@999
   429
	    {
admin@999
   430
	      if (current_program[meep->event.channel]==SPECIAL_PROGRAM)
admin@999
   431
		break;
admin@999
   432
	      /* Mark this instrument to be loaded */
admin@999
   433
	      if (!(song->tonebank[current_bank[meep->event.channel]]
admin@999
   434
		    ->instrument[current_program[meep->event.channel]]))
admin@999
   435
		song->tonebank[current_bank[meep->event.channel]]
admin@999
   436
		  ->instrument[current_program[meep->event.channel]] =
admin@999
   437
		    MAGIC_LOAD_INSTRUMENT;
admin@999
   438
	    }
admin@999
   439
	  break;
admin@999
   440
admin@999
   441
	case ME_TONE_BANK:
admin@999
   442
	  if (ISDRUMCHANNEL(song, meep->event.channel))
admin@999
   443
	    {
admin@999
   444
	      skip_this_event=1;
admin@999
   445
	      break;
admin@999
   446
	    }
admin@999
   447
	  if (song->tonebank[meep->event.a]) /* Is this a defined tone bank? */
admin@999
   448
	    new_value=meep->event.a;
admin@999
   449
	  else 
admin@999
   450
	    {
admin@999
   451
	      SNDDBG(("Tone bank %d is undefined\n", meep->event.a));
admin@999
   452
	      new_value=meep->event.a=0;
admin@999
   453
	    }
admin@999
   454
	  if (current_bank[meep->event.channel]!=new_value)
admin@999
   455
	    current_bank[meep->event.channel]=new_value;
admin@999
   456
	  else
admin@999
   457
	    skip_this_event=1;
admin@999
   458
	  break;
admin@999
   459
	}
admin@999
   460
admin@999
   461
      /* Recompute time in samples*/
admin@999
   462
      if ((dt=meep->event.time - at) && !counting_time)
admin@999
   463
	{
admin@999
   464
	  if (song->sample_increment  > 2147483647/dt ||
admin@999
   465
	      song->sample_correction > 2147483647/dt) {
admin@999
   466
	      goto _overflow;
admin@999
   467
	    }
admin@999
   468
	  samples_to_do = song->sample_increment * dt;
admin@999
   469
	  sample_cum += song->sample_correction * dt;
admin@999
   470
	  if (sample_cum & 0xFFFF0000)
admin@999
   471
	    {
admin@999
   472
	      samples_to_do += ((sample_cum >> 16) & 0xFFFF);
admin@999
   473
	      sample_cum &= 0x0000FFFF;
admin@999
   474
	    }
admin@999
   475
	  if (st >= 2147483647 - samples_to_do) {
admin@999
   476
	  _overflow:
admin@999
   477
	      SNDDBG(("Overflow in sample counter\n"));
admin@999
   478
	      free_midi_list(song);
admin@999
   479
	      free(groomed_list);
admin@999
   480
	      return NULL;
admin@999
   481
	    }
admin@999
   482
	  st += samples_to_do;
admin@999
   483
	}
admin@999
   484
      else if (counting_time==1) counting_time=0;
admin@999
   485
      if (meep->event.type==ME_TEMPO)
admin@999
   486
	{
admin@999
   487
	  tempo=
admin@999
   488
	    meep->event.channel + meep->event.b * 256 + meep->event.a * 65536;
admin@999
   489
	  compute_sample_increment(song, tempo, divisions);
admin@999
   490
	}
admin@999
   491
      if (!skip_this_event)
admin@999
   492
	{
admin@999
   493
	  /* Add the event to the list */
admin@999
   494
	  *lp=meep->event;
admin@999
   495
	  lp->time=st;
admin@999
   496
	  lp++;
admin@999
   497
	  our_event_count++;
admin@999
   498
	}
admin@999
   499
      at=meep->event.time;
admin@999
   500
      meep=meep->next;
admin@999
   501
    }
admin@999
   502
  /* Add an End-of-Track event */
admin@999
   503
  lp->time=st;
admin@999
   504
  lp->type=ME_EOT;
admin@999
   505
  our_event_count++;
admin@999
   506
  free_midi_list(song);
admin@999
   507
admin@999
   508
  *eventsp=our_event_count;
admin@999
   509
  *samplesp=st;
admin@999
   510
  return groomed_list;
admin@999
   511
}
admin@999
   512
admin@999
   513
MidiEvent *read_midi_file(MidiSong *song, Sint32 *count, Sint32 *sp)
admin@999
   514
{
admin@999
   515
  Sint32 len, divisions;
admin@999
   516
  Sint16 format, tracks, divisions_tmp;
admin@999
   517
  int i;
admin@999
   518
  char tmp[4];
admin@999
   519
admin@999
   520
  song->event_count=0;
admin@999
   521
  song->at=0;
admin@999
   522
  song->evlist = NULL;
admin@999
   523
admin@999
   524
  if (SDL_RWread(song->rw, tmp, 1, 4) != 4 || SDL_RWread(song->rw, &len, 4, 1) != 1)
admin@999
   525
    {
admin@999
   526
      SNDDBG(("Not a MIDI file!\n"));
admin@999
   527
      return NULL;
admin@999
   528
    }
admin@999
   529
  if (memcmp(tmp, "RIFF", 4) == 0) { /* RMID ?? */
admin@999
   530
    if (SDL_RWread(song->rw, tmp, 1, 4) != 4 || memcmp(tmp, "RMID", 4) != 0 ||
admin@999
   531
	SDL_RWread(song->rw, tmp, 1, 4) != 4 || memcmp(tmp, "data", 4) != 0 ||
admin@999
   532
	SDL_RWread(song->rw, tmp, 1, 4) != 4 ||
admin@999
   533
	/* SMF must begin from here onwards: */
admin@999
   534
	SDL_RWread(song->rw, tmp, 1, 4) != 4 || SDL_RWread(song->rw, &len, 4, 1) != 1)
admin@999
   535
      {
admin@999
   536
	SNDDBG(("Not an RMID file!\n"));
admin@999
   537
	return NULL;
admin@999
   538
      }
admin@999
   539
  }
sezeroz@1083
   540
  len=(Sint32)SDL_SwapBE32((Uint32)len);
admin@999
   541
  if (memcmp(tmp, "MThd", 4) || len < 6)
admin@999
   542
    {
admin@999
   543
      SNDDBG(("Not a MIDI file!\n"));
admin@999
   544
      return NULL;
admin@999
   545
    }
admin@999
   546
admin@999
   547
  SDL_RWread(song->rw, &format, 2, 1);
admin@999
   548
  SDL_RWread(song->rw, &tracks, 2, 1);
admin@999
   549
  SDL_RWread(song->rw, &divisions_tmp, 2, 1);
admin@999
   550
  format=SDL_SwapBE16(format);
admin@999
   551
  tracks=SDL_SwapBE16(tracks);
admin@999
   552
  divisions_tmp=SDL_SwapBE16(divisions_tmp);
admin@999
   553
admin@999
   554
  if (divisions_tmp<0)
admin@999
   555
    {
admin@999
   556
      /* SMPTE time -- totally untested. Got a MIDI file that uses this? */
admin@999
   557
      divisions=
admin@999
   558
	(Sint32)(-(divisions_tmp/256)) * (Sint32)(divisions_tmp & 0xFF);
admin@999
   559
    }
admin@999
   560
  else divisions=(Sint32)(divisions_tmp);
admin@999
   561
admin@999
   562
  if (len > 6)
admin@999
   563
    {
admin@999
   564
      SNDDBG(("MIDI file header size %u bytes", len));
admin@999
   565
      SDL_RWseek(song->rw, len-6, RW_SEEK_CUR); /* skip the excess */
admin@999
   566
    }
admin@999
   567
  if (format<0 || format >2)
admin@999
   568
    {
admin@999
   569
      SNDDBG(("Unknown MIDI file format %d\n", format));
admin@999
   570
      return NULL;
admin@999
   571
    }
admin@999
   572
  if (tracks<1)
admin@999
   573
    {
admin@999
   574
      SNDDBG(("Bad number of tracks %d\n", tracks));
admin@999
   575
      return NULL;
admin@999
   576
    }
admin@999
   577
  if (format==0 && tracks!=1)
admin@999
   578
    {
admin@999
   579
      SNDDBG(("%d tracks with Type-0 MIDI (must be 1.)\n", tracks));
admin@999
   580
      return NULL;
admin@999
   581
    }
admin@999
   582
  SNDDBG(("Format: %d  Tracks: %d  Divisions: %d\n",
admin@999
   583
	  format, tracks, divisions));
admin@999
   584
admin@999
   585
  /* Put a do-nothing event first in the list for easier processing */
admin@999
   586
  song->evlist=safe_malloc(sizeof(MidiEventList));
admin@999
   587
  memset(song->evlist, 0, sizeof(MidiEventList));
admin@999
   588
  song->event_count++;
admin@999
   589
admin@999
   590
  switch(format)
admin@999
   591
    {
admin@999
   592
    case 0:
admin@999
   593
      if (read_track(song, 0))
admin@999
   594
	{
admin@999
   595
	  free_midi_list(song);
admin@999
   596
	  return NULL;
admin@999
   597
	}
admin@999
   598
      break;
admin@999
   599
admin@999
   600
    case 1:
admin@999
   601
      for (i=0; i<tracks; i++)
admin@999
   602
	if (read_track(song, 0))
admin@999
   603
	  {
admin@999
   604
	    free_midi_list(song);
admin@999
   605
	    return NULL;
admin@999
   606
	  }
admin@999
   607
      break;
admin@999
   608
admin@999
   609
    case 2: /* We simply play the tracks sequentially */
admin@999
   610
      for (i=0; i<tracks; i++)
admin@999
   611
	if (read_track(song, 1))
admin@999
   612
	  {
admin@999
   613
	    free_midi_list(song);
admin@999
   614
	    return NULL;
admin@999
   615
	  }
admin@999
   616
      break;
admin@999
   617
    }
admin@999
   618
admin@999
   619
  return groom_list(song, divisions, count, sp);
admin@999
   620
}