~ubuntu-branches/ubuntu/hoary/kdemultimedia/hoary

« back to all changes in this revision

Viewing changes to kmidi/readmidi.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Martin Schulze
  • Date: 2003-01-22 15:00:51 UTC
  • Revision ID: james.westby@ubuntu.com-20030122150051-uihwkdoxf15mi1tn
Tags: upstream-2.2.2
ImportĀ upstreamĀ versionĀ 2.2.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
        $Id: readmidi.cpp,v 1.8 2000/01/22 17:52:22 greglee Exp $
 
3
 
 
4
    TiMidity -- Experimental MIDI to WAVE converter
 
5
    Copyright (C) 1995 Tuukka Toivonen <toivonen@clinet.fi>
 
6
 
 
7
    This program is free software; you can redistribute it and/or modify
 
8
    it under the terms of the GNU General Public License as published by
 
9
    the Free Software Foundation; either version 2 of the License, or
 
10
    (at your option) any later version.
 
11
 
 
12
    This program is distributed in the hope that it will be useful,
 
13
    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
    GNU General Public License for more details.
 
16
 
 
17
    You should have received a copy of the GNU General Public License
 
18
    along with this program; if not, write to the Free Software
 
19
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
20
 
 
21
*/
 
22
 
 
23
#include <stdio.h>
 
24
#include <stdlib.h>
 
25
#include <errno.h>
 
26
 
 
27
#ifndef NO_STRING_H
 
28
# include <string.h>
 
29
#else
 
30
#include <strings.h>
 
31
#endif
 
32
 
 
33
#ifndef __WIN32__
 
34
#include <unistd.h>
 
35
#endif
 
36
 
 
37
#include <ctype.h>
 
38
 
 
39
#include "config.h"
 
40
#include "common.h"
 
41
#include "instrum.h"
 
42
#include "playmidi.h"
 
43
#include "readmidi.h"
 
44
#include "output.h"
 
45
#include "controls.h"
 
46
#include "tables.h"
 
47
 
 
48
int32 quietchannels=0;
 
49
 
 
50
/* to avoid some unnecessary parameter passing */
 
51
static MidiEventList *evlist;
 
52
static int32 event_count;
 
53
static FILE *fp;
 
54
static uint32 at;
 
55
 
 
56
/* taken from tplus --gl */
 
57
static int midi_port_number;
 
58
 
 
59
#if MAXCHAN <= 16
 
60
#define MERGE_CHANNEL_PORT(ch) ((int)(ch))
 
61
#else
 
62
#define MERGE_CHANNEL_PORT(ch) ((int)(ch) | (midi_port_number << 4))
 
63
#endif
 
64
 
 
65
 
 
66
/* These would both fit into 32 bits, but they are often added in
 
67
   large multiples, so it's simpler to have two roomy ints */
 
68
static int32 sample_increment, sample_correction; /*samples per MIDI delta-t*/
 
69
 
 
70
unsigned char sfxdrum1[100] = {
 
71
0,0,0,0,0,0,0,0,0,0,
 
72
0,0,0,0,0,0,0,0,0,0,
 
73
0,0,0,0,0,0,0,0,0,0,
 
74
0,0,0,0,0,0,48,49,47,50,
 
75
0,46,0,0,0,0,0,0,0,0,
 
76
0,0,44,0,0,0,0,0,0,0,
 
77
0,0,0,0,0,0,0,0,79,80,
 
78
81,82,83,84,0,0,0,0,0,0,
 
79
0,0,0,0,76,77,78,0,0,0,
 
80
0,0,0,0,0,0,0,0,0,0
 
81
};
 
82
 
 
83
unsigned char sfxdrum2[100] = {
 
84
0,0,0,0,0,0,0,0,0,0,
 
85
0,0,0,0,0,0,0,0,0,0,
 
86
0,0,0,0,0,0,0,0,0,0,
 
87
0,0,0,0,0,0,0,59,60,41,
 
88
42,46,0,0,0,0,0,0,0,0,
 
89
0,0,63,64,65,66,67,68,69,69,
 
90
72,70,0,0,0,0,0,0,52,53,
 
91
54,56,57,58,0,0,0,0,0,0,
 
92
0,0,0,0,73,74,75,71,0,0,
 
93
0,0,0,0,0,0,0,0,0,0
 
94
};
 
95
 
 
96
/* Computes how many (fractional) samples one MIDI delta-time unit contains */
 
97
static void compute_sample_increment(int32 tempo, int32 divisions)
 
98
{
 
99
  double a;
 
100
  a = (double) (tempo) * (double) (play_mode->rate) * (65536.0/1000000.0) /
 
101
    (double)(divisions);
 
102
 
 
103
  sample_correction = (int32)(a) & 0xFFFF;
 
104
  sample_increment = (int32)(a) >> 16;
 
105
 
 
106
  ctl->cmsg(CMSG_INFO, VERB_DEBUG, "Samples per delta-t: %d (correction %d)",
 
107
       sample_increment, sample_correction);
 
108
}
 
109
 
 
110
#ifdef tplus
 
111
static int tf_getc(void)
 
112
{
 
113
    uint8 b;
 
114
    if (fread(&b,1,1,fp) != 1) return EOF;
 
115
    return (uint8)b;
 
116
}
 
117
 
 
118
/* Read variable-length number (7 bits per byte, MSB first) */
 
119
static int32 getvl(void)
 
120
{
 
121
    int32 l;
 
122
    int c;
 
123
 
 
124
    errno = 0;
 
125
    l = 0;
 
126
 
 
127
    /* 1 */
 
128
    if((c = tf_getc()) == EOF)
 
129
        goto eof;
 
130
    if(!(c & 0x80)) return l | c;
 
131
    l = (l | (c & 0x7f)) << 7;
 
132
 
 
133
    /* 2 */
 
134
    if((c = tf_getc()) == EOF)
 
135
        goto eof;
 
136
    if(!(c & 0x80)) return l | c;
 
137
    l = (l | (c & 0x7f)) << 7;
 
138
 
 
139
    /* 3 */
 
140
    if((c = tf_getc()) == EOF)
 
141
        goto eof;
 
142
    if(!(c & 0x80)) return l | c;
 
143
    l = (l | (c & 0x7f)) << 7;
 
144
 
 
145
    /* 4 */
 
146
    if((c = tf_getc()) == EOF)
 
147
        goto eof;
 
148
    if(!(c & 0x80)) return l | c;
 
149
 
 
150
    /* Error */
 
151
    ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
 
152
              "\"%s\": Illegal Variable-length quantity format.",
 
153
              current_filename);
 
154
    return -2;
 
155
 
 
156
  eof:
 
157
    if(errno)
 
158
        ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
 
159
                  "\"%s\": read_midi_event: %s",
 
160
                  current_filename, strerror(errno));
 
161
    else
 
162
        ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
 
163
                  "Warning: \"%s\": Short midi file.",
 
164
                  current_filename);
 
165
    return -1;
 
166
}
 
167
 
 
168
#else
 
169
 
 
170
/* Read variable-length number (7 bits per byte, MSB first) */
 
171
static uint32 getvl(void)
 
172
{
 
173
  uint32 l=0;
 
174
  uint8 c;
 
175
  for (;;)
 
176
    {
 
177
      fread(&c,1,1,fp);
 
178
      l += (c & 0x7f);
 
179
      if (!(c & 0x80)) return l;
 
180
      l<<=7;
 
181
    }
 
182
}
 
183
#endif
 
184
 
 
185
/**********************************/
 
186
struct meta_text_type *meta_text_list = NULL;
 
187
 
 
188
static void free_metatext()
 
189
{
 
190
    struct meta_text_type *meta;
 
191
    while (meta_text_list) {
 
192
        meta = meta_text_list;
 
193
        meta_text_list = meta->next;
 
194
        free(meta->text);
 
195
        meta->text = NULL;
 
196
        free(meta);
 
197
        meta = NULL;
 
198
    }
 
199
}
 
