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