]> git.alsa-project.org Git - alsa-utils.git/blob - amidi/amidi.c
9a2aaf7205f0cb657d51cbc789537a56c7dc1f17
[alsa-utils.git] / amidi / amidi.c
1 /*
2  *  amidi.c - read from/write to RawMIDI ports
3  *
4  *  Copyright (c) Clemens Ladisch <clemens@ladisch.de>
5  *
6  *
7  *   This program is free software; you can redistribute it and/or modify
8  *   it under the terms of the GNU General Public License as published by
9  *   the Free Software Foundation; either version 2 of the License, or
10  *   (at your option) any later version.
11  *
12  *   This program is distributed in the hope that it will be useful,
13  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *   GNU General Public License for more details.
16  *
17  *   You should have received a copy of the GNU General Public License
18  *   along with this program; if not, write to the Free Software
19  *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
20  */
21
22 #define _GNU_SOURCE
23 #include "aconfig.h"
24 #include "version.h"
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <stdarg.h>
28 #include <string.h>
29 #include <ctype.h>
30 #include <math.h>
31 #include <getopt.h>
32 #include <errno.h>
33 #include <signal.h>
34 #include <sys/timerfd.h>
35 #include <sys/types.h>
36 #include <poll.h>
37 #include <sys/stat.h>
38 #include <unistd.h>
39 #include <fcntl.h>
40 #include <alsa/asoundlib.h>
41 #include <time.h>
42
43 #define NSEC_PER_SEC 1000000000L
44
45 static int do_print_timestamp = 0;
46 static int do_device_list, do_rawmidi_list;
47 static char *port_name = "default";
48 static char *send_file_name;
49 static char *receive_file_name;
50 static char *send_hex;
51 static char *send_data;
52 static int send_data_length;
53 static int receive_file;
54 static int dump;
55 static float timeout;
56 static int stop;
57 static int sysex_interval;
58 static snd_rawmidi_t *input, **inputp;
59 static snd_rawmidi_t *output, **outputp;
60 static int list_all;
61
62 static void error(const char *format, ...)
63 {
64         va_list ap;
65
66         va_start(ap, format);
67         vfprintf(stderr, format, ap);
68         va_end(ap);
69         putc('\n', stderr);
70 }
71
72 static void usage(void)
73 {
74         printf(
75                 "Usage: amidi options\n"
76                 "\n"
77                 "-h, --help                      this help\n"
78                 "-V, --version                   print current version\n"
79                 "-l, --list-devices              list all hardware ports\n"
80                 "-x, --list-inactive             list inactive ports, too\n"
81                 "-L, --list-rawmidis             list all RawMIDI definitions\n"
82                 "-p, --port=name                 select port by name\n"
83                 "-s, --send=file                 send the contents of a (.syx) file\n"
84                 "-r, --receive=file              write received data into a file\n"
85                 "-S, --send-hex=\"...\"            send hexadecimal bytes\n"
86                 "-d, --dump                      print received data as hexadecimal bytes\n"
87                 "-T, --timestamp=...             adds a timestamp in front of each dumped message\n"
88                 "                realtime\n"
89                 "                monotonic\n"
90 #ifdef CLOCK_MONOTONIC_RAW
91                 "                raw\n"
92 #endif
93                 "-t, --timeout=seconds           exits when no data has been received\n"
94                 "                                for the specified duration\n"
95                 "-a, --active-sensing            include active sensing bytes\n"
96                 "-c, --clock                     include clock bytes\n"
97                 "-i, --sysex-interval=mseconds   delay in between each SysEx message\n");
98 }
99
100 static void version(void)
101 {
102         puts("amidi version " SND_UTIL_VERSION_STR);
103 }
104
105 static void *my_malloc(size_t size)
106 {
107         void *p = malloc(size);
108         if (!p) {
109                 error("out of memory");
110                 exit(EXIT_FAILURE);
111         }
112         return p;
113 }
114
115 static void list_device(snd_ctl_t *ctl, int card, int device)
116 {
117         snd_rawmidi_info_t *info;
118         const char *name;
119         const char *sub_name;
120         int subs, subs_in, subs_out;
121         int sub;
122         int err;
123
124         snd_rawmidi_info_alloca(&info);
125         snd_rawmidi_info_set_device(info, device);
126
127         snd_rawmidi_info_set_stream(info, SND_RAWMIDI_STREAM_INPUT);
128         err = snd_ctl_rawmidi_info(ctl, info);
129         if (err >= 0)
130                 subs_in = snd_rawmidi_info_get_subdevices_count(info);
131         else
132                 subs_in = 0;
133
134         snd_rawmidi_info_set_stream(info, SND_RAWMIDI_STREAM_OUTPUT);
135         err = snd_ctl_rawmidi_info(ctl, info);
136         if (err >= 0)
137                 subs_out = snd_rawmidi_info_get_subdevices_count(info);
138         else
139                 subs_out = 0;
140
141         subs = subs_in > subs_out ? subs_in : subs_out;
142         if (!subs)
143                 return;
144
145         for (sub = 0; sub < subs; ++sub) {
146                 snd_rawmidi_info_set_stream(info, sub < subs_in ?
147                                             SND_RAWMIDI_STREAM_INPUT :
148                                             SND_RAWMIDI_STREAM_OUTPUT);
149                 snd_rawmidi_info_set_subdevice(info, sub);
150                 err = snd_ctl_rawmidi_info(ctl, info);
151                 if (err < 0) {
152                         error("cannot get rawmidi information %d:%d:%d: %s\n",
153                               card, device, sub, snd_strerror(err));
154                         return;
155                 }
156                 if (!list_all &&
157                     (snd_rawmidi_info_get_flags(info) & SNDRV_RAWMIDI_INFO_STREAM_INACTIVE))
158                         continue;
159                 name = snd_rawmidi_info_get_name(info);
160                 sub_name = snd_rawmidi_info_get_subdevice_name(info);
161                 if (sub == 0 && sub_name[0] == '\0') {
162                         printf("%c%c  hw:%d,%d    %s",
163                                sub < subs_in ? 'I' : ' ',
164                                sub < subs_out ? 'O' : ' ',
165                                card, device, name);
166                         if (subs > 1)
167                                 printf(" (%d subdevices)", subs);
168                         putchar('\n');
169                         break;
170                 } else {
171                         printf("%c%c  hw:%d,%d,%d  %s\n",
172                                sub < subs_in ? 'I' : ' ',
173                                sub < subs_out ? 'O' : ' ',
174                                card, device, sub, sub_name);
175                 }
176         }
177 }
178
179 static void list_card_devices(int card)
180 {
181         snd_ctl_t *ctl;
182         char name[32];
183         int device;
184         int err;
185
186         sprintf(name, "hw:%d", card);
187         if ((err = snd_ctl_open(&ctl, name, 0)) < 0) {
188                 error("cannot open control for card %d: %s", card, snd_strerror(err));
189                 return;
190         }
191         device = -1;
192         for (;;) {
193                 if ((err = snd_ctl_rawmidi_next_device(ctl, &device)) < 0) {
194                         error("cannot determine device number: %s", snd_strerror(err));
195                         break;
196                 }
197                 if (device < 0)
198                         break;
199                 list_device(ctl, card, device);
200         }
201         snd_ctl_close(ctl);
202 }
203
204 static void device_list(void)
205 {
206         int card, err;
207
208         card = -1;
209         if ((err = snd_card_next(&card)) < 0) {
210                 error("cannot determine card number: %s", snd_strerror(err));
211                 return;
212         }
213         if (card < 0) {
214                 error("no sound card found");
215                 return;
216         }
217         puts("Dir Device    Name");
218         do {
219                 list_card_devices(card);
220                 if ((err = snd_card_next(&card)) < 0) {
221                         error("cannot determine card number: %s", snd_strerror(err));
222                         break;
223                 }
224         } while (card >= 0);
225 }
226
227 static void rawmidi_list(void)
228 {
229         snd_output_t *output;
230         snd_config_t *config;
231         int err;
232
233         if ((err = snd_config_update()) < 0) {
234                 error("snd_config_update failed: %s", snd_strerror(err));
235                 return;
236         }
237         if ((err = snd_output_stdio_attach(&output, stdout, 0)) < 0) {
238                 error("snd_output_stdio_attach failed: %s", snd_strerror(err));
239                 return;
240         }
241         if (snd_config_search(snd_config, "rawmidi", &config) >= 0) {
242                 puts("RawMIDI list:");
243                 snd_config_save(config, output);
244         }
245         snd_output_close(output);
246 }
247
248 static int send_midi_interleaved(void)
249 {
250         int err;
251         char *data = send_data;
252         size_t buffer_size;
253         snd_rawmidi_params_t *param;
254         snd_rawmidi_status_t *st;
255
256         snd_rawmidi_status_alloca(&st);
257
258         snd_rawmidi_params_alloca(&param);
259         snd_rawmidi_params_current(output, param);
260         buffer_size = snd_rawmidi_params_get_buffer_size(param);
261
262         while (data < (send_data + send_data_length)) {
263                 int len = send_data + send_data_length - data;
264                 char *temp;
265
266                 if (data > send_data) {
267                         snd_rawmidi_status(output, st);
268                         do {
269                                 /* 320 µs per byte as noted in Page 1 of MIDI spec */
270                                 usleep((buffer_size - snd_rawmidi_status_get_avail(st)) * 320);
271                                 snd_rawmidi_status(output, st);
272                         } while(snd_rawmidi_status_get_avail(st) < buffer_size);
273                         usleep(sysex_interval * 1000);
274                 }
275
276                 /* find end of SysEx */
277                 if ((temp = memchr(data, 0xf7, len)) != NULL)
278                         len = temp - data + 1;
279
280                 if ((err = snd_rawmidi_write(output, data, len)) < 0)
281                         return err;
282
283                 data += len;
284         }
285
286         return 0;
287 }
288
289 static void load_file(void)
290 {
291         int fd;
292         off_t length;
293
294         fd = open(send_file_name, O_RDONLY);
295         if (fd == -1) {
296                 error("cannot open %s - %s", send_file_name, strerror(errno));
297                 return;
298         }
299         length = lseek(fd, 0, SEEK_END);
300         if (length == (off_t)-1) {
301                 error("cannot determine length of %s: %s", send_file_name, strerror(errno));
302                 goto _error;
303         }
304         send_data = my_malloc(length);
305         lseek(fd, 0, SEEK_SET);
306         if (read(fd, send_data, length) != length) {
307                 error("cannot read from %s: %s", send_file_name, strerror(errno));
308                 goto _error;
309         }
310         if (length >= 4 && !memcmp(send_data, "MThd", 4)) {
311                 error("%s is a Standard MIDI File; use aplaymidi to send it", send_file_name);
312                 goto _error;
313         }
314         send_data_length = length;
315         goto _exit;
316 _error:
317         free(send_data);
318         send_data = NULL;
319 _exit:
320         close(fd);
321 }
322
323 static int hex_value(char c)
324 {
325         if ('0' <= c && c <= '9')
326                 return c - '0';
327         if ('A' <= c && c <= 'F')
328                 return c - 'A' + 10;
329         if ('a' <= c && c <= 'f')
330                 return c - 'a' + 10;
331         error("invalid character %c", c);
332         return -1;
333 }
334
335 static void parse_data(void)
336 {
337         const char *p;
338         int i, value;
339
340         send_data = my_malloc(strlen(send_hex)); /* guesstimate */
341         i = 0;
342         value = -1; /* value is >= 0 when the first hex digit of a byte has been read */
343         for (p = send_hex; *p; ++p) {
344                 int digit;
345                 if (isspace((unsigned char)*p)) {
346                         if (value >= 0) {
347                                 send_data[i++] = value;
348                                 value = -1;
349                         }
350                         continue;
351                 }
352                 digit = hex_value(*p);
353                 if (digit < 0) {
354                         send_data = NULL;
355                         return;
356                 }
357                 if (value < 0) {
358                         value = digit;
359                 } else {
360                         send_data[i++] = (value << 4) | digit;
361                         value = -1;
362                 }
363         }
364         if (value >= 0)
365                 send_data[i++] = value;
366         send_data_length = i;
367 }
368
369 /*
370  * prints MIDI commands, formatting them nicely
371  */
372 static void print_byte(unsigned char byte, struct timespec *ts)
373 {
374         static enum {
375                 STATE_UNKNOWN,
376                 STATE_1PARAM,
377                 STATE_1PARAM_CONTINUE,
378                 STATE_2PARAM_1,
379                 STATE_2PARAM_2,
380                 STATE_2PARAM_1_CONTINUE,
381                 STATE_SYSEX
382         } state = STATE_UNKNOWN;
383         int newline = 0;
384
385         if (byte >= 0xf8)
386                 newline = 1;
387         else if (byte >= 0xf0) {
388                 newline = 1;
389                 switch (byte) {
390                 case 0xf0:
391                         state = STATE_SYSEX;
392                         break;
393                 case 0xf1:
394                 case 0xf3:
395                         state = STATE_1PARAM;
396                         break;
397                 case 0xf2:
398                         state = STATE_2PARAM_1;
399                         break;
400                 case 0xf4:
401                 case 0xf5:
402                 case 0xf6:
403                         state = STATE_UNKNOWN;
404                         break;
405                 case 0xf7:
406                         newline = state != STATE_SYSEX;
407                         state = STATE_UNKNOWN;
408                         break;
409                 }
410         } else if (byte >= 0x80) {
411                 newline = 1;
412                 if (byte >= 0xc0 && byte <= 0xdf)
413                         state = STATE_1PARAM;
414                 else
415                         state = STATE_2PARAM_1;
416         } else /* b < 0x80 */ {
417                 int running_status = 0;
418                 newline = state == STATE_UNKNOWN;
419                 switch (state) {
420                 case STATE_1PARAM:
421                         state = STATE_1PARAM_CONTINUE;
422                         break;
423                 case STATE_1PARAM_CONTINUE:
424                         running_status = 1;
425                         break;
426                 case STATE_2PARAM_1:
427                         state = STATE_2PARAM_2;
428                         break;
429                 case STATE_2PARAM_2:
430                         state = STATE_2PARAM_1_CONTINUE;
431                         break;
432                 case STATE_2PARAM_1_CONTINUE:
433                         running_status = 1;
434                         state = STATE_2PARAM_2;
435                         break;
436                 default:
437                         break;
438                 }
439                 if (running_status)
440                         fputs("\n  ", stdout);
441         }
442
443         putchar(newline ? '\n' : ' ');
444         if (newline && do_print_timestamp) {
445                 /* Nanoseconds does not make a lot of sense for serial MIDI (the
446                  * 31250 bps one) but I'm not sure about MIDI over USB.
447                  */
448                 printf("%lld.%.9ld) ", (long long)ts->tv_sec, ts->tv_nsec);
449         }
450
451         printf("%02X", byte);
452 }
453
454 static void sig_handler(int sig ATTRIBUTE_UNUSED)
455 {
456         stop = 1;
457 }
458
459 static void add_send_hex_data(const char *str)
460 {
461         int length;
462         char *s;
463
464         length = (send_hex ? strlen(send_hex) + 1 : 0) + strlen(str) + 1;
465         s = my_malloc(length);
466         if (send_hex) {
467                 strcpy(s, send_hex);
468                 strcat(s, " ");
469         } else {
470                 s[0] = '\0';
471         }
472         strcat(s, str);
473         free(send_hex);
474         send_hex = s;
475 }
476
477 int main(int argc, char *argv[])
478 {
479         static const char short_options[] = "hVlxLp:s:r:S::dt:aci:T:";
480         static const struct option long_options[] = {
481                 {"help", 0, NULL, 'h'},
482                 {"version", 0, NULL, 'V'},
483                 {"list-devices", 0, NULL, 'l'},
484                 {"list-inactive", 0, NULL, 'x'},
485                 {"list-rawmidis", 0, NULL, 'L'},
486                 {"port", 1, NULL, 'p'},
487                 {"send", 1, NULL, 's'},
488                 {"receive", 1, NULL, 'r'},
489                 {"send-hex", 2, NULL, 'S'},
490                 {"dump", 0, NULL, 'd'},
491                 {"timestamp", 1, NULL, 'T'},
492                 {"timeout", 1, NULL, 't'},
493                 {"active-sensing", 0, NULL, 'a'},
494                 {"clock", 0, NULL, 'c'},
495                 {"sysex-interval", 1, NULL, 'i'},
496                 {0}
497         };
498         int c, err, ok = 0;
499         int ignore_active_sensing = 1;
500         int ignore_clock = 1;
501         int do_send_hex = 0;
502         clockid_t cid = CLOCK_REALTIME;
503         struct itimerspec itimerspec = { .it_interval = { 0, 0 } };
504
505         while ((c = getopt_long(argc, argv, short_options,
506                                 long_options, NULL)) != -1) {
507                 switch (c) {
508                 case 'h':
509                         usage();
510                         return 0;
511                 case 'V':
512                         version();
513                         return 0;
514                 case 'l':
515                         do_device_list = 1;
516                         break;
517                 case 'x':
518                         list_all = 1;
519                         break;
520                 case 'L':
521                         do_rawmidi_list = 1;
522                         break;
523                 case 'p':
524                         port_name = optarg;
525                         break;
526                 case 's':
527                         send_file_name = optarg;
528                         break;
529                 case 'r':
530                         receive_file_name = optarg;
531                         break;
532                 case 'S':
533                         do_send_hex = 1;
534                         if (optarg)
535                                 add_send_hex_data(optarg);
536                         break;
537                 case 'd':
538                         dump = 1;
539                         break;
540                 case 'T':
541                         do_print_timestamp = 1;
542                         if (optarg == NULL)
543                                 error("Clock type missing");
544                         else if (strcasecmp(optarg, "realtime") == 0)
545                                 cid = CLOCK_REALTIME;
546                         else if (strcasecmp(optarg, "monotonic") == 0)
547                                 cid = CLOCK_MONOTONIC;
548 #ifdef CLOCK_MONOTONIC_RAW
549                         else if (strcasecmp(optarg, "raw") == 0)
550                                 cid = CLOCK_MONOTONIC_RAW;
551 #endif
552                         else
553                                 error("Clock type not known");
554                         break;
555                 case 't':
556                         if (optarg)
557                                 timeout = atof(optarg);
558                         break;
559                 case 'a':
560                         ignore_active_sensing = 0;
561                         break;
562                 case 'c':
563                         ignore_clock = 0;
564                         break;
565                 case 'i':
566                         sysex_interval = atoi(optarg);
567                         break;
568                 default:
569                         error("Try `amidi --help' for more information.");
570                         return 1;
571                 }
572         }
573         if (do_send_hex) {
574                 /* data for -S can be specified as multiple arguments */
575                 if (!send_hex && !argv[optind]) {
576                         error("Please specify some data for --send-hex.");
577                         return 1;
578                 }
579                 for (; argv[optind]; ++optind)
580                         add_send_hex_data(argv[optind]);
581         } else {
582                 if (argv[optind]) {
583                         error("%s is not an option.", argv[optind]);
584                         return 1;
585                 }
586         }
587
588         if (do_rawmidi_list)
589                 rawmidi_list();
590         if (do_device_list)
591                 device_list();
592         if (do_rawmidi_list || do_device_list)
593                 return 0;
594
595         if (!send_file_name && !receive_file_name && !send_hex && !dump) {
596                 error("Please specify at least one of --send, --receive, --send-hex, or --dump.");
597                 return 1;
598         }
599         if (send_file_name && send_hex) {
600                 error("--send and --send-hex cannot be specified at the same time.");
601                 return 1;
602         }
603
604         if (send_file_name)
605                 load_file();
606         else if (send_hex)
607                 parse_data();
608         if ((send_file_name || send_hex) && !send_data)
609                 return 1;
610
611         if (receive_file_name) {
612                 receive_file = creat(receive_file_name, 0666);
613                 if (receive_file == -1) {
614                         error("cannot create %s: %s", receive_file_name, strerror(errno));
615                         return -1;
616                 }
617         } else {
618                 receive_file = -1;
619         }
620
621         if (receive_file_name || dump)
622                 inputp = &input;
623         else
624                 inputp = NULL;
625         if (send_data)
626                 outputp = &output;
627         else
628                 outputp = NULL;
629
630         if ((err = snd_rawmidi_open(inputp, outputp, port_name, SND_RAWMIDI_NONBLOCK)) < 0) {
631                 error("cannot open port \"%s\": %s", port_name, snd_strerror(err));
632                 goto _exit2;
633         }
634
635         if (inputp)
636                 snd_rawmidi_read(input, NULL, 0); /* trigger reading */
637
638         if (send_data) {
639                 if ((err = snd_rawmidi_nonblock(output, 0)) < 0) {
640                         error("cannot set blocking mode: %s", snd_strerror(err));
641                         goto _exit;
642                 }
643                 if (!sysex_interval) {
644                         if ((err = snd_rawmidi_write(output, send_data, send_data_length)) < 0) {
645                                 error("cannot send data: %s", snd_strerror(err));
646                                 return err;
647                         }
648                 } else {
649                         if ((err = send_midi_interleaved()) < 0) {
650                                 error("cannot send data: %s", snd_strerror(err));
651                                 return err;
652                         }
653                 }
654         }
655
656         if (inputp) {
657                 int read = 0;
658                 int npfds;
659                 struct pollfd *pfds;
660
661                 npfds = 1 + snd_rawmidi_poll_descriptors_count(input);
662                 pfds = alloca(npfds * sizeof(struct pollfd));
663
664                 if (timeout > 0) {
665                         pfds[0].fd = timerfd_create(CLOCK_MONOTONIC, 0);
666                         if (pfds[0].fd == -1) {
667                                 error("cannot create timer: %s", strerror(errno));
668                                 goto _exit;
669                         }
670                         pfds[0].events = POLLIN;
671                 } else {
672                         pfds[0].fd = -1;
673                 }
674
675                 snd_rawmidi_poll_descriptors(input, &pfds[1], npfds - 1);
676
677                 signal(SIGINT, sig_handler);
678
679                 if (timeout > 0) {
680                         float timeout_int;
681
682                         itimerspec.it_value.tv_nsec = modff(timeout, &timeout_int) * NSEC_PER_SEC;
683                         itimerspec.it_value.tv_sec = timeout_int;
684                         err = timerfd_settime(pfds[0].fd, 0, &itimerspec, NULL);
685                         if (err < 0) {
686                                 error("cannot set timer: %s", strerror(errno));
687                                 goto _exit;
688                         }
689                 }
690
691                 for (;;) {
692                         unsigned char buf[256];
693                         int i, length;
694                         unsigned short revents;
695                         struct timespec ts;
696
697                         err = poll(pfds, npfds, -1);
698                         if (stop || (err < 0 && errno == EINTR))
699                                 break;
700                         if (err < 0) {
701                                 error("poll failed: %s", strerror(errno));
702                                 break;
703                         }
704
705                         if (clock_gettime(cid, &ts) < 0) {
706                                 error("clock_getres (%d) failed: %s", cid, strerror(errno));
707                                 break;
708                         }
709
710                         err = snd_rawmidi_poll_descriptors_revents(input, &pfds[1], npfds - 1, &revents);
711                         if (err < 0) {
712                                 error("cannot get poll events: %s", snd_strerror(errno));
713                                 break;
714                         }
715                         if (revents & (POLLERR | POLLHUP))
716                                 break;
717                         if (!(revents & POLLIN)) {
718                                 if (pfds[0].revents & POLLIN)
719                                         break;
720                                 continue;
721                         }
722
723                         err = snd_rawmidi_read(input, buf, sizeof(buf));
724                         if (err == -EAGAIN)
725                                 continue;
726                         if (err < 0) {
727                                 error("cannot read from port \"%s\": %s", port_name, snd_strerror(err));
728                                 break;
729                         }
730                         length = 0;
731                         for (i = 0; i < err; ++i)
732                                 if ((buf[i] != MIDI_CMD_COMMON_CLOCK &&
733                                      buf[i] != MIDI_CMD_COMMON_SENSING) ||
734                                     (buf[i] == MIDI_CMD_COMMON_CLOCK   && !ignore_clock) ||
735                                     (buf[i] == MIDI_CMD_COMMON_SENSING && !ignore_active_sensing))
736                                         buf[length++] = buf[i];
737                         if (length == 0)
738                                 continue;
739                         read += length;
740
741                         if (receive_file != -1)
742                                 write(receive_file, buf, length);
743                         if (dump) {
744                                 for (i = 0; i < length; ++i)
745                                         print_byte(buf[i], &ts);
746
747                                 fflush(stdout);
748                         }
749
750                         if (timeout > 0) {
751                                 err = timerfd_settime(pfds[0].fd, 0, &itimerspec, NULL);
752                                 if (err < 0) {
753                                         error("cannot set timer: %s", strerror(errno));
754                                         break;
755                                 }
756                         }
757                 }
758                 if (isatty(fileno(stdout)))
759                         printf("\n%d bytes read\n", read);
760         }
761
762         ok = 1;
763 _exit:
764         if (inputp)
765                 snd_rawmidi_close(input);
766         if (outputp)
767                 snd_rawmidi_close(output);
768 _exit2:
769         if (receive_file != -1)
770                 close(receive_file);
771         return !ok;
772 }