Skip to content

Latest commit

 

History

History
620 lines (553 loc) · 15.4 KB

readmidi.c

File metadata and controls

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