200
 
 
201
static int metatext(int type, uint32 leng, char *mess)
 
202
{
 
203
    static int continued_flag = 0;
 
204
    int c;
 
205
    uint32 n;
 
206
    unsigned char *p = (unsigned char *)mess;
 
207
    struct meta_text_type *meta, *mlast;
 
208
    char *meta_string;
 
209
 
 
210
    /* if (at > 0 && (type == 1||type == 5||type == 6||type == 7)) { */
 
211
    if (type==5 || ( at > 0 && (type==1||type==6||type==7) )) {
 
212
        meta = (struct meta_text_type *)safe_malloc(sizeof(struct meta_text_type));
 
213
        if (leng > 72) leng = 72;
 
214
        meta_string = (char *)safe_malloc(leng+8);
 
215
        if (!leng) {
 
216
            continued_flag = 1;
 
217
            meta_string[leng++] = '\012';
 
218
        }
 
219
        else for (n = 0; n < leng; n++) {
 
220
            c = *p++;
 
221
            if (!n && (c == '/' || c == '\\')) {
 
222
                continued_flag = 1;
 
223
                meta_string[0] = '\012';
 
224
                if (c == '/') {
 
225
                meta_string[1] = ' ';
 
226
                meta_string[2] = ' ';
 
227
                meta_string[3] = ' ';
 
228
                meta_string[4] = ' ';
 
229
                leng += 4;
 
230
                n += 4;
 
231
                }
 
232
            }
 
233
            else {
 
234
                /*meta_string[n] = (isprint(c) || isspace(c)) ? c : '.';*/
 
235
                meta_string[n] = ((c > 0x7f) || isprint(c) || isspace(c)) ? c : '.';
 
236
                if (c == '\012' || c == '\015') continued_flag = 1;
 
237
                if (c == '\015') meta_string[n] = '\012';
 
238
            }
 
239
        }
 
240
        if (!continued_flag) {
 
241
                meta_string[leng++] = '\012';
 
242
        }
 
243
        meta_string[leng] = '\0';
 
244
        meta->type = type;
 
245
        meta->text = meta_string;
 
246
        meta->time = at;
 
247
        meta->next = NULL;
 
248
/*
 
249
while at==0
 
250
1. head = meta1; head->next = NULL
 
251
2. for ...: mlast = head
 
252
   if ...: not <
 
253
   else ...: meta2->next = NULL
 
254
             head->next = meta2
 
255
   so: (meta1, meta2)
 
256
3. for ...: mlast = meta2
 
257
   if ...:
 
258
   else: meta3->next = NULL
 
259
         meta2->next = meta3
 
260
   so: (meta1, meta2, meta3)
 
261
*/
 
262
        if (meta_text_list == NULL) {
 
263
            meta->next = meta_text_list;
 
264
            meta_text_list = meta;
 
265
        }
 
266
        else {
 
267
            for (mlast = meta_text_list; mlast->next != NULL; mlast = mlast->next)
 
268
                if (mlast->next->time > meta->time) break;
 
269
            if (mlast == meta_text_list && meta->time < mlast->time) {
 
270
                meta->next = meta_text_list;
 
271
                meta_text_list = meta;
 
272
            }
 
273
            else {
 
274
                meta->next = mlast->next;
 
275
                mlast->next = meta;
 
276
            }
 
277
        }
 
278
        return 1;
 
279
    }
 
280
    else return 0;
 
281
}
 
282
 
 
283
static int sysex(uint32 len, uint8 *syschan, uint8 *sysa, uint8 *sysb)
 
284
{
 
285
  unsigned char *s=(unsigned char *)safe_malloc(len);
 
286
  int id, model, ch, port, adhi, adlo, cd, dta, dtb, dtc;
 
287
  if (len != fread(s, 1, len, fp))
 
288
    {
 
289
      free(s);
 
290
      return 0;
 
291
    }
 
292
  if (len<5) { free(s); return 0; }
 
293
  id=s[0]; port=s[1]; model=s[2]; adhi=s[3]; adlo=s[4];
 
294
  if (id==0x7e && port==0x7f && model==0x09 && adhi==0x01)
 
295
    {
 
296
      ctl->cmsg(CMSG_TEXT, VERB_VERBOSE, "GM System On", len);
 
297
      GM_System_On=1;
 
298
      free(s);
 
299
      return 0;
 
300
    }
 
301
  ch = adlo & 0x0f;
 
302
  *syschan=(uint8)ch;
 
303
  if (id==0x7f && len==7 && port==0x7f && model==0x04 && adhi==0x01)
 
304
    {
 
305
      ctl->cmsg(CMSG_TEXT, VERB_DEBUG, "Master Volume %d", s[4]+(s[5]<<7));
 
306
      free(s);
 
307
      *sysa = s[4];
 
308
      *sysb = s[5];
 
309
      return ME_MASTERVOLUME;
 
310
      /** return s[4]+(s[5]<<7); **/
 
311
    }
 
312
  if (len<8) { free(s); return 0; }
 
313
  port &=0x0f;
 
314
  ch = (adlo & 0x0f) | ((port & 0x03) << 4);
 
315
  *syschan=(uint8)ch;
 
316
  cd=s[5]; dta=s[6];
 
317
  if (len >= 8) dtb=s[7];
 
318
  else dtb=-1;
 
319
  if (len >= 9) dtc=s[8];
 
320
  else dtc=-1;
 
321
  free(s);
 
322
  if (id==0x43 && model==0x4c)
 
323
    {
 
324
        if (!adhi && !adlo && cd==0x7e && !dta)
 
325
          {
 
326
            ctl->cmsg(CMSG_TEXT, VERB_VERBOSE, "XG System On", len);
 
327
            XG_System_On=1;
 
328
            #ifdef tplus
 
329
            vol_table = xg_vol_table;
 
330
            #endif
 
331
          }
 
332
        else if (adhi == 2 && adlo == 1)
 
333
         {
 
334
            if (dtb==8) dtb=3;
 
335
            switch (cd)
 
336
              {
 
337
                case 0x00:
 
338
                  XG_System_reverb_type=(dta<<3)+dtb;
 
339
                  break;
 
340
                case 0x20:
 
341
                  XG_System_chorus_type=((dta-64)<<3)+dtb;
 
342
                  break;
 
343
                case 0x40:
 
344
                  XG_System_variation_type=dta;
 
345
                  break;
 
346
                case 0x5a:
 
347
                  /* dta==0 Insertion; dta==1 System */
 
348
                  break;
 
349
                default: break;
 
350
              }
 
351
         }
 
352
        else if (adhi == 8 && cd <= 40)
 
353
         {
 
354
            *sysa = dta & 0x7f;
 
355
            switch (cd)
 
356
              {
 
357
                case 0x01: /* bank select MSB */
 
358
                  return ME_TONE_KIT;
 
359
                  break;
 
360
                case 0x02: /* bank select LSB */
 
361
                  return ME_TONE_BANK;
 
362
                  break;
 
363
                case 0x03: /* program number */
 
364
                        /** MIDIEVENT(at, ME_PROGRAM, lastchan, a, 0); **/
 
365
                  return ME_PROGRAM;
 
366
                  break;
 
367
                case 0x08: /*  */
 
368
                  /* channel[adlo&0x0f].transpose = (char)(dta-64); */
 
369
                  channel[ch].transpose = (char)(dta-64);
 
370
                  ctl->cmsg(CMSG_TEXT, VERB_DEBUG, "transpose channel %d by %d",
 
371
                        (adlo&0x0f)+1, dta-64);
 
372
                  break;
 
373
                case 0x0b: /* volume */
 
374
                  return ME_MAINVOLUME;
 
375
                  break;
 
376
                case 0x0e: /* pan */
 
377
                  return ME_PAN;
 
378
                  break;
 
379
                case 0x12: /* chorus send */
 
380
                  return ME_CHORUSDEPTH;
 
381
                  break;
 
382
                case 0x13: /* reverb send */
 
383
                  return ME_REVERBERATION;
 
384
                  break;
 
385
                case 0x14: /* variation send */
 
386
                  break;
 
387
                case 0x18: /* filter cutoff */
 
388
                  return ME_BRIGHTNESS;
 
389
                  break;
 
390
                case 0x19: /* filter resonance */
 
391
                  return ME_HARMONICCONTENT;
 
392
                  break;
 
393
                default: break;
 
394
              }
 
395
          }
 
396
      return 0;
 
397
    }
 
398
  else if (id==0x41 && model==0x42 && adhi==0x12 && adlo==0x40)
 
399
    {
 
400
        if (dtc<0) return 0;
 
401
        if (!cd && dta==0x7f && !dtb && dtc==0x41)
 
402
          {
 
403
            ctl->cmsg(CMSG_TEXT, VERB_VERBOSE, "GS System On", len);
 
404
            GS_System_On=1;
 
405
            #ifdef tplus
 
406
            vol_table = gs_vol_table;
 
407
            #endif
 
408
          }
 
409
        else if (dta==0x15 && (cd&0xf0)==0x10)
 
410
          {
 
411
            int chan=cd&0x0f;
 
412
            if (!chan) chan=9;
 
413
            else if (chan<10) chan--;
 
414
            chan = MERGE_CHANNEL_PORT(chan);
 
415
            channel[chan].kit=dtb;
 
416
          }
 
417
        else if (cd==0x01) switch(dta)
 
418
          {
 
419
            case 0x30:
 
420
                switch(dtb)
 
421
                  {
 
422
                    case 0: XG_System_reverb_type=16+0; break;
 
423
                    case 1: XG_System_reverb_type=16+1; break;
 
424
                    case 2: XG_System_reverb_type=16+2; break;
 
425
                    case 3: XG_System_reverb_type= 8+0; break;
 
426
                    case 4: XG_System_reverb_type= 8+1; break;
 
427
                    case 5: XG_System_reverb_type=32+0; break;
 
428
                    case 6: XG_System_reverb_type=8*17; break;
 
429
                    case 7: XG_System_reverb_type=8*18; break;
 
430
                  }
 
431
                break;
 
432
            case 0x38:
 
433
                switch(dtb)
 
434
                  {
 
435
                    case 0: XG_System_chorus_type= 8+0; break;
 
436
                    case 1: XG_System_chorus_type= 8+1; break;
 
437
                    case 2: XG_System_chorus_type= 8+2; break;
 
438
                    case 3: XG_System_chorus_type= 8+4; break;
 
439
                    case 4: XG_System_chorus_type=  -1; break;
 
440
                    case 5: XG_System_chorus_type= 8*3; break;
 
441
                    case 6: XG_System_chorus_type=  -1; break;
 
442
                    case 7: XG_System_chorus_type=  -1; break;
 
443
                  }
 
444
                break;
 
445
          }
 
446
      return 0;
 
447
    }
 
448
  return 0;
 
449
}
 
