/* alsa_rawmidi_exit: * Cleans up. */ static void alsa_rawmidi_exit(int input) { if (rawmidi_handle) { #if ALLEGRO_ALSA_VERSION == 9 snd_rawmidi_drain(rawmidi_handle); #else /* ALLEGRO_ALSA_VERSION == 5 */ snd_rawmidi_output_drain(rawmidi_handle); #endif snd_rawmidi_close(rawmidi_handle); } rawmidi_handle = NULL; }
void sighandler(int dum) { stop=1; fprintf(stderr,"Closing.\n"); if (handle_in) { snd_rawmidi_drain(handle_in); snd_rawmidi_close(handle_in); } lcm_destroy (lcm); exit(0); }
int main(int argc,char** argv) { int i; int err; int thru=0; int verbose = 0; char *device_in = NULL; char *device_out = NULL; char *node_in = NULL; char *node_out = NULL; int fd_in = -1,fd_out = -1; snd_rawmidi_t *handle_in = 0,*handle_out = 0; if (argc==1) { usage(); exit(0); } for (i = 1 ; i<argc ; i++) { if (argv[i][0]=='-') { switch (argv[i][1]) { case 'h': usage(); break; case 'v': verbose = 1; break; case 't': thru = 1; break; case 'i': if (i + 1 < argc) device_in = argv[++i]; break; case 'I': if (i + 1 < argc) node_in = argv[++i]; break; case 'o': if (i + 1 < argc) device_out = argv[++i]; break; case 'O': if (i + 1 < argc) node_out = argv[++i]; break; } } } if (verbose) { fprintf(stderr,"Using: \n"); fprintf(stderr,"Input: "); if (device_in) { fprintf(stderr,"device %s\n",device_in); }else if (node_in){ fprintf(stderr,"%s\n",node_in); }else{ fprintf(stderr,"NONE\n"); } fprintf(stderr,"Output: "); if (device_out) { fprintf(stderr,"device %s\n",device_out); }else if (node_out){ fprintf(stderr,"%s\n",node_out); }else{ fprintf(stderr,"NONE\n"); } } if (device_in) { err = snd_rawmidi_open(&handle_in,NULL,device_in,0); if (err) { fprintf(stderr,"snd_rawmidi_open %s failed: %d\n",device_in,err); } } if (node_in && (!node_out || strcmp(node_out,node_in))) { fd_in = open(node_in,O_RDONLY); if (fd_in<0) { fprintf(stderr,"open %s for input failed\n",node_in); } } signal(SIGINT,sighandler); if (device_out) { err = snd_rawmidi_open(NULL,&handle_out,device_out,0); if (err) { fprintf(stderr,"snd_rawmidi_open %s failed: %d\n",device_out,err); } } if (node_out && (!node_in || strcmp(node_out,node_in))) { fd_out = open(node_out,O_WRONLY); if (fd_out<0) { fprintf(stderr,"open %s for output failed\n",node_out); } } if (node_in && node_out && strcmp(node_out,node_in)==0) { fd_in = fd_out = open(node_out,O_RDWR); if (fd_out<0) { fprintf(stderr,"open %s for input and output failed\n",node_out); } } if (!thru) { if (handle_in || fd_in!=-1) { fprintf(stderr,"Read midi in\n"); fprintf(stderr,"Press ctrl-c to stop\n"); } if (handle_in) { unsigned char ch; while (!stop) { snd_rawmidi_read(handle_in,&ch,1); if (verbose) { fprintf(stderr,"read %02x\n",ch); } } } if (fd_in!=-1) { unsigned char ch; while (!stop) { read(fd_in,&ch,1); if (verbose) { fprintf(stderr,"read %02x\n",ch); } } } if (handle_out || fd_out!=-1) { fprintf(stderr,"Writing note on / note off\n"); } if (handle_out) { unsigned char ch; ch=0x90; snd_rawmidi_write(handle_out,&ch,1); ch=60; snd_rawmidi_write(handle_out,&ch,1); ch=100; snd_rawmidi_write(handle_out,&ch,1); snd_rawmidi_drain(handle_out); sleep(1); ch=0x90; snd_rawmidi_write(handle_out,&ch,1); ch=60; snd_rawmidi_write(handle_out,&ch,1); ch=0; snd_rawmidi_write(handle_out,&ch,1); snd_rawmidi_drain(handle_out); } if (fd_out!=-1) { unsigned char ch; ch=0x90; write(fd_out,&ch,1); ch=60; write(fd_out,&ch,1); ch=100; write(fd_out,&ch,1); sleep(1); ch=0x90; write(fd_out,&ch,1); ch=60; write(fd_out,&ch,1); ch=0; write(fd_out,&ch,1); } } else { if ((handle_in || fd_in!=-1) && (handle_out || fd_out!=-1)) { if (verbose) { fprintf(stderr,"Testing midi thru in\n"); } while (!stop) { unsigned char ch; if (handle_in) { snd_rawmidi_read(handle_in,&ch,1); } if (fd_in!=-1) { read(fd_in,&ch,1); } if (verbose) { fprintf(stderr,"thru: %02x\n",ch); } if (handle_out) { snd_rawmidi_write(handle_out,&ch,1); snd_rawmidi_drain(handle_out); } if (fd_out!=-1) { write(fd_out,&ch,1); } } }else{ fprintf(stderr,"Testing midi thru needs both input and output\n"); exit(-1); } } if (verbose) { fprintf(stderr,"Closing\n"); } if (handle_in) { snd_rawmidi_drain(handle_in); snd_rawmidi_close(handle_in); } if (handle_out) { snd_rawmidi_drain(handle_out); snd_rawmidi_close(handle_out); } if (fd_in!=-1) { close(fd_in); } if (fd_out!=-1) { close(fd_out); } return 0; }
int main(int argc, char** argv) { int i, j, k, opos, ipos, patsize; int err; int verbose = 0; snd_rawmidi_t *handle_in = NULL, *handle_out = NULL; unsigned char ibuf[512], obuf[512]; char *iname = "hw:0,0", *oname = "hw:0,0"; struct timeval start, end; long long diff; snd_rawmidi_status_t *istat, *ostat; for (i = 1 ; i<argc ; i++) { if (argv[i][0]=='-') { if (!strcmp(argv[i], "--help")) { usage(); return 0; } switch (argv[i][1]) { case 'h': usage(); return 0; case 'v': verbose = 1; break; case 'i': if (i + 1 < argc) iname = argv[++i]; break; case 'o': if (i + 1 < argc) oname = argv[++i]; break; } } } if (iname == NULL) iname = oname; if (oname == NULL) oname = iname; if (verbose) { fprintf(stderr, "Using: \n"); fprintf(stderr, " Input: %s Output: %s\n", iname, oname); } err = snd_rawmidi_open(&handle_in, NULL, iname, SND_RAWMIDI_NONBLOCK); if (err) { fprintf(stderr,"snd_rawmidi_open %s failed: %d\n",iname,err); exit(EXIT_FAILURE); } err = snd_rawmidi_open(NULL, &handle_out, oname, 0); if (err) { fprintf(stderr,"snd_rawmidi_open %s failed: %d\n",oname,err); exit(EXIT_FAILURE); } signal(SIGINT, sighandler); i = snd_rawmidi_read(handle_in, ibuf, sizeof(ibuf)); if (i > 0) { printf("Read ahead: %i\n", i); for (j = 0; j < i; j++) printf("%02x:", ibuf[j]); printf("\n"); exit(EXIT_FAILURE); } snd_rawmidi_nonblock(handle_in, 0); patsize = writepattern(handle_out, obuf); gettimeofday(&start, NULL); patsize = writepattern(handle_out, obuf); k = ipos = opos = err = 0; while (!stop) { i = snd_rawmidi_read(handle_in, ibuf, sizeof(ibuf)); for (j = 0; j < i; j++, ipos++) if (obuf[k] != ibuf[j]) { printf("ipos = %i, i[0x%x] != o[0x%x]\n", ipos, ibuf[j], obuf[k]); if (opos > 0) stop = 1; } else { printf("match success: ipos = %i, opos = %i [%i:0x%x]\n", ipos, opos, k, obuf[k]); k++; opos++; if (k >= patsize) { patsize = writepattern(handle_out, obuf); k = 0; } } } gettimeofday(&end, NULL); printf("End...\n"); snd_rawmidi_status_alloca(&istat); snd_rawmidi_status_alloca(&ostat); err = snd_rawmidi_status(handle_in, istat); if (err < 0) fprintf(stderr, "input stream status error: %d\n", err); err = snd_rawmidi_status(handle_out, ostat); if (err < 0) fprintf(stderr, "output stream status error: %d\n", err); printf("input.status.avail = %zi\n", snd_rawmidi_status_get_avail(istat)); printf("input.status.xruns = %zi\n", snd_rawmidi_status_get_xruns(istat)); printf("output.status.avail = %zi\n", snd_rawmidi_status_get_avail(ostat)); printf("output.status.xruns = %zi\n", snd_rawmidi_status_get_xruns(ostat)); diff = timediff(end, start); printf("Time diff: %Liusec (%Li bytes/sec)\n", diff, ((long long)opos * 1000000) / diff); if (verbose) { fprintf(stderr,"Closing\n"); } snd_rawmidi_drain(handle_in); snd_rawmidi_close(handle_in); snd_rawmidi_drain(handle_out); snd_rawmidi_close(handle_out); return 0; }
int main(int argc,char** argv) { int i; int err; int thru=0; int verbose = 0; char *device_in = NULL; char *lcm_out = NULL; int fd_in = -1,fd_out = -1; snd_rawmidi_t *handle_out = 0; if (argc!=3) { usage(); exit(0); } device_in = argv[1]; lcm_out = argv[2]; lcm = lcm_create ("udpm://239.255.76.67:7667?ttl=0"); if (!lcm) return 1; struct timeval thisTime; fprintf(stderr,"Using: \n"); fprintf(stderr,"Input: "); fprintf(stderr,"device %s\n",device_in); fprintf(stderr,"Output: "); fprintf(stderr,"LCM channel %s\n", lcm_out); fprintf(stderr,"Read midi in\n"); fprintf(stderr,"Press ctrl-c to stop\n"); fprintf(stderr,"Broadcasting LCM: %s\n", lcm_out); if (device_in) { err = snd_rawmidi_open(&handle_in,NULL,device_in,0); if (err) { fprintf(stderr,"snd_rawmidi_open %s failed: %d\n",device_in,err); return -1; } } signal(SIGINT,sighandler); int num = 0; int val[10]; int sysExclusive = 0; if (handle_in) { unsigned char ch; while (!stop) { snd_rawmidi_read(handle_in,&ch,1); // check to make sure this isn't a system exclusive message (begins with 0xF0. Ends with a 0xF7) if (ch == 0xF0) { sysExclusive = 1; //fprintf(stderr,"sys exclusive\n"); } if (sysExclusive == 0) { val[num] = (int)ch; if (num >= 2) { //fprintf(stderr,"read %02x --> %02x --> %d\n", val[0], val[1], val[2]); num = 0; // send LCM message lcmt_midi msg; gettimeofday(&thisTime, NULL); msg.timestamp = (thisTime.tv_sec * 1000.0) + (float)thisTime.tv_usec/1000.0 + 0.5; msg.event[0] = val[0]; msg.event[1] = val[1]; msg.event[2] = val[2]; lcmt_midi_publish (lcm, lcm_out, &msg); } else { num ++; } //fprintf(stderr,"read %02x\n", ch); } if (ch == 0xF7) { sysExclusive = 0; } } } fprintf(stderr,"Closing.\n"); if (handle_in) { snd_rawmidi_drain(handle_in); snd_rawmidi_close(handle_in); } lcm_destroy (lcm); return 0; }
static int do_midi_output(process_midi_t *proc) { int worked = 0; output_port_t *port = (output_port_t*) proc->port; if (!midi_is_ready(proc)) return 0; // eat events while (port->next_event.time <= proc->cur_time) { port->todo += port->next_event.size; if (jack_ringbuffer_read(port->base.event_ring, (char*)&port->next_event, sizeof(port->next_event))!=sizeof(port->next_event)) { port->next_event.time = 0; port->next_event.size = 0; break; } else debug_log("midi_out: at %ld got %d bytes for %ld", (long)proc->cur_time, (int)port->next_event.size, (long)port->next_event.time); } if (port->todo) debug_log("midi_out: todo = %d at %ld", (int)port->todo, (long)proc->cur_time); // calc next wakeup time if (!port->todo && port->next_event.time && port->next_event.time < proc->next_time) { proc->next_time = port->next_event.time; debug_log("midi_out: next_time = %ld", (long)proc->next_time); } if (port->todo && port->base.is_ready) { // write data int size = port->todo; int res; jack_ringbuffer_data_t vec[2]; jack_ringbuffer_get_read_vector(port->base.data_ring, vec); if (size > vec[0].len) { size = vec[0].len; assert (size > 0); } res = snd_rawmidi_write(port->base.rawmidi, vec[0].buf, size); if (res > 0) { jack_ringbuffer_read_advance(port->base.data_ring, res); debug_log("midi_out: written %d bytes to %s", res, port->base.name); port->todo -= res; worked = 1; } else if (res == -EWOULDBLOCK) { port->base.is_ready = 0; debug_log("midi_out: -EWOULDBLOCK on %s", port->base.name); return 1; } else { error_log("midi_out: writing to port %s failed: %s", port->base.name, snd_strerror(res)); return 0; } snd_rawmidi_drain(port->base.rawmidi); } // update pfds for this port if (!midi_update_pfds(proc)) return 0; if (!port->todo) { int i; if (worked) debug_log("midi_out: relaxing on %s", port->base.name); for (i=0; i<port->base.npfds; ++i) proc->wpfds[i].events &= ~POLLOUT; } else { int i; for (i=0; i<port->base.npfds; ++i) proc->wpfds[i].events |= POLLOUT; } return 1; }
int main(int argc, char *argv[]) { fd_set rfds; int res; int err; int version = -1, ioret = -1; unsigned numevs, c; unsigned char read_buffer[sizeof(struct input_event)*3]; /* max 3 events per read */ unsigned char ch; unsigned char cCommand[3]; unsigned char cStates[255]; struct input_event *currev; char device_name[1024]; char *device_out = NULL; device_out = argv[2]; int fd_in = -1,fd_out = -1; snd_rawmidi_t *handle_in = 0,*handle_out = 0; memset(&cStates, 0, sizeof(cStates)); /* struct sigaction sighandler; memset(&sighandler, 0, sizeof(sighandler)); sighandler.sa_handler = catch_signal; sigaction(SIGINT, &sighandler, NULL); sigaction(SIGQUIT, &sighandler, NULL); */ signal(SIGINT,catch_signal); if ( argc < 2 ) { fprintf(stderr, "Device needed\n"); return -1; } FD_ZERO(&rfds); fd = open(argv[1], O_RDONLY); if ( -1 == fd ) { fprintf(stderr, "unable to read from mice - please give me a keyboard.\n"); return -1; } ioret = ioctl(fd, EVIOCGVERSION, &version); ioret = ioctl(fd, EVIOCGNAME(sizeof(device_name)), device_name); ioret = ioctl(fd, EVIOCGRAB, &grab); if ( -1 == ioret ) { perror("ioctl()"); } fprintf(stdout, "ver: %d, ret = %d\n", version, ioret); printf("device name is: %s\n", device_name); printf("EVIOCGRAB is: %lu\n", EVIOCGRAB); if (device_out) { err = snd_rawmidi_open(NULL,&handle_out,device_out,0); if (err) { fprintf(stderr,"snd_rawmidi_open %s failed: %d\n",device_out,err); } } /* to read the config file: char *option; char *value; fscanf( "%20[^#=]s=%80[^\n]", &option, &value ); */ FD_SET(fd, &rfds); while ( 1 ) { res = select(fd + 1, &rfds, NULL, NULL, NULL); if ( -1 == res && EINTR == errno ) { continue; } if ( -1 == res ) { perror("select() failed"); fprintf(stderr, "failed to select, fd is %d\n", fd); return -1; } if ( FD_ISSET(fd, &rfds) ) { // fprintf(stdout, "got some data\n"); res = read(fd, read_buffer, sizeof(read_buffer)); if ( -1 == res) { fprintf(stderr, "error reading data\n"); return -1; } // fprintf(stdout, "got %d bytes\n", res); numevs = ( res / sizeof(struct input_event) ); /* get how many input events we got */ // fprintf(stdout, "got %u events\n", numevs); for ( c = 0; c < numevs; c++ ) { currev = (struct input_event *)(read_buffer + (sizeof(struct input_event) * c)); // fprintf(stdout, "event time %ld/%ld\n", currev->time.tv_sec, currev->time.tv_usec); // fprintf(stdout, "event type = %hd, code = %hd, value = %d\n", currev->type, currev->code, currev->value); if ( 1 == currev->type ) { if ( 1 == currev->value ) { // press fprintf(stdout, " %hd \n", currev->code); // note on ch=0x90; snd_rawmidi_write(handle_out,&ch,1); ch=currev->code; snd_rawmidi_write(handle_out,&ch,1); ch=127; snd_rawmidi_write(handle_out,&ch,1); // controller up ch=0xB0; snd_rawmidi_write(handle_out,&ch,1); ch=currev->code; snd_rawmidi_write(handle_out,&ch,1); ch=127; snd_rawmidi_write(handle_out,&ch,1); if ( 1 == cStates[currev->code] ) { // toggle was on already cStates[currev->code] = 0; // send a note-off ch=0x91; snd_rawmidi_write(handle_out,&ch,1); ch=currev->code; snd_rawmidi_write(handle_out,&ch,1); ch=0; snd_rawmidi_write(handle_out,&ch,1); // and a controller down ch=0xB1; snd_rawmidi_write(handle_out,&ch,1); ch=currev->code; snd_rawmidi_write(handle_out,&ch,1); ch=0; snd_rawmidi_write(handle_out,&ch,1); } else { // toggle was off or uninitialised cStates[currev->code] = 1; // send a note-on ch=0x91; snd_rawmidi_write(handle_out,&ch,1); ch=currev->code; snd_rawmidi_write(handle_out,&ch,1); ch=127; snd_rawmidi_write(handle_out,&ch,1); // and controller up ch=0xB1; snd_rawmidi_write(handle_out,&ch,1); ch=currev->code; snd_rawmidi_write(handle_out,&ch,1); ch=127; snd_rawmidi_write(handle_out,&ch,1); } snd_rawmidi_drain(handle_out); } else if ( 2 == currev->value ) { // repeat fprintf(stdout, "*%hd*\n", currev->code); } else if ( 0 == currev->value ) { // release fprintf(stdout, "[%hd]\n", currev->code); // note off ch=0x90; snd_rawmidi_write(handle_out,&ch,1); ch=currev->code; snd_rawmidi_write(handle_out,&ch,1); ch=0; snd_rawmidi_write(handle_out,&ch,1); // controller down ch=0xB0; snd_rawmidi_write(handle_out,&ch,1); ch=currev->code; snd_rawmidi_write(handle_out,&ch,1); ch=0; snd_rawmidi_write(handle_out,&ch,1); snd_rawmidi_drain(handle_out); // sleep(1); } else { // dunno fprintf(stdout, "?%hd? - What is value %d?\n", currev->code, currev->value); } } } } else { fprintf(stderr, "odd ... no data and we only listen in 1 fd\n"); } } return 0; }