File: playmidi.c

package info (click to toggle)
playmidi 2.4debian-10
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, stretch
  • size: 572 kB
  • ctags: 636
  • sloc: ansic: 3,691; makefile: 164; sh: 53; sed: 2
file content (624 lines) | stat: -rw-r--r-- 15,938 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
/************************************************************************
   playmidi.c -- last change: 1 Jan 96

   Plays a MIDI file to any supported synth (including midi) device

   Copyright (C) 1994-1996 Nathan I. Laredo

   This program is modifiable/redistributable under the terms
   of the GNU General Public Licence.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
   Send your comments and all your spare pocket change to
   laredo@gnu.ai.mit.edu (Nathan Laredo) or to PSC 1, BOX 709, 2401
   Kelly Drive, Lackland AFB, TX 78236-5128, USA.
 *************************************************************************/
#define _GNU_SOURCE
#ifndef __FreeBSD__
#include <getopt.h>
#endif
#include <fcntl.h>
#include <ctype.h>
#include <unistd.h>
#include <sys/stat.h>
#include <errno.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/wait.h>
#include "playmidi.h"

#define GZIP "gzip"

SEQ_DEFINEBUF(SEQUENCERBLOCKSIZE);

#ifdef PLAY_FM
int play_fm = 0xffff, play_gus = 0, play_ext = 0, play_awe = 0;
#endif
#ifdef PLAY_GUS
int play_fm = 0, play_gus = 0xffff, play_ext = 0, play_awe = 0;
#endif
#ifdef PLAY_MIDI
int play_fm = 0, play_gus = 0, play_ext = 0xffff, play_awe = 0;
#endif
#ifdef PLAY_AWE32
int play_fm = 0, play_gus = 0, play_ext = 0, play_awe = 0xffff;
#endif

struct miditrack seq[MAXTRKS];

int verbose = 0, chanmask = 0xffff, perc = PERCUSSION;
int dochan = 1, force8bit = 0, wantopl3 = FM_DEFAULT_MODE;
int patchloaded[256], fmloaded[256], useprog[16], usevol[16];
int graphics = 0, reverb = 0, chorus = 0, nrsynths, nrmidis;
int sb_dev = -1, gus_dev = -1, ext_dev = -1, awe_dev = 2, p_remap = 0;
int seqfd, find_header = 0, MT32 = 0;
FILE *mfd;
int FORCE_EXT_DEV = DEFAULT_MIDI_DEV;
const char *seqdev = SEQUENCER_DEV;
unsigned long int default_tempo;
char *filename;
float skew = 1.0;
extern int ntrks;
extern char *gmvoice[256];
extern int mt32pgm[128];
extern int playevents();
extern int gus_load(int);
extern int readmidi(unsigned char *, off_t);
extern void loadfm();
extern void setup_show(int, char **);
extern void close_show(int, const char *);

static void perror_seq(const char *reason)
{
  fprintf(stderr, "%s %s: %s\n", reason, seqdev, strerror (errno));
}

int xasprintf (char **strp, const char *fmt, ...)
{
  int ret;
  va_list ap;
  va_start (ap, fmt);
  ret = vasprintf (strp, fmt, ap);
  if (!*strp)
  {
    errno = ENOMEM;
    close_show (-1, NULL);
  }
  return ret;
}

static void handle_sigchld (int sig GCC_UNUSED)
{
  int status;
  wait (&status);
}

static FILE *do_pipe (const char *cmd, char *const args[])
{
  int fp[2], cpid;
  FILE *mfd;

  if (pipe (fp))
    close_show (-1, cmd);
  signal (SIGCHLD, handle_sigchld);
  switch (cpid = vfork ())
  {
  case -1:
    close_show (-1, cmd);
  case 0:
    if (!close (fp[0]) && !close (STDOUT_FILENO)
	&& dup2 (fp[1], STDOUT_FILENO) != -1)
      execvp (cmd, args);
    close_show (0, cmd);
    _exit (2);
  }
  /*default:*/
  close (fp[1]);
  if (!(mfd = fdopen (fp[0], "r")))
    close_show (-1, cmd);
  return mfd;
}