450
 
 
451
/**********************************/
 
452
 
 
453
 
 
454
/* Print a string from the file, followed by a newline. Any non-ASCII
 
455
   or unprintable characters will be converted to periods. */
 
456
static int dumpstring(uint32 len, const char *label, int type)
 
457
{
 
458
  char *s=(char *)safe_malloc(len+1);
 
459
  if (len != fread(s, 1, len, fp))
 
460
    {
 
461
      free(s);
 
462
      return -1;
 
463
    }
 
464
  s[len]='\0';
 
465
  if (!metatext(type, len, s))
 
466
  {
 
467
   while (len--)
 
468
    {
 
469
      if (s[len]<32)
 
470
        s[len]='.';
 
471
    }
 
472
   ctl->cmsg(CMSG_TEXT, VERB_VERBOSE, "%s%s", label, s);
 
473
  }
 
474
  free(s);
 
475
  return 0;
 
476
}
 
477
 
 
478
#ifdef POLYPHONY_COUNT
 
479
#define MIDIEVENT(at,t,ch,pa,pb) \
 
480
  newev=(MidiEventList *)safe_malloc(sizeof(MidiEventList)); \
 
481
  newev->event.time=at; newev->event.type=t; newev->event.channel=ch; \
 
482
  newev->event.a=pa; newev->event.b=pb; newev->next=0; newev->event.polyphony = 0; \
 
483
  return newev;
 
484
#else
 
485
#define MIDIEVENT(at,t,ch,pa,pb) \
 
486
  newev=(MidiEventList *)safe_malloc(sizeof(MidiEventList)); \
 
487
  newev->event.time=at; newev->event.type=t; newev->event.channel=ch; \
 
488
  newev->event.a=pa; newev->event.b=pb; newev->next=0; \
 
489
  return newev;
 
490
#endif
 
491
 
 
492
#define MAGIC_EOT ((MidiEventList *)(-1))
 
493
 
 
494
 
 
495
/* Read a MIDI event, returning a freshly allocated element that can
 
496
   be linked to the event list */
 
497
static MidiEventList *read_midi_event(void)
 
498
{
 
499
  static uint8 laststatus, lastchan;
 
500
  static uint8 nrpn=0, rpn_msb[MAXCHAN], rpn_lsb[MAXCHAN]; /* one per channel */
 
501
  uint8 me, type, a,b,c;
 
502
#ifdef tplus
 
503
  int32 len, i;
 
504
#else
 
505
  int32 len, i;
 
506
#endif
 
507
  MidiEventList *newev;
 
508
 
 
509
  for (;;)
 
510
    {
 
511
#ifdef tplus
 
512
      if ( (len=getvl()) < 0) return 0;
 
513
      at+= len;
 
514
#else
 
515
      at+=getvl();
 
516
#endif
 
517
 
 
518
#ifdef tplus
 
519
      if((i = tf_getc()) == EOF)
 
520
        {
 
521
            if(errno)
 
522
                ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
 
523
                          "\"%s\": read_midi_event: %s",
 
524
                          current_filename, strerror(errno));
 
525
            else
 
526
                ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
 
527
                          "Warning: \"%s\": Short midi file.",
 
528
                          current_filename);
 
529
            return 0;
 
530
        }
 
531
      me = (uint8)i;
 
532
#else
 
533
      if (fread(&me,1,1,fp)!=1)
 
534
        {
 
535
          ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "\"%s\": read_midi_event: %s", 
 
536
               current_filename, sys_errlist[errno]);
 
537
          return 0;
 
538
        }
 
539
#endif
 
540
      
 
541
      if(me==0xF0 || me == 0xF7) /* SysEx event */
 
542
        {
 
543
          int32 sret;
 
544
          uint8 sysa=0, sysb=0, syschan=0;
 
545
#ifdef tplus
 
546
          if ( (len=getvl()) < 0) return 0;
 
547
#else
 
548
          len=getvl();
 
549
#endif
 
550
          sret=sysex(len, &syschan, &sysa, &sysb);
 
551
          if (sret)
 
552
           {
 
553
             MIDIEVENT(at, sret, syschan, sysa, sysb);
 
554
           }
 
555
        }
 
556
      else if(me==0xFF) /* Meta event */
 
557
        {
 
558
#ifdef tplus
 
559
      if((i = tf_getc()) == EOF)
 
560
        {
 
561
            if(errno)
 
562
                ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
 
563
                          "\"%s\": read_midi_event: %s",
 
564
                          current_filename, strerror(errno));
 
565
            else
 
566
                ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
 
567
                          "Warning: \"%s\": Short midi file.",
 
568
                          current_filename);
 
569
            return 0;
 
570
        }
 
571
      type = (uint8)i;
 
572
#else
 
573
          fread(&type,1,1,fp);
 
574
#endif
 
575
#ifdef tplus
 
576
          if ( (len=getvl()) < 0) return 0;
 
577
#else
 
578
          len=getvl();
 
579
#endif
 
580
          if (type>0 && type<16)
 
