Home | History | Annotate | Line # | Download | only in midirecord
midirecord.c revision 1.3
      1 /*	$NetBSD: midirecord.c,v 1.3 2014/12/30 12:14:34 wiz Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2014 Matthew R. Green
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  * SUCH DAMAGE.
     27  */
     28 
     29 /*
     30  * midirecord(1), similar to audiorecord(1).
     31  */
     32 
     33 #include <sys/cdefs.h>
     34 
     35 #ifndef lint
     36 __RCSID("$NetBSD: midirecord.c,v 1.3 2014/12/30 12:14:34 wiz Exp $");
     37 #endif
     38 
     39 #include <sys/param.h>
     40 #include <sys/midiio.h>
     41 #include <sys/ioctl.h>
     42 #include <sys/time.h>
     43 #include <sys/uio.h>
     44 
     45 #include <err.h>
     46 #include <errno.h>
     47 #include <ctype.h>
     48 #include <fcntl.h>
     49 #include <paths.h>
     50 #include <signal.h>
     51 #include <stdio.h>
     52 #include <stdlib.h>
     53 #include <string.h>
     54 #include <unistd.h>
     55 #include <util.h>
     56 #include <assert.h>
     57 #include <stdbool.h>
     58 
     59 #include "libaudio.h"
     60 
     61 static const char *midi_device;
     62 static unsigned	*filt_devnos = NULL;
     63 static unsigned	*filt_chans = NULL;
     64 static unsigned num_filt_devnos, num_filt_chans;
     65 static char	*raw_output;
     66 static int	midifd;
     67 static int	aflag, qflag, oflag;
     68 static bool debug = false;
     69 int	verbose;
     70 static int	outfd, rawfd = -1;
     71 static ssize_t	data_size;
     72 static struct timeval record_time;
     73 static struct timeval start_time;
     74 static int	tempo = 120;
     75 static unsigned	notes_per_beat = 24;
     76 static bool ignore_timer_fail = false;
     77 
     78 static void debug_log(const char *, size_t, const char *, ...);
     79 static size_t midi_event_local_to_output(seq_event_t, u_char *, size_t);
     80 static size_t midi_event_timer_wait_abs_to_output(seq_event_t, u_char *,
     81 						  size_t);
     82 static size_t midi_event_timer_to_output(seq_event_t, u_char *, size_t);
     83 static size_t midi_event_chn_common_to_output(seq_event_t, u_char *, size_t);
     84 static size_t midi_event_chn_voice_to_output(seq_event_t, u_char *, size_t);
     85 static size_t midi_event_sysex_to_output(seq_event_t, u_char *, size_t);
     86 static size_t midi_event_fullsize_to_output(seq_event_t, u_char *, size_t);
     87 static size_t midi_event_to_output(seq_event_t, u_char *, size_t);
     88 static int timeleft(struct timeval *, struct timeval *);
     89 static bool filter_array(unsigned, unsigned *, size_t);
     90 static bool filter_dev(unsigned);
     91 static bool filter_chan(unsigned);
     92 static bool filter_devchan(unsigned, unsigned);
     93 static void parse_ints(const char *, unsigned **, unsigned *, const char *);
     94 static void cleanup(int) __dead;
     95 static void rewrite_header(void);
     96 static void write_midi_header(void);
     97 static void write_midi_trailer(void);
     98 static void usage(void) __dead;
     99 
    100 #define PATH_DEV_MUSIC "/dev/music"
    101 
    102 int
    103 main(int argc, char *argv[])
    104 {
    105 	u_char	*buffer;
    106 	size_t	bufsize = 0;
    107 	int	ch, no_time_limit = 1;
    108 
    109 	while ((ch = getopt(argc, argv, "aB:c:Dd:f:hn:qr:t:T:V")) != -1) {
    110 		switch (ch) {
    111 		case 'a':
    112 			aflag++;
    113 			break;
    114 		case 'B':
    115 			bufsize = strsuftoll("read buffer size", optarg,
    116 					     1, UINT_MAX);
    117 			break;
    118 		case 'c':
    119 			parse_ints(optarg, &filt_chans, &num_filt_chans,
    120 			    "channels");
    121 			break;
    122 		case 'D':
    123 			debug++;
    124 			break;
    125 		case 'd':
    126 			parse_ints(optarg, &filt_devnos, &num_filt_devnos,
    127 			    "devices");
    128 			break;
    129 		case 'f':
    130 			midi_device = optarg;
    131 			ignore_timer_fail = true;
    132 			break;
    133 		case 'n':
    134 			decode_uint(optarg, &notes_per_beat);
    135 			break;
    136 		case 'o':
    137 			oflag++;	/* time stamp starts at proc start */
    138 			break;
    139 		case 'q':
    140 			qflag++;
    141 			break;
    142 		case 'r':
    143 			raw_output = optarg;
    144 			break;
    145 		case 't':
    146 			no_time_limit = 0;
    147 			decode_time(optarg, &record_time);
    148 			break;
    149 		case 'T':
    150 			decode_int(optarg, &tempo);
    151 			break;
    152 		case 'V':
    153 			verbose++;
    154 			break;
    155 		/* case 'h': */
    156 		default:
    157 			usage();
    158 			/* NOTREACHED */
    159 		}
    160 	}
    161 	argc -= optind;
    162 	argv += optind;
    163 
    164 	if (argc != 1)
    165 		usage();
    166 
    167 	/*
    168 	 * work out the buffer size to use, and allocate it.  don't allow it
    169 	 * to be too small.
    170 	 */
    171 	if (bufsize < 32)
    172 		bufsize = 32 * 1024;
    173 	buffer = malloc(bufsize);
    174 	if (buffer == NULL)
    175 		err(1, "couldn't malloc buffer of %d size", (int)bufsize);
    176 
    177 	/*
    178 	 * open the music device
    179 	 */
    180 	if (midi_device == NULL && (midi_device = getenv("MIDIDEVICE")) == NULL)
    181 		midi_device = PATH_DEV_MUSIC;
    182 	midifd = open(midi_device, O_RDONLY);
    183 	if (midifd < 0)
    184 		err(1, "failed to open %s", midi_device);
    185 
    186 	/* open the output file */
    187 	if (argv[0][0] != '-' || argv[0][1] != '\0') {
    188 		int mode = O_CREAT|(aflag ? O_APPEND : O_TRUNC)|O_WRONLY;
    189 
    190 		outfd = open(*argv, mode, 0666);
    191 		if (outfd < 0)
    192 			err(1, "could not open %s", *argv);
    193 	} else
    194 		outfd = STDOUT_FILENO;
    195 
    196 	/* open the raw output file */
    197 	if (raw_output) {
    198 		int mode = O_CREAT|(aflag ? O_APPEND : O_TRUNC)|O_WRONLY;
    199 
    200 		rawfd = open(raw_output, mode, 0666);
    201 		if (rawfd < 0)
    202 			err(1, "could not open %s", raw_output);
    203 	}
    204 
    205 	/* start the midi timer */
    206 	if (ioctl(midifd, SEQUENCER_TMR_START, NULL) < 0) {
    207 		if (ignore_timer_fail)
    208 			warn("failed to start midi timer");
    209 		else
    210 			err(1, "failed to start midi timer");
    211 	}
    212 
    213 	/* set the timebase */
    214 	if (ioctl(midifd, SEQUENCER_TMR_TIMEBASE, &notes_per_beat) < 0) {
    215 		if (ignore_timer_fail)
    216 			warn("SEQUENCER_TMR_TIMEBASE: notes_per_beat %d",
    217 			     notes_per_beat);
    218 		else
    219 			err(1, "SEQUENCER_TMR_TIMEBASE: notes_per_beat %d",
    220 			    notes_per_beat);
    221 	}
    222 
    223 	/* set the tempo */
    224 	if (ioctl(midifd, SEQUENCER_TMR_TEMPO, &tempo) < 0) {
    225 		if (ignore_timer_fail)
    226 			warn("SEQUENCER_TMR_TIMEBASE: tempo %d", tempo);
    227 		else
    228 			err(1, "SEQUENCER_TMR_TIMEBASE: tempo %d", tempo);
    229 	}
    230 
    231 	signal(SIGINT, cleanup);
    232 
    233 	data_size = 0;
    234 
    235 	if (verbose)
    236 		fprintf(stderr, "tempo=%d notes_per_beat=%d\n",
    237 		   tempo, notes_per_beat);
    238 
    239 	if (!no_time_limit && verbose)
    240 		fprintf(stderr, "recording for %lu seconds, %lu microseconds\n",
    241 		    (u_long)record_time.tv_sec, (u_long)record_time.tv_usec);
    242 
    243 	/* Create a midi header. */
    244 	write_midi_header();
    245 
    246 	(void)gettimeofday(&start_time, NULL);
    247 	while (no_time_limit || timeleft(&start_time, &record_time)) {
    248 		seq_event_t e;
    249 		size_t wrsize;
    250 		size_t rdsize;
    251 
    252 		rdsize = (size_t)read(midifd, &e, sizeof e);
    253 		if (rdsize == 0)
    254 			break;
    255 
    256 		if (rdsize != sizeof e)
    257 			err(1, "read failed");
    258 
    259 		if (rawfd != -1 && write(rawfd, &e, sizeof e) != sizeof e)
    260 			err(1, "write to raw file failed");
    261 
    262 		/* convert 'e' into something useful for output */
    263 		wrsize = midi_event_to_output(e, buffer, bufsize);
    264 
    265 		if (wrsize) {
    266 			if ((size_t)write(outfd, buffer, wrsize) != wrsize)
    267 				err(1, "write failed");
    268 			data_size += wrsize;
    269 		}
    270 	}
    271 	cleanup(0);
    272 }
    273 
    274 static void
    275 debug_log(const char *file, size_t line, const char *fmt, ...)
    276 {
    277 	va_list ap;
    278 
    279 	if (!debug)
    280 		return;
    281 	fprintf(stderr, "%s:%zd: ", file, line);
    282 	va_start(ap, fmt);
    283 	vfprintf(stderr, fmt, ap);
    284 	va_end(ap);
    285 	fprintf(stderr, "\n");
    286 }
    287 
    288 #define LOG(fmt...) \
    289 	debug_log(__func__, __LINE__, fmt)
    290 
    291 
    292 /*
    293  * handle a SEQ_LOCAL event.  NetBSD /dev/music doesn't generate these.
    294  */
    295 static size_t
    296 midi_event_local_to_output(seq_event_t e, u_char *buffer, size_t bufsize)
    297 {
    298 	size_t	size = 0;
    299 
    300 	LOG("UNHANDLED SEQ_COCAL");
    301 
    302 	return size;
    303 }
    304 
    305 /*
    306  * convert a midi absolute time event to a variable length delay
    307  */
    308 static size_t
    309 midi_event_timer_wait_abs_to_output(
    310 	seq_event_t e,
    311 	u_char *buffer,
    312 	size_t bufsize)
    313 {
    314 	static unsigned prev_div;
    315 	unsigned cur_div;
    316 	unsigned val = 0, div;
    317 	int vallen = 0, i;
    318 
    319 	if (prev_div == 0 && !oflag)
    320 		prev_div = e.t_WAIT_ABS.divisions;
    321 	cur_div = e.t_WAIT_ABS.divisions;
    322 
    323 	div = cur_div - prev_div;
    324 	if (div) {
    325 		while (div) {
    326 			uint32_t extra = val ? 0x80 : 0;
    327 
    328 			val <<= 8;
    329 			val |= (div & 0x7f) | extra;
    330 			div >>= 7;
    331 			vallen++;
    332 		}
    333 	} else
    334 		vallen = 1;
    335 
    336 	for (i = 0; i < vallen; i++) {
    337 		buffer[i] = val & 0xff;
    338 		val >>= 8;
    339 	}
    340 	for (; i < 4; i++)
    341 		buffer[i] = 0;
    342 	LOG("TMR_WAIT_ABS: new div %x (cur %x prev %x): bufval (len=%u): "
    343 	    "%02x:%02x:%02x:%02x",
    344 	    cur_div - prev_div, cur_div, prev_div,
    345 	    vallen, buffer[0], buffer[1], buffer[2], buffer[3]);
    346 
    347 	prev_div = cur_div;
    348 
    349 	return vallen;
    350 }
    351 
    352 /*
    353  * handle a SEQ_TIMING event.
    354  */
    355 static size_t
    356 midi_event_timer_to_output(seq_event_t e, u_char *buffer, size_t bufsize)
    357 {
    358 	size_t	size = 0;
    359 
    360 	LOG("SEQ_TIMING");
    361 	switch (e.timing.op) {
    362 	case TMR_WAIT_REL:
    363 		/* NetBSD /dev/music doesn't generate these. */
    364 		LOG("UNHANDLED TMR_WAIT_REL: divisions: %x", e.t_WAIT_REL.divisions);
    365 		break;
    366 
    367 	case TMR_WAIT_ABS:
    368 		size = midi_event_timer_wait_abs_to_output(e, buffer, bufsize);
    369 		break;
    370 
    371 	case TMR_STOP:
    372 	case TMR_START:
    373 	case TMR_CONTINUE:
    374 	case TMR_TEMPO:
    375 	case TMR_ECHO:
    376 	case TMR_CLOCK:
    377 	case TMR_SPP:
    378 	case TMR_TIMESIG:
    379 		/* NetBSD /dev/music doesn't generate these. */
    380 		LOG("UNHANDLED timer op: %x", e.timing.op);
    381 		break;
    382 
    383 	default:
    384 		LOG("unknown timer op: %x", e.timing.op);
    385 		break;
    386 	}
    387 
    388 	return size;
    389 }
    390 
    391 /*
    392  * handle a SEQ_CHN_COMMON event.
    393  */
    394 static size_t
    395 midi_event_chn_common_to_output(seq_event_t e, u_char *buffer, size_t bufsize)
    396 {
    397 	size_t	size = 0;
    398 
    399 	assert(e.common.channel < 16);
    400 	LOG("SEQ_CHN_COMMON");
    401 
    402 	if (filter_devchan(e.common.device, e.common.channel))
    403 		return 0;
    404 
    405 	switch (e.common.op) {
    406 	case MIDI_CTL_CHANGE:
    407 		buffer[0] = MIDI_CTL_CHANGE | e.c_CTL_CHANGE.channel;
    408 		buffer[1] = e.c_CTL_CHANGE.controller;
    409 		buffer[2] = e.c_CTL_CHANGE.value;
    410 		LOG("MIDI_CTL_CHANGE: channel %x ctrl %x val %x",
    411 		    e.c_CTL_CHANGE.channel, e.c_CTL_CHANGE.controller,
    412 		    e.c_CTL_CHANGE.value);
    413 		size = 3;
    414 		break;
    415 
    416 	case MIDI_PGM_CHANGE:
    417 		buffer[0] = MIDI_PGM_CHANGE | e.c_PGM_CHANGE.channel;
    418 		buffer[1] = e.c_PGM_CHANGE.program;
    419 		LOG("MIDI_PGM_CHANGE: channel %x program %x",
    420 		    e.c_PGM_CHANGE.channel, e.c_PGM_CHANGE.program);
    421 		size = 2;
    422 		break;
    423 
    424 	case MIDI_CHN_PRESSURE:
    425 		buffer[0] = MIDI_CHN_PRESSURE | e.c_CHN_PRESSURE.channel;
    426 		buffer[1] = e.c_CHN_PRESSURE.pressure;
    427 		LOG("MIDI_CHN_PRESSURE: channel %x pressure %x",
    428 		    e.c_CHN_PRESSURE.channel, e.c_CHN_PRESSURE.pressure);
    429 		size = 2;
    430 		break;
    431 
    432 	case MIDI_PITCH_BEND:
    433 		buffer[0] = MIDI_PITCH_BEND | e.c_PITCH_BEND.channel;
    434 		/* 14 bits split over 2 data bytes, lsb first */
    435 		buffer[1] = e.c_PITCH_BEND.value & 0x7f;
    436 		buffer[2] = (e.c_PITCH_BEND.value >> 7) & 0x7f;
    437 		LOG("MIDI_PITCH_BEND: channel %x val %x",
    438 		    e.c_PITCH_BEND.channel, e.c_PITCH_BEND.value);
    439 		size = 3;
    440 		break;
    441 
    442 	default:
    443 		LOG("unknown common op: %x", e.voice.op);
    444 		break;
    445 	}
    446 
    447 	return size;
    448 }
    449 
    450 /*
    451  * handle a SEQ_CHN_VOICE event.
    452  */
    453 static size_t
    454 midi_event_chn_voice_to_output(seq_event_t e, u_char *buffer, size_t bufsize)
    455 {
    456 	size_t	size = 0;
    457 
    458 	assert(e.common.channel < 16);
    459 	LOG("SEQ_CHN_VOICE");
    460 
    461 	if (filter_devchan(e.voice.device, e.voice.channel))
    462 		return 0;
    463 
    464 	switch (e.voice.op) {
    465 	case MIDI_NOTEOFF:
    466 		buffer[0] = MIDI_NOTEOFF | e.c_NOTEOFF.channel;
    467 		buffer[1] = e.c_NOTEOFF.key;
    468 		buffer[2] = e.c_NOTEOFF.velocity;
    469 
    470 		LOG("MIDI_NOTEOFF: channel %x key %x velocity %x",
    471 		    e.c_NOTEOFF.channel, e.c_NOTEOFF.key, e.c_NOTEOFF.velocity);
    472 		size = 3;
    473 		break;
    474 
    475 	case MIDI_NOTEON:
    476 		buffer[0] = MIDI_NOTEON | e.c_NOTEON.channel;
    477 		buffer[1] = e.c_NOTEON.key;
    478 		buffer[2] = e.c_NOTEON.velocity;
    479 
    480 		LOG("MIDI_NOTEON: channel %x key %x velocity %x",
    481 		    e.c_NOTEON.channel, e.c_NOTEON.key, e.c_NOTEON.velocity);
    482 		size = 3;
    483 		break;
    484 
    485 	case MIDI_KEY_PRESSURE:
    486 		buffer[0] = MIDI_KEY_PRESSURE | e.c_KEY_PRESSURE.channel;
    487 		buffer[1] = e.c_KEY_PRESSURE.key;
    488 		buffer[2] = e.c_KEY_PRESSURE.pressure;
    489 
    490 		LOG("MIDI_KEY_PRESSURE: channel %x key %x pressure %x",
    491 		    e.c_KEY_PRESSURE.channel, e.c_KEY_PRESSURE.key,
    492 		    e.c_KEY_PRESSURE.pressure);
    493 		size = 3;
    494 		break;
    495 
    496 	default:
    497 		LOG("unknown voice op: %x", e.voice.op);
    498 		break;
    499 	}
    500 
    501 	return size;
    502 }
    503 
    504 /*
    505  * handle a SEQ_SYSEX event.  NetBSD /dev/music doesn't generate these.
    506  */
    507 static size_t
    508 midi_event_sysex_to_output(seq_event_t e, u_char *buffer, size_t bufsize)
    509 {
    510 	size_t	size = 0;
    511 
    512 	LOG("UNHANDLED SEQ_SYSEX");
    513 
    514 	return size;
    515 }
    516 
    517 /*
    518  * handle a SEQ_FULLSIZE event.  NetBSD /dev/music doesn't generate these.
    519  */
    520 static size_t
    521 midi_event_fullsize_to_output(seq_event_t e, u_char *buffer, size_t bufsize)
    522 {
    523 	size_t	size = 0;
    524 
    525 	LOG("UNHANDLED SEQ_FULLSIZE");
    526 
    527 	return size;
    528 }
    529 
    530 /*
    531  * main handler for MIDI events.
    532  */
    533 static size_t
    534 midi_event_to_output(seq_event_t e, u_char *buffer, size_t bufsize)
    535 {
    536 	size_t size = 0;
    537 
    538 	/* XXX so far we only process 4 byte returns */
    539 	assert(bufsize >= 4);
    540 
    541 	LOG("event: %02x:%02x:%02x:%02x %02x:%02x:%02x:%02x", e.tag,
    542 	     e.unknown.byte[0], e.unknown.byte[1],
    543 	     e.unknown.byte[2], e.unknown.byte[3],
    544 	     e.unknown.byte[4], e.unknown.byte[5],
    545 	     e.unknown.byte[6]);
    546 
    547 	switch (e.tag) {
    548 	case SEQ_LOCAL:
    549 		size = midi_event_local_to_output(e, buffer, bufsize);
    550 		break;
    551 
    552 	case SEQ_TIMING:
    553 		size = midi_event_timer_to_output(e, buffer, bufsize);
    554 		break;
    555 
    556 	case SEQ_CHN_COMMON:
    557 		size = midi_event_chn_common_to_output(e, buffer, bufsize);
    558 		break;
    559 
    560 	case SEQ_CHN_VOICE:
    561 		size = midi_event_chn_voice_to_output(e, buffer, bufsize);
    562 		break;
    563 
    564 	case SEQ_SYSEX:
    565 		size = midi_event_sysex_to_output(e, buffer, bufsize);
    566 		break;
    567 
    568 	case SEQ_FULLSIZE:
    569 		size = midi_event_fullsize_to_output(e, buffer, bufsize);
    570 		break;
    571 
    572 	default:
    573 		errx(1, "don't understand midi tag %x", e.tag);
    574 	}
    575 
    576 	return size;
    577 }
    578 
    579 static bool
    580 filter_array(unsigned val, unsigned *array, size_t arraylen)
    581 {
    582 
    583 	if (array == NULL)
    584 		return false;
    585 
    586 	for (; arraylen; arraylen--)
    587 		if (array[arraylen - 1] == val)
    588 			return false;
    589 
    590 	return true;
    591 }
    592 
    593 static bool
    594 filter_dev(unsigned device)
    595 {
    596 
    597 	if (filter_array(device, filt_devnos, num_filt_devnos))
    598 		return true;
    599 
    600 	return false;
    601 }
    602 
    603 static bool
    604 filter_chan(unsigned channel)
    605 {
    606 
    607 	if (filter_array(channel, filt_chans, num_filt_chans))
    608 		return true;
    609 
    610 	return false;
    611 }
    612 
    613 static bool
    614 filter_devchan(unsigned device, unsigned channel)
    615 {
    616 
    617 	if (filter_dev(device) || filter_chan(channel))
    618 		return true;
    619 
    620 	return false;
    621 }
    622 
    623 static int
    624 timeleft(struct timeval *start_tvp, struct timeval *record_tvp)
    625 {
    626 	struct timeval now, diff;
    627 
    628 	(void)gettimeofday(&now, NULL);
    629 	timersub(&now, start_tvp, &diff);
    630 	timersub(record_tvp, &diff, &now);
    631 
    632 	return (now.tv_sec > 0 || (now.tv_sec == 0 && now.tv_usec > 0));
    633 }
    634 
    635 static void
    636 parse_ints(const char *str, unsigned **arrayp, unsigned *sizep, const char *msg)
    637 {
    638 	unsigned count = 1, u, longest = 0, c = 0;
    639 	unsigned *ip;
    640 	const char *s, *os;
    641 	char *num_buf;
    642 
    643 	/*
    644 	 * count all the comma separated values, and figre out
    645 	 * the longest one.
    646 	 */
    647 	for (s = str; *s; s++) {
    648 		c++;
    649 		if (*s == ',') {
    650 			count++;
    651 			if (c > longest)
    652 				longest = c;
    653 			c = 0;
    654 		}
    655 	}
    656 	*sizep = count;
    657 
    658 	num_buf = malloc(longest + 1);
    659 	ip = malloc(sizeof(*ip) * count);
    660 	if (!ip || !num_buf)
    661 		errx(1, "malloc failed");
    662 
    663 	for (count = 0, s = os = str, u = 0; *s; s++) {
    664 		if (*s == ',') {
    665 			num_buf[u] = '\0';
    666 			decode_uint(num_buf, &ip[count++]);
    667 			os = s + 1;
    668 			u = 0;
    669 		} else
    670 			num_buf[u++] = *s;
    671 	}
    672 	num_buf[u] = '\0';
    673 	decode_uint(num_buf, &ip[count++]);
    674 	*arrayp = ip;
    675 
    676 	if (verbose) {
    677 		fprintf(stderr, "Filtering %s in:", msg);
    678 		for (size_t i = 0; i < *sizep; i++)
    679 			fprintf(stderr, " %u", ip[i]);
    680 		fprintf(stderr, "\n");
    681 	}
    682 
    683 	free(num_buf);
    684 }
    685 
    686 static void
    687 cleanup(int signo)
    688 {
    689 
    690 	write_midi_trailer();
    691 	rewrite_header();
    692 
    693 	if (ioctl(midifd, SEQUENCER_TMR_STOP, NULL) < 0) {
    694 		if (ignore_timer_fail)
    695 			warn("failed to stop midi timer");
    696 		else
    697 			err(1, "failed to stop midi timer");
    698 	}
    699 
    700 	close(outfd);
    701 	close(midifd);
    702 	if (signo != 0)
    703 		(void)raise_default_signal(signo);
    704 
    705 	exit(0);
    706 }
    707 
    708 static void
    709 rewrite_header(void)
    710 {
    711 
    712 	/* can't do this here! */
    713 	if (outfd == STDOUT_FILENO)
    714 		return;
    715 
    716 	if (lseek(outfd, (off_t)0, SEEK_SET) == (off_t)-1)
    717 		err(1, "could not seek to start of file for header rewrite");
    718 	write_midi_header();
    719 }
    720 
    721 #define BYTE1(x)        ((x) & 0xff)
    722 #define BYTE2(x)        (((x) >> 8) & 0xff)
    723 #define BYTE3(x)        (((x) >> 16) & 0xff)
    724 #define BYTE4(x)        (((x) >> 24) & 0xff)
    725 
    726 static void
    727 write_midi_header(void)
    728 {
    729 	unsigned char header[] = {
    730 		'M', 'T', 'h', 'd',
    731 		0, 0, 0, 6,
    732 		0, 1,
    733 		0, 0, /* ntracks */
    734 		0, 0, /* notes per beat */
    735 	};
    736 	/* XXX only spport one track so far */
    737 	unsigned ntracks = 1;
    738 	unsigned char track[] = {
    739 		'M', 'T', 'r', 'k',
    740 		0, 0, 0, 0,
    741 	};
    742 	unsigned char bpm[] = {
    743 		0, 0xff, 0x51, 0x3,
    744 		0, 0, 0, /* inverse tempo */
    745 	};
    746 	unsigned total_size = data_size + sizeof header + sizeof track + sizeof bpm;
    747 
    748 	header[10] = BYTE2(ntracks);
    749 	header[11] = BYTE1(ntracks);
    750 	header[12] = BYTE2(notes_per_beat);
    751 	header[13] = BYTE1(notes_per_beat);
    752 
    753 	track[4] = BYTE4(total_size);
    754 	track[5] = BYTE3(total_size);
    755 	track[6] = BYTE2(total_size);
    756 	track[7] = BYTE1(total_size);
    757 
    758 #define TEMPO_INV(x)    (60000000UL / (x))
    759 	bpm[4] = BYTE3(TEMPO_INV(tempo));
    760 	bpm[5] = BYTE2(TEMPO_INV(tempo));
    761 	bpm[6] = BYTE1(TEMPO_INV(tempo));
    762 
    763 	if (write(outfd, header, sizeof header) != sizeof header)
    764 		err(1, "write of header failed");
    765 	if (write(outfd, track, sizeof track) != sizeof track)
    766 		err(1, "write of track header failed");
    767 	if (write(outfd, bpm, sizeof bpm) != sizeof bpm)
    768 		err(1, "write of bpm header failed");
    769 
    770 	LOG("wrote header: ntracks=%u notes_per_beat=%u tempo=%d total_size=%u",
    771 	    ntracks, notes_per_beat, tempo, total_size);
    772 }
    773 
    774 static void
    775 write_midi_trailer(void)
    776 {
    777 	unsigned char trailer[] = {
    778 		0, 0xff, 0x2f, 0,
    779 	};
    780 
    781 	if (write(outfd, trailer, sizeof trailer) != sizeof trailer)
    782 		err(1, "write of trailer failed");
    783 }
    784 
    785 static void
    786 usage(void)
    787 {
    788 
    789 	fprintf(stderr, "Usage: %s [-aDfhqV] [options] {outfile|-}\n",
    790 	    getprogname());
    791 	fprintf(stderr, "Options:\n"
    792 	    "\t-B buffer size\n"
    793 	    "\t-c channels\n"
    794 	    "\t-d devices\n"
    795 	    "\t-f sequencerdev\n"
    796 	    "\t-n notesperbeat\n"
    797 	    "\t-r raw_output\n"
    798 	    "\t-T tempo\n"
    799 	    "\t-t recording time\n");
    800 	exit(EXIT_FAILURE);
    801 }
    802