| 1 | /* |
| 2 | * |
| 3 | * Copyright (C) 1999-2003 Steve Ratcliffe |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License version 2 as |
| 7 | * published by the Free Software Foundation. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | */ |
| 15 | |
| 16 | |
| 17 | #include "glib.h" |
| 18 | #include "elements.h" |
| 19 | #include "except.h" |
| 20 | #include "intl.h" |
| 21 | |
| 22 | #if HAVE_ALSA_ASOUNDLIB_H |
| 23 | #include <alsa/asoundlib.h> |
| 24 | #else |
| 25 | #include <sys/asoundlib.h> |
| 26 | #endif |
| 27 | |
| 28 | #include <stdio.h> |
| 29 | #include "seqlib.h" |
| 30 | #include "md.h" |
| 31 | #include "midi.h" |
| 32 | #include <stdlib.h> |
| 33 | #include <unistd.h> |
| 34 | #include <getopt.h> |
| 35 | #include <string.h> |
| 36 | #include <errno.h> |
| 37 | #include <signal.h> |
| 38 | |
| 39 | /* |
| 40 | * Play a midi file |
| 41 | * pmidi [-p client:port,... ] [-l] [-d delay] file ... |
| 42 | * |
| 43 | * -p client:port - A alsa client and port number to send midi to |
| 44 | * -l - List possible output ports that could be used |
| 45 | * -d delay - Delay after song ends (default 2 sec) |
| 46 | * -v - Print version and exit |
| 47 | */ |
| 48 | |
| 49 | |
| 50 | /* Options for the command */ |
| 51 | #define HAS_ARG 1 |
| 52 | static struct option long_opts[] = { |
| 53 | {"port", HAS_ARG, NULL, 'p'}, |
| 54 | {"list", 0, NULL, 'l'}, |
| 55 | {"delay", HAS_ARG, NULL, 'd'}, |
| 56 | {"version", 0, NULL, 'v'}, |
| 57 | {0, 0, 0, 0}, |
| 58 | }; |
| 59 | |
| 60 | /* Delay at the end of a song */ |
| 61 | static int delay = 2; |
| 62 | |
| 63 | /* Pointer to root context */ |
| 64 | seq_context_t *g_ctxp; |
| 65 | |
| 66 | /* Number of elements in an array */ |
| 67 | #define NELEM(a) ( sizeof(a)/sizeof((a)[0]) ) |
| 68 | |
| 69 | #define ADDR_PARTS 4 /* Number of part in a port description addr 1:2:3:4 */ |
| 70 | #define SEP ", \t" /* Separators for port description */ |
| 71 | |
| 72 | static seq_context_t *openports(char *portdesc); |
| 73 | static void playfile(seq_context_t *ctxp, char *filename); |
| 74 | static void showlist(); |
| 75 | static void showusage(); |
| 76 | static void play(void *arg, struct element *el); |
| 77 | static void no_errors_please(const char *file, int line, const char *function, int err, const char *fmt, ...); |
| 78 | static void set_signal_handler(seq_context_t *ctxp); |
| 79 | static void signal_handler(int sig); |
| 80 | static void showversion(void); |
| 81 | |
| 82 | /* |
| 83 | * Play a midi file |
| 84 | * pmidi [-p client:port ...] [-l] [-d delay] file ... |
| 85 | * |
| 86 | * -p, --port=client:port - An ALSA client and port number to use |
| 87 | * -l, --list - List possible output ports that could be used |
| 88 | * -d, --delay=SECS - Delay after song ends (default 2 sec) |
| 89 | * -v, --version - Show version number |
| 90 | * Arguments: |
| 91 | * argc - arg count |
| 92 | * argv - arg vector |
| 93 | */ |
| 94 | int |
| 95 | main(int argc, char **argv) |
| 96 | { |
| 97 | char opts[NELEM(long_opts) * 2 + 1]; |
| 98 | char *portdesc; |
| 99 | seq_context_t *ctxp; |
| 100 | char *cp; |
| 101 | int c; |
| 102 | struct option *op; |
| 103 | |
| 104 | /* Build up the short option string */ |
| 105 | cp = opts; |
| 106 | for (op = long_opts; op < &long_opts[NELEM(long_opts)]; op++) { |
| 107 | *cp++ = op->val; |
| 108 | if (op->has_arg) |
| 109 | *cp++ = ':'; |
| 110 | } |
| 111 | |
| 112 | /* Libaries shouldn't print errors by default! */ |
| 113 | snd_lib_error_set_handler(no_errors_please); |
| 114 | |
| 115 | portdesc = NULL; |
| 116 | |
| 117 | /* Deal with the options */ |
| 118 | for (;;) { |
| 119 | c = getopt_long(argc, argv, opts, long_opts, NULL); |
| 120 | if (c == -1) |
| 121 | break; |
| 122 | |
| 123 | switch(c) { |
| 124 | case 'p': |
| 125 | portdesc = optarg; |
| 126 | break; |
| 127 | case 'd': |
| 128 | delay = atoi(optarg); |
| 129 | break; |
| 130 | case 'l': |
| 131 | showlist(); |
| 132 | exit(0); |
| 133 | break; |
| 134 | case 'v': |
| 135 | showversion(); |
| 136 | exit(0); |
| 137 | case '?': |
| 138 | showusage(); |
| 139 | exit(1); |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | if (portdesc == NULL || portdesc[0] == '\0') { |
| 144 | portdesc = getenv("ALSA_OUTPUT_PORTS"); |
| 145 | /* Try the old name for the environment variable */ |
| 146 | if (portdesc == NULL) |
| 147 | portdesc = getenv("ALSA_OUT_PORT"); |
| 148 | if (portdesc == NULL) { |
| 149 | fprintf(stderr, "No client/port specified.\n" |
| 150 | "You must supply one with the -p option or with the\n" |
| 151 | "environment variable ALSA_OUTPUT_PORTS\n" |
| 152 | ); |
| 153 | exit(1); |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | ctxp = openports(portdesc); |
| 158 | if (ctxp == NULL) |
| 159 | return 1; |
| 160 | |
| 161 | /* Set signal handler */ |
| 162 | set_signal_handler(ctxp); |
| 163 | |
| 164 | /* Now play all the files */ |
| 165 | for (; optind < argc; optind++) |
| 166 | playfile(ctxp, argv[optind]); |
| 167 | |
| 168 | seq_free_context(ctxp); |
| 169 | |
| 170 | /* Restore signal handler */ |
| 171 | signal(SIGINT, SIG_DFL); |
| 172 | |
| 173 | return 0; |
| 174 | } |
| 175 | |
| 176 | /* |
| 177 | * Read a list of client/port specifications and return an |
| 178 | * array of snd_seq_addr_t that describes them. |
| 179 | * |
| 180 | * Arguments: |
| 181 | * portdesc - |
| 182 | */ |
| 183 | static seq_context_t * |
| 184 | openports(char *portdesc) |
| 185 | { |
| 186 | char *astr; |
| 187 | char *cp; |
| 188 | seq_context_t *ctxp; |
| 189 | snd_seq_addr_t *addr; |
| 190 | snd_seq_addr_t *ap; |
| 191 | int a[ADDR_PARTS]; |
| 192 | int count, naddr; |
| 193 | int i; |
| 194 | |
| 195 | if (portdesc == NULL) |
| 196 | return NULL; |
| 197 | |
| 198 | ctxp = seq_create_context(); |
| 199 | |
| 200 | addr = g_new(snd_seq_addr_t, strlen(portdesc)); |
| 201 | |
| 202 | naddr = 0; |
| 203 | ap = addr; |
| 204 | |
| 205 | for (astr = strtok(portdesc, SEP); astr; astr = strtok(NULL, SEP)) { |
| 206 | for (cp = astr, count = 0; cp && *cp; cp++) { |
| 207 | if (count < ADDR_PARTS) |
| 208 | a[count++] = atoi(cp); |
| 209 | cp = strchr(cp, ':'); |
| 210 | if (cp == NULL) |
| 211 | break; |
| 212 | } |
| 213 | |
| 214 | switch (count) { |
| 215 | case 2: |
| 216 | ap->client = a[0]; |
| 217 | ap->port = a[1]; |
| 218 | break; |
| 219 | default: |
| 220 | printf("Addresses in %d parts not supported yet\n", count); |
| 221 | break; |
| 222 | } |
| 223 | ap++; |
| 224 | naddr++; |
| 225 | } |
| 226 | |
| 227 | count = 0; |
| 228 | for (i = 0; i < naddr; i++) { |
| 229 | int err; |
| 230 | |
| 231 | err = seq_connect_add(ctxp, addr[i].client, addr[i].port); |
| 232 | if (err < 0) { |
| 233 | fprintf(stderr, _("Could not connect to port %d:%d\n"), |
| 234 | addr[i].client, addr[i].port); |
| 235 | } else |
| 236 | count++; |
| 237 | } |
| 238 | |
| 239 | g_free(addr); |
| 240 | |
| 241 | if (count == 0) { |
| 242 | seq_free_context(ctxp); |
| 243 | return NULL; |
| 244 | } |
| 245 | |
| 246 | return ctxp; |
| 247 | } |
| 248 | |
| 249 | /* |
| 250 | * Play a single file. |
| 251 | * Arguments: |
| 252 | * ctxp - |
| 253 | * filename - |
| 254 | */ |
| 255 | static void |
| 256 | playfile(seq_context_t *ctxp, char *filename) |
| 257 | { |
| 258 | struct rootElement *root; |
| 259 | struct sequenceState *seq; |
| 260 | struct element *el; |
| 261 | unsigned long end; |
| 262 | snd_seq_event_t *ep; |
| 263 | |
| 264 | if (strcmp(filename, "-") == 0) |
| 265 | root = midi_read(stdin); |
| 266 | else |
| 267 | root = midi_read_file(filename); |
| 268 | if (!root) |
| 269 | return; |
| 270 | |
| 271 | /* Loop through all the elements in the song and play them */ |
| 272 | seq = md_sequence_init(root); |
| 273 | while ((el = md_sequence_next(seq)) != NULL) { |
| 274 | play(ctxp, el); |
| 275 | } |
| 276 | |
| 277 | |
| 278 | /* Get the end time for the tracks and echo an event to |
| 279 | * wake us up at that time |
| 280 | */ |
| 281 | end = md_sequence_end_time(seq); |
| 282 | seq_midi_echo(ctxp, end); |
| 283 | |
| 284 | #ifdef USE_DRAIN |
| 285 | snd_seq_drain_output(seq_handle(ctxp)); |
| 286 | #else |
| 287 | snd_seq_flush_output(seq_handle(ctxp)); |
| 288 | #endif |
| 289 | |
| 290 | /* Wait for all the events to be played */ |
| 291 | snd_seq_event_input(seq_handle(ctxp), &ep); |
| 292 | |
| 293 | /* Wait some extra time to allow for note to decay etc */ |
| 294 | sleep(delay); |
| 295 | seq_stop_timer(ctxp); |
| 296 | |
| 297 | md_free(MD_ELEMENT(root)); |
| 298 | } |
| 299 | |
| 300 | /* |
| 301 | * Show a list of possible output ports that midi could be sent |
| 302 | * to. |
| 303 | */ |
| 304 | static void |
| 305 | showlist() |
| 306 | { |
| 307 | snd_seq_client_info_t *cinfo; |
| 308 | snd_seq_port_info_t *pinfo; |
| 309 | int client; |
| 310 | int err; |
| 311 | snd_seq_t *handle; |
| 312 | |
| 313 | err = snd_seq_open(&handle, "hw", SND_SEQ_OPEN_DUPLEX, 0); |
| 314 | if (err < 0) |
| 315 | except(ioError, _("Could not open sequencer %s"), snd_strerror(errno)); |
| 316 | |
| 317 | #ifdef USE_DRAIN |
| 318 | /* |
| 319 | * NOTE: This is here so that it will give an error if the wrong |
| 320 | * version of alsa is used with USE_DRAIN set. An incompatible change |
| 321 | * was made to ALSA on 29 Sep 2000 so that after that date you should |
| 322 | * define USE_DRAIN. |
| 323 | * |
| 324 | * Unfortunately with USE_DRAIN defined with an earlier version of alsa |
| 325 | * it will appear to compile fine but simply not work properly. This |
| 326 | * unneeded call will give an error in this case. |
| 327 | * |
| 328 | * IF you see an error about this line, comment out the line beginning |
| 329 | * with USE_DRAIN in the make.conf file or compile with: |
| 330 | * make USE_DRAIN='' |
| 331 | */ |
| 332 | snd_seq_drop_output(handle); |
| 333 | #endif |
| 334 | |
| 335 | snd_seq_client_info_alloca(&cinfo); |
| 336 | snd_seq_client_info_set_client(cinfo, -1); |
| 337 | printf(_(" Port %-30.30s %s\n"), _("Client name"), _("Port name")); |
| 338 | |
| 339 | while (snd_seq_query_next_client(handle, cinfo) >= 0) { |
| 340 | client = snd_seq_client_info_get_client(cinfo); |
| 341 | snd_seq_port_info_alloca(&pinfo); |
| 342 | snd_seq_port_info_set_client(pinfo, client); |
| 343 | |
| 344 | snd_seq_port_info_set_port(pinfo, -1); |
| 345 | while (snd_seq_query_next_port(handle, pinfo) >= 0) { |
| 346 | int cap; |
| 347 | |
| 348 | cap = (SND_SEQ_PORT_CAP_SUBS_WRITE|SND_SEQ_PORT_CAP_WRITE); |
| 349 | if ((snd_seq_port_info_get_capability(pinfo) & cap) == cap) { |
| 350 | printf("%3d:%-3d %-30.30s %s\n", |
| 351 | snd_seq_port_info_get_client(pinfo), |
| 352 | snd_seq_port_info_get_port(pinfo), |
| 353 | snd_seq_client_info_get_name(cinfo), |
| 354 | snd_seq_port_info_get_name(pinfo) |
| 355 | ); |
| 356 | } |
| 357 | } |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | /* |
| 362 | * Show a usage message |
| 363 | */ |
| 364 | static void |
| 365 | showusage() |
| 366 | { |
| 367 | char **cpp; |
| 368 | static char *msg[] = { |
| 369 | N_("Usage: pmidi [-p client:port ...] [-l] [-d delay] file ..."), |
| 370 | "", |
| 371 | N_(" -p client:port - A alsa client and port number to send midi to"), |
| 372 | N_(" -l - List possible output ports that could be used"), |
| 373 | N_(" -d delay - Delay after song ends (default 2 sec)"), |
| 374 | N_(" -v - Print version and exit"), |
| 375 | }; |
| 376 | |
| 377 | for (cpp = msg; cpp < msg+NELEM(msg); cpp++) { |
| 378 | fprintf(stderr, "%s\n", _(*cpp)); |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | static void |
| 383 | showversion() |
| 384 | { |
| 385 | printf("pmidi-%s\n", VERSION); |
| 386 | } |
| 387 | |
| 388 | static void |
| 389 | play(void *arg, struct element *el) |
| 390 | { |
| 391 | seq_context_t *ctxp = arg; |
| 392 | snd_seq_event_t ev; |
| 393 | |
| 394 | seq_midi_event_init(ctxp, &ev, el->element_time, el->device_channel); |
| 395 | switch (el->type) { |
| 396 | case MD_TYPE_ROOT: |
| 397 | seq_init_tempo(ctxp, MD_ROOT(el)->time_base, 120, 1); |
| 398 | seq_start_timer(ctxp); |
| 399 | break; |
| 400 | case MD_TYPE_NOTE: |
| 401 | seq_midi_note(ctxp, &ev, el->device_channel, MD_NOTE(el)->note, MD_NOTE(el)->vel, |
| 402 | MD_NOTE(el)->length); |
| 403 | break; |
| 404 | case MD_TYPE_CONTROL: |
| 405 | seq_midi_control(ctxp, &ev, el->device_channel, MD_CONTROL(el)->control, |
| 406 | MD_CONTROL(el)->value); |
| 407 | break; |
| 408 | case MD_TYPE_PROGRAM: |
| 409 | seq_midi_program(ctxp, &ev, el->device_channel, MD_PROGRAM(el)->program); |
| 410 | break; |
| 411 | case MD_TYPE_TEMPO: |
| 412 | seq_midi_tempo(ctxp, &ev, MD_TEMPO(el)->micro_tempo); |
| 413 | break; |
| 414 | case MD_TYPE_PITCH: |
| 415 | seq_midi_pitchbend(ctxp, &ev, el->device_channel, MD_PITCH(el)->pitch); |
| 416 | break; |
| 417 | case MD_TYPE_PRESSURE: |
| 418 | seq_midi_chanpress(ctxp, &ev, el->device_channel, MD_PRESSURE(el)->velocity); |
| 419 | break; |
| 420 | case MD_TYPE_KEYTOUCH: |
| 421 | seq_midi_keypress(ctxp, &ev, el->device_channel, MD_KEYTOUCH(el)->note, |
| 422 | MD_KEYTOUCH(el)->velocity); |
| 423 | break; |
| 424 | case MD_TYPE_SYSEX: |
| 425 | seq_midi_sysex(ctxp, &ev, MD_SYSEX(el)->status, MD_SYSEX(el)->data, |
| 426 | MD_SYSEX(el)->length); |
| 427 | break; |
| 428 | case MD_TYPE_TEXT: |
| 429 | case MD_TYPE_KEYSIG: |
| 430 | case MD_TYPE_TIMESIG: |
| 431 | case MD_TYPE_SMPTEOFFSET: |
| 432 | /* Ones that have no sequencer action */ |
| 433 | break; |
| 434 | default: |
| 435 | printf("WARNING: play: not implemented yet %d\n", el->type); |
| 436 | break; |
| 437 | } |
| 438 | } |
| 439 | |
| 440 | /** |
| 441 | * alsa-lib has taken to printing errors on system call failures, even |
| 442 | * though such failed calls are part of normal operation. So we have to |
| 443 | * install our own handler to shut it up and prevent it messing up the list |
| 444 | * output. |
| 445 | * |
| 446 | * Possibly not needed any more Dec 2003. |
| 447 | */ |
| 448 | static void |
| 449 | no_errors_please(const char *file, int line, |
| 450 | const char *function, int err, const char *fmt, ...) |
| 451 | { |
| 452 | } |
| 453 | |
| 454 | static void |
| 455 | set_signal_handler(seq_context_t *ctxp) |
| 456 | { |
| 457 | struct sigaction *sap = calloc(1, sizeof(struct sigaction)); |
| 458 | |
| 459 | g_ctxp = ctxp; |
| 460 | |
| 461 | sap->sa_handler = signal_handler; |
| 462 | sigaction(SIGINT, sap, NULL); |
| 463 | } |
| 464 | |
| 465 | /* signal handler */ |
| 466 | static void |
| 467 | signal_handler(int sig) |
| 468 | { |
| 469 | /* Close device */ |
| 470 | if (g_ctxp) { |
| 471 | seq_free_context(g_ctxp); |
| 472 | } |
| 473 | |
| 474 | exit(1); |
| 475 | } |