581
            {
 
582
              static const char *label[]={
 
583
                "text: ", "text: ", "Copyright: ", "track: ",
 
584
                "instrument: ", "lyric: ", "marker: ", "cue point: "};
 
585
              dumpstring(len, label[(type>7) ? 0 : type], type);
 
586
            }
 
587
          else
 
588
            switch(type)
 
589
              {
 
590
 
 
591
              case 0x21: /* MIDI port number */
 
592
                if(len == 1)
 
593
                {
 
594
                    fread(&midi_port_number,1,1,fp);
 
595
                    if(midi_port_number == EOF)
 
596
                    {
 
597
                            ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
 
598
                                      "Warning: \"%s\": Short midi file.",
 
599
                                      current_filename);
 
600
                            return 0;
 
601
                    }
 
602
                    midi_port_number &= 0x0f;
 
603
                    if (midi_port_number)
 
604
                        ctl->cmsg(CMSG_INFO, VERB_VERBOSE,
 
605
                          "(MIDI port number %d)", midi_port_number);
 
606
                    midi_port_number &= 0x03;
 
607
                }
 
608
                else skip(fp, len);
 
609
                break;
 
610
 
 
611
              case 0x2F: /* End of Track */
 
612
                return MAGIC_EOT;
 
613
 
 
614
              case 0x51: /* Tempo */
 
615
#ifdef tplus
 
616
      if((i = tf_getc()) == EOF)
 
617
        {
 
618
            if(errno)
 
619
                ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
 
620
                          "\"%s\": read_midi_event: %s",
 
621
                          current_filename, strerror(errno));
 
622
            else
 
623
                ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
 
624
                          "Warning: \"%s\": Short midi file.",
 
625
                          current_filename);
 
626
            return 0;
 
627
        }
 
628
      a = (uint8)i;
 
629
      if((i = tf_getc()) == EOF)
 
630
        {
 
631
            if(errno)
 
632
                ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
 
633
                          "\"%s\": read_midi_event: %s",
 
634
                          current_filename, strerror(errno));
 
635
            else
 
636
                ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
 
637
                          "Warning: \"%s\": Short midi file.",
 
638
                          current_filename);
 
639
            return 0;
 
640
        }
 
641
      b = (uint8)i;
 
642
      if((i = tf_getc()) == EOF)
 
643
        {
 
644
            if(errno)
 
645
                ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
 
646
                          "\"%s\": read_midi_event: %s",
 
647
                          current_filename, strerror(errno));
 
648
            else
 
649
                ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
 
650
                          "Warning: \"%s\": Short midi file.",
 
651
                          current_filename);
 
652
            return 0;
 
653
        }
 
654
      c = (uint8)i;
 
655
#else
 
656
                fread(&a,1,1,fp); fread(&b,1,1,fp); fread(&c,1,1,fp);
 
657
#endif
 
658
                MIDIEVENT(at, ME_TEMPO, c, a, b);
 
659
                
 
660
              default:
 
661
                ctl->cmsg(CMSG_INFO, VERB_DEBUG, 
 
662
                     "(Meta event type 0x%02x, length %ld)", type, len);
 
663
                skip(fp, len);
 
664
                break;
 
665
              }
 
666
        }
 
667
      else
 
668
        {
 
669
          a=me;
 
670
          if (a & 0x80) /* status byte */
 
671
            {
 
672
              lastchan = MERGE_CHANNEL_PORT(a & 0x0F);
 
673
              laststatus=(a>>4) & 0x07;
 
674
#ifdef tplus
 
675
      if((i = tf_getc()) == EOF)
 
676
        {
 
677
            if(errno)
 
678
                ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
 
679
                          "\"%s\": read_midi_event: %s",
 
680
                          current_filename, strerror(errno));
 
681
            else
 
682
                ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
 
683
                          "Warning: \"%s\": Short midi file.",
 
684
                          current_filename);
 
685
            return 0;
 
686
        }
 
687
      a = (uint8)i;
 
688
#else
 
689
              fread(&a, 1,1, fp);
 
690
#endif
 
691
              a &= 0x7F;
 
692
            }
 
693
          switch(laststatus)
 
694
            {
 
695
            case 0: /* Note off */
 
696
#ifdef tplus
 
697
      if((i = tf_getc()) == EOF)
 
698
        {
 
699
            if(errno)
 
700
                ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
 
701
                          "\"%s\": read_midi_event: %s",
 
702
                          current_filename, strerror(errno));
 
703
            else
 
704
                ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
 
705
                          "Warning: \"%s\": Short midi file.",
 
706
                          current_filename);
 
707
            return 0;
 
708
        }
 
709
      b = (uint8)i;
 
710
#else
 
711
              fread(&b, 1,1, fp);
 
712
#endif
 
713
              b &= 0x7F;
 
714
              /*MIDIEVENT(at, ME_NOTEOFF, lastchan, a,b);*/
 
715
              MIDIEVENT(at, ME_NOTEON, lastchan, a,0);
 
716
 
 
717
            case 1: /* Note on */
 
718
#ifdef tplus
 
719
      if((i = tf_getc()) == EOF)
 
720
        {
 
721
            if(errno)
 
722
                ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
 
723
                          "\"%s\": read_midi_event: %s",
 
724
                          current_filename, strerror(errno));
 
725
            else
 
726
                ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
 
727
                          "Warning: \"%s\": Short midi file.",
 
728
                          current_filename);
 
729
            return 0;
 
730
        }
 
731
      b = (uint8)i;
 
732
#else
 
733
              fread(&b, 1,1, fp);
 
734
#endif
 
735
              b &= 0x7F;
 
736
              MIDIEVENT(at, ME_NOTEON, lastchan, a,b);
 
737
 
 
738
            case 2: /* Key Pressure */
 
739
#ifdef tplus
 
740
      if((i = tf_getc()) == EOF)
 
741
        {
 
742
            if(errno)
 
743
                ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
 
744
                          "\"%s\": read_midi_event: %s",
 
745
                          current_filename, strerror(errno));
 
746
            else
 
747
                ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
 
748
                          "Warning: \"%s\": Short midi file.",
 
749
                          current_filename);
 
750
            return 0;
 
751
        }
 
752
      b = (uint8)i;
 
753
#else
 
754
              fread(&b, 1,1, fp);
 
755
#endif
 
756
              b &= 0x7F;
 
757
              MIDIEVENT(at, ME_KEYPRESSURE, lastchan, a, b);
 
758
 
 
759
            case 3: /* Control change */
 
760
#ifdef tplus
 
761
      if((i = tf_getc()) == EOF)
 
762
        {
 
763
            if(errno)
 
764
                ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
 
765
                          "\"%s\": read_midi_event: %s",
 
766
                          current_filename, strerror(errno));
 
767
            else
 
768
                ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
 
769
                          "Warning: \"%s\": Short midi file.",
 
770
                          current_filename);
 
771
            return 0;
 
772
        }
 
773
      b = (uint8)i;
 
774
#else
 
775
              fread(&b, 1,1, fp);
 
776
#endif
 
777
              b &= 0x7F;
 
