2 * amidi.c - read from/write to RawMIDI ports
4 * Copyright (c) Clemens Ladisch <clemens@ladisch.de>
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.
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.
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
34 #include <sys/timerfd.h>
35 #include <sys/types.h>
40 #include <alsa/asoundlib.h>
43 #define NSEC_PER_SEC 1000000000L
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;
57 static int sysex_interval;
58 static snd_rawmidi_t *input, **inputp;
59 static snd_rawmidi_t *output, **outputp;
62 static void error(const char *format, ...)
67 vfprintf(stderr, format, ap);
72 static void usage(void)
75 "Usage: amidi options\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"
90 #ifdef CLOCK_MONOTONIC_RAW
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");
100 static void version(void)
102 puts("amidi version " SND_UTIL_VERSION_STR);
105 static void *my_malloc(size_t size)
107 void *p = malloc(size);
109 error("out of memory");
115 static void list_device(snd_ctl_t *ctl, int card, int device)
117 snd_rawmidi_info_t *info;
119 const char *sub_name;
120 int subs, subs_in, subs_out;
124 snd_rawmidi_info_alloca(&info);
125 snd_rawmidi_info_set_device(info, device);
127 snd_rawmidi_info_set_stream(info, SND_RAWMIDI_STREAM_INPUT);
128 err = snd_ctl_rawmidi_info(ctl, info);
130 subs_in = snd_rawmidi_info_get_subdevices_count(info);
134 snd_rawmidi_info_set_stream(info, SND_RAWMIDI_STREAM_OUTPUT);
135 err = snd_ctl_rawmidi_info(ctl, info);
137 subs_out = snd_rawmidi_info_get_subdevices_count(info);
141 subs = subs_in > subs_out ? subs_in : subs_out;
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);
152 error("cannot get rawmidi information %d:%d:%d: %s\n",
153 card, device, sub, snd_strerror(err));
157 (snd_rawmidi_info_get_flags(info) & SNDRV_RAWMIDI_INFO_STREAM_INACTIVE))
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' : ' ',
167 printf(" (%d subdevices)", subs);
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);
179 static void list_card_devices(int card)
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));
193 if ((err = snd_ctl_rawmidi_next_device(ctl, &device)) < 0) {
194 error("cannot determine device number: %s", snd_strerror(err));
199 list_device(ctl, card, device);
204 static void device_list(void)
209 if ((err = snd_card_next(&card)) < 0) {
210 error("cannot determine card number: %s", snd_strerror(err));
214 error("no sound card found");
217 puts("Dir Device Name");
219 list_card_devices(card);
220 if ((err = snd_card_next(&card)) < 0) {
221 error("cannot determine card number: %s", snd_strerror(err));
227 static void rawmidi_list(void)
229 snd_output_t *output;
230 snd_config_t *config;
233 if ((err = snd_config_update()) < 0) {
234 error("snd_config_update failed: %s", snd_strerror(err));
237 if ((err = snd_output_stdio_attach(&output, stdout, 0)) < 0) {
238 error("snd_output_stdio_attach failed: %s", snd_strerror(err));
241 if (snd_config_search(snd_config, "rawmidi", &config) >= 0) {
242 puts("RawMIDI list:");
243 snd_config_save(config, output);
245 snd_output_close(output);
248 static int send_midi_interleaved(void)
251 char *data = send_data;
253 snd_rawmidi_params_t *param;
254 snd_rawmidi_status_t *st;
256 snd_rawmidi_status_alloca(&st);
258 snd_rawmidi_params_alloca(¶m);
259 snd_rawmidi_params_current(output, param);
260 buffer_size = snd_rawmidi_params_get_buffer_size(param);
262 while (data < (send_data + send_data_length)) {
263 int len = send_data + send_data_length - data;
266 if (data > send_data) {
267 snd_rawmidi_status(output, st);
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);
276 /* find end of SysEx */
277 if ((temp = memchr(data, 0xf7, len)) != NULL)
278 len = temp - data + 1;
280 if ((err = snd_rawmidi_write(output, data, len)) < 0)
289 static void load_file(void)
294 fd = open(send_file_name, O_RDONLY);
296 error("cannot open %s - %s", send_file_name, strerror(errno));
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));
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));
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);
314 send_data_length = length;
323 static int hex_value(char c)
325 if ('0' <= c && c <= '9')
327 if ('A' <= c && c <= 'F')
329 if ('a' <= c && c <= 'f')
331 error("invalid character %c", c);
335 static void parse_data(void)
340 send_data = my_malloc(strlen(send_hex)); /* guesstimate */
342 value = -1; /* value is >= 0 when the first hex digit of a byte has been read */
343 for (p = send_hex; *p; ++p) {
345 if (isspace((unsigned char)*p)) {
347 send_data[i++] = value;
352 digit = hex_value(*p);
360 send_data[i++] = (value << 4) | digit;
365 send_data[i++] = value;
366 send_data_length = i;
370 * prints MIDI commands, formatting them nicely
372 static void print_byte(unsigned char byte, struct timespec *ts)
377 STATE_1PARAM_CONTINUE,
380 STATE_2PARAM_1_CONTINUE,
382 } state = STATE_UNKNOWN;
387 else if (byte >= 0xf0) {
395 state = STATE_1PARAM;
398 state = STATE_2PARAM_1;
403 state = STATE_UNKNOWN;
406 newline = state != STATE_SYSEX;
407 state = STATE_UNKNOWN;
410 } else if (byte >= 0x80) {
412 if (byte >= 0xc0 && byte <= 0xdf)
413 state = STATE_1PARAM;
415 state = STATE_2PARAM_1;
416 } else /* b < 0x80 */ {
417 int running_status = 0;
418 newline = state == STATE_UNKNOWN;
421 state = STATE_1PARAM_CONTINUE;
423 case STATE_1PARAM_CONTINUE:
427 state = STATE_2PARAM_2;
430 state = STATE_2PARAM_1_CONTINUE;
432 case STATE_2PARAM_1_CONTINUE:
434 state = STATE_2PARAM_2;
440 fputs("\n ", stdout);
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.
448 printf("%lld.%.9ld) ", (long long)ts->tv_sec, ts->tv_nsec);
451 printf("%02X", byte);
454 static void sig_handler(int sig ATTRIBUTE_UNUSED)
459 static void add_send_hex_data(const char *str)
464 length = (send_hex ? strlen(send_hex) + 1 : 0) + strlen(str) + 1;
465 s = my_malloc(length);
477 int main(int argc, char *argv[])
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'},
499 int ignore_active_sensing = 1;
500 int ignore_clock = 1;
502 clockid_t cid = CLOCK_REALTIME;
503 struct itimerspec itimerspec = { .it_interval = { 0, 0 } };
505 while ((c = getopt_long(argc, argv, short_options,
506 long_options, NULL)) != -1) {
527 send_file_name = optarg;
530 receive_file_name = optarg;
535 add_send_hex_data(optarg);
541 do_print_timestamp = 1;
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;
553 error("Clock type not known");
557 timeout = atof(optarg);
560 ignore_active_sensing = 0;
566 sysex_interval = atoi(optarg);
569 error("Try `amidi --help' for more information.");
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.");
579 for (; argv[optind]; ++optind)
580 add_send_hex_data(argv[optind]);
583 error("%s is not an option.", argv[optind]);
592 if (do_rawmidi_list || do_device_list)
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.");
599 if (send_file_name && send_hex) {
600 error("--send and --send-hex cannot be specified at the same time.");
608 if ((send_file_name || send_hex) && !send_data)
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));
621 if (receive_file_name || dump)
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));
636 snd_rawmidi_read(input, NULL, 0); /* trigger reading */
639 if ((err = snd_rawmidi_nonblock(output, 0)) < 0) {
640 error("cannot set blocking mode: %s", snd_strerror(err));
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));
649 if ((err = send_midi_interleaved()) < 0) {
650 error("cannot send data: %s", snd_strerror(err));
661 npfds = 1 + snd_rawmidi_poll_descriptors_count(input);
662 pfds = alloca(npfds * sizeof(struct pollfd));
665 pfds[0].fd = timerfd_create(CLOCK_MONOTONIC, 0);
666 if (pfds[0].fd == -1) {
667 error("cannot create timer: %s", strerror(errno));
670 pfds[0].events = POLLIN;
675 snd_rawmidi_poll_descriptors(input, &pfds[1], npfds - 1);
677 signal(SIGINT, sig_handler);
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);
686 error("cannot set timer: %s", strerror(errno));
692 unsigned char buf[256];
694 unsigned short revents;
697 err = poll(pfds, npfds, -1);
698 if (stop || (err < 0 && errno == EINTR))
701 error("poll failed: %s", strerror(errno));
705 if (clock_gettime(cid, &ts) < 0) {
706 error("clock_getres (%d) failed: %s", cid, strerror(errno));
710 err = snd_rawmidi_poll_descriptors_revents(input, &pfds[1], npfds - 1, &revents);
712 error("cannot get poll events: %s", snd_strerror(errno));
715 if (revents & (POLLERR | POLLHUP))
717 if (!(revents & POLLIN)) {
718 if (pfds[0].revents & POLLIN)
723 err = snd_rawmidi_read(input, buf, sizeof(buf));
727 error("cannot read from port \"%s\": %s", port_name, snd_strerror(err));
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];
741 if (receive_file != -1)
742 write(receive_file, buf, length);
744 for (i = 0; i < length; ++i)
745 print_byte(buf[i], &ts);
751 err = timerfd_settime(pfds[0].fd, 0, &itimerspec, NULL);
753 error("cannot set timer: %s", strerror(errno));
758 if (isatty(fileno(stdout)))
759 printf("\n%d bytes read\n", read);
765 snd_rawmidi_close(input);
767 snd_rawmidi_close(output);
769 if (receive_file != -1)