struct synth_info card_info[MAX_CARDS];
void synth_setup()
{
#ifdef ULTRA_DRIVER
    gus_dev = 0;
    sb_dev = -1;
    ext_dev = -1;
    awe_dev = -1;
    play_ext = 0;
    play_fm = 0;
    play_gus = 0xffff;
    play_awe = 0;
#else
    int i;

    if (ioctl(seqfd, SNDCTL_SEQ_NRSYNTHS, &nrsynths) == -1) {
	fprintf(stderr, "there is no soundcard\n");
	exit(-1);
    }
    for (i = 0; i < nrsynths; i++) {
	card_info[i].device = i;
	if (ioctl(seqfd, SNDCTL_SYNTH_INFO, &card_info[i]) == -1) {
	    fprintf(stderr, "cannot get info on soundcard\n");
	    perror(seqdev);
	    exit(-1);
	}
	if (card_info[i].synth_type == SYNTH_TYPE_SAMPLE
	    && card_info[i].synth_subtype == SAMPLE_TYPE_GUS)
	    gus_dev = i;
	else if (card_info[i].synth_type == SYNTH_TYPE_SAMPLE
	    && card_info[i].synth_subtype == SAMPLE_TYPE_AWE32)
	    awe_dev = i;
	else if (card_info[i].synth_type == SYNTH_TYPE_FM) {
	    sb_dev = i;
	    if (play_fm)
		loadfm();
	    if (wantopl3)
		card_info[i].nr_voices = 12;	/* we have 12 with 4-op */
	}
    }

    if (gus_dev >= 0) {
	if (ioctl(seqfd, SNDCTL_SEQ_RESETSAMPLES, &gus_dev) == -1) {
	    perror("Sample reset");
	    exit(-1);
	}
    }
    if (ioctl(seqfd, SNDCTL_SEQ_NRMIDIS, &nrmidis) == -1) {
	fprintf(stderr, "can't get info about midi ports\n");
	exit(-1);
    }
    if (nrmidis > 0) {
	if (FORCE_EXT_DEV >= 0)
	    ext_dev = FORCE_EXT_DEV;
	else
	    ext_dev = nrmidis - 1;
    }
    if (!play_gus)
	gus_dev = -1;
    if (!play_fm)
	sb_dev = -1;
    if (!play_ext)
	ext_dev = -1;
    if (!play_awe)
	awe_dev = -1;
    if (ext_dev < 0)
	play_ext = 0;
    if (sb_dev < 0)
	play_fm = 0;
    if (gus_dev < 0)
	play_gus = 0;
    if (awe_dev < 0)
	play_awe = 0;
#endif
}

void seqbuf_dump()
{
    if (_seqbufptr)
	if (write(seqfd, _seqbuf, _seqbufptr) == -1) {
	    perror_seq("write");
	    exit(-1);
	}
    _seqbufptr = 0;
}

int hextoi(s)
char *s;
{
    int i, j, k, l;

    j = 0;
    k = strlen(s);
    for (i = 0; i < k; i++) {
	l = toupper(s[i]);
	if (l > 64 && l < 71)
	    j += (l - 55) << ((k - i - 1) * 4);
	else if (l > 47 && l < 58)
	    j += (l - 48) << ((k - i - 1) * 4);
	else if (l != 88) {
	    fprintf(stderr, "invalid character in hexidecimal mask\n");
	    exit(1);
	}
    }
    if (j < 0 || j > 0xffff) {
	fprintf(stderr, "mask must be between 0 and ffff hexidecimal\n");
	exit(1);
    }
    return j;

}