778
              {
 
779
                int control=255;
 
780
                switch(a)
 
781
                  {
 
782
#ifdef tplus
 
783
                  case 1: control=ME_MODULATION_WHEEL; break;
 
784
                  case 5: control=ME_PORTAMENTO_TIME_MSB; break;
 
785
                  case 37: control=ME_PORTAMENTO_TIME_LSB; break;
 
786
                  case 65: control=ME_PORTAMENTO; break;
 
787
#endif
 
788
                  case 7: control=ME_MAINVOLUME; break;
 
789
                  case 10: control=ME_PAN; break;
 
790
                  case 11: control=ME_EXPRESSION; break;
 
791
#ifdef tplus
 
792
                  case 64: control=ME_SUSTAIN; b = (b >= 64); break;
 
793
#else
 
794
                  case 64: control=ME_SUSTAIN; break;
 
795
#endif
 
796
                  case 71: control=ME_HARMONICCONTENT; break;
 
797
                  case 72: control=ME_RELEASETIME; break;
 
798
                  case 73: control=ME_ATTACKTIME; break;
 
799
                  case 74: control=ME_BRIGHTNESS; break;
 
800
                  case 91: control=ME_REVERBERATION; break;
 
801
                  case 93: control=ME_CHORUSDEPTH; break;
 
802
#ifdef CHANNEL_EFFECT
 
803
                  case 94: control=ME_CELESTE; break;
 
804
ctl->cmsg(CMSG_INFO, VERB_NORMAL, "CELESTE");
 
805
                  case 95: control=ME_PHASER; break;
 
806
ctl->cmsg(CMSG_INFO, VERB_NORMAL, "PHASER");
 
807
#endif
 
808
                  case 120: control=ME_ALL_SOUNDS_OFF; break;
 
809
                  case 121: control=ME_RESET_CONTROLLERS; break;
 
810
                  case 123: control=ME_ALL_NOTES_OFF; break;
 
811
 
 
812
                    /* These should be the SCC-1 tone bank switch
 
813
                       commands. I don't know why there are two, or
 
814
                       why the latter only allows switching to bank 0.
 
815
                       Also, some MIDI files use 0 as some sort of
 
816
                       continuous controller. This will cause lots of
 
817
                       warnings about undefined tone banks. */
 
818
                  case 0:
 
819
                    if (XG_System_On)
 
820
                        control=ME_TONE_KIT;
 
821
                    else control=ME_TONE_BANK;
 
822
                    break;
 
823
                  case 32:
 
824
                    if (XG_System_On) control=ME_TONE_BANK;
 
825
                    break;
 
826
 
 
827
                  case 100: nrpn=0; rpn_msb[lastchan]=b; break;
 
828
                  case 101: nrpn=0; rpn_lsb[lastchan]=b; break;
 
829
                  case 98: nrpn=1; rpn_lsb[lastchan]=b; break;
 
830
                  case 99: nrpn=1; rpn_msb[lastchan]=b; break;
 
831
                  case 6:
 
832
                    if (nrpn)
 
833
                      {
 
834
                        if (rpn_msb[lastchan]==1) switch (rpn_lsb[lastchan])
 
835
                         {
 
836
#ifdef tplus
 
837
                           case 0x08: control=ME_VIBRATO_RATE; break;
 
838
                           case 0x09: control=ME_VIBRATO_DEPTH; break;
 
839
                           case 0x0a: control=ME_VIBRATO_DELAY; break;
 
840
#endif
 
841
                           case 0x20: control=ME_BRIGHTNESS; break;
 
842
                           case 0x21: control=ME_HARMONICCONTENT; break;
 
843
                        /*
 
844
                           case 0x63: envelope attack rate
 
845
                           case 0x64: envelope decay rate
 
846
                           case 0x66: envelope release rate
 
847
                        */
 
848
                         }
 
849
                        else switch (rpn_msb[lastchan])
 
850
                         {
 
851
                        /*
 
852
                           case 0x14: filter cutoff frequency
 
853
                           case 0x15: filter resonance
 
854
                           case 0x16: envelope attack rate
 
855
                           case 0x17: envelope decay rate
 
856
                           case 0x18: pitch coarse
 
857
                           case 0x19: pitch fine
 
858
                        */
 
859
                           case 0x1a: drumvolume[lastchan][0x7f & rpn_lsb[lastchan]] = b; break;
 
860
                           case 0x1c:
 
861
                             if (!b) b=(int) (127.0*rand()/(RAND_MAX));
 
862
                             drumpanpot[lastchan][0x7f & rpn_lsb[lastchan]] = b;
 
863
                             break;
 
864
                           case 0x1d: drumreverberation[lastchan][0x7f & rpn_lsb[lastchan]] = b; break;
 
865
                           case 0x1e: drumchorusdepth[lastchan][0x7f & rpn_lsb[lastchan]] = b; break;
 
866
                        /*
 
867
                           case 0x1f: variation send level
 
868
                        */
 
869
                         }
 
870
                        ctl->cmsg(CMSG_INFO, VERB_DEBUG, 
 
871
                                  "(Data entry (MSB) for NRPN %02x,%02x: %ld)",
 
872
                                  rpn_msb[lastchan], rpn_lsb[lastchan],
 
873
                                  b);
 
874
                        break;
 
875
                      }
 
876
                    
 
877
                    switch((rpn_msb[lastchan]<<8) | rpn_lsb[lastchan])
 
878
                      {
 
879
                      case 0x0000: /* Pitch bend sensitivity */
 
880
                        control=ME_PITCH_SENS;
 
881
                        break;
 
882
#ifdef tplus
 
883
                      case 0x0001: control=ME_FINE_TUNING; break;
 
884
                      case 0x0002: control=ME_COARSE_TUNING; break;
 
885
#endif
 
886
 
 
887
                      case 0x7F7F: /* RPN reset */
 
888
                        /* reset pitch bend sensitivity to 2 */
 
889
                        MIDIEVENT(at, ME_PITCH_SENS, lastchan, 2, 0);
 
890
 
 
891
                      default:
 
892
                        ctl->cmsg(CMSG_INFO, VERB_DEBUG, 
 
893
                                  "(Data entry (MSB) for RPN %02x,%02x: %ld)",
 
894
                                  rpn_msb[lastchan], rpn_lsb[lastchan],
 
895
                                  b);
 
896
                        break;
 
897
                      }
 
898
                    break;
 
899
                    
 
900
                  default:
 
901
                    ctl->cmsg(CMSG_INFO, VERB_DEBUG, 
 
902
                              "(Control %d: %d)", a, b);
 
903
                    break;
 
904
                  }
 
905
                if (control != 255)
 
906
                  { 
 
907
                    MIDIEVENT(at, control, lastchan, b, 0); 
 
908
                  }
 
909
              }
 
910
              break;
 
911
 
 
912
            case 4: /* Program change */
 
913
              a &= 0x7f;
 
914
              MIDIEVENT(at, ME_PROGRAM, lastchan, a, 0);
 
915
 
 
916
#ifdef tplus
 
917
            case 5: /* Channel pressure */
 
918
              MIDIEVENT(at, ME_CHANNEL_PRESSURE, lastchan, a, 0);
 
919
#else
 
920
            case 5: /* Channel pressure - NOT IMPLEMENTED */
 
921
              break;
 
922
#endif
 
923
 
 
924
            case 6: /* Pitch wheel */
 
925
#ifdef tplus
 
926
      if((i = tf_getc()) == EOF)
 
927
        {
 
928
            if(errno)
 
929
                ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
 
930
                          "\"%s\": read_midi_event: %s",
 
931
                          current_filename, strerror(errno));
 
932
            else
 
933
                ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
 
934
                          "Warning: \"%s\": Short midi file.",
 
935
                          current_filename);
 
936
            return 0;
 
937
        }
 
938
      b = (uint8)i;
 
939
#else
 
940
              fread(&b, 1,1, fp);
 
941
#endif
 
942
              b &= 0x7F;
 
943
              MIDIEVENT(at, ME_PITCHWHEEL, lastchan, a, b);
 
944
 
 
945
            case 7:
 
946
              break;
 
947
 
 
948
            default: 
 
949
              ctl->cmsg(CMSG_ERROR, VERB_NORMAL, 
 
950
                   "*** Can't happen: status 0x%02X, channel 0x%02X",
 
951
                   laststatus, lastchan);
 
952
              break;
 
953
            }
 
954
        }
 
955
    }
 
956
  
 
957
  return newev;
 
958
}
 
959
 
 
960
#undef MIDIEVENT
 
961
 
 
962
/* Read a midi track into the linked list, either merging with any previous
 
963
   tracks or appending to them. */
 
964
static int read_track(int append)
 
