diff --git a/alsa.c b/alsa.c index 87d13ff..6765b66 100644 --- a/alsa.c +++ b/alsa.c @@ -73,9 +73,10 @@ alsa_fd_setup(int *nfds, fd_set *rfds, fd_set *wfds) void alsa_read_ready() { - static snd_seq_event_t *ev; - + snd_midi_event_init(midi_event_parser); + for (;;) { + snd_seq_event_t *ev; unsigned char buf[ALSA_BUFSIZE]; long converted; int r; @@ -83,19 +84,34 @@ alsa_read_ready() r = snd_seq_event_input(snd_handle, &ev); if (r == -EAGAIN) { + // input queue empty break; - } - if (r == -ENOSPC) { - warn("Out of space on input queue"); + } else if (r == -ENOSPC) { + warn("Input queue overflow"); + continue; + } else if (r < 0) { + warn("snd_seq_event_input() = %d", r); + continue; } converted = snd_midi_event_decode(midi_event_parser, buf, ALSA_BUFSIZE, ev); if (converted < 0) { - warn("Can't decode MIDI event type %d", ev->type); - } else { - DUMP_d(converted); - usb_write(buf, converted); + warn("Can't decode MIDI event type %x", ev->type); + continue; } + + if (converted < 3) { + int i; + + warn("Uh oh, weird MIDI packet with length %ld (does this make sense if prefixed with 90?)", converted); + + for (i = 0; i < converted; i += 1) { + fprintf(stderr, " %02x", buf[i]); + } + fprintf(stderr, "\n"); + } + + usb_write(buf, converted); } } @@ -119,6 +135,8 @@ alsa_write(uint8_t *data, size_t datalen) { size_t offset = 0; + snd_midi_event_init(midi_event_parser); + for (; datalen > offset;) { snd_seq_event_t ev; long encoded; diff --git a/dump.h b/dump.h index 9d002c8..1fb863b 100644 --- a/dump.h +++ b/dump.h @@ -9,7 +9,7 @@ #ifdef NODUMP # define DUMPf(fmt, args...) #else -# define DUMPf(fmt, args...) fprintf(stderr, "%s:%s:%d " fmt "\n", __FILE__, __FUNCTION__, __LINE__, ##args) +# define DUMPf(fmt, args...) fprintf(stderr, "%s:%d " fmt "\n", __FILE__, __LINE__, ##args) #endif #define DUMP() DUMPf("") #define DUMP_d(v) DUMPf("%s = %d", #v, (int)v) diff --git a/usb.c b/usb.c index 70b5206..fb3951b 100644 --- a/usb.c +++ b/usb.c @@ -156,7 +156,9 @@ usb_check_fds(fd_set *rfds, fd_set *wfds) void usb_write_done(struct libusb_transfer *xfer) { - DUMP_d(xfer->status); + if (xfer->status) { + warn("USB Write status %d", xfer->status); + } writes_pending -= 1; free(xfer->buffer); libusb_free_transfer(xfer); @@ -167,14 +169,7 @@ usb_write(uint8_t *data, size_t datalen) { struct libusb_transfer *xfer; unsigned char *buf; - int i; - - DUMP_d(datalen); - for (i = 0; i < datalen; i += 1) { - fprintf(stderr, " %02x", data[i]); - } - fprintf(stderr, "\n"); - + writes_pending += 1; xfer = libusb_alloc_transfer(0); buf = (unsigned char *)malloc(datalen);