diff --git a/Makefile b/Makefile index d0ce221..aee51a0 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,6 @@ CFLAGS += -Wall CFLAGS += -Werror +CFLAGS += -g all: hdjd aac123 @@ -17,3 +18,6 @@ aac123: LDLIBS += -lfaad -lmp4v2 aactest: CFLAGS += $(shell pkg-config --cflags alsa) aactest: LDLIBS += $(shell pkg-config --libs alsa) aactest: LDLIBS += -lfaad -lmp4v2 + +clean: + rm -f hdjd *.o diff --git a/alsa.c b/alsa.c index ce61e4c..ef22b5a 100644 --- a/alsa.c +++ b/alsa.c @@ -3,11 +3,11 @@ #include #include #include "alsa.h" +#include "usb.h" #include "dump.h" static snd_seq_t *snd_handle; static int seq_port; -static snd_midi_event_t *midi_event_parser; static snd_seq_event_t *ev; int @@ -24,11 +24,6 @@ alsa_setup(const char *name) SND_SEQ_PORT_CAP_READ | SND_SEQ_PORT_CAP_WRITE | SND_SEQ_PORT_CAP_SUBS_READ | SND_SEQ_PORT_CAP_SUBS_WRITE, SND_SEQ_PORT_TYPE_MIDI_GENERIC); - if (snd_midi_event_new(256, &midi_event_parser) < 0) { - perror("snd_midi_event_new"); - return -1; - } - return 0; } @@ -56,31 +51,36 @@ alsa_fd_setup(int *nfds, fd_set *rfds, fd_set *wfds) FD_SET(pfd[i].fd, rfds); } } + + } +#define ALSA_BUFSIZE 4096 void alsa_read_ready() { + static snd_midi_event_t *midi_event_parser; + + if (snd_midi_event_new(ALSA_BUFSIZE, &midi_event_parser) < 0) { + fprintf(stderr, "ALSA cannot allocate MIDI event parser\n"); + abort(); + } + for (;;) { - unsigned char buf[256]; + unsigned char buf[ALSA_BUFSIZE]; long converted; - int i; if (snd_seq_event_input(snd_handle, &ev) < 0) { break; } - if (!midi_event_parser) { - if (snd_midi_event_new(256, &midi_event_parser) < 0) { - continue; - } + + converted = snd_midi_event_decode(midi_event_parser, buf, ALSA_BUFSIZE, ev); + if (converted < 0) { + DUMP_l(converted); + } else { + usb_write(buf, converted); } - converted = snd_midi_event_decode(midi_event_parser, buf, 256, ev); - printf(" << "); - for (i = 0; i < converted; i += 1) { - printf("%02x ", buf[i]); - } - printf(":\n"); } } @@ -98,3 +98,30 @@ alsa_check_fds(fd_set *rfds, fd_set *wfds) } } } + +void +alsa_write(uint8_t *data, size_t datalen) +{ + static snd_midi_event_t *midi_event_parser; + snd_seq_event_t ev; + long r; + + if (snd_midi_event_new(ALSA_BUFSIZE, &midi_event_parser) < 0) { + fprintf(stderr, "ALSA cannot allocate MIDI event parser\n"); + abort(); + } + + r = snd_midi_event_encode(midi_event_parser, data, datalen, &ev); + if (r < datalen) { + fprintf(stderr, "ALSA didn't parse that message\n"); + abort(); + } + + if ((r = snd_seq_event_output(snd_handle, &ev)) < 0) { + fprintf(stderr, "ALSA couldn't write an event: %ld\n", r); + abort(); + } + + snd_seq_drain_output(snd_handle); + DUMP(); +} \ No newline at end of file diff --git a/alsa.h b/alsa.h index c98b3f2..52afcb5 100644 --- a/alsa.h +++ b/alsa.h @@ -1,10 +1,12 @@ #ifndef _ALSA_H_ #define _ALSA_H_ +#include #include int alsa_setup(const char *name); void alsa_fd_setup(int *nfds, fd_set *rfds, fd_set *wfds); void alsa_read_ready(); void alsa_check_fds(fd_set *rfds, fd_set *wfds); +void alsa_write(uint8_t *data, size_t datalen); #endif diff --git a/dump.h b/dump.h index 7cc4a9c..c65dd92 100644 --- a/dump.h +++ b/dump.h @@ -19,6 +19,7 @@ #endif #define DUMP() DUMPf("") #define DUMP_d(v) DUMPf("%s = %d", #v, v) +#define DUMP_l(v) DUMPf("%s = %ld", #v, v) #define DUMP_x(v) DUMPf("%s = 0x%x", #v, v) #define DUMP_s(v) DUMPf("%s = %s", #v, v) #define DUMP_c(v) DUMPf("%s = '%c' (0x%02x)", #v, v, v) diff --git a/usb.c b/usb.c index 4e03740..98a5d8f 100644 --- a/usb.c +++ b/usb.c @@ -5,6 +5,7 @@ #include #include #include "usb.h" +#include "alsa.h" #include "dump.h" static struct libusb_device_handle *usb_dev; @@ -30,6 +31,7 @@ static void usb_initiate_transfer() { // Tell libusb we want to know about bulk transfers + xfer = libusb_alloc_transfer(0); libusb_fill_bulk_transfer(xfer, usb_dev, d->ep_in, data, sizeof(data), usb_xfer_done, NULL, 0); libusb_submit_transfer(xfer); } @@ -39,18 +41,14 @@ usb_xfer_done(struct libusb_transfer *transfer) { uint8_t *data = transfer->buffer; int datalen = transfer->actual_length; - int i; - for (i = 0; i < datalen; i += 1) { - printf("%02x ", data[i]); - } - printf("\n"); + alsa_write(data, datalen); usb_initiate_transfer(); } int -usb_setup(const char *name, size_t namelen) +usb_setup(char *name, size_t namelen) { if (libusb_init(NULL) < 0) { return -1; @@ -76,16 +74,12 @@ usb_setup(const char *name, size_t namelen) { int ret; struct libusb_device_descriptor ddesc; - char name_[200]; - char *p = name_; - - strncpy(name_, name, sizeof(name_) - 1); - name_[sizeof(name_) - 1] = 0; libusb_get_device_descriptor(libusb_get_device(usb_dev), &ddesc); ret = libusb_get_string_descriptor_ascii(usb_dev, ddesc.iManufacturer, (unsigned char *)name, namelen); if (ret > 0) { - p = name_ + ret; + char *p = name + ret; + *p = ' '; p += 1; ret = libusb_get_string_descriptor_ascii(usb_dev, ddesc.iProduct, (unsigned char *)p, namelen - ret - 1); @@ -93,10 +87,9 @@ usb_setup(const char *name, size_t namelen) if (ret < 0) { printf("Warning: I can't figure out what to call this thing.\n"); } - printf("Opened a %s\n", name); + printf("Opened [%s]\n", name); } - xfer = libusb_alloc_transfer(0); return 0; } @@ -139,3 +132,15 @@ usb_check_fds(fd_set *rfds, fd_set *wfds) } } } + +void +usb_write(uint8_t *data, size_t datalen) +{ + int i; + + printf("U> "); + for (i = 0; i < datalen; i += 1) { + printf("%02x ", data[i]); + } + printf("\n"); +} \ No newline at end of file diff --git a/usb.h b/usb.h index 98d8b18..e4538a2 100644 --- a/usb.h +++ b/usb.h @@ -1,10 +1,12 @@ #ifndef _USB_H_ #define _USB_H_ +#include #include -int usb_setup(const char *name, size_t namelen); +int usb_setup(char *name, size_t namelen); void usb_fd_setup(int *nfds, fd_set *rfds, fd_set *wfds); void usb_check_fds(fd_set *rfds, fd_set *wfds); +void usb_write(uint8_t *data, size_t datalen); #endif