965
{
 
966
  MidiEventList *meep;
 
967
  MidiEventList *next, *newev;
 
968
  int32 len;
 
969
  char tmp[4];
 
970
 
 
971
  midi_port_number = 0;
 
972
  meep=evlist;
 
973
  if (append && meep)
 
974
    {
 
975
      /* find the last event in the list */
 
976
      for (; meep->next; meep=(MidiEventList *)meep->next)
 
977
        ;
 
978
      at=meep->event.time;
 
979
    }
 
980
  else
 
981
    at=0;
 
982
 
 
983
  /* Check the formalities */
 
984
  
 
985
  if ((fread(tmp,1,4,fp) != 4) || (fread(&len,4,1,fp) != 1))
 
986
    {
 
987
      ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
 
988
           "\"%s\": Can't read track header.", current_filename);
 
989
      return -1;
 
990
    }
 
991
  len=BE_LONG(len);
 
992
  if (memcmp(tmp, "MTrk", 4))
 
993
    {
 
994
      ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
 
995
           "\"%s\": Corrupt MIDI file.", current_filename);
 
996
      return -2;
 
997
    }
 
998
 
 
999
  for (;;)
 
1000
    {
 
1001
      if (!(newev=read_midi_event())) /* Some kind of error  */
 
1002
        return -2;
 
1003
 
 
1004
      if (newev==MAGIC_EOT) /* End-of-track Hack. */
 
1005
        {
 
1006
          return 0;
 
1007
        }
 
1008
 
 
1009
      next=(MidiEventList *)meep->next;
 
1010
      while (next && (next->event.time < newev->event.time))
 
1011
        {
 
1012
          meep=next;
 
1013
          next=(MidiEventList *)meep->next;
 
1014
        }
 
1015
          
 
1016
      newev->next=next;
 
1017
      meep->next=newev;
 
1018
 
 
1019
      event_count++; /* Count the event. (About one?) */
 
1020
      meep=newev;
 
1021
    }
 
1022
}
 
1023
 
 
1024
/* Free the linked event list from memory. */
 
1025
static void free_midi_list(void)
 
1026
{
 
1027
  MidiEventList *meep, *next;
 
1028
  if (!(meep=evlist)) return;
 
1029
  while (meep)
 
1030
    {
 
1031
      next=(MidiEventList *)meep->next;
 
1032
      free(meep);
 
1033
      meep=next;
 
1034
    }
 
1035
  evlist=0;
 
1036
}
 
1037
 
 
1038
/* Allocate an array of MidiEvents and fill it from the linked list of
 
1039
   events, marking used instruments for loading. Convert event times to
 
1040
   samples: handle tempo changes. Strip unnecessary events from the list.
 
1041
   Free the linked list. */
 
1042
static MidiEvent *groom_list(int32 divisions, uint32 *eventsp, uint32 *samplesp)
 
