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 #include "pent_include.h"
23 
24 #ifdef USE_TIMIDITY_MIDI
25 
26 #include <cstdio>
27 #include <cstdlib>
28 #include <cstring>
29 #include <cerrno>
30 
31 #include "timidity.h"
32 #include "timidity_common.h"
33 #include "timidity_instrum.h"
34 #include "timidity_playmidi.h"
35 #include "timidity_readmidi.h"
36 #include "timidity_output.h"
37 #include "timidity_controls.h"
38 
39 #ifdef NS_TIMIDITY
40 namespace NS_TIMIDITY {
41 #endif
42 
43 sint32 quietchannels=0;
44 
45 /* to avoid some unnecessary parameter passing */
46 static MidiEventList *evlist;
47 static sint32 event_count;
48 static FILE *fp;
49 static sint32 at;
50 
51 /* These would both fit into 32 bits, but they are often added in
52    large multiples, so it's simpler to have two roomy ints */
53 static sint32 sample_increment, sample_correction; /*samples per MIDI delta-t*/
54 
55 /* Computes how many (fractional) samples one MIDI delta-time unit contains */
compute_sample_increment(sint32 tempo,sint32 divisions)56 static void compute_sample_increment(sint32 tempo, sint32 divisions)
57 {
58 	double a;
59 	a = static_cast<double>(tempo) * static_cast<double>(play_mode->rate) * (65536.0/1000000.0) /
60 		static_cast<double>(divisions);
61 
62 	sample_correction = static_cast<sint32>(a) & 0xFFFF;
63 	sample_increment = static_cast<sint32>(a) >> 16;
64 
65 	ctl->cmsg(CMSG_INFO, VERB_DEBUG, "Samples per delta-t: %d (correction %d)",
66 	          sample_increment, sample_correction);
67 }
68 
69 /* Read variable-length number (7 bits per byte, MSB first) */
getvl()70 static sint32 getvl()
71 {
72 	sint32 l=0;
73 	for (;;)
74 	{
75 		uint8 c;
76 		size_t err = fread(&c,1,1,fp);
77 		assert (err == 1);
78 		l += (c & 0x7f);
79 		if (!(c & 0x80)) return l;
80 		l<<=7;
81 	}
82 }
83 
84 /* Print a string from the file, followed by a newline. Any non-ASCII
85    or unprintable characters will be converted to periods. */
dumpstring(sint32 len,const char * label)86 static int dumpstring(sint32 len, const char *label)
87 {
88 	auto *s=safe_Malloc<signed char>(len+1);
89 	if (len != static_cast<sint32>(fread(s, 1, len, fp)))
90 	{
91 		free(s);
92 		return -1;
93 	}
94 	s[len]='\0';
95 	while (len--)
96 	{
97 		if (s[len]<32)
98 			s[len]='.';
99 	}
100 	ctl->cmsg(CMSG_TEXT, VERB_VERBOSE, "%s%s", label, s);
101 	free(s);
102 	return 0;
103 }
104 
105 #define MIDIEVENT(at,t,ch,pa,pb) \
106 		event=safe_Malloc<MidiEventList>(); \
107 		event->event.time=at; event->event.type=t; event->event.channel=ch; \
108 		event->event.a=pa; event->event.b=pb; event->next=nullptr;\
109 		return event;
110 
111 #define MAGIC_EOT (reinterpret_cast<MidiEventList *>(-1))
112 
113 /* Read a MIDI event, returning a freshly allocated element that can
114    be linked to the event list */
read_midi_event()115 static MidiEventList *read_midi_event()
116 {
117 	static uint8 laststatus;
118 	static uint8 lastchan;
119 	static uint8 nrpn=0;
120 	static uint8 rpn_msb[16];
121 	static uint8 rpn_lsb[16]; /* one per channel */
122 	MidiEventList *event;
123 
124 	for (;;)
125 	{
126 		at+=getvl();
127 		uint8 me;
128 		if (fread(&me,1,1,fp)!=1)
129 		{
130 			ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "%s: read_midi_event: %s",
131 			          current_filename, strerror(errno));
132 			return nullptr;
133 		}
134 
135 		if(me==0xF0 || me == 0xF7) /* SysEx event */
136 		{
137 			sint32 len=getvl();
138 			skip(fp, len);
139 		}
140 		else if(me==0xFF) /* Meta event */
141 		{
142 			uint8 type;
143 			size_t err = fread(&type,1,1,fp);
144 			assert (err == 1);
145 
146 			sint32 len=getvl();
147 			if (type>0 && type<16)
148 			{
149 				static const char *label[]={
150 					"Text event: ", "Text: ", "Copyright: ", "Track name: ",
151 					"Instrument: ", "Lyric: ", "Marker: ", "Cue point: "};
152 				dumpstring(len, label[(type>7) ? 0 : type]);
153 			}
154 			else
155 				switch(type)
156 				{
157 					case 0x2F: /* End of Track */
158 						return MAGIC_EOT;
159 
160 					case 0x51: { /* Tempo */
161 						uint8 a;
162 						uint8 b;
163 						uint8 c;
164 						err = fread(&a,1,1,fp);
165 						assert (err == 1);
166 						err = fread(&b,1,1,fp);
167 						assert (err == 1);
168 						err = fread(&c,1,1,fp);
169 						assert (err == 1);
170 						MIDIEVENT(at, ME_TEMPO, c, a, b);
171 					}
172 					default:
173 						ctl->cmsg(CMSG_INFO, VERB_DEBUG,
174 							      "(Meta event type 0x%02x, length %d)", type, len);
175 						skip(fp, len);
176 						break;
177 				}
178 		}
179 		else
180 		{
181 			uint8 a=me;
182 			if (a & 0x80) /* status byte */
183 			{
184 				lastchan=a & 0x0F;
185 				laststatus=(a>>4) & 0x07;
186 				size_t err = fread(&a, 1,1, fp);
187 				assert (err == 1);
188 				a &= 0x7F;
189 			}
190 			uint8 b;
191 			size_t err;
192 			switch(laststatus)
193 			{
194 				case 0: /* Note off */
195 					err = fread(&b, 1,1, fp);
196 					assert (err == 1);
197 					b &= 0x7F;
198 					MIDIEVENT(at, ME_NOTEOFF, lastchan, a,b);
199 
200 				case 1: /* Note on */
201 					err = fread(&b, 1,1, fp);
202 					assert (err == 1);
203 					b &= 0x7F;
204 					MIDIEVENT(at, ME_NOTEON, lastchan, a,b);
205 
206 				case 2: /* Key Pressure */
207 					err = fread(&b, 1,1, fp);
208 					assert (err == 1);
209 					b &= 0x7F;
210 					MIDIEVENT(at, ME_KEYPRESSURE, lastchan, a, b);
211 
212 				case 3: /* Control change */
213 					err = fread(&b, 1,1, fp);
214 					assert (err == 1);
215 					b &= 0x7F;
216 					{
217 						int control=255;
218 						switch(a)
219 						{
220 							case 7: control=ME_MAINVOLUME; break;
221 							case 10: control=ME_PAN; break;
222 							case 11: control=ME_EXPRESSION; break;
223 							case 64: control=ME_SUSTAIN; break;
224 							case 120: control=ME_ALL_SOUNDS_OFF; break;
225 							case 121: control=ME_RESET_CONTROLLERS; break;
226 							case 123: control=ME_ALL_NOTES_OFF; break;
227 
228 							/* These should be the SCC-1 tone bank switch
229 							 commands. I don't know why there are two, or
230 							 why the latter only allows switching to bank 0.
231 							 Also, some MIDI files use 0 as some sort of
232 							 continuous controller. This will cause lots of
233 							 warnings about undefined tone banks. */
234 							case 0: control=ME_TONE_BANK; break;
235 							case 32:
236 								if (b!=0)
237 									ctl->cmsg(CMSG_INFO, VERB_DEBUG,
238 										      "(Strange: tone bank change 0x20%02x)", b);
239 								else
240 									control=ME_TONE_BANK;
241 								break;
242 
243 								case 100: nrpn=0; rpn_msb[lastchan]=b; break;
244 								case 101: nrpn=0; rpn_lsb[lastchan]=b; break;
245 								case 99: nrpn=1; rpn_msb[lastchan]=b; break;
246 								case 98: nrpn=1; rpn_lsb[lastchan]=b; break;
247 
248 							case 6:
249 								if (nrpn)
250 								{
251 									ctl->cmsg(CMSG_INFO, VERB_DEBUG,
252 											  "(Data entry (MSB) for NRPN %02x,%02x: %d)",
253 											  rpn_msb[lastchan], rpn_lsb[lastchan], b);
254 									break;
255 								}
256 
257 								switch((rpn_msb[lastchan]<<8) | rpn_lsb[lastchan])
258 								{
259 									case 0x0000: /* Pitch bend sensitivity */
260 										control=ME_PITCH_SENS;
261 									break;
262 
263 									case 0x7F7F: /* RPN reset */
264 										/* reset pitch bend sensitivity to 2 */
265 										MIDIEVENT(at, ME_PITCH_SENS, lastchan, 2, 0);
266 
267 									default:
268 										ctl->cmsg(CMSG_INFO, VERB_DEBUG,
269 												  "(Data entry (MSB) for RPN %02x,%02x: %d)",
270 												  rpn_msb[lastchan], rpn_lsb[lastchan], b);
271 										break;
272 								}
273 								break;
274 
275 							default:
276 								ctl->cmsg(CMSG_INFO, VERB_DEBUG,
277 									      "(Control %d: %d)", a, b);
278 								break;
279 						}
280 						if (control != 255)
281 						{
282 							MIDIEVENT(at, control, lastchan, b, 0);
283 						}
284 					}
285 					break;
286 
287 				case 4: /* Program change */
288 					a &= 0x7f;
289 					MIDIEVENT(at, ME_PROGRAM, lastchan, a, 0);
290 
291 				case 5: /* Channel pressure - NOT IMPLEMENTED */
292 					break;
293 
294 				case 6: /* Pitch wheel */
295 					err = fread(&b, 1,1, fp);
296 					assert (err == 1);
297 					b &= 0x7F;
298 					MIDIEVENT(at, ME_PITCHWHEEL, lastchan, a, b);
299 
300 				default:
301 					ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
302 					          "*** Can't happen: status 0x%02X, channel 0x%02X",
303 					          laststatus, lastchan);
304 					break;
305 			}
306 		}
307 	}
308 
309 	return event;
310 }
311 
312 #undef MIDIEVENT
313 
314 /* Read a midi track into the linked list, either merging with any previous
315  tracks or appending to them. */
read_track(int append)316 static int read_track(int append)
317 {
318 	MidiEventList *meep;
319 	MidiEventList *next;
320 	MidiEventList *new_event;
321 	sint32 len;
322 	char tmp[4];
323 
324 	meep=evlist;
325 	if (append)
326 	{
327 		/* find the last event in the list */
328 		for (; meep->next; meep=meep->next)
329 			;
330 		at=meep->event.time;
331 	}
332 	else
333 		at=0;
334 
335 	/* Check the formalities */
336 
337 	if ((fread(tmp,1,4,fp) != 4) || (fread(&len,4,1,fp) != 1))
338 	{
339 		ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
340 		          "%s: Can't read track header.", current_filename);
341 		return -1;
342 	}
343 	len=BE_LONG(len);
344 	if (memcmp(tmp, "MTrk", 4) != 0)
345 	{
346 		ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
347 		          "%s: Corrupt MIDI file.", current_filename);
348 		return -2;
349 	}
350 
351 	for (;;)
352 	{
353 		if (!(new_event=read_midi_event())) /* Some kind of error  */
354 			return -2;
355 
356 		if (new_event==MAGIC_EOT) /* End-of-track Hack. */
357 		{
358 			return 0;
359 		}
360 
361 		next=meep->next;
362 		while (next && (next->event.time < new_event->event.time))
363 		{
364 			meep=next;
365 			next=meep->next;
366 		}
367 
368 		new_event->next=next;
369 		meep->next=new_event;
370 
371 		event_count++; /* Count the event. (About one?) */
372 		meep=new_event;
373 	}
374 }
375 
376 /* Free the linked event list from memory. */
free_midi_list()377 static void free_midi_list()
378 {
379 	MidiEventList *meep;
380 	MidiEventList *next;
381 	if (!(meep=evlist)) return;
382 	while (meep)
383 	{
384 		next=meep->next;
385 		free(meep);
386 		meep=next;
387 	}
388 	evlist=nullptr;
389 }
390 
391 /* Allocate an array of MidiEvents and fill it from the linked list of
392  events, marking used instruments for loading. Convert event times to
393  samples: handle tempo changes. Strip unnecessary events from the list.
394  Free the linked list. */
groom_list(sint32 divisions,sint32 * eventsp,sint32 * samplesp)395 static MidiEvent *groom_list(sint32 divisions,sint32 *eventsp,sint32 *samplesp)
396 {
397 	MidiEvent *groomed_list;
398 	MidiEvent *lp;
399 	MidiEventList *meep;
400 
401 	int current_bank[16];
402 	int current_set[16];
403 	int current_program[16];
404 	/* Or should each bank have its own current program? */
405 
406 	for (sint32 i=0; i<16; i++)
407 	{
408 		current_bank[i]=0;
409 		current_set[i]=0;
410 		current_program[i]=default_program;
411 	}
412 
413 	sint32 tempo=500000;
414 	compute_sample_increment(tempo, divisions);
415 
416 	/* This may allocate a bit more than we need */
417 	groomed_list=lp=safe_Malloc<MidiEvent>(event_count+1);
418 	meep=evlist;
419 
420 	sint32 our_event_count=0;
421 	sint32 sample_cum = 0;
422 	sint32 st=at=0;
423 	sint32 counting_time=2; /* We strip any silence before the first NOTE ON. */
424 
425 	for (sint32 i=0; i<event_count; i++)
426 	{
427 		sint32 skip_this_event=0;
428 		ctl->cmsg(CMSG_INFO, VERB_DEBUG_SILLY,
429 		          "%6d: ch %2d: event %d (%d,%d)",
430 		          meep->event.time, meep->event.channel + 1,
431 		          meep->event.type, meep->event.a, meep->event.b);
432 
433 		sint32 new_value;
434 		if (meep->event.type==ME_TEMPO)
435 		{
436 			tempo=
437 				meep->event.channel + meep->event.b * 256 + meep->event.a * 65536;
438 			compute_sample_increment(tempo, divisions);
439 			skip_this_event=1;
440 		}
441 		else if ((quietchannels & (1<<meep->event.channel)))
442 			skip_this_event=1;
443 		else switch (meep->event.type)
444 			{
445 			case ME_PROGRAM:
446 				if (ISDRUMCHANNEL(meep->event.channel))
447 				{
448 					if (drumset[meep->event.a]) /* Is this a defined drumset? */
449 						new_value=meep->event.a;
450 					else
451 					{
452 						ctl->cmsg(CMSG_WARNING, VERB_VERBOSE,
453 							      "Drum set %d is undefined", meep->event.a);
454 						new_value=meep->event.a=0;
455 					}
456 					if (current_set[meep->event.channel] != new_value)
457 						current_set[meep->event.channel]=new_value;
458 					else
459 						skip_this_event=1;
460 				}
461 				else
462 				{
463 					new_value=meep->event.a;
464 					if ((current_program[meep->event.channel] != SPECIAL_PROGRAM)
465 						&& (current_program[meep->event.channel] != new_value))
466 						current_program[meep->event.channel] = new_value;
467 					else
468 						skip_this_event=1;
469 				}
470 				break;
471 
472 			case ME_NOTEON:
473 				if (counting_time)
474 					counting_time=1;
475 				if (ISDRUMCHANNEL(meep->event.channel))
476 				{
477 					/* Mark this instrument to be loaded */
478 					if (!(drumset[current_set[meep->event.channel]]->tone[meep->event.a].instrument))
479 						drumset[current_set[meep->event.channel]]->tone[meep->event.a].instrument=
480 						MAGIC_LOAD_INSTRUMENT;
481 				}
482 				else
483 				{
484 					if (current_program[meep->event.channel]==SPECIAL_PROGRAM)
485 						break;
486 					/* Mark this instrument to be loaded */
487 					if (!(tonebank[current_bank[meep->event.channel]]->tone[current_program[meep->event.channel]].instrument))
488 						tonebank[current_bank[meep->event.channel]]->tone[current_program[meep->event.channel]].instrument=
489 						MAGIC_LOAD_INSTRUMENT;
490 				}
491 				break;
492 
493 			case ME_TONE_BANK:
494 				if (ISDRUMCHANNEL(meep->event.channel))
495 				{
496 					skip_this_event=1;
497 					break;
498 				}
499 				if (tonebank[meep->event.a]) /* Is this a defined tone bank? */
500 					new_value=meep->event.a;
501 				else
502 				{
503 					ctl->cmsg(CMSG_WARNING, VERB_VERBOSE,
504 						      "Tone bank %d is undefined", meep->event.a);
505 					new_value=meep->event.a=0;
506 				}
507 				if (current_bank[meep->event.channel]!=new_value)
508 					current_bank[meep->event.channel]=new_value;
509 				else
510 					skip_this_event=1;
511 				break;
512 			}
513 
514 		/* Recompute time in samples*/
515 		sint32 dt;
516 		if ((dt=meep->event.time - at) && !counting_time)
517 		{
518 			sint32 samples_to_do=sample_increment * dt;
519 			sample_cum += sample_correction * dt;
520 			if (sample_cum & 0xFFFF0000)
521 			{
522 				samples_to_do += ((sample_cum >> 16) & 0xFFFF);
523 				sample_cum &= 0x0000FFFF;
524 			}
525 			st += samples_to_do;
526 		}
527 		else if (counting_time==1) counting_time=0;
528 		if (!skip_this_event)
529 		{
530 			/* Add the event to the list */
531 			*lp=meep->event;
532 			lp->time=st;
533 			lp++;
534 			our_event_count++;
535 		}
536 		at=meep->event.time;
537 		meep=meep->next;
538 	}
539 	/* Add an End-of-Track event */
540 	lp->time=st;
541 	lp->type=ME_EOT;
542 	our_event_count++;
543 	free_midi_list();
544 
545 	*eventsp=our_event_count;
546 	*samplesp=st;
547 	return groomed_list;
548 }
549 
read_midi_file(FILE * mfp,sint32 * count,sint32 * sp)550 MidiEvent *read_midi_file(FILE *mfp, sint32 *count, sint32 *sp)
551 {
552 	sint32 len;
553 	sint32 divisions;
554 	sint16 format;
555 	sint16 tracks;
556 	sint16 divisions_tmp;
557 	int i;
558 	char tmp[4];
559 	size_t err;
560 
561 	fp=mfp;
562 	event_count=0;
563 	at=0;
564 	evlist=nullptr;
565 
566 	if ((fread(tmp,1,4,fp) != 4) || (fread(&len,4,1,fp) != 1))
567 	{
568 		if (ferror(fp))
569 		{
570 			ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "%s: %s", current_filename,
571 			          strerror(errno));
572 		}
573 		else
574 			ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
575 			          "%s: Not a MIDI file!", current_filename);
576 		return nullptr;
577 	}
578 	len=BE_LONG(len);
579 	if (memcmp(tmp, "MThd", 4) != 0 || len < 6)
580 	{
581 		ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
582 		          "%s: Not a MIDI file!", current_filename);
583 		return nullptr;
584 	}
585 
586 	err = fread(&format, 2, 1, fp);
587 	assert (err == 1);
588 	err = fread(&tracks, 2, 1, fp);
589 	assert (err == 1);
590 	err = fread(&divisions_tmp, 2, 1, fp);
591 	assert (err == 1);
592 	format=BE_SHORT(format);
593 	tracks=BE_SHORT(tracks);
594 	divisions_tmp=BE_SHORT(divisions_tmp);
595 
596 	if (divisions_tmp<0)
597 	{
598 		/* SMPTE time -- totally untested. Got a MIDI file that uses this? */
599 		divisions = -(divisions_tmp/256) * (divisions_tmp & 0xFF);
600 	}
601 	else divisions=static_cast<sint32>(divisions_tmp);
602 
603 	if (len > 6)
604 	{
605 		ctl->cmsg(CMSG_WARNING, VERB_NORMAL,
606 		          "%s: MIDI file header size %d bytes",
607 		          current_filename, len);
608 		skip(fp, len-6); /* skip the excess */
609 	}
610 	if (format<0 || format >2)
611 	{
612 		ctl->cmsg(CMSG_ERROR, VERB_NORMAL,
613 		          "%s: Unknown MIDI file format %d", current_filename, format);
614 		return nullptr;
615 	}
616 	ctl->cmsg(CMSG_INFO, VERB_VERBOSE,
617 	          "Format: %d  Tracks: %d  Divisions: %d", format, tracks, divisions);
618 
619 	/* Put a do-nothing event first in the list for easier processing */
620 	evlist=safe_Malloc<MidiEventList>();
621 	evlist->event.time=0;
622 	evlist->event.type=ME_NONE;
623 	evlist->next=nullptr;
624 	event_count++;
625 
626 	switch(format)
627 	{
628 		case 0:
629 			if (read_track(0))
630 			{
631 				free_midi_list();
632 				return nullptr;
633 			}
634 			break;
635 
636 		case 1:
637 			for (i=0; i<tracks; i++)
638 				if (read_track(0))
639 				{
640 					free_midi_list();
641 					return nullptr;
642 				}
643 			break;
644 
645 		case 2: /* We simply play the tracks sequentially */
646 			for (i=0; i<tracks; i++)
647 				if (read_track(1))
648 				{
649 					free_midi_list();
650 					return nullptr;
651 				}
652 			break;
653 	}
654 	return groom_list(divisions, count, sp);
655 }
656 
657 #ifdef NS_TIMIDITY
658 }
659 #endif
660 
661 #endif //USE_TIMIDITY_MIDI
662