comparison src/Input/timidity/libtimidity/readmidi.c @ 0:13389e613d67 trunk

[svn] - initial import of audacious-plugins tree (lots to do)
author nenolod
date Mon, 18 Sep 2006 01:11:49 -0700
parents
children 088092a52fea
comparison
equal deleted inserted replaced
-1:000000000000 0:13389e613d67
1 /*
2
3 TiMidity -- Experimental MIDI to WAVE converter
4 Copyright (C) 1995 Tuukka Toivonen <toivonen@clinet.fi>
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19
20 */
21
22 #if HAVE_CONFIG_H
23 # include <config.h>
24 #endif
25
26 #include "libaudacious/vfs.h"
27 #include <stdlib.h>
28 #include <string.h>
29
30 #include "timidity.h"
31 #include "timidity_internal.h"
32 #include "options.h"
33 #include "common.h"
34 #include "instrum.h"
35 #include "playmidi.h"
36
37 /* Computes how many (fractional) samples one MIDI delta-time unit contains */
38 static void compute_sample_increment(MidSong *song, sint32 tempo,
39 sint32 divisions)
40 {
41 double a;
42 a = (double) (tempo) * (double) (song->rate) * (65536.0/1000000.0) /
43 (double)(divisions);
44
45 song->sample_correction = (sint32)(a) & 0xFFFF;
46 song->sample_increment = (sint32)(a) >> 16;
47
48 DEBUG_MSG("Samples per delta-t: %d (correction %d)\n",
49 song->sample_increment, song->sample_correction);
50 }
51
52 /* Read variable-length number (7 bits per byte, MSB first) */
53 static sint32 getvl(MidIStream *stream)
54 {
55 sint32 l=0;
56 uint8 c;
57 for (;;)
58 {
59 mid_istream_read(stream, &c, 1, 1);
60 l += (c & 0x7f);
61 if (!(c & 0x80)) return l;
62 l<<=7;
63 }
64 }
65
66 /* Print a string from the file, followed by a newline. Any non-ASCII
67 or unprintable characters will be converted to periods. */
68 static int read_meta_data(MidIStream *stream, sint32 len, uint8 type, MidSong *song)
69 {
70 char *s=safe_malloc(len+1);
71 MidSongMetaId id;
72
73 if (len != (sint32) mid_istream_read(stream, s, 1, len))
74 {
75 free(s);
76 return -1;
77 }
78 s[len]='\0';
79 while (len--)
80 {
81 if (((unsigned char)s[len])<32)
82 s[len]='.';
83 }
84
85 switch (type)
86 {
87 case 1: id = MID_SONG_TEXT; break;
88 case 2: id = MID_SONG_COPYRIGHT; break;
89 default: free(s); s = NULL;
90 }
91 if (s)
92 {
93 if (song->meta_data[id])
94 free(song->meta_data[id]);
95 song->meta_data[id] = s;
96 }
97
98 return 0;
99 }
100
101 #define MIDIEVENT(at,t,ch,pa,pb) \
102 new=safe_malloc(sizeof(MidEventList)); \
103 new->event.time=at; new->event.type=t; new->event.channel=ch; \
104 new->event.a=pa; new->event.b=pb; new->next=0;\
105 return new;
106
107 #define MAGIC_EOT ((MidEventList *)(-1))
108
109 /* Read a MIDI event, returning a freshly allocated element that can
110 be linked to the event list */
111 static MidEventList *read_midi_event(MidIStream *stream, MidSong *song)
112 {
113 static uint8 laststatus, lastchan;
114 static uint8 nrpn=0, rpn_msb[16], rpn_lsb[16]; /* one per channel */
115 uint8 me, type, a,b,c;
116 sint32 len;
117 MidEventList *new;
118
119 for (;;)
120 {
121 song->at += getvl(stream);
122 if (mid_istream_read(stream, &me, 1, 1) != 1)
123 {
124 DEBUG_MSG("read_midi_event: mid_istream_read() failure\n");
125 return 0;
126 }
127
128 if(me==0xF0 || me == 0xF7) /* SysEx event */
129 {
130 len=getvl(stream);
131 mid_istream_skip(stream, len);
132 }
133 else if(me==0xFF) /* Meta event */
134 {
135 mid_istream_read(stream, &type, 1, 1);
136 len=getvl(stream);
137 if (type>0 && type<16)
138 {
139 read_meta_data(stream, len, type, song);
140 }
141 else
142 switch(type)
143 {
144 case 0x2F: /* End of Track */
145 return MAGIC_EOT;
146
147 case 0x51: /* Tempo */
148 mid_istream_read(stream, &a, 1, 1);
149 mid_istream_read(stream, &b, 1, 1);
150 mid_istream_read(stream, &c, 1, 1);
151 MIDIEVENT(song->at, ME_TEMPO, c, a, b);
152
153 default:
154 DEBUG_MSG("(Meta event type 0x%02x, length %d)\n", type, len);
155 mid_istream_skip(stream, len);
156 break;
157 }
158 }
159 else
160 {
161 a=me;
162 if (a & 0x80) /* status byte */
163 {
164 lastchan=a & 0x0F;
165 laststatus=(a>>4) & 0x07;
166 mid_istream_read(stream, &a, 1, 1);
167 a &= 0x7F;
168 }
169 switch(laststatus)
170 {
171 case 0: /* Note off */
172 mid_istream_read(stream, &b, 1, 1);
173 b &= 0x7F;
174 MIDIEVENT(song->at, ME_NOTEOFF, lastchan, a,b);
175
176 case 1: /* Note on */
177 mid_istream_read(stream, &b, 1, 1);
178 b &= 0x7F;
179 MIDIEVENT(song->at, ME_NOTEON, lastchan, a,b);
180
181 case 2: /* Key Pressure */
182 mid_istream_read(stream, &b, 1, 1);
183 b &= 0x7F;
184 MIDIEVENT(song->at, ME_KEYPRESSURE, lastchan, a, b);
185
186 case 3: /* Control change */
187 mid_istream_read(stream, &b, 1, 1);
188 b &= 0x7F;
189 {
190 int control=255;
191 switch(a)
192 {
193 case 7: control=ME_MAINVOLUME; break;
194 case 10: control=ME_PAN; break;
195 case 11: control=ME_EXPRESSION; break;
196 case 64: control=ME_SUSTAIN; break;
197 case 120: control=ME_ALL_SOUNDS_OFF; break;
198 case 121: control=ME_RESET_CONTROLLERS; break;
199 case 123: control=ME_ALL_NOTES_OFF; break;
200
201 /* These should be the SCC-1 tone bank switch
202 commands. I don't know why there are two, or
203 why the latter only allows switching to bank 0.
204 Also, some MIDI files use 0 as some sort of
205 continuous controller. This will cause lots of
206 warnings about undefined tone banks. */
207 case 0: control=ME_TONE_BANK; break;
208 case 32:
209 if (b!=0)
210 DEBUG_MSG("(Strange: tone bank change 0x20%02x)\n", b);
211 else
212 control=ME_TONE_BANK;
213 break;
214
215 case 100: nrpn=0; rpn_msb[lastchan]=b; break;
216 case 101: nrpn=0; rpn_lsb[lastchan]=b; break;
217 case 99: nrpn=1; rpn_msb[lastchan]=b; break;
218 case 98: nrpn=1; rpn_lsb[lastchan]=b; break;
219
220 case 6:
221 if (nrpn)
222 {
223 DEBUG_MSG("(Data entry (MSB) for NRPN %02x,%02x: %d)\n",
224 rpn_msb[lastchan], rpn_lsb[lastchan], b);
225 break;
226 }
227
228 switch((rpn_msb[lastchan]<<8) | rpn_lsb[lastchan])
229 {
230 case 0x0000: /* Pitch bend sensitivity */
231 control=ME_PITCH_SENS;
232 break;
233
234 case 0x7F7F: /* RPN reset */
235 /* reset pitch bend sensitivity to 2 */
236 MIDIEVENT(song->at, ME_PITCH_SENS, lastchan, 2, 0);
237
238 default:
239 DEBUG_MSG("(Data entry (MSB) for RPN %02x,%02x: %d)\n",
240 rpn_msb[lastchan], rpn_lsb[lastchan], b);
241 break;
242 }
243 break;
244
245 default:
246 DEBUG_MSG("(Control %d: %d)\n", a, b);
247 break;
248 }
249 if (control != 255)
250 {
251 MIDIEVENT(song->at, control, lastchan, b, 0);
252 }
253 }
254 break;
255
256 case 4: /* Program change */
257 a &= 0x7f;
258 MIDIEVENT(song->at, ME_PROGRAM, lastchan, a, 0);
259
260 case 5: /* Channel pressure - NOT IMPLEMENTED */
261 break;
262
263 case 6: /* Pitch wheel */
264 mid_istream_read(stream, &b, 1, 1);
265 b &= 0x7F;
266 MIDIEVENT(song->at, ME_PITCHWHEEL, lastchan, a, b);
267
268 default:
269 DEBUG_MSG("*** Can't happen: status 0x%02X, channel 0x%02X\n",
270 laststatus, lastchan);
271 break;
272 }
273 }
274 }
275
276 return new;
277 }
278
279 #undef MIDIEVENT
280
281 /* Read a midi track into the linked list, either merging with any previous
282 tracks or appending to them. */
283 static int read_track(MidIStream *stream, MidSong *song, int append)
284 {
285 MidEventList *meep;
286 MidEventList *next, *new;
287 sint32 len;
288 char tmp[4];
289
290 meep = song->evlist;
291 if (append && meep)
292 {
293 /* find the last event in the list */
294 for (; meep->next; meep=meep->next)
295 ;
296 song->at = meep->event.time;
297 }
298 else
299 song->at=0;
300
301 /* Check the formalities */
302
303 if (mid_istream_read(stream, tmp, 1, 4) != 4 || mid_istream_read(stream, &len, 4, 1) != 1)
304 {
305 DEBUG_MSG("Can't read track header.\n");
306 return -1;
307 }
308 len=SWAPBE32(len);
309 if (memcmp(tmp, "MTrk", 4))
310 {
311 DEBUG_MSG("Corrupt MIDI file.\n");
312 return -2;
313 }
314
315 for (;;)
316 {
317 if (!(new=read_midi_event(stream, song))) /* Some kind of error */
318 return -2;
319
320 if (new==MAGIC_EOT) /* End-of-track Hack. */
321 {
322 return 0;
323 }
324
325 next=meep->next;
326 while (next && (next->event.time < new->event.time))
327 {
328 meep=next;
329 next=meep->next;
330 }
331
332 new->next=next;
333 meep->next=new;
334
335 song->event_count++; /* Count the event. (About one?) */
336 meep=new;
337 }
338 }
339
340 /* Free the linked event list from memory. */
341 static void free_midi_list(MidSong *song)
342 {
343 MidEventList *meep, *next;
344 if (!(meep = song->evlist)) return;
345 while (meep)
346 {
347 next=meep->next;
348 free(meep);
349 meep=next;
350 }
351 song->evlist=0;
352 }
353
354 /* Allocate an array of MidiEvents and fill it from the linked list of
355 events, marking used instruments for loading. Convert event times to
356 samples: handle tempo changes. Strip unnecessary events from the list.
357 Free the linked list. */
358 static MidEvent *groom_list(MidSong *song, sint32 divisions,sint32 *eventsp,
359 sint32 *samplesp)
360 {
361 MidEvent *groomed_list, *lp;
362 MidEventList *meep;
363 sint32 i, our_event_count, tempo, skip_this_event, new_value;
364 sint32 sample_cum, samples_to_do, at, st, dt, counting_time;
365
366 int current_bank[16], current_set[16], current_program[16];
367 /* Or should each bank have its own current program? */
368
369 for (i=0; i<16; i++)
370 {
371 current_bank[i]=0;
372 current_set[i]=0;
373 current_program[i]=song->default_program;
374 }
375
376 tempo=500000;
377 compute_sample_increment(song, tempo, divisions);
378
379 /* This may allocate a bit more than we need */
380 groomed_list=lp=safe_malloc(sizeof(MidEvent) * (song->event_count+1));
381 meep=song->evlist;
382
383 our_event_count=0;
384 st=at=sample_cum=0;
385 counting_time=2; /* We strip any silence before the first NOTE ON. */
386
387 for (i = 0; i < song->event_count; i++)
388 {
389 skip_this_event=0;
390
391 if (meep->event.type==ME_TEMPO)
392 {
393 tempo=
394 meep->event.channel + meep->event.b * 256 + meep->event.a * 65536;
395 compute_sample_increment(song, tempo, divisions);
396 skip_this_event=1;
397 }
398 else switch (meep->event.type)
399 {
400 case ME_PROGRAM:
401 if (ISDRUMCHANNEL(song, meep->event.channel))
402 {
403 if (song->drumset[meep->event.a]) /* Is this a defined drumset? */
404 new_value=meep->event.a;
405 else
406 {
407 DEBUG_MSG("Drum set %d is undefined\n", meep->event.a);
408 new_value=meep->event.a=0;
409 }
410 if (current_set[meep->event.channel] != new_value)
411 current_set[meep->event.channel]=new_value;
412 else
413 skip_this_event=1;
414 }
415 else
416 {
417 new_value=meep->event.a;
418 if ((current_program[meep->event.channel] != SPECIAL_PROGRAM)
419 && (current_program[meep->event.channel] != new_value))
420 current_program[meep->event.channel] = new_value;
421 else
422 skip_this_event=1;
423 }
424 break;
425
426 case ME_NOTEON:
427 if (counting_time)
428 counting_time=1;
429 if (ISDRUMCHANNEL(song, meep->event.channel))
430 {
431 /* Mark this instrument to be loaded */
432 if (!(song->drumset[current_set[meep->event.channel]]
433 ->instrument[meep->event.a]))
434 song->drumset[current_set[meep->event.channel]]
435 ->instrument[meep->event.a] = MAGIC_LOAD_INSTRUMENT;
436 }
437 else
438 {
439 if (current_program[meep->event.channel]==SPECIAL_PROGRAM)
440 break;
441 /* Mark this instrument to be loaded */
442 if (!(song->tonebank[current_bank[meep->event.channel]]
443 ->instrument[current_program[meep->event.channel]]))
444 song->tonebank[current_bank[meep->event.channel]]
445 ->instrument[current_program[meep->event.channel]] =
446 MAGIC_LOAD_INSTRUMENT;
447 }
448 break;
449
450 case ME_TONE_BANK:
451 if (ISDRUMCHANNEL(song, meep->event.channel))
452 {
453 skip_this_event=1;
454 break;
455 }
456 if (song->tonebank[meep->event.a]) /* Is this a defined tone bank? */
457 new_value=meep->event.a;
458 else
459 {
460 DEBUG_MSG("Tone bank %d is undefined\n", meep->event.a);
461 new_value=meep->event.a=0;
462 }
463 if (current_bank[meep->event.channel]!=new_value)
464 current_bank[meep->event.channel]=new_value;
465 else
466 skip_this_event=1;
467 break;
468 }
469
470 /* Recompute time in samples*/
471 if ((dt=meep->event.time - at) && !counting_time)
472 {
473 samples_to_do = song->sample_increment * dt;
474 sample_cum += song->sample_correction * dt;
475 if (sample_cum & 0xFFFF0000)
476 {
477 samples_to_do += ((sample_cum >> 16) & 0xFFFF);
478 sample_cum &= 0x0000FFFF;
479 }
480 st += samples_to_do;
481 }
482 else if (counting_time==1) counting_time=0;
483 if (!skip_this_event)
484 {
485 /* Add the event to the list */
486 *lp=meep->event;
487 lp->time=st;
488 lp++;
489 our_event_count++;
490 }
491 at=meep->event.time;
492 meep=meep->next;
493 }
494 /* Add an End-of-Track event */
495 lp->time=st;
496 lp->type=ME_EOT;
497 our_event_count++;
498 free_midi_list(song);
499
500 *eventsp=our_event_count;
501 *samplesp=st;
502 return groomed_list;
503 }
504
505 MidEvent *read_midi_file(MidIStream *stream, MidSong *song, sint32 *count, sint32 *sp)
506 {
507 sint32 len, divisions;
508 sint16 format, tracks, divisions_tmp;
509 int i;
510 char tmp[4];
511
512 song->event_count=0;
513 song->at=0;
514 song->evlist=0;
515
516 if (mid_istream_read(stream, tmp, 1, 4) != 4 || mid_istream_read(stream, &len, 4, 1) != 1)
517 {
518 DEBUG_MSG("Not a MIDI file!\n");
519 return 0;
520 }
521 len=SWAPBE32(len);
522 if (strncmp(tmp, "MThd", 4) != 0)
523 {
524 if ( !strncmp( tmp , "RIFF" , 4 ) )
525 {
526 mid_istream_read(stream, tmp, 1, 4);
527 if ( strncmp( tmp , "RMID" , 4 ) != 0)
528 {
529 DEBUG_MSG("Not a MIDI file!\n");
530 return 0;
531 }
532 else
533 {
534 mid_istream_read(stream, tmp, 1, 4);
535 mid_istream_read(stream, tmp, 1, 4);
536 mid_istream_read(stream, tmp, 1, 4);
537 mid_istream_read(stream, &len, 4, 1);
538 len=SWAPBE32(len);
539 }
540 }
541 else
542 {
543 DEBUG_MSG("Not a MIDI file!\n");
544 return 0;
545 }
546 }
547 if (len < 6)
548 {
549 DEBUG_MSG("Not a MIDI file!\n");
550 return 0;
551 }
552
553 mid_istream_read(stream, &format, 2, 1);
554 mid_istream_read(stream, &tracks, 2, 1);
555 mid_istream_read(stream, &divisions_tmp, 2, 1);
556 format=SWAPBE16(format);
557 tracks=SWAPBE16(tracks);
558 divisions_tmp=SWAPBE16(divisions_tmp);
559
560 if (divisions_tmp<0)
561 {
562 /* SMPTE time -- totally untested. Got a MIDI file that uses this? */
563 divisions=
564 (sint32)(-(divisions_tmp/256)) * (sint32)(divisions_tmp & 0xFF);
565 }
566 else divisions=(sint32)(divisions_tmp);
567
568 if (len > 6)
569 {
570 DEBUG_MSG("MIDI file header size %u bytes", len);
571 mid_istream_skip(stream, len-6); /* skip the excess */
572 }
573 if (format<0 || format >2)
574 {
575 DEBUG_MSG("Unknown MIDI file format %d\n", format);
576 return 0;
577 }
578 DEBUG_MSG("Format: %d Tracks: %d Divisions: %d\n",
579 format, tracks, divisions);
580
581 /* Put a do-nothing event first in the list for easier processing */
582 song->evlist=safe_malloc(sizeof(MidEventList));
583 song->evlist->event.time=0;
584 song->evlist->event.type=ME_NONE;
585 song->evlist->next=0;
586 song->event_count++;
587
588 switch(format)
589 {
590 case 0:
591 if (read_track(stream, song, 0))
592 {
593 free_midi_list(song);
594 return 0;
595 }
596 break;
597
598 case 1:
599 for (i=0; i<tracks; i++)
600 if (read_track(stream, song, 0))
601 {
602 free_midi_list(song);
603 return 0;
604 }
605 break;
606
607 case 2: /* We simply play the tracks sequentially */
608 for (i=0; i<tracks; i++)
609 if (read_track(stream, song, 1))
610 {
611 free_midi_list(song);
612 return 0;
613 }
614 break;
615 }
616 return groom_list(song, divisions, count, sp);
617 }