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