1043
{
 
1044
  MidiEvent *groomed_list, *lp;
 
1045
  MidiEventList *meep;
 
1046
  int32 i, our_event_count, tempo, skip_this_event, new_value;
 
1047
#ifdef POLYPHONY_COUNT
 
1048
  uint32 current_polyphony, future_interval;
 
1049
  MidiEvent *hind_list;
 
1050
#endif
 
1051
  int32 sample_cum, samples_to_do, st, dt, counting_time;
 
1052
  uint32 at;
 
1053
  struct meta_text_type *meta = meta_text_list;
 
1054
 
 
1055
  int current_bank[MAXCHAN], current_banktype[MAXCHAN], current_set[MAXCHAN],
 
1056
    current_kit[MAXCHAN], current_program[MAXCHAN]; 
 
1057
  /* Or should each bank have its own current program? */
 
1058
 
 
1059
  for (i=0; i<MAXCHAN; i++)
 
1060
    {
 
1061
      current_bank[i]=0;
 
1062
      current_banktype[i]=0;
 
1063
      current_set[i]=0;
 
1064
      current_kit[i]=channel[i].kit;
 
1065
      current_program[i]=default_program;
 
1066
    }
 
1067
 
 
1068
  tempo=500000;
 
1069
  compute_sample_increment(tempo, divisions);
 
1070
 
 
1071
  /* This may allocate a bit more than we need */
 
1072
  groomed_list=lp=(MidiEvent *)safe_malloc(sizeof(MidiEvent) * (event_count+1));
 
1073
  meep=evlist;
 
1074
 
 
1075
  our_event_count=0;
 
1076
  st=at=sample_cum=0;
 
1077
  counting_time=2; /* We strip any silence before the first NOTE ON. */
 
1078
 
 
1079
#ifdef POLYPHONY_COUNT
 
1080
  current_polyphony = 0;
 
1081
  hind_list = lp;
 
1082
  future_interval = play_mode->rate / 2;
 
1083
#endif
 
1084
 
 
1085
  for (i=0; i<event_count; i++)
 
1086
    {
 
1087
      skip_this_event=0;
 
1088
      ctl->cmsg(CMSG_INFO, VERB_DEBUG_SILLY,
 
1089
                "%6d: ch %2d: event %d (%d,%d)",
 
1090
                meep->event.time, meep->event.channel + 1,
 
1091
                meep->event.type, meep->event.a, meep->event.b);
 
1092
      while (meta && meta->time <= at)
 
1093
        {
 
1094
           meta->time = st;
 
1095
           meta = meta->next;
 
1096
        }
 
1097
      if (meep->event.type==ME_TEMPO)
 
1098
        {
 
1099
#ifndef tplus
 
1100
          tempo=
 
1101
            meep->event.channel + meep->event.b * 256 + meep->event.a * 65536;
 
1102
          compute_sample_increment(tempo, divisions);
 
1103
#endif
 
1104
          skip_this_event=1;
 
1105
        }
 
1106
      else if ((quietchannels & (1<<meep->event.channel)))
 
1107
        skip_this_event=1;
 
1108
      else switch (meep->event.type)
 
1109
        {
 
1110
        case ME_PROGRAM:
 
1111
          if (current_kit[meep->event.channel])
 
1112
            {
 
1113
              if (current_kit[meep->event.channel]==126)
 
1114
                {
 
1115
                  /* note request for 2nd sfx rhythm kit */
 
1116
                  if (meep->event.a && drumset[SFXDRUM2])
 
1117
                  {
 
1118
                         current_kit[meep->event.channel]=125;
 
1119
                         current_set[meep->event.channel]=SFXDRUM2;
 
1120
                         new_value=SFXDRUM2;
 
1121
                  }
 
1122
                  else if (!meep->event.a && drumset[SFXDRUM1])
 
1123
                  {
 
1124
                         current_set[meep->event.channel]=SFXDRUM1;
 
1125
                         new_value=SFXDRUM1;
 
1126
                  }
 
1127
                  else
 
1128
                  {
 
1129
                        ctl->cmsg(CMSG_WARNING, VERB_VERBOSE,
 
1130
                                "XG SFX drum set is undefined");
 
1131
                        skip_this_event=1;
 
1132
                        break;
 
1133
                  }
 
1134
                }
 
1135
              if (drumset[meep->event.a]) /* Is this a defined drumset? */
 
1136
                new_value=meep->event.a;
 
1137
              else
 
1138
                {
 
1139
                  ctl->cmsg(CMSG_WARNING, VERB_VERBOSE,
 
1140
                       "Drum set %d is undefined", meep->event.a);
 
1141
                  if (drumset[0])
 
1142
                      new_value=meep->event.a=0;
 
1143
                  else
 
1144
                    {
 
1145
                        skip_this_event=1;
 
1146
                        break;
 
1147
                    }
 
1148
                }
 
1149
              if (current_set[meep->event.channel] != new_value)
 
1150
                current_set[meep->event.channel]=new_value;
 
1151
              else 
 
1152
                skip_this_event=1;
 
1153
            }
 
1154
          else
 
1155
            {
 
1156
              new_value=meep->event.a;
 
1157
#if 0
 
1158
              if ((current_program[meep->event.channel] != SPECIAL_PROGRAM)
 
1159
                  && (current_program[meep->event.channel] != new_value))
 
1160
#endif
 
1161
              if (current_program[meep->event.channel] != new_value)
 
1162
                current_program[meep->event.channel] = new_value;
 
1163
              else
 
1164
                skip_this_event=1;
 
1165
            }
 
1166
          break;
 
1167
 
 
1168
        case ME_NOTEON:
 
1169
        #ifdef POLYPHONY_COUNT
 
1170
          if (meep->event.b) current_polyphony++;
 
1171
          else current_polyphony--;
 
1172
        #endif
 
1173
 
 
1174
          if (counting_time)
 
1175
            counting_time=1;
 
1176
#if 0
 
1177
/* trying to play canyon.mid, but this doesn't work */
 
1178
          if (meep->event.channel == 15 && current_program[15] == SPECIAL_PROGRAM)
 
1179
            channel[15].kit = current_kit[15] = 127;
 
1180
#endif
 
1181
          if (current_kit[meep->event.channel])
 
1182
            {
 
1183
              int dset = current_set[meep->event.channel];
 
1184
#if 0
 
1185
              int dnote=meep->event.a;
 
1186
 
 
1187
              if (dnote>99) dnote=0;
 
1188
              if (current_kit[meep->event.channel]==125)
 
1189
                meep->event.a=sfxdrum2[dnote];
 
1190
              else if (current_kit[meep->event.channel]==126)
 
1191
                meep->event.a=sfxdrum1[dnote];
 
1192
#endif
 
1193
              /* Mark this instrument to be loaded */
 
1194
              if (!(drumset[dset]->tone[meep->event.a].layer))
 
1195
               {
 
1196
                drumset[dset]->tone[meep->event.a].layer=
 
1197
                    MAGIC_LOAD_INSTRUMENT;
 
1198
               }
 
1199
              else drumset[dset]->tone[meep->event.a].last_used
 
1200
                 = current_tune_number;
 
1201
              if (!channel[meep->event.channel].name) channel[meep->event.channel].name=
 
1202
                    drumset[dset]->name;
 
1203
            }
 
1204
          else
 
1205
            {
 
1206
              int chan=meep->event.channel;
 
1207
              int banknum;
 
1208
 
 
1209
              if (current_banktype[chan]) banknum=SFXBANK;
 
1210
              else banknum=current_bank[chan];
 
1211
 
 
1212
              if (current_program[chan]==SPECIAL_PROGRAM)
 
1213
                break;
 
1214
              /* Mark this instrument to be loaded */
 
1215
              if (!(tonebank[banknum]->tone[current_program[chan]].layer))
 
1216
                {
 
1217
                  tonebank[banknum]->tone[current_program[chan]].layer=
 
1218
                    MAGIC_LOAD_INSTRUMENT;
 
1219
                }
 
1220
              else tonebank[banknum]->tone[current_program[chan]].last_used
 
1221
                 = current_tune_number;
 
1222
              if (!channel[meep->event.channel].name) channel[meep->event.channel].name=
 
1223
                    tonebank[banknum]->tone[current_program[chan]].name;
 
1224
            }
 
1225
          break;
 
1226
 
 
1227
        case ME_TONE_KIT:
 
1228
          if (!meep->event.a || meep->event.a == 127)
 
1229
            {
 
1230
              new_value=meep->event.a;
 
1231
              if (current_kit[meep->event.channel] != new_value)
 
1232
                current_kit[meep->event.channel]=new_value;
 
1233
              else 
 
1234
                skip_this_event=1;
 
1235
              break;
 
1236
            }
 
1237
          else if (meep->event.a == 126)
 
1238
            {
 
1239
              if (drumset[SFXDRUM1]) /* Is this a defined tone bank? */
 
1240
                new_value=meep->event.a;
 
1241
              else
 
1242
                {
 
1243
                  ctl->cmsg(CMSG_WARNING, VERB_VERBOSE,
 
1244
                   "XG rhythm kit %d is undefined", meep->event.a);
 
1245
                  skip_this_event=1;
 
1246
                  break;
 
1247
                }
 
1248
              current_set[meep->event.channel]=SFXDRUM1;
 
1249
              current_kit[meep->event.channel]=new_value;
 
1250
              break;
 
1251
            }
 
1252
          else if (meep->event.a != SFX_BANKTYPE)
 
1253
            {
 
1254
              ctl->cmsg(CMSG_WARNING, VERB_VERBOSE,
 
1255
                   "XG kit %d is impossible", meep->event.a);
 
1256
              skip_this_event=1;
 
1257
              break;
 
1258
            }
 
1259
 
 
1260
          if (current_kit[meep->event.channel])
 
1261
            {
 
1262
              skip_this_event=1;
 
1263
              break;
 
1264
            }
 
1265
          if (tonebank[SFXBANK]) /* Is this a defined tone bank? */
 
1266
            new_value=SFX_BANKTYPE;
 
1267
          else 
 
1268
            {
 
1269
              ctl->cmsg(CMSG_WARNING, VERB_VERBOSE,
 
1270
                   "XG Sfx bank is undefined");
 
1271
              skip_this_event=1;
 
1272
              break;
 
1273
            }
 
1274
          if (current_banktype[meep->event.channel]!=new_value)
 
1275
            current_banktype[meep->event.channel]=new_value;
 
1276
          else
 
1277
            skip_this_event=1;
 
1278
          break;
 
1279
 
 
1280
 
 
1281
        case ME_TONE_BANK:
 
1282
          if (current_kit[meep->event.channel])
 
1283
            {
 
1284
              skip_this_event=1;
 
1285
              break;
 
1286
            }
 
1287
          if (XG_System_On && meep->event.a > 0 && meep->event.a < 48) {
 
1288
              channel[meep->event.channel].variationbank=meep->event.a;
 
1289
              ctl->cmsg(CMSG_WARNING, VERB_VERBOSE,
 
1290
                   "XG variation bank %d", meep->event.a);
 
1291
              new_value=meep->event.a=0;
 
1292
          }
 
1293
          else if (tonebank[meep->event.a]) /* Is this a defined tone bank? */
 
1294
            new_value=meep->event.a;
 
1295
          else 
 
1296
            {
 
1297
              ctl->cmsg(CMSG_WARNING, VERB_VERBOSE,
 
1298
                   "Tone bank %d is undefined", meep->event.a);
 
1299
              new_value=meep->event.a=0;
 
1300
            }
 
1301
 
 
1302
          if (current_bank[meep->event.channel]!=new_value)
 
1303
            current_bank[meep->event.channel]=new_value;
 
1304
          else
 
1305
            skip_this_event=1;
 
1306
          break;
 
1307
 
 
1308
        case ME_HARMONICCONTENT:
 
1309
          channel[meep->event.channel].harmoniccontent=meep->event.a;
 
1310
          break;
 
1311
        case ME_BRIGHTNESS:
 
1312
          channel[meep->event.channel].brightness=meep->event.a;
 
1313
          break;
 
1314
 
 
1315
        }
 
1316
 
 
1317
      /* Recompute time in samples*/
 
1318
      if ((dt=meep->event.time - at) && !counting_time)
 
1319
        {
 
1320
          samples_to_do=sample_increment * dt;
 
1321
          sample_cum += sample_correction * dt;
 
1322
          if (sample_cum & 0xFFFF0000)
 
1323
            {
 
1324
              samples_to_do += ((sample_cum >> 16) & 0xFFFF);
 
1325
              sample_cum &= 0x0000FFFF;
 
1326
            }
 
1327
          st += samples_to_do;
 
1328
        }
 
1329
      else if (counting_time==1) counting_time=0;
 
1330
#ifdef tplus
 
1331
      if (meep->event.type==ME_TEMPO)
 
1332
        {
 
1333
          tempo=
 
1334
            meep->event.channel + meep->event.b * 256 + meep->event.a * 65536;
 
1335
          compute_sample_increment(tempo, divisions);
 
1336
        }
 
1337
#endif
 
1338
      if (!skip_this_event)
 
1339
        {
 
1340
          /* Add the event to the list */
 
1341
          *lp=meep->event;
 
1342
          lp->time=st;
 
1343
        #ifdef POLYPHONY_COUNT
 
1344
          while (hind_list < lp && hind_list->time <= st - future_interval)
 
1345
            {
 
1346
                hind_list->polyphony = current_polyphony;
 
1347
                hind_list++;
 
1348
            }
 
1349
        #endif
 
1350
          lp++;
 
1351
          our_event_count++;
 
1352
        }
 
1353
      at=meep->event.time;
 
1354
      meep=(MidiEventList *)meep->next;
 
1355
    }
 
1356
  /* Add an End-of-Track event */
 
1357
  lp->time=st;
 
1358
  lp->type=ME_EOT;
 
1359
  our_event_count++;
 
1360
  free_midi_list();
 
1361
 
 
1362
  *eventsp=our_event_count;
 
1363
  *samplesp=st;
 
1364
  return groomed_list;
 
1365
}
 
