2
$Id: readmidi.cpp,v 1.8 2000/01/22 17:52:22 greglee Exp $
4
TiMidity -- Experimental MIDI to WAVE converter
5
Copyright (C) 1995 Tuukka Toivonen <toivonen@clinet.fi>
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.
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.
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.
48
int32 quietchannels=0;
50
/* to avoid some unnecessary parameter passing */
51
static MidiEventList *evlist;
52
static int32 event_count;
56
/* taken from tplus --gl */
57
static int midi_port_number;
60
#define MERGE_CHANNEL_PORT(ch) ((int)(ch))
62
#define MERGE_CHANNEL_PORT(ch) ((int)(ch) | (midi_port_number << 4))
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*/
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,
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,
96
/* Computes how many (fractional) samples one MIDI delta-time unit contains */
97
static void compute_sample_increment(int32 tempo, int32 divisions)
100
a = (double) (tempo) * (double) (play_mode->rate) * (65536.0/1000000.0) /
103
sample_correction = (int32)(a) & 0xFFFF;
104
sample_increment = (int32)(a) >> 16;
106
ctl->cmsg(CMSG_INFO, VERB_DEBUG, "Samples per delta-t: %d (correction %d)",
107
sample_increment, sample_correction);
111
static int tf_getc(void)
114
if (fread(&b,1,1,fp) != 1) return EOF;
118
/* Read variable-length number (7 bits per byte, MSB first) */
119
static int32 getvl(void)
128
if((c = tf_getc()) == EOF)
130
if(!(c & 0x80)) return l | c;
131
l = (l | (c & 0x7f)) << 7;
134
if((c = tf_getc()) == EOF)
136
if(!(c & 0x80)) return l | c;
137
l = (l | (c & 0x7f)) << 7;
140
if((c = tf_getc()) == EOF)
142
if(!(c & 0x80)) return l | c;
143
l = (l | (c & 0x7f)) << 7;
146
if((c = tf_getc()) == EOF)
148
if(!(c & 0x80)) return l | c;
151
ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
152
"\"%s\": Illegal Variable-length quantity format.",
158
ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
159
"\"%s\": read_midi_event: %s",
160
current_filename, strerror(errno));
162
ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
163
"Warning: \"%s\": Short midi file.",
170
/* Read variable-length number (7 bits per byte, MSB first) */
171
static uint32 getvl(void)
179
if (!(c & 0x80)) return l;
185
/**********************************/
186
struct meta_text_type *meta_text_list = NULL;
188
static void free_metatext()
190
struct meta_text_type *meta;
191
while (meta_text_list) {
192
meta = meta_text_list;
193
meta_text_list = meta->next;
201
static int metatext(int type, uint32 leng, char *mess)
203
static int continued_flag = 0;
206
unsigned char *p = (unsigned char *)mess;
207
struct meta_text_type *meta, *mlast;
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);
217
meta_string[leng++] = '\012';
219
else for (n = 0; n < leng; n++) {
221
if (!n && (c == '/' || c == '\\')) {
223
meta_string[0] = '\012';
225
meta_string[1] = ' ';
226
meta_string[2] = ' ';
227
meta_string[3] = ' ';
228
meta_string[4] = ' ';
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';
240
if (!continued_flag) {
241
meta_string[leng++] = '\012';
243
meta_string[leng] = '\0';
245
meta->text = meta_string;
250
1. head = meta1; head->next = NULL
251
2. for ...: mlast = head
253
else ...: meta2->next = NULL
256
3. for ...: mlast = meta2
258
else: meta3->next = NULL
260
so: (meta1, meta2, meta3)
262
if (meta_text_list == NULL) {
263
meta->next = meta_text_list;
264
meta_text_list = meta;
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;
274
meta->next = mlast->next;
283
static int sysex(uint32 len, uint8 *syschan, uint8 *sysa, uint8 *sysb)
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))
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)
296
ctl->cmsg(CMSG_TEXT, VERB_VERBOSE, "GM System On", len);
303
if (id==0x7f && len==7 && port==0x7f && model==0x04 && adhi==0x01)
305
ctl->cmsg(CMSG_TEXT, VERB_DEBUG, "Master Volume %d", s[4]+(s[5]<<7));
309
return ME_MASTERVOLUME;
310
/** return s[4]+(s[5]<<7); **/
312
if (len<8) { free(s); return 0; }
314
ch = (adlo & 0x0f) | ((port & 0x03) << 4);
317
if (len >= 8) dtb=s[7];
319
if (len >= 9) dtc=s[8];
322
if (id==0x43 && model==0x4c)
324
if (!adhi && !adlo && cd==0x7e && !dta)
326
ctl->cmsg(CMSG_TEXT, VERB_VERBOSE, "XG System On", len);
329
vol_table = xg_vol_table;
332
else if (adhi == 2 && adlo == 1)
338
XG_System_reverb_type=(dta<<3)+dtb;
341
XG_System_chorus_type=((dta-64)<<3)+dtb;
344
XG_System_variation_type=dta;
347
/* dta==0 Insertion; dta==1 System */
352
else if (adhi == 8 && cd <= 40)
357
case 0x01: /* bank select MSB */
360
case 0x02: /* bank select LSB */
363
case 0x03: /* program number */
364
/** MIDIEVENT(at, ME_PROGRAM, lastchan, a, 0); **/
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);
373
case 0x0b: /* volume */
374
return ME_MAINVOLUME;
379
case 0x12: /* chorus send */
380
return ME_CHORUSDEPTH;
382
case 0x13: /* reverb send */
383
return ME_REVERBERATION;
385
case 0x14: /* variation send */
387
case 0x18: /* filter cutoff */
388
return ME_BRIGHTNESS;
390
case 0x19: /* filter resonance */
391
return ME_HARMONICCONTENT;
398
else if (id==0x41 && model==0x42 && adhi==0x12 && adlo==0x40)
401
if (!cd && dta==0x7f && !dtb && dtc==0x41)
403
ctl->cmsg(CMSG_TEXT, VERB_VERBOSE, "GS System On", len);
406
vol_table = gs_vol_table;
409
else if (dta==0x15 && (cd&0xf0)==0x10)
413
else if (chan<10) chan--;
414
chan = MERGE_CHANNEL_PORT(chan);
415
channel[chan].kit=dtb;
417
else if (cd==0x01) switch(dta)
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;
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;
451
/**********************************/
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)
458
char *s=(char *)safe_malloc(len+1);
459
if (len != fread(s, 1, len, fp))
465
if (!metatext(type, len, s))
472
ctl->cmsg(CMSG_TEXT, VERB_VERBOSE, "%s%s", label, s);
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; \
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; \
492
#define MAGIC_EOT ((MidiEventList *)(-1))
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)
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;
507
MidiEventList *newev;
512
if ( (len=getvl()) < 0) return 0;
519
if((i = tf_getc()) == EOF)
522
ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
523
"\"%s\": read_midi_event: %s",
524
current_filename, strerror(errno));
526
ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
527
"Warning: \"%s\": Short midi file.",
533
if (fread(&me,1,1,fp)!=1)
535
ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "\"%s\": read_midi_event: %s",
536
current_filename, sys_errlist[errno]);
541
if(me==0xF0 || me == 0xF7) /* SysEx event */
544
uint8 sysa=0, sysb=0, syschan=0;
546
if ( (len=getvl()) < 0) return 0;
550
sret=sysex(len, &syschan, &sysa, &sysb);
553
MIDIEVENT(at, sret, syschan, sysa, sysb);
556
else if(me==0xFF) /* Meta event */
559
if((i = tf_getc()) == EOF)
562
ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
563
"\"%s\": read_midi_event: %s",
564
current_filename, strerror(errno));
566
ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
567
"Warning: \"%s\": Short midi file.",
576
if ( (len=getvl()) < 0) return 0;
580
if (type>0 && type<16)
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);
591
case 0x21: /* MIDI port number */
594
fread(&midi_port_number,1,1,fp);
595
if(midi_port_number == EOF)
597
ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
598
"Warning: \"%s\": Short midi file.",
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;
611
case 0x2F: /* End of Track */
614
case 0x51: /* Tempo */
616
if((i = tf_getc()) == EOF)
619
ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
620
"\"%s\": read_midi_event: %s",
621
current_filename, strerror(errno));
623
ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
624
"Warning: \"%s\": Short midi file.",
629
if((i = tf_getc()) == EOF)
632
ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
633
"\"%s\": read_midi_event: %s",
634
current_filename, strerror(errno));
636
ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
637
"Warning: \"%s\": Short midi file.",
642
if((i = tf_getc()) == EOF)
645
ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
646
"\"%s\": read_midi_event: %s",
647
current_filename, strerror(errno));
649
ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
650
"Warning: \"%s\": Short midi file.",
656
fread(&a,1,1,fp); fread(&b,1,1,fp); fread(&c,1,1,fp);
658
MIDIEVENT(at, ME_TEMPO, c, a, b);
661
ctl->cmsg(CMSG_INFO, VERB_DEBUG,
662
"(Meta event type 0x%02x, length %ld)", type, len);
670
if (a & 0x80) /* status byte */
672
lastchan = MERGE_CHANNEL_PORT(a & 0x0F);
673
laststatus=(a>>4) & 0x07;
675
if((i = tf_getc()) == EOF)
678
ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
679
"\"%s\": read_midi_event: %s",
680
current_filename, strerror(errno));
682
ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
683
"Warning: \"%s\": Short midi file.",
695
case 0: /* Note off */
697
if((i = tf_getc()) == EOF)
700
ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
701
"\"%s\": read_midi_event: %s",
702
current_filename, strerror(errno));
704
ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
705
"Warning: \"%s\": Short midi file.",
714
/*MIDIEVENT(at, ME_NOTEOFF, lastchan, a,b);*/
715
MIDIEVENT(at, ME_NOTEON, lastchan, a,0);
717
case 1: /* Note on */
719
if((i = tf_getc()) == EOF)
722
ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
723
"\"%s\": read_midi_event: %s",
724
current_filename, strerror(errno));
726
ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
727
"Warning: \"%s\": Short midi file.",
736
MIDIEVENT(at, ME_NOTEON, lastchan, a,b);
738
case 2: /* Key Pressure */
740
if((i = tf_getc()) == EOF)
743
ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
744
"\"%s\": read_midi_event: %s",
745
current_filename, strerror(errno));
747
ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
748
"Warning: \"%s\": Short midi file.",
757
MIDIEVENT(at, ME_KEYPRESSURE, lastchan, a, b);
759
case 3: /* Control change */
761
if((i = tf_getc()) == EOF)
764
ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
765
"\"%s\": read_midi_event: %s",
766
current_filename, strerror(errno));
768
ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
769
"Warning: \"%s\": Short midi file.",
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;
788
case 7: control=ME_MAINVOLUME; break;
789
case 10: control=ME_PAN; break;
790
case 11: control=ME_EXPRESSION; break;
792
case 64: control=ME_SUSTAIN; b = (b >= 64); break;
794
case 64: control=ME_SUSTAIN; break;
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");
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;
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. */
821
else control=ME_TONE_BANK;
824
if (XG_System_On) control=ME_TONE_BANK;
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;
834
if (rpn_msb[lastchan]==1) switch (rpn_lsb[lastchan])
837
case 0x08: control=ME_VIBRATO_RATE; break;
838
case 0x09: control=ME_VIBRATO_DEPTH; break;
839
case 0x0a: control=ME_VIBRATO_DELAY; break;
841
case 0x20: control=ME_BRIGHTNESS; break;
842
case 0x21: control=ME_HARMONICCONTENT; break;
844
case 0x63: envelope attack rate
845
case 0x64: envelope decay rate
846
case 0x66: envelope release rate
849
else switch (rpn_msb[lastchan])
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
859
case 0x1a: drumvolume[lastchan][0x7f & rpn_lsb[lastchan]] = b; break;
861
if (!b) b=(int) (127.0*rand()/(RAND_MAX));
862
drumpanpot[lastchan][0x7f & rpn_lsb[lastchan]] = b;
864
case 0x1d: drumreverberation[lastchan][0x7f & rpn_lsb[lastchan]] = b; break;
865
case 0x1e: drumchorusdepth[lastchan][0x7f & rpn_lsb[lastchan]] = b; break;
867
case 0x1f: variation send level
870
ctl->cmsg(CMSG_INFO, VERB_DEBUG,
871
"(Data entry (MSB) for NRPN %02x,%02x: %ld)",
872
rpn_msb[lastchan], rpn_lsb[lastchan],
877
switch((rpn_msb[lastchan]<<8) | rpn_lsb[lastchan])
879
case 0x0000: /* Pitch bend sensitivity */
880
control=ME_PITCH_SENS;
883
case 0x0001: control=ME_FINE_TUNING; break;
884
case 0x0002: control=ME_COARSE_TUNING; break;
887
case 0x7F7F: /* RPN reset */
888
/* reset pitch bend sensitivity to 2 */
889
MIDIEVENT(at, ME_PITCH_SENS, lastchan, 2, 0);
892
ctl->cmsg(CMSG_INFO, VERB_DEBUG,
893
"(Data entry (MSB) for RPN %02x,%02x: %ld)",
894
rpn_msb[lastchan], rpn_lsb[lastchan],
901
ctl->cmsg(CMSG_INFO, VERB_DEBUG,
902
"(Control %d: %d)", a, b);
907
MIDIEVENT(at, control, lastchan, b, 0);
912
case 4: /* Program change */
914
MIDIEVENT(at, ME_PROGRAM, lastchan, a, 0);
917
case 5: /* Channel pressure */
918
MIDIEVENT(at, ME_CHANNEL_PRESSURE, lastchan, a, 0);
920
case 5: /* Channel pressure - NOT IMPLEMENTED */
924
case 6: /* Pitch wheel */
926
if((i = tf_getc()) == EOF)
929
ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
930
"\"%s\": read_midi_event: %s",
931
current_filename, strerror(errno));
933
ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
934
"Warning: \"%s\": Short midi file.",
943
MIDIEVENT(at, ME_PITCHWHEEL, lastchan, a, b);
949
ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
950
"*** Can't happen: status 0x%02X, channel 0x%02X",
951
laststatus, lastchan);
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)
967
MidiEventList *next, *newev;
971
midi_port_number = 0;
975
/* find the last event in the list */
976
for (; meep->next; meep=(MidiEventList *)meep->next)
983
/* Check the formalities */
985
if ((fread(tmp,1,4,fp) != 4) || (fread(&len,4,1,fp) != 1))
987
ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
988
"\"%s\": Can't read track header.", current_filename);
992
if (memcmp(tmp, "MTrk", 4))
994
ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
995
"\"%s\": Corrupt MIDI file.", current_filename);
1001
if (!(newev=read_midi_event())) /* Some kind of error */
1004
if (newev==MAGIC_EOT) /* End-of-track Hack. */
1009
next=(MidiEventList *)meep->next;
1010
while (next && (next->event.time < newev->event.time))
1013
next=(MidiEventList *)meep->next;
1019
event_count++; /* Count the event. (About one?) */
1024
/* Free the linked event list from memory. */
1025
static void free_midi_list(void)
1027
MidiEventList *meep, *next;
1028
if (!(meep=evlist)) return;
1031
next=(MidiEventList *)meep->next;
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)
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;
1051
int32 sample_cum, samples_to_do, st, dt, counting_time;
1053
struct meta_text_type *meta = meta_text_list;
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? */
1059
for (i=0; i<MAXCHAN; i++)
1062
current_banktype[i]=0;
1064
current_kit[i]=channel[i].kit;
1065
current_program[i]=default_program;
1069
compute_sample_increment(tempo, divisions);
1071
/* This may allocate a bit more than we need */
1072
groomed_list=lp=(MidiEvent *)safe_malloc(sizeof(MidiEvent) * (event_count+1));
1077
counting_time=2; /* We strip any silence before the first NOTE ON. */
1079
#ifdef POLYPHONY_COUNT
1080
current_polyphony = 0;
1082
future_interval = play_mode->rate / 2;
1085
for (i=0; i<event_count; i++)
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)
1097
if (meep->event.type==ME_TEMPO)
1101
meep->event.channel + meep->event.b * 256 + meep->event.a * 65536;
1102
compute_sample_increment(tempo, divisions);
1106
else if ((quietchannels & (1<<meep->event.channel)))
1108
else switch (meep->event.type)
1111
if (current_kit[meep->event.channel])
1113
if (current_kit[meep->event.channel]==126)
1115
/* note request for 2nd sfx rhythm kit */
1116
if (meep->event.a && drumset[SFXDRUM2])
1118
current_kit[meep->event.channel]=125;
1119
current_set[meep->event.channel]=SFXDRUM2;
1122
else if (!meep->event.a && drumset[SFXDRUM1])
1124
current_set[meep->event.channel]=SFXDRUM1;
1129
ctl->cmsg(CMSG_WARNING, VERB_VERBOSE,
1130
"XG SFX drum set is undefined");
1135
if (drumset[meep->event.a]) /* Is this a defined drumset? */
1136
new_value=meep->event.a;
1139
ctl->cmsg(CMSG_WARNING, VERB_VERBOSE,
1140
"Drum set %d is undefined", meep->event.a);
1142
new_value=meep->event.a=0;
1149
if (current_set[meep->event.channel] != new_value)
1150
current_set[meep->event.channel]=new_value;
1156
new_value=meep->event.a;
1158
if ((current_program[meep->event.channel] != SPECIAL_PROGRAM)
1159
&& (current_program[meep->event.channel] != new_value))
1161
if (current_program[meep->event.channel] != new_value)
1162
current_program[meep->event.channel] = new_value;
1169
#ifdef POLYPHONY_COUNT
1170
if (meep->event.b) current_polyphony++;
1171
else current_polyphony--;
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;
1181
if (current_kit[meep->event.channel])
1183
int dset = current_set[meep->event.channel];
1185
int dnote=meep->event.a;
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];
1193
/* Mark this instrument to be loaded */
1194
if (!(drumset[dset]->tone[meep->event.a].layer))
1196
drumset[dset]->tone[meep->event.a].layer=
1197
MAGIC_LOAD_INSTRUMENT;
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;
1206
int chan=meep->event.channel;
1209
if (current_banktype[chan]) banknum=SFXBANK;
1210
else banknum=current_bank[chan];
1212
if (current_program[chan]==SPECIAL_PROGRAM)
1214
/* Mark this instrument to be loaded */
1215
if (!(tonebank[banknum]->tone[current_program[chan]].layer))
1217
tonebank[banknum]->tone[current_program[chan]].layer=
1218
MAGIC_LOAD_INSTRUMENT;
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;
1228
if (!meep->event.a || meep->event.a == 127)
1230
new_value=meep->event.a;
1231
if (current_kit[meep->event.channel] != new_value)
1232
current_kit[meep->event.channel]=new_value;
1237
else if (meep->event.a == 126)
1239
if (drumset[SFXDRUM1]) /* Is this a defined tone bank? */
1240
new_value=meep->event.a;
1243
ctl->cmsg(CMSG_WARNING, VERB_VERBOSE,
1244
"XG rhythm kit %d is undefined", meep->event.a);
1248
current_set[meep->event.channel]=SFXDRUM1;
1249
current_kit[meep->event.channel]=new_value;
1252
else if (meep->event.a != SFX_BANKTYPE)
1254
ctl->cmsg(CMSG_WARNING, VERB_VERBOSE,
1255
"XG kit %d is impossible", meep->event.a);
1260
if (current_kit[meep->event.channel])
1265
if (tonebank[SFXBANK]) /* Is this a defined tone bank? */
1266
new_value=SFX_BANKTYPE;
1269
ctl->cmsg(CMSG_WARNING, VERB_VERBOSE,
1270
"XG Sfx bank is undefined");
1274
if (current_banktype[meep->event.channel]!=new_value)
1275
current_banktype[meep->event.channel]=new_value;
1282
if (current_kit[meep->event.channel])
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;
1293
else if (tonebank[meep->event.a]) /* Is this a defined tone bank? */
1294
new_value=meep->event.a;
1297
ctl->cmsg(CMSG_WARNING, VERB_VERBOSE,
1298
"Tone bank %d is undefined", meep->event.a);
1299
new_value=meep->event.a=0;
1302
if (current_bank[meep->event.channel]!=new_value)
1303
current_bank[meep->event.channel]=new_value;
1308
case ME_HARMONICCONTENT:
1309
channel[meep->event.channel].harmoniccontent=meep->event.a;
1312
channel[meep->event.channel].brightness=meep->event.a;
1317
/* Recompute time in samples*/
1318
if ((dt=meep->event.time - at) && !counting_time)
1320
samples_to_do=sample_increment * dt;
1321
sample_cum += sample_correction * dt;
1322
if (sample_cum & 0xFFFF0000)
1324
samples_to_do += ((sample_cum >> 16) & 0xFFFF);
1325
sample_cum &= 0x0000FFFF;
1327
st += samples_to_do;
1329
else if (counting_time==1) counting_time=0;
1331
if (meep->event.type==ME_TEMPO)
1334
meep->event.channel + meep->event.b * 256 + meep->event.a * 65536;
1335
compute_sample_increment(tempo, divisions);
1338
if (!skip_this_event)
1340
/* Add the event to the list */
1343
#ifdef POLYPHONY_COUNT
1344
while (hind_list < lp && hind_list->time <= st - future_interval)
1346
hind_list->polyphony = current_polyphony;
1353
at=meep->event.time;
1354
meep=(MidiEventList *)meep->next;
1356
/* Add an End-of-Track event */
1362
*eventsp=our_event_count;
1364
return groomed_list;
1367
MidiEvent *read_midi_file(FILE *mfp, uint32 *count, uint32 *sp)
1371
int16 format, tracks, divisions_tmp;
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++)
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;
1400
if ((fread(tmp,1,4,fp) != 4) || (fread(&len,4,1,fp) != 1))
1404
ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "\"%s\": %s", current_filename,
1405
sys_errlist[errno]);
1408
ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
1409
"\"%s\": Not a MIDI file!", current_filename);
1413
if (memcmp(tmp, "MThd", 4) || len < 6)
1415
ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
1416
"\"%s\": Not a MIDI file!", current_filename);
1420
if ( fread(&format, 2, 1, fp) != 1 )
1424
ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "\"%s\": %s", current_filename,
1425
sys_errlist[errno]);
1428
ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
1429
"\"%s\": Not a MIDI file!", current_filename);
1432
if ( fread(&tracks, 2, 1, fp) != 1 )
1436
ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "\"%s\": %s", current_filename,
1437
sys_errlist[errno]);
1440
ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
1441
"\"%s\": Not a MIDI file!", current_filename);
1444
if ( fread(&divisions_tmp, 2, 1, fp) != 1 )
1448
ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "\"%s\": %s", current_filename,
1449
sys_errlist[errno]);
1452
ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
1453
"\"%s\": Not a MIDI file!", current_filename);
1456
/* fread(&divisions_tmp, 2, 1, fp); */
1457
format=BE_SHORT(format);
1458
tracks=BE_SHORT(tracks);
1459
divisions_tmp=BE_SHORT(divisions_tmp);
1461
if (divisions_tmp<0)
1463
/* SMPTE time -- totally untested. Got a MIDI file that uses this? */
1465
(int32)(-(divisions_tmp/256)) * (int32)(divisions_tmp & 0xFF);
1467
else divisions=(int32)(divisions_tmp);
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 */
1476
if (format<0 || format >2)
1478
ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
1479
"\"%s\": Unknown MIDI file format %d", current_filename, format);
1482
ctl->cmsg(CMSG_INFO, VERB_VERBOSE,
1483
"Format: %d Tracks: %d Divisions: %d", format, tracks, divisions);
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;
1503
for (i=0; i<tracks; i++)
1511
case 2: /* We simply play the tracks sequentially */
1512
for (i=0; i<tracks; i++)
1520
return groom_list(divisions, count, sp);