aseqdump.c (12030B)
1 /* 2 * aseqdump.c - show the events received at an ALSA sequencer port 3 * 4 * Copyright (c) 2005 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 20 */ 21 22 #include <stdio.h> 23 #include <stdlib.h> 24 #include <stdarg.h> 25 #include <string.h> 26 #include <signal.h> 27 #include <getopt.h> 28 #include <sys/poll.h> 29 #include <alsa/asoundlib.h> 30 #include "aconfig.h" 31 #include "version.h" 32 33 static snd_seq_t *seq; 34 static int port_count; 35 static snd_seq_addr_t *ports; 36 static volatile sig_atomic_t stop = 0; 37 38 39 /* prints an error message to stderr, and dies */ 40 static void fatal(const char *msg, ...) 41 { 42 va_list ap; 43 44 va_start(ap, msg); 45 vfprintf(stderr, msg, ap); 46 va_end(ap); 47 fputc('\n', stderr); 48 exit(EXIT_FAILURE); 49 } 50 51 /* memory allocation error handling */ 52 static void check_mem(void *p) 53 { 54 if (!p) 55 fatal("Out of memory"); 56 } 57 58 /* error handling for ALSA functions */ 59 static void check_snd(const char *operation, int err) 60 { 61 if (err < 0) 62 fatal("Cannot %s - %s", operation, snd_strerror(err)); 63 } 64 65 static void init_seq(void) 66 { 67 int err; 68 69 /* open sequencer */ 70 err = snd_seq_open(&seq, "default", SND_SEQ_OPEN_DUPLEX, 0); 71 check_snd("open sequencer", err); 72 73 /* set our client's name */ 74 err = snd_seq_set_client_name(seq, "aseqdump"); 75 check_snd("set client name", err); 76 } 77 78 /* parses one or more port addresses from the string */ 79 static void parse_ports(const char *arg) 80 { 81 char *buf, *s, *port_name; 82 int err; 83 84 /* make a copy of the string because we're going to modify it */ 85 buf = strdup(arg); 86 check_mem(buf); 87 88 for (port_name = s = buf; s; port_name = s + 1) { 89 /* Assume that ports are separated by commas. We don't use 90 * spaces because those are valid in client names. */ 91 s = strchr(port_name, ','); 92 if (s) 93 *s = '\0'; 94 95 ++port_count; 96 ports = realloc(ports, port_count * sizeof(snd_seq_addr_t)); 97 check_mem(ports); 98 99 err = snd_seq_parse_address(seq, &ports[port_count - 1], port_name); 100 if (err < 0) 101 fatal("Invalid port %s - %s", port_name, snd_strerror(err)); 102 } 103 104 free(buf); 105 } 106 107 static void create_port(void) 108 { 109 int err; 110 111 err = snd_seq_create_simple_port(seq, "aseqdump", 112 SND_SEQ_PORT_CAP_WRITE | 113 SND_SEQ_PORT_CAP_SUBS_WRITE, 114 SND_SEQ_PORT_TYPE_MIDI_GENERIC | 115 SND_SEQ_PORT_TYPE_APPLICATION); 116 check_snd("create port", err); 117 } 118 119 static void connect_ports(void) 120 { 121 int i, err; 122 123 for (i = 0; i < port_count; ++i) { 124 err = snd_seq_connect_from(seq, 0, ports[i].client, ports[i].port); 125 if (err < 0) 126 fatal("Cannot connect from port %d:%d - %s", 127 ports[i].client, ports[i].port, snd_strerror(err)); 128 } 129 } 130 131 static void dump_event(const snd_seq_event_t *ev) 132 { 133 printf("%3d:%-3d ", ev->source.client, ev->source.port); 134 switch (ev->type) { 135 case SND_SEQ_EVENT_NOTEON: 136 if (ev->data.note.velocity) 137 printf("Note on %2d, note %d, velocity %d\n", 138 ev->data.note.channel, ev->data.note.note, ev->data.note.velocity); 139 else 140 printf("Note off %2d, note %d\n", 141 ev->data.note.channel, ev->data.note.note); 142 break; 143 case SND_SEQ_EVENT_NOTEOFF: 144 printf("Note off %2d, note %d, velocity %d\n", 145 ev->data.note.channel, ev->data.note.note, ev->data.note.velocity); 146 break; 147 case SND_SEQ_EVENT_KEYPRESS: 148 printf("Polyphonic aftertouch %2d, note %d, value %d\n", 149 ev->data.note.channel, ev->data.note.note, ev->data.note.velocity); 150 break; 151 case SND_SEQ_EVENT_CONTROLLER: 152 printf("Control change %2d, controller %d, value %d\n", 153 ev->data.control.channel, ev->data.control.param, ev->data.control.value); 154 break; 155 case SND_SEQ_EVENT_PGMCHANGE: 156 printf("Program change %2d, program %d\n", 157 ev->data.control.channel, ev->data.control.value); 158 break; 159 case SND_SEQ_EVENT_CHANPRESS: 160 printf("Channel aftertouch %2d, value %d\n", 161 ev->data.control.channel, ev->data.control.value); 162 break; 163 case SND_SEQ_EVENT_PITCHBEND: 164 printf("Pitch bend %2d, value %d\n", 165 ev->data.control.channel, ev->data.control.value); 166 break; 167 case SND_SEQ_EVENT_CONTROL14: 168 printf("Control change %2d, controller %d, value %5d\n", 169 ev->data.control.channel, ev->data.control.param, ev->data.control.value); 170 break; 171 case SND_SEQ_EVENT_NONREGPARAM: 172 printf("Non-reg. parameter %2d, parameter %d, value %d\n", 173 ev->data.control.channel, ev->data.control.param, ev->data.control.value); 174 break; 175 case SND_SEQ_EVENT_REGPARAM: 176 printf("Reg. parameter %2d, parameter %d, value %d\n", 177 ev->data.control.channel, ev->data.control.param, ev->data.control.value); 178 break; 179 case SND_SEQ_EVENT_SONGPOS: 180 printf("Song position pointer value %d\n", 181 ev->data.control.value); 182 break; 183 case SND_SEQ_EVENT_SONGSEL: 184 printf("Song select value %d\n", 185 ev->data.control.value); 186 break; 187 case SND_SEQ_EVENT_QFRAME: 188 printf("MTC quarter frame %02xh\n", 189 ev->data.control.value); 190 break; 191 case SND_SEQ_EVENT_TIMESIGN: 192 // XXX how is this encoded? 193 printf("SMF time signature (%#010x)\n", 194 ev->data.control.value); 195 break; 196 case SND_SEQ_EVENT_KEYSIGN: 197 // XXX how is this encoded? 198 printf("SMF key signature (%#010x)\n", 199 ev->data.control.value); 200 break; 201 case SND_SEQ_EVENT_START: 202 if (ev->source.client == SND_SEQ_CLIENT_SYSTEM && 203 ev->source.port == SND_SEQ_PORT_SYSTEM_TIMER) 204 printf("Queue start queue %d\n", 205 ev->data.queue.queue); 206 else 207 printf("Start\n"); 208 break; 209 case SND_SEQ_EVENT_CONTINUE: 210 if (ev->source.client == SND_SEQ_CLIENT_SYSTEM && 211 ev->source.port == SND_SEQ_PORT_SYSTEM_TIMER) 212 printf("Queue continue queue %d\n", 213 ev->data.queue.queue); 214 else 215 printf("Continue\n"); 216 break; 217 case SND_SEQ_EVENT_STOP: 218 if (ev->source.client == SND_SEQ_CLIENT_SYSTEM && 219 ev->source.port == SND_SEQ_PORT_SYSTEM_TIMER) 220 printf("Queue stop queue %d\n", 221 ev->data.queue.queue); 222 else 223 printf("Stop\n"); 224 break; 225 case SND_SEQ_EVENT_SETPOS_TICK: 226 printf("Set tick queue pos. queue %d\n", ev->data.queue.queue); 227 break; 228 case SND_SEQ_EVENT_SETPOS_TIME: 229 printf("Set rt queue pos. queue %d\n", ev->data.queue.queue); 230 break; 231 case SND_SEQ_EVENT_TEMPO: 232 printf("Set queue tempo queue %d\n", ev->data.queue.queue); 233 break; 234 case SND_SEQ_EVENT_CLOCK: 235 printf("Clock\n"); 236 break; 237 case SND_SEQ_EVENT_TICK: 238 printf("Tick\n"); 239 break; 240 case SND_SEQ_EVENT_QUEUE_SKEW: 241 printf("Queue timer skew queue %d\n", ev->data.queue.queue); 242 break; 243 case SND_SEQ_EVENT_TUNE_REQUEST: 244 printf("Tune request\n"); 245 break; 246 case SND_SEQ_EVENT_RESET: 247 printf("Reset\n"); 248 break; 249 case SND_SEQ_EVENT_SENSING: 250 printf("Active Sensing\n"); 251 break; 252 case SND_SEQ_EVENT_CLIENT_START: 253 printf("Client start client %d\n", 254 ev->data.addr.client); 255 break; 256 case SND_SEQ_EVENT_CLIENT_EXIT: 257 printf("Client exit client %d\n", 258 ev->data.addr.client); 259 break; 260 case SND_SEQ_EVENT_CLIENT_CHANGE: 261 printf("Client changed client %d\n", 262 ev->data.addr.client); 263 break; 264 case SND_SEQ_EVENT_PORT_START: 265 printf("Port start %d:%d\n", 266 ev->data.addr.client, ev->data.addr.port); 267 break; 268 case SND_SEQ_EVENT_PORT_EXIT: 269 printf("Port exit %d:%d\n", 270 ev->data.addr.client, ev->data.addr.port); 271 break; 272 case SND_SEQ_EVENT_PORT_CHANGE: 273 printf("Port changed %d:%d\n", 274 ev->data.addr.client, ev->data.addr.port); 275 break; 276 case SND_SEQ_EVENT_PORT_SUBSCRIBED: 277 printf("Port subscribed %d:%d -> %d:%d\n", 278 ev->data.connect.sender.client, ev->data.connect.sender.port, 279 ev->data.connect.dest.client, ev->data.connect.dest.port); 280 break; 281 case SND_SEQ_EVENT_PORT_UNSUBSCRIBED: 282 printf("Port unsubscribed %d:%d -> %d:%d\n", 283 ev->data.connect.sender.client, ev->data.connect.sender.port, 284 ev->data.connect.dest.client, ev->data.connect.dest.port); 285 break; 286 case SND_SEQ_EVENT_SYSEX: 287 { 288 unsigned int i; 289 printf("System exclusive "); 290 for (i = 0; i < ev->data.ext.len; ++i) 291 printf(" %02X", ((unsigned char*)ev->data.ext.ptr)[i]); 292 printf("\n"); 293 } 294 break; 295 default: 296 printf("Event type %d\n", ev->type); 297 } 298 } 299 300 static void list_ports(void) 301 { 302 snd_seq_client_info_t *cinfo; 303 snd_seq_port_info_t *pinfo; 304 305 snd_seq_client_info_alloca(&cinfo); 306 snd_seq_port_info_alloca(&pinfo); 307 308 puts(" Port Client name Port name"); 309 310 snd_seq_client_info_set_client(cinfo, -1); 311 while (snd_seq_query_next_client(seq, cinfo) >= 0) { 312 int client = snd_seq_client_info_get_client(cinfo); 313 314 snd_seq_port_info_set_client(pinfo, client); 315 snd_seq_port_info_set_port(pinfo, -1); 316 while (snd_seq_query_next_port(seq, pinfo) >= 0) { 317 /* we need both READ and SUBS_READ */ 318 if ((snd_seq_port_info_get_capability(pinfo) 319 & (SND_SEQ_PORT_CAP_READ | SND_SEQ_PORT_CAP_SUBS_READ)) 320 != (SND_SEQ_PORT_CAP_READ | SND_SEQ_PORT_CAP_SUBS_READ)) 321 continue; 322 printf("%3d:%-3d %-32.32s %s\n", 323 snd_seq_port_info_get_client(pinfo), 324 snd_seq_port_info_get_port(pinfo), 325 snd_seq_client_info_get_name(cinfo), 326 snd_seq_port_info_get_name(pinfo)); 327 } 328 } 329 } 330 331 static void help(const char *argv0) 332 { 333 printf("Usage: %s [options]\n" 334 "\nAvailable options:\n" 335 " -h,--help this help\n" 336 " -V,--version show version\n" 337 " -l,--list list input ports\n" 338 " -p,--port=client:port,... source port(s)\n", 339 argv0); 340 } 341 342 static void version(void) 343 { 344 puts("aseqdump version " SND_UTIL_VERSION_STR); 345 } 346 347 static void sighandler(int sig) 348 { 349 stop = 1; 350 } 351 352 int main(int argc, char *argv[]) 353 { 354 static const char short_options[] = "hVlp:"; 355 static const struct option long_options[] = { 356 {"help", 0, NULL, 'h'}, 357 {"version", 0, NULL, 'V'}, 358 {"list", 0, NULL, 'l'}, 359 {"port", 1, NULL, 'p'}, 360 { } 361 }; 362 363 int do_list = 0; 364 struct pollfd *pfds; 365 int npfds; 366 int c, err; 367 368 init_seq(); 369 370 while ((c = getopt_long(argc, argv, short_options, 371 long_options, NULL)) != -1) { 372 switch (c) { 373 case 'h': 374 help(argv[0]); 375 return 0; 376 case 'V': 377 version(); 378 return 0; 379 case 'l': 380 do_list = 1; 381 break; 382 case 'p': 383 parse_ports(optarg); 384 break; 385 default: 386 help(argv[0]); 387 return 1; 388 } 389 } 390 if (optind < argc) { 391 help(argv[0]); 392 return 1; 393 } 394 395 if (do_list) { 396 list_ports(); 397 return 0; 398 } 399 400 create_port(); 401 connect_ports(); 402 403 err = snd_seq_nonblock(seq, 1); 404 check_snd("set nonblock mode", err); 405 406 if (port_count > 0) 407 printf("Waiting for data."); 408 else 409 printf("Waiting for data at port %d:0.", 410 snd_seq_client_id(seq)); 411 printf(" Press Ctrl+C to end.\n"); 412 printf("Source Event Ch Data\n"); 413 414 signal(SIGINT, sighandler); 415 signal(SIGTERM, sighandler); 416 417 npfds = snd_seq_poll_descriptors_count(seq, POLLIN); 418 pfds = alloca(sizeof(*pfds) * npfds); 419 for (;;) { 420 snd_seq_poll_descriptors(seq, pfds, npfds, POLLIN); 421 if (poll(pfds, npfds, -1) < 0) 422 break; 423 do { 424 snd_seq_event_t *event; 425 err = snd_seq_event_input(seq, &event); 426 if (err < 0) 427 break; 428 if (event) 429 dump_event(event); 430 } while (err > 0); 431 fflush(stdout); 432 if (stop) 433 break; 434 } 435 436 snd_seq_close(seq); 437 return 0; 438 }