1366
 
 
1367
MidiEvent *read_midi_file(FILE *mfp, uint32 *count, uint32 *sp)
 
1368
{
 
1369
  uint32 len;
 
1370
  int32 divisions;
 
1371
  int16 format, tracks, divisions_tmp;
 
1372
  int i;
 
1373
  char tmp[4];
 
1374
 
 
1375
  fp=mfp;
 
1376
  event_count=0;
 
1377
  at=0;
 
1378
  evlist=0;
 
1379
  free_metatext();
 
1380
  GM_System_On=GS_System_On=XG_System_On=0;
 
1381
  vol_table = def_vol_table;
 
1382
  XG_System_reverb_type=XG_System_chorus_type=XG_System_variation_type=0;
 
1383
  memset(&drumvolume,-1,sizeof(drumvolume));
 
1384
  memset(&drumchorusdepth,-1,sizeof(drumchorusdepth));
 
1385
  memset(&drumreverberation,-1,sizeof(drumreverberation));
 
1386
  memset(&drumpanpot,NO_PANNING,sizeof(drumpanpot));
 
1387
  for (i=0; i<MAXCHAN; i++)
 
1388
     {
 
1389
        channel[i].transpose = 0;
 
1390
        if (ISDRUMCHANNEL(i)) channel[i].kit = 127;
 
1391
        else channel[i].kit = 0;
 
1392
        channel[i].brightness = 64;
 
1393
        channel[i].harmoniccontent = 64;
 
1394
        channel[i].variationbank = 0;
 
1395
        channel[i].chorusdepth = 0;
 
1396
        channel[i].reverberation = 0;
 
1397
        channel[i].name = 0;
 
1398
     }
 
1399
 
 
1400
  if ((fread(tmp,1,4,fp) != 4) || (fread(&len,4,1,fp) != 1))
 
1401
    {
 
1402
      if (ferror(fp))
 
1403
        {
 
1404
          ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "\"%s\": %s", current_filename, 
 
1405
               sys_errlist[errno]);
 
1406
        }
 
1407
      else
 
1408
        ctl->cmsg(CMSG_ERROR, VERB_NORMAL, 
 
1409
             "\"%s\": Not a MIDI file!", current_filename);
 
1410
      return 0;
 
1411
    }
 
1412
  len=BE_LONG(len);
 
1413
  if (memcmp(tmp, "MThd", 4) || len < 6)
 
1414
    {
 
1415
      ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
 
1416
           "\"%s\": Not a MIDI file!", current_filename);
 
1417
      return 0;
 
1418
    }
 
1419
 
 
1420
  if ( fread(&format, 2, 1, fp) != 1 )
 
1421
    {
 
1422
      if (ferror(fp))
 
1423
        {
 
1424
          ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "\"%s\": %s", current_filename, 
 
1425
               sys_errlist[errno]);
 
1426
        }
 
1427
      else
 
1428
        ctl->cmsg(CMSG_ERROR, VERB_NORMAL, 
 
1429
             "\"%s\": Not a MIDI file!", current_filename);
 
1430
      return 0;
 
1431
    }
 
1432
  if ( fread(&tracks, 2, 1, fp) != 1 )
 
1433
    {
 
1434
      if (ferror(fp))
 
1435
        {
 
1436
          ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "\"%s\": %s", current_filename, 
 
1437
               sys_errlist[errno]);
 
1438
        }
 
1439
      else
 
1440
        ctl->cmsg(CMSG_ERROR, VERB_NORMAL, 
 
1441
             "\"%s\": Not a MIDI file!", current_filename);
 
1442
      return 0;
 
1443
    }
 
1444
  if ( fread(&divisions_tmp, 2, 1, fp) != 1 )
 
1445
    {
 
1446
      if (ferror(fp))
 
1447
        {
 
1448
          ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "\"%s\": %s", current_filename, 
 
1449
               sys_errlist[errno]);
 
1450
        }
 
1451
      else
 
1452
        ctl->cmsg(CMSG_ERROR, VERB_NORMAL, 
 
1453
             "\"%s\": Not a MIDI file!", current_filename);
 
1454
      return 0;
 
1455
    }
 
1456
  /* fread(&divisions_tmp, 2, 1, fp); */
 
1457
  format=BE_SHORT(format);
 
1458
  tracks=BE_SHORT(tracks);
 
1459
  divisions_tmp=BE_SHORT(divisions_tmp);
 
1460
 
 
1461
  if (divisions_tmp<0)
 
1462
    {
 
1463
      /* SMPTE time -- totally untested. Got a MIDI file that uses this? */
 
1464
      divisions=
 
1465
        (int32)(-(divisions_tmp/256)) * (int32)(divisions_tmp & 0xFF);
 
1466
    }
 
1467
  else divisions=(int32)(divisions_tmp);
 
1468
 
 
1469
  if (len > 6)
 
1470
    {
 
1471
      ctl->cmsg(CMSG_WARNING, VERB_NORMAL, 
 
1472
           "%s: MIDI file header size %ld bytes", 
 
1473
           current_filename, len);
 
1474
      skip(fp, len-6); /* skip the excess */
 
1475
    }
 
1476
  if (format<0 || format >2)
 
1477
    {
 
1478
      ctl->cmsg(CMSG_ERROR, VERB_NORMAL, 
 
1479
           "\"%s\": Unknown MIDI file format %d", current_filename, format);
 
1480
      return 0;
 
1481
    }
 
1482
  ctl->cmsg(CMSG_INFO, VERB_VERBOSE, 
 
1483
       "Format: %d  Tracks: %d  Divisions: %d", format, tracks, divisions);
 
1484
 
 
1485
  /* Put a do-nothing event first in the list for easier processing */
 
1486
  evlist=(MidiEventList *)safe_malloc(sizeof(MidiEventList));
 
1487
  evlist->event.time=0;
 
1488
  evlist->event.type=ME_NONE;
 
1489
  evlist->next=0;
 
1490
  event_count++;
 
1491
 
 
1492
  switch(format)
 
1493
    {
 
1494
    case 0:
 
1495
      if (read_track(0))
 
1496
        {
 
1497
          free_midi_list();
 
1498
          return 0;
 
1499
        }
 
1500
      break;
 
1501
 
 
1502
    case 1:
 
1503
      for (i=0; i<tracks; i++)
 
1504
        if (read_track(0))
 
1505
          {
 
1506
            free_midi_list();
 
1507
            return 0;
 
1508
          }
 
1509
      break;
 
1510
 
 
1511
    case 2: /* We simply play the tracks sequentially */
 
1512
      for (i=0; i<tracks; i++)
 
1513
        if (read_track(1))
 
1514
          {
 
1515
            free_midi_list();
 
1516
            return 0;
 
1517
          }
 
1518
      break;
 
1519
    }
 
1520
  return groom_list(divisions, count, sp);
 
1521
}