Annotation of src/usr.bin/midiplay/midiplay.c, Revision 1.3
1.3 ! mpech 1: /* $OpenBSD: midiplay.c,v 1.2 2002/02/16 21:27:49 millert Exp $ */
1.1 niklas 2: /* $NetBSD: midiplay.c,v 1.8 1998/11/25 22:17:07 augustss Exp $ */
3:
4: /*
5: * Copyright (c) 1998 The NetBSD Foundation, Inc.
6: * All rights reserved.
7: *
8: * This code is derived from software contributed to The NetBSD Foundation
9: * by Lennart Augustsson (augustss@netbsd.org).
10: *
11: * Redistribution and use in source and binary forms, with or without
12: * modification, are permitted provided that the following conditions
13: * are met:
14: * 1. Redistributions of source code must retain the above copyright
15: * notice, this list of conditions and the following disclaimer.
16: * 2. Redistributions in binary form must reproduce the above copyright
17: * notice, this list of conditions and the following disclaimer in the
18: * documentation and/or other materials provided with the distribution.
19: * 3. All advertising materials mentioning features or use of this software
20: * must display the following acknowledgement:
21: * This product includes software developed by the NetBSD
22: * Foundation, Inc. and its contributors.
23: * 4. Neither the name of The NetBSD Foundation nor the names of its
24: * contributors may be used to endorse or promote products derived
25: * from this software without specific prior written permission.
26: *
27: * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28: * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29: * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31: * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37: * POSSIBILITY OF SUCH DAMAGE.
38: */
39:
40: #include <stdio.h>
41: #include <stdlib.h>
42: #include <fcntl.h>
43: #include <err.h>
44: #include <unistd.h>
45: #include <string.h>
46: #include <sys/types.h>
47: #include <sys/stat.h>
48: #include <sys/ioctl.h>
49: #include <sys/midiio.h>
50:
51: #define DEVMUSIC "/dev/music"
52:
53: struct track {
54: u_char *start, *end;
55: u_long curtime;
56: u_char status;
57: };
58:
59: #define MIDI_META 0xff
60:
61: #define META_SEQNO 0x00
62: #define META_TEXT 0x01
63: #define META_COPYRIGHT 0x02
64: #define META_TRACK 0x03
65: #define META_INSTRUMENT 0x04
66: #define META_LYRIC 0x05
67: #define META_MARKER 0x06
68: #define META_CUE 0x07
69: #define META_CHPREFIX 0x20
70: #define META_EOT 0x2f
71: #define META_SET_TEMPO 0x51
72: #define META_KEY 0x59
73: #define META_SMPTE 0x54
74: #define META_TIMESIGN 0x58
75:
76: char *metanames[] = {
77: "", "Text", "Copyright", "Track", "Instrument",
78: "Lyric", "Marker", "Cue",
79: };
80:
81: static int midi_lengths[] = { 2,2,2,2,1,1,2,0 };
82: /* Number of bytes in a MIDI command */
83: #define MIDI_LENGTH(d) (midi_lengths[((d) >> 4) & 7])
84:
1.2 millert 85: void usage(void);
86: void send_event(seq_event_rec *);
87: void dometa(u_int, u_char *, u_int);
88: void midireset(void);
89: void send_sysex(u_char *, u_int);
90: u_long getvar(struct track *);
91: void playfile(FILE *, char *);
92: void playdata(u_char *, u_int, char *);
93: int main(int argc, char **argv);
1.1 niklas 94:
95: extern char *__progname;
96:
97: #define P(c) 1,0x90,c,0x7f,4,0x80,c,0
98: #define PL(c) 1,0x90,c,0x7f,8,0x80,c,0
99: #define C 0x3c
100: #define D 0x3e
101: #define E 0x40
102: #define F 0x41
103:
104: u_char sample[] = {
105: 'M','T','h','d', 0,0,0,6, 0,1, 0,1, 0,8,
106: 'M','T','r','k', 0,0,0,4+13*8,
107: P(C), P(C), P(C), P(E), P(D), P(D), P(D),
108: P(F), P(E), P(E), P(D), P(D), PL(C),
109: 0, 0xff, 0x2f, 0
110: };
111: #undef P
112: #undef PL
113: #undef C
114: #undef D
115: #undef E
116: #undef F
117:
118: #define MARK_HEADER "MThd"
119: #define MARK_TRACK "MTrk"
120: #define MARK_LEN 4
121:
122: #define SIZE_LEN 4
123: #define HEADER_LEN 6
124:
125: #define GET8(p) ((p)[0])
126: #define GET16(p) (((p)[0] << 8) | (p)[1])
127: #define GET24(p) (((p)[0] << 16) | ((p)[1] << 8) | (p)[2])
128: #define GET32(p) (((p)[0] << 24) | ((p)[1] << 16) | ((p)[2] << 8) | (p)[3])
129:
130: void
131: usage()
132: {
133: printf("Usage: %s [-d unit] [-f file] [-l] [-m] [-q] [-t tempo] [-v] [-x] [file ...]\n",
134: __progname);
135: exit(1);
136: }
137:
138: int showmeta = 0;
139: int verbose = 0;
140: #define BASETEMPO 400000
141: u_int tempo = BASETEMPO; /* microsec / quarter note */
142: u_int ttempo = 100;
143: int unit = 0;
144: int play = 1;
145: int fd;
146:
147: void
148: send_event(ev)
149: seq_event_rec *ev;
150: {
151: /*
152: printf("%02x %02x %02x %02x %02x %02x %02x %02x\n",
153: ev->arr[0], ev->arr[1], ev->arr[2], ev->arr[3],
154: ev->arr[4], ev->arr[5], ev->arr[6], ev->arr[7]);
155: */
156: if (play)
157: write(fd, ev, sizeof *ev);
158: }
159:
160: u_long
161: getvar(tp)
162: struct track *tp;
163: {
164: u_long r, c;
165:
166: r = 0;
167: do {
168: c = *tp->start++;
169: r = (r << 7) | (c & 0x7f);
170: } while ((c & 0x80) && tp->start < tp->end);
171: return (r);
172: }
173:
174: void
175: dometa(meta, p, len)
176: u_int meta;
177: u_char *p;
178: u_int len;
179: {
180: switch (meta) {
181: case META_TEXT:
182: case META_COPYRIGHT:
183: case META_TRACK:
184: case META_INSTRUMENT:
185: case META_LYRIC:
186: case META_MARKER:
187: case META_CUE:
188: if (showmeta) {
189: printf("%s: ", metanames[meta]);
190: fwrite(p, len, 1, stdout);
191: printf("\n");
192: }
193: break;
194: case META_SET_TEMPO:
195: tempo = GET24(p);
196: if (showmeta)
197: printf("Tempo: %d us / quarter note\n", tempo);
198: break;
199: case META_TIMESIGN:
200: if (showmeta) {
201: int n = p[1];
202: int d = 1;
203: while (n-- > 0)
204: d *= 2;
205: printf("Time signature: %d/%d %d,%d\n",
206: p[0], d, p[2], p[3]);
207: }
208: break;
209: case META_KEY:
210: if (showmeta)
211: printf("Key: %d %s\n", (char)p[0],
212: p[1] ? "minor" : "major");
213: break;
214: default:
215: break;
216: }
217: }
218:
219: void
220: midireset()
221: {
222: /* General MIDI reset sequence */
223: static u_char gm_reset[] = { 0x7e, 0x7f, 0x09, 0x01, 0xf7 };
224:
225: send_sysex(gm_reset, sizeof gm_reset);
226: }
227:
228: #define SYSEX_CHUNK 6
229: void
230: send_sysex(p, l)
231: u_char *p;
232: u_int l;
233: {
234: seq_event_rec event;
235: u_int n;
236:
237: event.arr[0] = SEQ_SYSEX;
238: event.arr[1] = unit;
239: do {
240: n = SYSEX_CHUNK;
241: if (l < n) {
242: memset(&event.arr[2], 0xff, SYSEX_CHUNK);
243: n = l;
244: }
245: memcpy(&event.arr[2], p, n);
246: send_event(&event);
247: l -= n;
248: } while (l > 0);
249: }
250:
251: void
252: playfile(f, name)
253: FILE *f;
254: char *name;
255: {
256: u_char *buf;
257: u_int tot, n, size, nread;
258:
259: /*
260: * We need to read the whole file into memory for easy processing.
261: * Using mmap() would be nice, but some file systems do not support
262: * it, nor does reading from e.g. a pipe. The latter also precludes
263: * finding out the file size without reading it.
264: */
265: size = 1000;
266: buf = malloc(size);
267: if (buf == 0)
1.3 ! mpech 268: errx(1, "malloc() failed");
1.1 niklas 269: nread = size;
270: tot = 0;
271: for (;;) {
272: n = fread(buf + tot, 1, nread, f);
273: tot += n;
274: if (n < nread)
275: break;
276: /* There must be more to read. */
277: nread = size;
278: size *= 2;
279: buf = realloc(buf, size);
280: if (buf == NULL)
1.3 ! mpech 281: errx(1, "realloc() failed");
1.1 niklas 282: }
283: playdata(buf, tot, name);
284: free(buf);
285: }
286:
287: void
288: playdata(buf, tot, name)
289: u_char *buf;
290: u_int tot;
291: char *name;
292: {
293: int format, ntrks, divfmt, ticks, t, besttrk = 0;
294: u_int len, mlen, status, chan;
295: u_char *p, *end, byte, meta, *msg;
296: struct track *tracks;
297: u_long bestcur, now;
298: struct track *tp;
299: seq_event_rec event;
300:
301: end = buf + tot;
302: if (verbose)
303: printf("Playing %s (%d bytes) ... \n", name, tot);
304:
305: if (memcmp(buf, MARK_HEADER, MARK_LEN) != 0) {
1.3 ! mpech 306: warnx("Not a MIDI file, missing header");
1.1 niklas 307: return;
308: }
309: if (GET32(buf + MARK_LEN) != HEADER_LEN) {
1.3 ! mpech 310: warnx("Not a MIDI file, bad header");
1.1 niklas 311: return;
312: }
313: format = GET16(buf + MARK_LEN + SIZE_LEN);
314: ntrks = GET16(buf + MARK_LEN + SIZE_LEN + 2);
315: divfmt = GET8(buf + MARK_LEN + SIZE_LEN + 4);
316: ticks = GET8(buf + MARK_LEN + SIZE_LEN + 5);
317: p = buf + MARK_LEN + SIZE_LEN + HEADER_LEN;
318: if ((divfmt & 0x80) == 0)
319: ticks |= divfmt << 8;
320: else
1.3 ! mpech 321: errx(1, "Absolute time codes not implemented yet");
1.1 niklas 322: if (verbose > 1)
323: printf("format=%d ntrks=%d divfmt=%x ticks=%d\n",
324: format, ntrks, divfmt, ticks);
325: if (format != 0 && format != 1) {
1.3 ! mpech 326: warnx("Cannnot play MIDI file of type %d", format);
1.1 niklas 327: return;
328: }
329: if (ntrks == 0)
330: return;
331: tracks = malloc(ntrks * sizeof(struct track));
332: if (tracks == NULL)
1.3 ! mpech 333: errx(1, "malloc() tracks failed");
1.1 niklas 334: for (t = 0; t < ntrks; ) {
335: if (p >= end - MARK_LEN - SIZE_LEN) {
1.3 ! mpech 336: warnx("Cannot find track %d", t);
1.1 niklas 337: goto ret;
338: }
339: len = GET32(p + MARK_LEN);
340: if (len > 1000000) { /* a safe guard */
1.3 ! mpech 341: warnx("Crazy track length");
1.1 niklas 342: goto ret;
343: }
344: if (memcmp(p, MARK_TRACK, MARK_LEN) == 0) {
345: tracks[t].start = p + MARK_LEN + SIZE_LEN;
346: tracks[t].end = tracks[t].start + len;
347: tracks[t].curtime = getvar(&tracks[t]);
348: t++;
349: }
350: p += MARK_LEN + SIZE_LEN + len;
351: }
352:
353: /*
354: * Play MIDI events by selecting the track track with the lowest
355: * curtime. Execute the event, update the curtime and repeat.
356: */
357:
358: /*
359: * The ticks variable is the number of ticks that make up a quarter
360: * note and is used as a reference value for the delays between
361: * the MIDI events.
362: * The sequencer has two "knobs": the TIMEBASE and the TEMPO.
363: * The delay specified in TMR_WAIT_REL is specified in
364: * sequencer time units. The length of a unit is
365: * 60*1000000 / (TIMEBASE * TEMPO).
366: * Set it to 1ms/unit (adjusted by user tempo changes).
367: */
368: t = 500 * ttempo / 100;
369: if (ioctl(fd, SEQUENCER_TMR_TIMEBASE, &t) < 0)
370: err(1, "SEQUENCER_TMR_TIMEBASE");
371: t = 120;
372: if (ioctl(fd, SEQUENCER_TMR_TEMPO, &t) < 0)
373: err(1, "SEQUENCER_TMR_TEMPO");
374: if (ioctl(fd, SEQUENCER_TMR_START, 0) < 0)
375: err(1, "SEQUENCER_TMR_START");
376: now = 0;
377: for (;;) {
378: /* Locate lowest curtime */
379: bestcur = ~0;
380: for (t = 0; t < ntrks; t++) {
381: if (tracks[t].curtime < bestcur) {
382: bestcur = tracks[t].curtime;
383: besttrk = t;
384: }
385: }
386: if (bestcur == ~0)
387: break;
388: if (verbose > 1) {
389: printf("DELAY %4ld TRACK %2d ", bestcur-now, besttrk);
390: fflush(stdout);
391: }
392: if (now < bestcur) {
393: union {
394: u_int32_t i;
395: u_int8_t b[4];
396: } u;
397: u_int32_t delta = bestcur - now;
398: delta = (int)((double)delta * tempo / (1000.0*ticks));
399: u.i = delta;
400: if (delta != 0) {
401: event.arr[0] = SEQ_TIMING;
402: event.arr[1] = TMR_WAIT_REL;
403: event.arr[4] = u.b[0];
404: event.arr[5] = u.b[1];
405: event.arr[6] = u.b[2];
406: event.arr[7] = u.b[3];
407: send_event(&event);
408: }
409: }
410: now = bestcur;
411: tp = &tracks[besttrk];
412: byte = *tp->start++;
413: if (byte == MIDI_META) {
414: meta = *tp->start++;
415: mlen = getvar(tp);
416: if (verbose > 1)
417: printf("META %02x (%d)\n", meta, mlen);
418: dometa(meta, tp->start, mlen);
419: tp->start += mlen;
420: } else {
421: if (MIDI_IS_STATUS(byte))
422: tp->status = byte;
423: else
424: tp->start--;
425: mlen = MIDI_LENGTH(tp->status);
426: msg = tp->start;
427: if (verbose > 1) {
428: if (mlen == 1)
429: printf("MIDI %02x (%d) %02x\n",
430: tp->status, mlen, msg[0]);
431: else
432: printf("MIDI %02x (%d) %02x %02x\n",
433: tp->status, mlen, msg[0], msg[1]);
434: }
435: status = MIDI_GET_STATUS(tp->status);
436: chan = MIDI_GET_CHAN(tp->status);
437: switch (status) {
438: case MIDI_NOTEOFF:
439: case MIDI_NOTEON:
440: case MIDI_KEY_PRESSURE:
441: SEQ_MK_CHN_VOICE(&event, unit, status, chan,
442: msg[0], msg[1]);
443: send_event(&event);
444: break;
445: case MIDI_CTL_CHANGE:
446: SEQ_MK_CHN_COMMON(&event, unit, status, chan,
447: msg[0], 0, msg[1]);
448: send_event(&event);
449: break;
450: case MIDI_PGM_CHANGE:
451: case MIDI_CHN_PRESSURE:
452: SEQ_MK_CHN_COMMON(&event, unit, status, chan,
453: msg[0], 0, 0);
454: send_event(&event);
455: break;
456: case MIDI_PITCH_BEND:
457: SEQ_MK_CHN_COMMON(&event, unit, status, chan,
458: 0, 0,
459: (msg[0] & 0x7f) |
460: ((msg[1] & 0x7f) << 7));
461: send_event(&event);
462: break;
463: case MIDI_SYSTEM_PREFIX:
464: mlen = getvar(tp);
465: if (tp->status == MIDI_SYSEX_START)
466: send_sysex(tp->start, mlen);
467: else
468: /* Sorry, can't do this yet */;
469: break;
470: default:
471: if (verbose)
472: printf("MIDI event 0x%02x ignored\n",
473: tp->status);
474: }
475: tp->start += mlen;
476: }
477: if (tp->start >= tp->end)
478: tp->curtime = ~0;
479: else
480: tp->curtime += getvar(tp);
481: }
482: if (ioctl(fd, SEQUENCER_SYNC, 0) < 0)
483: err(1, "SEQUENCER_SYNC");
484:
485: ret:
486: free(tracks);
487: }
488:
489: int
490: main(argc, argv)
491: int argc;
492: char **argv;
493: {
494: int ch;
495: int listdevs = 0;
496: int example = 0;
497: int nmidi;
498: char *file = DEVMUSIC;
499: struct synth_info info;
500: FILE *f;
501:
502: while ((ch = getopt(argc, argv, "?d:f:lmqt:vx")) != -1) {
503: switch(ch) {
504: case 'd':
505: unit = atoi(optarg);
506: break;
507: case 'f':
508: file = optarg;
509: break;
510: case 'l':
511: listdevs++;
512: break;
513: case 'm':
514: showmeta++;
515: break;
516: case 'q':
517: play = 0;
518: break;
519: case 't':
520: ttempo = atoi(optarg);
521: break;
522: case 'v':
523: verbose++;
524: break;
525: case 'x':
526: example++;
527: break;
528: case '?':
529: default:
530: usage();
531: }
532: }
533: argc -= optind;
534: argv += optind;
535:
536: fd = open(file, O_WRONLY);
537: if (fd < 0)
538: err(1, "%s", file);
539: if (ioctl(fd, SEQUENCER_NRMIDIS, &nmidi) < 0)
540: err(1, "ioctl(SEQUENCER_NRMIDIS) failed, ");
541: if (nmidi == 0)
1.3 ! mpech 542: errx(1, "Sorry, no MIDI devices available");
1.1 niklas 543: if (listdevs) {
544: for (info.device = 0; info.device < nmidi; info.device++) {
545: if (ioctl(fd, SEQUENCER_INFO, &info) < 0)
546: err(1, "ioctl(SEQUENCER_INFO) failed, ");
547: printf("%d: %s\n", info.device, info.name);
548: }
549: exit(0);
550: }
551: midireset();
552: if (example)
553: playdata(sample, sizeof sample, "<Gubben Noa>");
554: else if (argc == 0)
555: playfile(stdin, "<stdin>");
556: else
557: while (argc--) {
558: f = fopen(*argv, "r");
559: if (f == NULL)
560: err(1, "%s", *argv);
561: else {
562: playfile(f, *argv);
563: fclose(f);
564: }
565: argv++;
566: }
567:
568: exit(0);
569: }