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