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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31 #include <sys/types.h>
36 #include <alsa/asoundlib.h>
40 static int do_device_list, do_rawmidi_list;
41 static char *port_name = "default";
42 static char *send_file_name;
43 static char *receive_file_name;
44 static char *send_hex;
45 static char *send_data;
46 static int send_data_length;
47 static int receive_file;
51 static snd_rawmidi_t *input, **inputp;
52 static snd_rawmidi_t *output, **outputp;
54 static void error(const char *format, ...)
59 vfprintf(stderr, format, ap);
64 static void usage(void)
67 "Usage: amidi options\n"
69 "-h, --help this help\n"
70 "-V, --version print current version\n"
71 "-l, --list-devices list all hardware ports\n"
72 "-L, --list-rawmidis list all RawMIDI definitions\n"
73 "-p, --port=name select port by name\n"
74 "-s, --send=file send the contents of a (.syx) file\n"
75 "-r, --receive=file write received data into a file\n"
76 "-S, --send-hex=\"...\" send hexadecimal bytes\n"
77 "-d, --dump print received data as hexadecimal bytes\n"
78 "-t, --timeout=seconds exits when no data has been received\n"
79 " for the specified duration\n"
80 "-a, --active-sensing don't ignore active sensing bytes\n");
83 static void version(void)
85 puts("amidi version " SND_UTIL_VERSION_STR);
88 static void *my_malloc(size_t size)
90 void *p = malloc(size);
92 error("out of memory");
98 static void list_device(snd_ctl_t *ctl, int card, int device)
100 snd_rawmidi_info_t *info;
102 const char *sub_name;
103 int subs, subs_in, subs_out;
107 snd_rawmidi_info_alloca(&info);
108 snd_rawmidi_info_set_device(info, device);
110 snd_rawmidi_info_set_stream(info, SND_RAWMIDI_STREAM_INPUT);
111 err = snd_ctl_rawmidi_info(ctl, info);
113 subs_in = snd_rawmidi_info_get_subdevices_count(info);
117 snd_rawmidi_info_set_stream(info, SND_RAWMIDI_STREAM_OUTPUT);
118 err = snd_ctl_rawmidi_info(ctl, info);
120 subs_out = snd_rawmidi_info_get_subdevices_count(info);
124 subs = subs_in > subs_out ? subs_in : subs_out;
128 for (sub = 0; sub < subs; ++sub) {
129 snd_rawmidi_info_set_stream(info, sub < subs_in ?
130 SND_RAWMIDI_STREAM_INPUT :
131 SND_RAWMIDI_STREAM_OUTPUT);
132 snd_rawmidi_info_set_subdevice(info, sub);
133 err = snd_ctl_rawmidi_info(ctl, info);
135 error("cannot get rawmidi information %d:%d:%d: %s\n",
136 card, device, sub, snd_strerror(err));
139 name = snd_rawmidi_info_get_name(info);
140 sub_name = snd_rawmidi_info_get_subdevice_name(info);
141 if (sub == 0 && sub_name[0] == '\0') {
142 printf("%c%c hw:%d,%d %s",
143 sub < subs_in ? 'I' : ' ',
144 sub < subs_out ? 'O' : ' ',
147 printf(" (%d subdevices)", subs);
151 printf("%c%c hw:%d,%d,%d %s\n",
152 sub < subs_in ? 'I' : ' ',
153 sub < subs_out ? 'O' : ' ',
154 card, device, sub, sub_name);
159 static void list_card_devices(int card)
166 sprintf(name, "hw:%d", card);
167 if ((err = snd_ctl_open(&ctl, name, 0)) < 0) {
168 error("cannot open control for card %d: %s", card, snd_strerror(err));
173 if ((err = snd_ctl_rawmidi_next_device(ctl, &device)) < 0) {
174 error("cannot determine device number: %s", snd_strerror(err));
179 list_device(ctl, card, device);
184 static void device_list(void)
189 if ((err = snd_card_next(&card)) < 0) {
190 error("cannot determine card number: %s", snd_strerror(err));
194 error("no sound card found");
197 puts("Dir Device Name");
199 list_card_devices(card);
200 if ((err = snd_card_next(&card)) < 0) {
201 error("cannot determine card number: %s", snd_strerror(err));
207 static void rawmidi_list(void)
209 snd_output_t *output;
210 snd_config_t *config;
213 if ((err = snd_config_update()) < 0) {
214 error("snd_config_update failed: %s", snd_strerror(err));
217 if ((err = snd_output_stdio_attach(&output, stdout, 0)) < 0) {
218 error("snd_output_stdio_attach failed: %s", snd_strerror(err));
221 if (snd_config_search(snd_config, "rawmidi", &config) >= 0) {
222 puts("RawMIDI list:");
223 snd_config_save(config, output);
225 snd_output_close(output);
228 static void load_file(void)
233 fd = open(send_file_name, O_RDONLY);
235 error("cannot open %s - %s", send_file_name, strerror(errno));
238 length = lseek(fd, 0, SEEK_END);
239 if (length == (off_t)-1) {
240 error("cannot determine length of %s: %s", send_file_name, strerror(errno));
243 send_data = my_malloc(length);
244 lseek(fd, 0, SEEK_SET);
245 if (read(fd, send_data, length) != length) {
246 error("cannot read from %s: %s", send_file_name, strerror(errno));
249 if (length >= 4 && !memcmp(send_data, "MThd", 4)) {
250 error("%s is a Standard MIDI File; use aplaymidi to send it", send_file_name);
253 send_data_length = length;
262 static int hex_value(char c)
264 if ('0' <= c && c <= '9')
266 if ('A' <= c && c <= 'F')
268 if ('a' <= c && c <= 'f')
270 error("invalid character %c", c);
274 static void parse_data(void)
279 send_data = my_malloc(strlen(send_hex)); /* guesstimate */
281 value = -1; /* value is >= 0 when the first hex digit of a byte has been read */
282 for (p = send_hex; *p; ++p) {
284 if (isspace((unsigned char)*p)) {
286 send_data[i++] = value;
291 digit = hex_value(*p);
299 send_data[i++] = (value << 4) | digit;
304 send_data[i++] = value;
305 send_data_length = i;
309 * prints MIDI commands, formatting them nicely
311 static void print_byte(unsigned char byte)
316 STATE_1PARAM_CONTINUE,
319 STATE_2PARAM_1_CONTINUE,
321 } state = STATE_UNKNOWN;
326 else if (byte >= 0xf0) {
334 state = STATE_1PARAM;
337 state = STATE_2PARAM_1;
342 state = STATE_UNKNOWN;
345 newline = state != STATE_SYSEX;
346 state = STATE_UNKNOWN;
349 } else if (byte >= 0x80) {
351 if (byte >= 0xc0 && byte <= 0xdf)
352 state = STATE_1PARAM;
354 state = STATE_2PARAM_1;
355 } else /* b < 0x80 */ {
356 int running_status = 0;
357 newline = state == STATE_UNKNOWN;
360 state = STATE_1PARAM_CONTINUE;
362 case STATE_1PARAM_CONTINUE:
366 state = STATE_2PARAM_2;
369 state = STATE_2PARAM_1_CONTINUE;
371 case STATE_2PARAM_1_CONTINUE:
373 state = STATE_2PARAM_2;
379 fputs("\n ", stdout);
381 printf("%c%02X", newline ? '\n' : ' ', byte);
384 static void sig_handler(int dummy)
389 static void add_send_hex_data(const char *str)
394 length = (send_hex ? strlen(send_hex) + 1 : 0) + strlen(str) + 1;
395 s = my_malloc(length);
407 int main(int argc, char *argv[])
409 static const char short_options[] = "hVlLp:s:r:S::dt:a";
410 static const struct option long_options[] = {
411 {"help", 0, NULL, 'h'},
412 {"version", 0, NULL, 'V'},
413 {"list-devices", 0, NULL, 'l'},
414 {"list-rawmidis", 0, NULL, 'L'},
415 {"port", 1, NULL, 'p'},
416 {"send", 1, NULL, 's'},
417 {"receive", 1, NULL, 'r'},
418 {"send-hex", 2, NULL, 'S'},
419 {"dump", 0, NULL, 'd'},
420 {"timeout", 1, NULL, 't'},
421 {"active-sensing", 0, NULL, 'a'},
425 int ignore_active_sensing = 1;
428 while ((c = getopt_long(argc, argv, short_options,
429 long_options, NULL)) != -1) {
447 send_file_name = optarg;
450 receive_file_name = optarg;
455 add_send_hex_data(optarg);
461 timeout = atoi(optarg);
464 ignore_active_sensing = 0;
467 error("Try `amidi --help' for more information.");
472 /* data for -S can be specified as multiple arguments */
473 if (!send_hex && !argv[optind]) {
474 error("Please specify some data for --send-hex.");
477 for (; argv[optind]; ++optind)
478 add_send_hex_data(argv[optind]);
481 error("%s is not an option.", argv[optind]);
490 if (do_rawmidi_list || do_device_list)
493 if (!send_file_name && !receive_file_name && !send_hex && !dump) {
494 error("Please specify at least one of --send, --receive, --send-hex, or --dump.");
497 if (send_file_name && send_hex) {
498 error("--send and --send-hex cannot be specified at the same time.");
506 if ((send_file_name || send_hex) && !send_data)
509 if (receive_file_name) {
510 receive_file = creat(receive_file_name, 0666);
511 if (receive_file == -1) {
512 error("cannot create %s: %s", receive_file_name, strerror(errno));
519 if (receive_file_name || dump)
528 if ((err = snd_rawmidi_open(inputp, outputp, port_name, SND_RAWMIDI_NONBLOCK)) < 0) {
529 error("cannot open port \"%s\": %s", port_name, snd_strerror(err));
534 snd_rawmidi_read(input, NULL, 0); /* trigger reading */
537 if ((err = snd_rawmidi_nonblock(output, 0)) < 0) {
538 error("cannot set blocking mode: %s", snd_strerror(err));
541 if ((err = snd_rawmidi_write(output, send_data, send_data_length)) < 0) {
542 error("cannot send data: %s", snd_strerror(err));
553 npfds = snd_rawmidi_poll_descriptors_count(input);
554 pfds = alloca(npfds * sizeof(struct pollfd));
555 snd_rawmidi_poll_descriptors(input, pfds, npfds);
556 signal(SIGINT, sig_handler);
558 unsigned char buf[256];
560 unsigned short revents;
562 err = poll(pfds, npfds, 200);
563 if (stop || (err < 0 && errno == EINTR))
566 error("poll failed: %s", strerror(errno));
571 if (timeout && time >= timeout)
575 if ((err = snd_rawmidi_poll_descriptors_revents(input, pfds, npfds, &revents)) < 0) {
576 error("cannot get poll events: %s", snd_strerror(errno));
579 if (revents & (POLLERR | POLLHUP))
581 if (!(revents & POLLIN))
583 err = snd_rawmidi_read(input, buf, sizeof(buf));
587 error("cannot read from port \"%s\": %s", port_name, snd_strerror(err));
591 for (i = 0; i < err; ++i)
592 if (!ignore_active_sensing || buf[i] != 0xfe)
593 buf[length++] = buf[i];
598 if (receive_file != -1)
599 write(receive_file, buf, length);
601 for (i = 0; i < length; ++i)
606 if (isatty(fileno(stdout)))
607 printf("\n%d bytes read\n", read);
613 snd_rawmidi_close(input);
615 snd_rawmidi_close(output);
617 if (receive_file != -1)