Skip to content

Latest commit

 

History

History
1070 lines (966 loc) · 27.1 KB

readmidi.c

File metadata and controls

1070 lines (966 loc) · 27.1 KB
 
Oct 21, 1999
Oct 21, 1999
1
2
3
4
5
/*
TiMidity -- Experimental MIDI to WAVE converter
Copyright (C) 1995 Tuukka Toivonen <toivonen@clinet.fi>
This program is free software; you can redistribute it and/or modify
Dec 31, 2011
Dec 31, 2011
6
7
it under the terms of the Perl Artistic License, available in COPYING.
*/
Oct 21, 1999
Oct 21, 1999
8
9
10
11
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
Dec 17, 2001
Dec 17, 2001
12
#include <string.h>
Oct 21, 1999
Oct 21, 1999
13
Jul 4, 2005
Jul 4, 2005
14
15
#include <SDL_rwops.h>
Oct 21, 1999
Oct 21, 1999
16
17
18
19
20
21
#include "config.h"
#include "common.h"
#include "instrum.h"
#include "playmidi.h"
#include "readmidi.h"
#include "output.h"
May 14, 2006
May 14, 2006
22
#include "ctrlmode.h"
Oct 21, 1999
Oct 21, 1999
23
24
25
int32 quietchannels=0;
Aug 21, 2004
Aug 21, 2004
26
27
28
29
30
31
32
33
34
35
36
37
static int midi_port_number;
char midi_name[FILENAME_MAX+1];
static int track_info, curr_track, curr_title_track;
static char title[128];
#if MAXCHAN <= 16
#define MERGE_CHANNEL_PORT(ch) ((int)(ch))
#else
#define MERGE_CHANNEL_PORT(ch) ((int)(ch) | (midi_port_number << 4))
#endif
Oct 21, 1999
Oct 21, 1999
38
39
40
/* to avoid some unnecessary parameter passing */
static MidiEventList *evlist;
static int32 event_count;
Jul 4, 2005
Jul 4, 2005
41
static SDL_RWops *rw;
Oct 21, 1999
Oct 21, 1999
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
static int32 at;
/* These would both fit into 32 bits, but they are often added in
large multiples, so it's simpler to have two roomy ints */
static int32 sample_increment, sample_correction; /*samples per MIDI delta-t*/
/* Computes how many (fractional) samples one MIDI delta-time unit contains */
static void compute_sample_increment(int32 tempo, int32 divisions)
{
double a;
a = (double) (tempo) * (double) (play_mode->rate) * (65536.0/1000000.0) /
(double)(divisions);
sample_correction = (int32)(a) & 0xFFFF;
sample_increment = (int32)(a) >> 16;
ctl->cmsg(CMSG_INFO, VERB_DEBUG, "Samples per delta-t: %d (correction %d)",
sample_increment, sample_correction);
}
/* Read variable-length number (7 bits per byte, MSB first) */
static int32 getvl(void)
{
int32 l=0;
uint8 c;
for (;;)
{
Jul 4, 2005
Jul 4, 2005
69
SDL_RWread(rw,&c,1,1);
Oct 21, 1999
Oct 21, 1999
70
71
72
73
74
75
l += (c & 0x7f);
if (!(c & 0x80)) return l;
l<<=7;
}
}
Aug 21, 2004
Aug 21, 2004
76
Jul 4, 2005
Jul 4, 2005
77
static int sysex(uint32 len, uint8 *syschan, uint8 *sysa, uint8 *sysb, SDL_RWops *rw)
Aug 21, 2004
Aug 21, 2004
78
79
80
{
unsigned char *s=(unsigned char *)safe_malloc(len);
int id, model, ch, port, adhi, adlo, cd, dta, dtb, dtc;
May 11, 2006
May 11, 2006
81
if (len != (uint32)SDL_RWread(rw, s, 1, len))
Aug 21, 2004
Aug 21, 2004
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
{
free(s);
return 0;
}
if (len<5) { free(s); return 0; }
if (curr_track == curr_title_track && track_info > 1) title[0] = '\0';
id=s[0]; port=s[1]; model=s[2]; adhi=s[3]; adlo=s[4];
if (id==0x7e && port==0x7f && model==0x09 && adhi==0x01)
{
ctl->cmsg(CMSG_TEXT, VERB_VERBOSE, "GM System On", len);
GM_System_On=1;
free(s);
return 0;
}
ch = adlo & 0x0f;
*syschan=(uint8)ch;
if (id==0x7f && len==7 && port==0x7f && model==0x04 && adhi==0x01)
{
ctl->cmsg(CMSG_TEXT, VERB_DEBUG, "Master Volume %d", s[4]+(s[5]<<7));
*sysa = s[4];
*sysb = s[5];
Aug 25, 2011
Aug 25, 2011
103
free(s);
Aug 21, 2004
Aug 21, 2004
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
return ME_MASTERVOLUME;
/** return s[4]+(s[5]<<7); **/
}
if (len<8) { free(s); return 0; }
port &=0x0f;
ch = (adlo & 0x0f) | ((port & 0x03) << 4);
*syschan=(uint8)ch;
cd=s[5]; dta=s[6];
if (len >= 8) dtb=s[7];
else dtb=-1;
if (len >= 9) dtc=s[8];
else dtc=-1;
free(s);
if (id==0x43 && model==0x4c)
{
if (!adhi && !adlo && cd==0x7e && !dta)
{
ctl->cmsg(CMSG_TEXT, VERB_VERBOSE, "XG System On", len);
XG_System_On=1;
#ifdef tplus
vol_table = xg_vol_table;
#endif
}
else if (adhi == 2 && adlo == 1)
{
if (dtb==8) dtb=3;
switch (cd)
{
case 0x00:
XG_System_reverb_type=(dta<<3)+dtb;
break;
case 0x20:
XG_System_chorus_type=((dta-64)<<3)+dtb;
break;
case 0x40:
XG_System_variation_type=dta;
break;
case 0x5a:
/* dta==0 Insertion; dta==1 System */
break;
default: break;
}
}
else if (adhi == 8 && cd <= 40)
{
*sysa = dta & 0x7f;
switch (cd)
{
case 0x01: /* bank select MSB */
return ME_TONE_KIT;
break;
case 0x02: /* bank select LSB */
return ME_TONE_BANK;
break;
case 0x03: /* program number */
/** MIDIEVENT(d->at, ME_PROGRAM, lastchan, a, 0); **/
return ME_PROGRAM;
break;
case 0x08: /* */
/* d->channel[adlo&0x0f].transpose = (char)(dta-64); */
channel[ch].transpose = (char)(dta-64);
ctl->cmsg(CMSG_TEXT, VERB_DEBUG, "transpose channel %d by %d",
(adlo&0x0f)+1, dta-64);
break;
case 0x0b: /* volume */
return ME_MAINVOLUME;
break;
case 0x0e: /* pan */
return ME_PAN;
break;
case 0x12: /* chorus send */
return ME_CHORUSDEPTH;
break;
case 0x13: /* reverb send */
return ME_REVERBERATION;
break;
case 0x14: /* variation send */
break;
case 0x18: /* filter cutoff */
return ME_BRIGHTNESS;
break;
case 0x19: /* filter resonance */
return ME_HARMONICCONTENT;
break;
default: break;
}
}
return 0;
}
else if (id==0x41 && model==0x42 && adhi==0x12 && adlo==0x40)
{
if (dtc<0) return 0;
if (!cd && dta==0x7f && !dtb && dtc==0x41)
{
ctl->cmsg(CMSG_TEXT, VERB_VERBOSE, "GS System On", len);
GS_System_On=1;
#ifdef tplus
vol_table = gs_vol_table;
#endif
}
else if (dta==0x15 && (cd&0xf0)==0x10)
{
int chan=cd&0x0f;
if (!chan) chan=9;
else if (chan<10) chan--;
chan = MERGE_CHANNEL_PORT(chan);
channel[chan].kit=dtb;
}
else if (cd==0x01) switch(dta)
{
case 0x30:
switch(dtb)
{
case 0: XG_System_reverb_type=16+0; break;
case 1: XG_System_reverb_type=16+1; break;
case 2: XG_System_reverb_type=16+2; break;
case 3: XG_System_reverb_type= 8+0; break;
case 4: XG_System_reverb_type= 8+1; break;
case 5: XG_System_reverb_type=32+0; break;
case 6: XG_System_reverb_type=8*17; break;
case 7: XG_System_reverb_type=8*18; break;
}
break;
case 0x38:
switch(dtb)
{
case 0: XG_System_chorus_type= 8+0; break;
case 1: XG_System_chorus_type= 8+1; break;
case 2: XG_System_chorus_type= 8+2; break;
case 3: XG_System_chorus_type= 8+4; break;
case 4: XG_System_chorus_type= -1; break;
case 5: XG_System_chorus_type= 8*3; break;
case 6: XG_System_chorus_type= -1; break;
case 7: XG_System_chorus_type= -1; break;
}
break;
}
return 0;
}
return 0;
}
Oct 21, 1999
Oct 21, 1999
246
247
/* Print a string from the file, followed by a newline. Any non-ASCII
or unprintable characters will be converted to periods. */
Oct 10, 2009
Oct 10, 2009
248
static int dumpstring(int32 len, const char *label)
Oct 21, 1999
Oct 21, 1999
249
250
{
signed char *s=safe_malloc(len+1);
Jul 4, 2005
Jul 4, 2005
251
if (len != (int32)SDL_RWread(rw, s, 1, len))
Oct 21, 1999
Oct 21, 1999
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
{
free(s);
return -1;
}
s[len]='\0';
while (len--)
{
if (s[len]<32)
s[len]='.';
}
ctl->cmsg(CMSG_TEXT, VERB_VERBOSE, "%s%s", label, s);
free(s);
return 0;
}
#define MIDIEVENT(at,t,ch,pa,pb) \
new=safe_malloc(sizeof(MidiEventList)); \
new->event.time=at; new->event.type=t; new->event.channel=ch; \
new->event.a=pa; new->event.b=pb; new->next=0;\
return new;
#define MAGIC_EOT ((MidiEventList *)(-1))
/* Read a MIDI event, returning a freshly allocated element that can
be linked to the event list */
static MidiEventList *read_midi_event(void)
{
static uint8 laststatus, lastchan;
static uint8 nrpn=0, rpn_msb[16], rpn_lsb[16]; /* one per channel */
uint8 me, type, a,b,c;
int32 len;
MidiEventList *new;
for (;;)
{
at+=getvl();
Jul 4, 2005
Jul 4, 2005
288
if (SDL_RWread(rw,&me,1,1)!=1)
Oct 21, 1999
Oct 21, 1999
289
290
291
292
293
294
295
296
{
ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "%s: read_midi_event: %s",
current_filename, strerror(errno));
return 0;
}
if(me==0xF0 || me == 0xF7) /* SysEx event */
{
Aug 21, 2004
Aug 21, 2004
297
298
299
int32 sret;
uint8 sysa=0, sysb=0, syschan=0;
Oct 21, 1999
Oct 21, 1999
300
len=getvl();
Jul 4, 2005
Jul 4, 2005
301
sret=sysex(len, &syschan, &sysa, &sysb, rw);
Aug 21, 2004
Aug 21, 2004
302
303
304
305
if (sret)
{
MIDIEVENT(at, sret, syschan, sysa, sysb);
}
Oct 21, 1999
Oct 21, 1999
306
307
308
}
else if(me==0xFF) /* Meta event */
{
Jul 4, 2005
Jul 4, 2005
309
SDL_RWread(rw,&type,1,1);
Oct 21, 1999
Oct 21, 1999
310
311
312
313
314
315
316
317
318
319
320
len=getvl();
if (type>0 && type<16)
{
static char *label[]={
"Text event: ", "Text: ", "Copyright: ", "Track name: ",
"Instrument: ", "Lyric: ", "Marker: ", "Cue point: "};
dumpstring(len, label[(type>7) ? 0 : type]);
}
else
switch(type)
{
Aug 21, 2004
Aug 21, 2004
321
322
323
324
case 0x21: /* MIDI port number */
if(len == 1)
{
Jul 4, 2005
Jul 4, 2005
325
SDL_RWread(rw,&midi_port_number,1,1);
Aug 21, 2004
Aug 21, 2004
326
327
328
329
330
331
332
333
334
335
336
337
338
if(midi_port_number == EOF)
{
ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
"Warning: \"%s\": Short midi file.",
midi_name);
return 0;
}
midi_port_number &= 0x0f;
if (midi_port_number)
ctl->cmsg(CMSG_INFO, VERB_VERBOSE,
"(MIDI port number %d)", midi_port_number);
midi_port_number &= 0x03;
}
Nov 8, 2009
Nov 8, 2009
339
else SDL_RWseek(rw, len, RW_SEEK_CUR);
Aug 21, 2004
Aug 21, 2004
340
341
break;
Oct 21, 1999
Oct 21, 1999
342
343
344
345
case 0x2F: /* End of Track */
return MAGIC_EOT;
case 0x51: /* Tempo */
Jul 4, 2005
Jul 4, 2005
346
SDL_RWread(rw,&a,1,1); SDL_RWread(rw,&b,1,1); SDL_RWread(rw,&c,1,1);
Oct 21, 1999
Oct 21, 1999
347
348
349
350
351
MIDIEVENT(at, ME_TEMPO, c, a, b);
default:
ctl->cmsg(CMSG_INFO, VERB_DEBUG,
"(Meta event type 0x%02x, length %ld)", type, len);
Nov 8, 2009
Nov 8, 2009
352
SDL_RWseek(rw, len, RW_SEEK_CUR);
Oct 21, 1999
Oct 21, 1999
353
354
355
356
357
358
359
360
361
362
break;
}
}
else
{
a=me;
if (a & 0x80) /* status byte */
{
lastchan=a & 0x0F;
laststatus=(a>>4) & 0x07;
Jul 4, 2005
Jul 4, 2005
363
SDL_RWread(rw,&a, 1,1);
Oct 21, 1999
Oct 21, 1999
364
365
366
367
368
a &= 0x7F;
}
switch(laststatus)
{
case 0: /* Note off */
Jul 4, 2005
Jul 4, 2005
369
SDL_RWread(rw,&b, 1,1);
Oct 21, 1999
Oct 21, 1999
370
371
372
373
b &= 0x7F;
MIDIEVENT(at, ME_NOTEOFF, lastchan, a,b);
case 1: /* Note on */
Jul 4, 2005
Jul 4, 2005
374
SDL_RWread(rw,&b, 1,1);
Oct 21, 1999
Oct 21, 1999
375
b &= 0x7F;
Aug 21, 2004
Aug 21, 2004
376
if (curr_track == curr_title_track && track_info > 1) title[0] = '\0';
Oct 21, 1999
Oct 21, 1999
377
378
MIDIEVENT(at, ME_NOTEON, lastchan, a,b);
Aug 21, 2004
Aug 21, 2004
379
Oct 21, 1999
Oct 21, 1999
380
case 2: /* Key Pressure */
Jul 4, 2005
Jul 4, 2005
381
SDL_RWread(rw,&b, 1,1);
Oct 21, 1999
Oct 21, 1999
382
383
384
385
b &= 0x7F;
MIDIEVENT(at, ME_KEYPRESSURE, lastchan, a, b);
case 3: /* Control change */
Jul 4, 2005
Jul 4, 2005
386
SDL_RWread(rw,&b, 1,1);
Oct 21, 1999
Oct 21, 1999
387
388
389
390
391
392
393
394
395
b &= 0x7F;
{
int control=255;
switch(a)
{
case 7: control=ME_MAINVOLUME; break;
case 10: control=ME_PAN; break;
case 11: control=ME_EXPRESSION; break;
case 64: control=ME_SUSTAIN; break;
Aug 21, 2004
Aug 21, 2004
396
397
398
399
400
401
402
403
case 71: control=ME_HARMONICCONTENT; break;
case 72: control=ME_RELEASETIME; break;
case 73: control=ME_ATTACKTIME; break;
case 74: control=ME_BRIGHTNESS; break;
case 91: control=ME_REVERBERATION; break;
case 93: control=ME_CHORUSDEPTH; break;
Oct 21, 1999
Oct 21, 1999
404
405
406
407
408
409
410
411
412
413
case 120: control=ME_ALL_SOUNDS_OFF; break;
case 121: control=ME_RESET_CONTROLLERS; break;
case 123: control=ME_ALL_NOTES_OFF; break;
/* These should be the SCC-1 tone bank switch
commands. I don't know why there are two, or
why the latter only allows switching to bank 0.
Also, some MIDI files use 0 as some sort of
continuous controller. This will cause lots of
warnings about undefined tone banks. */
Aug 21, 2004
Aug 21, 2004
414
415
416
case 0: if (XG_System_On) control = ME_TONE_KIT; else control=ME_TONE_BANK; break;
case 32: if (XG_System_On) control = ME_TONE_BANK; break;
Oct 21, 1999
Oct 21, 1999
417
418
419
420
421
422
423
424
425
case 100: nrpn=0; rpn_msb[lastchan]=b; break;
case 101: nrpn=0; rpn_lsb[lastchan]=b; break;
case 99: nrpn=1; rpn_msb[lastchan]=b; break;
case 98: nrpn=1; rpn_lsb[lastchan]=b; break;
case 6:
if (nrpn)
{
Aug 21, 2004
Aug 21, 2004
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
if (rpn_msb[lastchan]==1) switch (rpn_lsb[lastchan])
{
#ifdef tplus
case 0x08: control=ME_VIBRATO_RATE; break;
case 0x09: control=ME_VIBRATO_DEPTH; break;
case 0x0a: control=ME_VIBRATO_DELAY; break;
#endif
case 0x20: control=ME_BRIGHTNESS; break;
case 0x21: control=ME_HARMONICCONTENT; break;
/*
case 0x63: envelope attack rate
case 0x64: envelope decay rate
case 0x66: envelope release rate
*/
}
else switch (rpn_msb[lastchan])
{
/*
case 0x14: filter cutoff frequency
case 0x15: filter resonance
case 0x16: envelope attack rate
case 0x17: envelope decay rate
case 0x18: pitch coarse
case 0x19: pitch fine
*/
case 0x1a: drumvolume[lastchan][0x7f & rpn_lsb[lastchan]] = b; break;
case 0x1c:
if (!b) b=(int) (127.0*rand()/(RAND_MAX));
drumpanpot[lastchan][0x7f & rpn_lsb[lastchan]] = b;
break;
case 0x1d: drumreverberation[lastchan][0x7f & rpn_lsb[lastchan]] = b; break;
case 0x1e: drumchorusdepth[lastchan][0x7f & rpn_lsb[lastchan]] = b; break;
/*
case 0x1f: variation send level
*/
}
Oct 21, 1999
Oct 21, 1999
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
ctl->cmsg(CMSG_INFO, VERB_DEBUG,
"(Data entry (MSB) for NRPN %02x,%02x: %ld)",
rpn_msb[lastchan], rpn_lsb[lastchan],
b);
break;
}
switch((rpn_msb[lastchan]<<8) | rpn_lsb[lastchan])
{
case 0x0000: /* Pitch bend sensitivity */
control=ME_PITCH_SENS;
break;
case 0x7F7F: /* RPN reset */
/* reset pitch bend sensitivity to 2 */
MIDIEVENT(at, ME_PITCH_SENS, lastchan, 2, 0);
default:
ctl->cmsg(CMSG_INFO, VERB_DEBUG,
"(Data entry (MSB) for RPN %02x,%02x: %ld)",
rpn_msb[lastchan], rpn_lsb[lastchan],
b);
break;
}
break;
default:
ctl->cmsg(CMSG_INFO, VERB_DEBUG,
"(Control %d: %d)", a, b);
break;
}
if (control != 255)
{
MIDIEVENT(at, control, lastchan, b, 0);
}
}
break;
case 4: /* Program change */
a &= 0x7f;
MIDIEVENT(at, ME_PROGRAM, lastchan, a, 0);
case 5: /* Channel pressure - NOT IMPLEMENTED */
break;
case 6: /* Pitch wheel */
Jul 4, 2005
Jul 4, 2005
509
SDL_RWread(rw,&b, 1,1);
Oct 21, 1999
Oct 21, 1999
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
b &= 0x7F;
MIDIEVENT(at, ME_PITCHWHEEL, lastchan, a, b);
default:
ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
"*** Can't happen: status 0x%02X, channel 0x%02X",
laststatus, lastchan);
break;
}
}
}
return new;
}
#undef MIDIEVENT
/* Read a midi track into the linked list, either merging with any previous
tracks or appending to them. */
static int read_track(int append)
{
MidiEventList *meep;
MidiEventList *next, *new;
Jan 1, 2012
Jan 1, 2012
533
int32 len, next_pos, pos;
Oct 21, 1999
Oct 21, 1999
534
535
536
537
538
539
540
541
542
543
544
545
546
547
char tmp[4];
meep=evlist;
if (append && meep)
{
/* find the last event in the list */
for (; meep->next; meep=meep->next)
;
at=meep->event.time;
}
else
at=0;
/* Check the formalities */
Jul 4, 2005
Jul 4, 2005
548
if ((SDL_RWread(rw,tmp,1,4) != 4) || (SDL_RWread(rw,&len,4,1) != 1))
Oct 21, 1999
Oct 21, 1999
549
550
551
552
553
554
{
ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
"%s: Can't read track header.", current_filename);
return -1;
}
len=BE_LONG(len);
Jan 1, 2012
Jan 1, 2012
555
next_pos = SDL_RWtell(rw) + len;
Oct 21, 1999
Oct 21, 1999
556
557
558
559
560
561
562
563
564
565
566
567
568
569
if (memcmp(tmp, "MTrk", 4))
{
ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
"%s: Corrupt MIDI file.", current_filename);
return -2;
}
for (;;)
{
if (!(new=read_midi_event())) /* Some kind of error */
return -2;
if (new==MAGIC_EOT) /* End-of-track Hack. */
{
Jan 1, 2012
Jan 1, 2012
570
571
572
pos = SDL_RWtell(rw);
if (pos < next_pos)
SDL_RWseek(rw, next_pos - pos, RW_SEEK_CUR);
Oct 21, 1999
Oct 21, 1999
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
return 0;
}
next=meep->next;
while (next && (next->event.time < new->event.time))
{
meep=next;
next=meep->next;
}
new->next=next;
meep->next=new;
event_count++; /* Count the event. (About one?) */
meep=new;
}
}
/* Free the linked event list from memory. */
static void free_midi_list(void)
{
MidiEventList *meep, *next;
if (!(meep=evlist)) return;
while (meep)
{
next=meep->next;
free(meep);
meep=next;
}
evlist=0;
}
Aug 21, 2004
Aug 21, 2004
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
static void xremap_percussion(int *banknumpt, int *this_notept, int this_kit) {
int i, newmap;
int banknum = *banknumpt;
int this_note = *this_notept;
int newbank, newnote;
if (this_kit != 127 && this_kit != 126) return;
for (i = 0; i < XMAPMAX; i++) {
newmap = xmap[i][0];
if (!newmap) return;
if (this_kit == 127 && newmap != XGDRUM) continue;
if (this_kit == 126 && newmap != SFXDRUM1) continue;
if (xmap[i][1] != banknum) continue;
if (xmap[i][3] != this_note) continue;
newbank = xmap[i][2];
newnote = xmap[i][4];
if (newbank == banknum && newnote == this_note) return;
if (!drumset[newbank]) return;
*banknumpt = newbank;
*this_notept = newnote;
return;
}
}
Oct 21, 1999
Oct 21, 1999
631
632
633
634
635
636
637
638
639
640
641
/* Allocate an array of MidiEvents and fill it from the linked list of
events, marking used instruments for loading. Convert event times to
samples: handle tempo changes. Strip unnecessary events from the list.
Free the linked list. */
static MidiEvent *groom_list(int32 divisions,int32 *eventsp,int32 *samplesp)
{
MidiEvent *groomed_list, *lp;
MidiEventList *meep;
int32 i, our_event_count, tempo, skip_this_event, new_value;
int32 sample_cum, samples_to_do, at, st, dt, counting_time;
Aug 21, 2004
Aug 21, 2004
642
643
int current_bank[MAXCHAN], current_banktype[MAXCHAN], current_set[MAXCHAN],
current_kit[MAXCHAN], current_program[MAXCHAN];
Oct 21, 1999
Oct 21, 1999
644
/* Or should each bank have its own current program? */
Aug 21, 2004
Aug 21, 2004
645
int dset, dnote, drumsflag, mprog;
Oct 21, 1999
Oct 21, 1999
646
Aug 21, 2004
Aug 21, 2004
647
for (i=0; i<MAXCHAN; i++)
Oct 21, 1999
Oct 21, 1999
648
649
{
current_bank[i]=0;
Aug 21, 2004
Aug 21, 2004
650
current_banktype[i]=0;
Oct 21, 1999
Oct 21, 1999
651
current_set[i]=0;
Aug 21, 2004
Aug 21, 2004
652
current_kit[i]=channel[i].kit;
Oct 21, 1999
Oct 21, 1999
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
current_program[i]=default_program;
}
tempo=500000;
compute_sample_increment(tempo, divisions);
/* This may allocate a bit more than we need */
groomed_list=lp=safe_malloc(sizeof(MidiEvent) * (event_count+1));
meep=evlist;
our_event_count=0;
st=at=sample_cum=0;
counting_time=2; /* We strip any silence before the first NOTE ON. */
for (i=0; i<event_count; i++)
{
skip_this_event=0;
ctl->cmsg(CMSG_INFO, VERB_DEBUG_SILLY,
"%6d: ch %2d: event %d (%d,%d)",
meep->event.time, meep->event.channel + 1,
meep->event.type, meep->event.a, meep->event.b);
if (meep->event.type==ME_TEMPO)
{
tempo=
meep->event.channel + meep->event.b * 256 + meep->event.a * 65536;
compute_sample_increment(tempo, divisions);
skip_this_event=1;
}
else if ((quietchannels & (1<<meep->event.channel)))
skip_this_event=1;
else switch (meep->event.type)
{
case ME_PROGRAM:
Aug 21, 2004
Aug 21, 2004
687
688
if (current_kit[meep->event.channel])
Oct 21, 1999
Oct 21, 1999
689
{
Aug 21, 2004
Aug 21, 2004
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
if (current_kit[meep->event.channel]==126)
{
/* note request for 2nd sfx rhythm kit */
if (meep->event.a && drumset[SFXDRUM2])
{
current_kit[meep->event.channel]=125;
current_set[meep->event.channel]=SFXDRUM2;
new_value=SFXDRUM2;
}
else if (!meep->event.a && drumset[SFXDRUM1])
{
current_set[meep->event.channel]=SFXDRUM1;
new_value=SFXDRUM1;
}
else
{
ctl->cmsg(CMSG_WARNING, VERB_VERBOSE,
"XG SFX drum set is undefined");
skip_this_event=1;
break;
}
}
Oct 21, 1999
Oct 21, 1999
712
713
714
715
716
717
if (drumset[meep->event.a]) /* Is this a defined drumset? */
new_value=meep->event.a;
else
{
ctl->cmsg(CMSG_WARNING, VERB_VERBOSE,
"Drum set %d is undefined", meep->event.a);
Aug 21, 2004
Aug 21, 2004
718
719
720
721
722
723
724
if (drumset[0])
new_value=meep->event.a=0;
else
{
skip_this_event=1;
break;
}
Oct 21, 1999
Oct 21, 1999
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
}
if (current_set[meep->event.channel] != new_value)
current_set[meep->event.channel]=new_value;
else
skip_this_event=1;
}
else
{
new_value=meep->event.a;
if ((current_program[meep->event.channel] != SPECIAL_PROGRAM)
&& (current_program[meep->event.channel] != new_value))
current_program[meep->event.channel] = new_value;
else
skip_this_event=1;
}
break;
case ME_NOTEON:
if (counting_time)
counting_time=1;
Aug 21, 2004
Aug 21, 2004
745
746
747
748
drumsflag = current_kit[meep->event.channel];
if (drumsflag) /* percussion channel? */
Oct 21, 1999
Oct 21, 1999
749
{
Aug 21, 2004
Aug 21, 2004
750
751
752
753
754
755
756
757
dset = current_set[meep->event.channel];
dnote=meep->event.a;
if (XG_System_On) xremap_percussion(&dset, &dnote, drumsflag);
/*if (current_config_pc42b) pcmap(&dset, &dnote, &mprog, &drumsflag);*/
if (drumsflag)
{
Oct 21, 1999
Oct 21, 1999
758
/* Mark this instrument to be loaded */
Aug 21, 2004
Aug 21, 2004
759
760
761
if (!(drumset[dset]->tone[dnote].layer))
{
drumset[dset]->tone[dnote].layer=
Oct 21, 1999
Oct 21, 1999
762
MAGIC_LOAD_INSTRUMENT;
Aug 21, 2004
Aug 21, 2004
763
764
765
766
767
768
}
else drumset[dset]->tone[dnote].last_used
= current_tune_number;
if (!channel[meep->event.channel].name) channel[meep->event.channel].name=
drumset[dset]->name;
}
Oct 21, 1999
Oct 21, 1999
769
}
Aug 21, 2004
Aug 21, 2004
770
771
if (!drumsflag) /* not percussion */
Oct 21, 1999
Oct 21, 1999
772
{
Aug 21, 2004
Aug 21, 2004
773
774
775
776
777
778
779
780
781
int chan=meep->event.channel;
int banknum;
if (current_banktype[chan]) banknum=SFXBANK;
else banknum=current_bank[chan];
mprog = current_program[chan];
if (mprog==SPECIAL_PROGRAM)
Oct 21, 1999
Oct 21, 1999
782
break;
Aug 21, 2004
Aug 21, 2004
783
784
785
786
787
788
789
790
if (XG_System_On && banknum==SFXBANK && !tonebank[SFXBANK] && tonebank[120])
banknum = 120;
/*if (current_config_pc42b) pcmap(&banknum, &dnote, &mprog, &drumsflag);*/
if (drumsflag)
{
Oct 21, 1999
Oct 21, 1999
791
/* Mark this instrument to be loaded */
Aug 21, 2004
Aug 21, 2004
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
if (!(drumset[dset]->tone[dnote].layer))
{
drumset[dset]->tone[dnote].layer=MAGIC_LOAD_INSTRUMENT;
}
else drumset[dset]->tone[dnote].last_used = current_tune_number;
if (!channel[meep->event.channel].name) channel[meep->event.channel].name=
drumset[dset]->name;
}
if (!drumsflag)
{
/* Mark this instrument to be loaded */
if (!(tonebank[banknum]->tone[mprog].layer))
{
tonebank[banknum]->tone[mprog].layer=MAGIC_LOAD_INSTRUMENT;
}
else tonebank[banknum]->tone[mprog].last_used = current_tune_number;
if (!channel[meep->event.channel].name) channel[meep->event.channel].name=
tonebank[banknum]->tone[mprog].name;
}
}
break;
case ME_TONE_KIT:
if (!meep->event.a || meep->event.a == 127)
{
new_value=meep->event.a;
if (current_kit[meep->event.channel] != new_value)
current_kit[meep->event.channel]=new_value;
else
skip_this_event=1;
break;
}
else if (meep->event.a == 126)
{
if (drumset[SFXDRUM1]) /* Is this a defined tone bank? */
new_value=meep->event.a;
else
{
ctl->cmsg(CMSG_WARNING, VERB_VERBOSE,
"XG rhythm kit %d is undefined", meep->event.a);
skip_this_event=1;
break;
}
current_set[meep->event.channel]=SFXDRUM1;
current_kit[meep->event.channel]=new_value;
break;
}
else if (meep->event.a != SFX_BANKTYPE)
{
ctl->cmsg(CMSG_WARNING, VERB_VERBOSE,
"XG kit %d is impossible", meep->event.a);
skip_this_event=1;
break;
}
if (current_kit[meep->event.channel])
{
skip_this_event=1;
break;
}
if (tonebank[SFXBANK] || tonebank[120]) /* Is this a defined tone bank? */
new_value=SFX_BANKTYPE;
else
{
ctl->cmsg(CMSG_WARNING, VERB_VERBOSE,
"XG Sfx bank is undefined");
skip_this_event=1;
break;
Oct 21, 1999
Oct 21, 1999
860
}
Aug 21, 2004
Aug 21, 2004
861
862
863
864
if (current_banktype[meep->event.channel]!=new_value)
current_banktype[meep->event.channel]=new_value;
else
skip_this_event=1;
Oct 21, 1999
Oct 21, 1999
865
866
867
break;
case ME_TONE_BANK:
Aug 21, 2004
Aug 21, 2004
868
if (current_kit[meep->event.channel])
Oct 21, 1999
Oct 21, 1999
869
870
871
872
{
skip_this_event=1;
break;
}
Aug 21, 2004
Aug 21, 2004
873
874
875
876
877
878
879
if (XG_System_On && meep->event.a > 0 && meep->event.a < 48) {
channel[meep->event.channel].variationbank=meep->event.a;
ctl->cmsg(CMSG_WARNING, VERB_VERBOSE,
"XG variation bank %d", meep->event.a);
new_value=meep->event.a=0;
}
else if (tonebank[meep->event.a]) /* Is this a defined tone bank? */
Oct 21, 1999
Oct 21, 1999
880
881
882
883
884
885
886
new_value=meep->event.a;
else
{
ctl->cmsg(CMSG_WARNING, VERB_VERBOSE,
"Tone bank %d is undefined", meep->event.a);
new_value=meep->event.a=0;
}
Aug 21, 2004
Aug 21, 2004
887
Oct 21, 1999
Oct 21, 1999
888
889
890
891
892
if (current_bank[meep->event.channel]!=new_value)
current_bank[meep->event.channel]=new_value;
else
skip_this_event=1;
break;
Aug 21, 2004
Aug 21, 2004
893
894
895
896
897
898
899
900
case ME_HARMONICCONTENT:
channel[meep->event.channel].harmoniccontent=meep->event.a;
break;
case ME_BRIGHTNESS:
channel[meep->event.channel].brightness=meep->event.a;
break;
Oct 21, 1999
Oct 21, 1999
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
}
/* Recompute time in samples*/
if ((dt=meep->event.time - at) && !counting_time)
{
samples_to_do=sample_increment * dt;
sample_cum += sample_correction * dt;
if (sample_cum & 0xFFFF0000)
{
samples_to_do += ((sample_cum >> 16) & 0xFFFF);
sample_cum &= 0x0000FFFF;
}
st += samples_to_do;
}
else if (counting_time==1) counting_time=0;
if (!skip_this_event)
{
/* Add the event to the list */
*lp=meep->event;
lp->time=st;
lp++;
our_event_count++;
}
at=meep->event.time;
meep=meep->next;
}
/* Add an End-of-Track event */
lp->time=st;
lp->type=ME_EOT;
our_event_count++;
free_midi_list();
*eventsp=our_event_count;
*samplesp=st;
return groomed_list;
}
Jul 4, 2005
Jul 4, 2005
938
MidiEvent *read_midi_file(SDL_RWops *mrw, int32 *count, int32 *sp)
Oct 21, 1999
Oct 21, 1999
939
940
941
942
943
944
{
int32 len, divisions;
int16 format, tracks, divisions_tmp;
int i;
char tmp[4];
Jul 4, 2005
Jul 4, 2005
945
rw = mrw;
Oct 21, 1999
Oct 21, 1999
946
947
948
949
event_count=0;
at=0;
evlist=0;
Aug 21, 2004
Aug 21, 2004
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
GM_System_On=GS_System_On=XG_System_On=0;
/* vol_table = def_vol_table; */
XG_System_reverb_type=XG_System_chorus_type=XG_System_variation_type=0;
memset(&drumvolume,-1,sizeof(drumvolume));
memset(&drumchorusdepth,-1,sizeof(drumchorusdepth));
memset(&drumreverberation,-1,sizeof(drumreverberation));
memset(&drumpanpot,NO_PANNING,sizeof(drumpanpot));
for (i=0; i<MAXCHAN; i++)
{
if (ISDRUMCHANNEL(i)) channel[i].kit = 127;
else channel[i].kit = 0;
channel[i].brightness = 64;
channel[i].harmoniccontent = 64;
channel[i].variationbank = 0;
channel[i].chorusdepth = 0;
channel[i].reverberation = 0;
channel[i].transpose = 0;
}
past_riff:
Jul 4, 2005
Jul 4, 2005
972
if ((SDL_RWread(rw,tmp,1,4) != 4) || (SDL_RWread(rw,&len,4,1) != 1))
Oct 21, 1999
Oct 21, 1999
973
{
Jul 4, 2005
Jul 4, 2005
974
/* if (ferror(fp))
Oct 21, 1999
Oct 21, 1999
975
976
977
978
{
ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "%s: %s", current_filename,
strerror(errno));
}
Jul 4, 2005
Jul 4, 2005
979
else*/
Oct 21, 1999
Oct 21, 1999
980
981
982
983
984
ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
"%s: Not a MIDI file!", current_filename);
return 0;
}
len=BE_LONG(len);
Aug 21, 2004
Aug 21, 2004
985
986
987
if (!memcmp(tmp, "RIFF", 4))
{
Jul 4, 2005
Jul 4, 2005
988
SDL_RWread(rw,tmp,1,12);
Aug 21, 2004
Aug 21, 2004
989
990
goto past_riff;
}
Oct 21, 1999
Oct 21, 1999
991
992
993
994
995
996
997
if (memcmp(tmp, "MThd", 4) || len < 6)
{
ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
"%s: Not a MIDI file!", current_filename);
return 0;
}
Jul 4, 2005
Jul 4, 2005
998
999
1000
SDL_RWread(rw,&format, 2, 1);
SDL_RWread(rw,&tracks, 2, 1);
SDL_RWread(rw,&divisions_tmp, 2, 1);