int main(argc, argv)
int argc;
char **argv;
{
    extern char *optarg;
    extern int optind;
    unsigned int i, j, newprog;
    int error = 0;
    char *extra;
    unsigned char *filebuf;
    struct stat info;
    int piped = 0;

    /* CPhipps 2000/02/04 - this might be splaymidi, in which case we're
     * probably setuid root. Drop privs immediately.. io_svgalib.c can
     * regain them */
    if (getuid() != geteuid())
	if (seteuid(getuid())) {
	    perror("seteuid");
	    exit(EPERM); /* Seems appropriate */
	}

    printf("%s Copyright (C) 1994-1997 Nathan I. Laredo,"
	   " AWE32 by Takashi Iwai\n"
	   "This is free software with ABSOLUTELY NO WARRANTY.\n"
	   "For details please see the file COPYING.\n", RELEASE);
    for (i = 0; i < 16; i++)
	useprog[i] = usevol[i] = 0;	/* reset options */
    while ((i = getopt(argc, argv,
		     "48c:aA:C:dD:eE:fF:gh:G:i:IMp:P:rR:t:vV:x:zH-:")) != -1)
    {
	if (i == 'H')
	{
	  error = -1;
	  break;
	}
	else if (i == '-')
	{
	  if (optarg && !strcmp (optarg, "help"))
	  {
	    error = -1;
	    break;
	  }
	  fprintf (stderr, "%s: invalid option -- -\n", argv[0]);
	}
	switch (i) {
	case '8':
	    force8bit++;
	    break;
	case 'x':
	    j = atoi(optarg);
	    if (j < 1 || j > 16) {
		fprintf(stderr, "option -x channel must be 1 - 16\n");
		exit(1);
	    }
	    j = 1 << (j - 1);
	    chanmask &= ~j;
	    break;
	case 'c':
	    if (chanmask == 0xffff)
		chanmask = hextoi(optarg);
	    else
		chanmask |= hextoi(optarg);
	    break;
	case 'D':
	    FORCE_EXT_DEV = atoi(optarg);
	    break;
	case 'e':
	    play_ext = 0xffff;
	    play_gus = play_fm = play_awe = 0;
	    break;
	case 'g':
	    play_gus = 0xffff;
	    play_ext = play_fm = play_awe = 0;
	    break;
	case 'a':
	    play_awe = 0xffff;
	    play_ext = play_fm = play_gus = 0;
	    break;
	case 'h':
	    find_header = atoi(optarg);
	    if (find_header < 1) {
		fprintf(stderr, "option -h header must be > 0\n");
		exit(1);
	    }
	    break;
	case 'f':
	case '4':
	    play_fm = 0xffff;
	    play_ext = play_gus = play_awe = 0;
	    wantopl3 = (i == '4');
	    break;
	case 'I':
	    {
		int k;
		printf("Gravis Ultrasound Program Info:");
		for (j = 0; j < 128; j++) {
		    extra = gmvoice[j];
		    printf("%c%3d %s", j % 6 ? ' ' : '\n', j + 1, extra);
		    for (k = strlen(extra); k < 8; k++)
			putchar(' ');
		}
		putchar('\n');
		exit(1);
	    }
	    break;
	case 'i':
	    chanmask &= ~hextoi(optarg);
	    break;
	case 'M':
	    MT32++;
	    break;
	case 'p':
	    if (strchr(optarg, ',') == NULL) {	/* set all channels */
		newprog = atoi(optarg);
		if (newprog < 1 || newprog > 129) {
		    fprintf(stderr, "option -p prog must be 1 - 129\n");
		    exit(1);
		}
		for (j = 0; j < 16; j++)
		    useprog[j] = newprog;
	    } else {		/* set channels individually */
		extra = optarg;
		while (extra != NULL) {
		    j = atoi(extra);
		    if (j < 1 || j > 16) {
			fprintf(stderr, "opton -p chan must be 1 - 16\n");
			exit(1);
		    }
		    extra = strchr(extra, ',');
		    if (extra == NULL) {
			fprintf(stderr, "option -p prog needed for chan %d\n",
				j);
			exit(1);
		    } else
			extra++;
		    newprog = atoi(extra);
		    if (newprog < 1 || newprog > 129) {
			fprintf(stderr, "option -p prog must be 1 - 129\n");
			fprintf(stderr, "DANGER: 129 may screw everything!\n");
			exit(1);
		    }
		    useprog[j - 1] = newprog;
		    extra = strchr(extra, ',');
		    if (extra != NULL)
			extra++;
		}
	    }
	    break;
	case 'r':
	    graphics++;
	    break;
	case 't':
	    if ((skew = atof(optarg)) < .25) {
		fprintf(stderr, "option -t skew under 0.25 unplayable\n");
		exit(1);
	    }
	    break;
	case 'E':
	    play_ext = hextoi(optarg);
	    play_fm &= ~play_ext;
	    play_gus &= ~play_ext;
	    play_awe &= ~play_ext;
	    break;
	case 'F':
	    play_fm = hextoi(optarg);
	    play_ext &= ~play_fm;
	    play_gus &= ~play_fm;
	    play_awe &= ~play_fm;
	    break;
	case 'G':
	    play_gus = hextoi(optarg);
	    play_fm &= ~play_gus;
	    play_ext &= ~play_gus;
	    play_awe &= ~play_gus;
	    break;
	case 'A':
	    play_awe = hextoi(optarg);
	    play_ext &= ~play_awe;
	    play_fm &= ~play_awe;
	    play_gus &= ~play_awe;
	    break;
	case 'R':
	    reverb = atoi(optarg);
	    if (reverb < 0 || reverb > 127) {
		fprintf(stderr, "option -R reverb must be 0 - 127\n");
		exit(1);
	    }
	    break;
	case 'C':
	    chorus = atoi(optarg);
	    if (chorus < 0 || chorus > 127) {
		fprintf(stderr, "option -C chorus must be 0 - 127\n");
		exit(1);
	    }
	    break;
	case 'P':
	    p_remap = atoi(optarg);
	    if (p_remap < 1 || p_remap > 16) {
		fprintf(stderr, "option -P channel must be 1 - 16\n");
		exit(1);
	    }
	    break;
	case 'v':
	    verbose++;
	    break;
	case 'V':
	    extra = optarg;
	    while (extra != NULL) {
		j = atoi(extra);
		if (j < 1 || j > 16) {
		    fprintf(stderr, "opton -V chan must be 1 - 16\n");
		    exit(1);
		}
		extra = strchr(extra, ',');
		if (extra == NULL) {
		    fprintf(stderr, "option -V volume needed for chan %d\n",
			    j);
		    exit(1);
		}
		extra++;
		newprog = atoi(extra);
		if (newprog < 1 || newprog > 127) {
		    fprintf(stderr, "option -V volume must be 1 - 127\n");
		    exit(1);
		}
		usevol[j - 1] = newprog;
		extra = strchr(extra, ',');
		if (extra != NULL)
		    extra++;
	    }
	    break;
	case 'z':
	    dochan = 0;
	    break;
	case 'd':
	    chanmask &= ~perc;
	    break;
	default:
	    error++;
	    break;
	}
    }

    if (error || optind >= argc) {
	printf( "usage: %s [-options] file1 [file2 ...]\n", argv[0]);
	if (error >= 0)
	  printf ("type '%s --help' for more details\n", argv[0]);
	else
	printf( "  -v       verbosity (additive)\n"
		"  -i x     ignore channels set in bitmask x (hex)\n"
		"  -c x     play only channels set in bitmask x (hex)\n"
		"  -x x     exclude channel x from playable bitmask\n"
		"  -p [c,]x play program x on channel c (all if no c)\n"
		"  -V [c,]x play channel c with volume x (all if no c)\n"
		"  -t x     skew tempo by x (float)\n"
		"  -d       don't play any percussion\n"
		"  -P x     play percussion on channel x\n"
		"  -e       output to external midi\n"
		"  -D x     output to midi device x\n"
		"  -f       output to fm (sb patches)\n"
		"  -4       output to 4-op fm (opl/3 patches)\n"
		"  -a       output to awe32 wave synth\n"
		"  -h x     skip to header x in large archive\n"
		"  -g       output to gravis ultrasound\n"
		"  -E x     play channels in bitmask x external\n"
		"  -F x     play channels in bitmask x on fm\n"
		"  -G x     play channels in bitmask x on gus\n"
		"  -A x     play channels in bitmask x on awe32\n"
		"  -z       ignore channel of all events\n"
		"  -8       force 8-bit samples on GUS\n"
		"  -M       enable MT-32 to GM translation mode\n"
		"  -I       show list of all GM programs (see -p)\n"
		"  -R x     set initial reverb to x (0-127)\n"
		"  -C x     set initial chorus to x (0-127)\n"
		"  -r       real-time playback graphics\n");
	exit(error >= 0);
    }
    if (FORCE_EXT_DEV)
	xasprintf((char **)&seqdev, "%s%d", SEQUENCER_DEV, FORCE_EXT_DEV);
    if ((seqfd = open(seqdev, O_WRONLY, 0)) < 0) {
	perror_seq("open");
	exit(-1);
    }
    synth_setup();
    if (!(play_gus || play_fm || play_ext || play_awe)) {
	fprintf(stderr, "%s: No playback device found.\n", argv[0]);
	exit(-1);
    }
    setup_show(argc, argv);
    /* play all filenames listed on command line */
    for (i = optind; i < argc;) {
	filename = argv[i];
	if (stat(filename, &info) == -1) {
	    int fl = strlen (filename);
	    if (fl < 4 || strcmp (filename + fl - 4, ".mid"))
	    {
	      xasprintf(&extra, "%s.mid", filename);
	      if (stat(extra, &info) == -1)
		close_show(-1, extra);
	      if ((mfd = fopen(extra, "r")) == NULL)
		close_show(-1, extra);
	      free(extra);
	    }
	    else
		close_show (-1, filename);
	} else {
	    char *ext = strrchr(filename, '.');
	    if (ext && strcmp(ext, ".gz") == 0) {
		char temp[1024];
		char *tok;
		static char *args_l[] = { GZIP, "-l", NULL, NULL };
		static char *args_c[] = { GZIP, "-d", "-c", NULL, NULL };

		piped = 1;

                args_l[2] = filename;
                mfd = do_pipe (GZIP, args_l);
                temp[0] = 0;
		fgets(temp, sizeof(temp), mfd); /* skip 1st line */
		fgets(temp, sizeof(temp), mfd);
		strtok(temp, " "); /* compressed size */
		tok = strtok (NULL, " ");
		if (!tok)
		{
		  fprintf (stderr, "%s: %s: corrupt, or not a gzipped file\n",
			   argv[0], filename);
		  exit(-1);
		}
		info.st_size = atoi(tok); /* original size */
		fclose (mfd);

                args_c[3] = filename;
                mfd = do_pipe (GZIP, args_c);
	    } else if ((mfd = fopen(filename, "r")) == NULL)
		close_show(-1, filename);
	}
	if ((filebuf = malloc(info.st_size)) == NULL)
	    close_show(-1, 0);
	fread(filebuf, 1, info.st_size, mfd);
	if (piped)
	    pclose(mfd);
	else
	    fclose(mfd);
	do {
	    if (play_gus)
		gus_load(-1);
	    default_tempo = 500000;
	    /* error holds number of tracks read */
	    error = readmidi(filebuf, info.st_size);
	    if (play_gus && error > 0) {
		int i;		/* need to keep other i safe */
#define CMD (seq[i].data[j] & 0xf0)
#define CHN (seq[i].data[j] & 0x0f)
#define PGM (seq[i].data[j + 1])
		/* REALLY STUPID way to preload GUS, but it works */
		for (i = 0; i < ntrks; i++)
		    for (j = 0; j < seq[i].length - 5; j++)
			if (ISGUS(CHN) && !(PGM & 0x80) &&
			    ((CMD == MIDI_PGM_CHANGE && !ISPERC(CHN))
			     || (CMD == MIDI_NOTEON && ISPERC(CHN))))
			    gus_load(ISPERC(CHN) ? PGM + 128 :
				     useprog[CHN] ? useprog[CHN] - 1 :
				     MT32 ? mt32pgm[PGM] : PGM);
		/* make sure that some program was loaded to use */
		for (j = 0; patchloaded[j] != 1 && j < 128; j++);
		if (j > 127)
		    gus_load(0);
	    }
	    newprog = 1;	/* if there's an error skip to next file */
	    if (error > 0)	/* error holds number of tracks read */
		while ((newprog = playevents()) == 0);
	    if (find_header)	/* play headers following selected */
		find_header += newprog;
	} while (find_header);
	if ((i += newprog) < optind)
	    i = optind;		/* can't skip back past first file */
        /* sync to avoid truncation when we're about to play another tune */
	ioctl(seqfd, SNDCTL_SEQ_SYNC);
	free(filebuf);
    }
    close(seqfd);
    close_show(0, 0);
    exit(0);
}
/* end of file */