FTDI USB MIDI connection
authorTomas Mudrunka <tomas@mudrunka.cz>
Tue, 16 Apr 2013 08:09:35 +0000 (10:09 +0200)
committerTomas Mudrunka <tomas@mudrunka.cz>
Tue, 16 Apr 2013 08:09:35 +0000 (10:09 +0200)
c/ghetto-sound-system/.gitignore [new file with mode: 0644]
c/ghetto-sound-system/Makefile
c/ghetto-sound-system/ftdi-midi.c [new file with mode: 0644]

diff --git a/c/ghetto-sound-system/.gitignore b/c/ghetto-sound-system/.gitignore
new file mode 100644 (file)
index 0000000..b9d7d15
--- /dev/null
@@ -0,0 +1,6 @@
+.gitignore
+sampler
+synth
+beatdetect
+midi-rx
+ftdi-midi
index 260a436f3aaacf0e120881a0581b6ab6224b9232..d99fdb5ace9b02796a4874fc3abf026159ee8c15 100644 (file)
@@ -6,9 +6,9 @@ sdl_CFLAGS+=$(shell sdl-config --cflags --libs) -lSDL_mixer
 sdl_CFLAGS+=-lm #synth
 alsa_CFLAGS+=-lasound
 
-.PHONY: all clean sdl alsa go
+.PHONY: all clean sdl alsa go midi
 all: sdl alsa
-       echo .gitignore $(sdl_BIN) $(alsa_BIN) | tr ' ' '\n' > .gitignore
+       echo .gitignore $(sdl_BIN) $(alsa_BIN) ftdi-midi | tr ' ' '\n' > .gitignore
 clean:
        rm .gitignore $(sdl_BIN) $(alsa_BIN)
 
@@ -17,6 +17,9 @@ sdl:
 alsa:
        $(MAKE) CFLAGS="$(alsa_CFLAGS)" $(alsa_BIN)
 
+midi:
+       $(MAKE) CFLAGS="-lftdi -lasound" ftdi-midi
+
 
 go: sampler
        ./$? samples/*/* 2>/dev/null
diff --git a/c/ghetto-sound-system/ftdi-midi.c b/c/ghetto-sound-system/ftdi-midi.c
new file mode 100644 (file)
index 0000000..652141e
--- /dev/null
@@ -0,0 +1,57 @@
+#include <stdio.h>
+#include <unistd.h>
+#include <alsa/asoundlib.h>
+#include <ftdi.h>
+
+
+#define BAUD 31250
+
+static snd_rawmidi_t *midi_in, *midi_out;
+struct ftdi_context ftdi;
+
+int main(void) {
+
+
+       snd_rawmidi_open(&midi_in, &midi_out, "virtual", SND_RAWMIDI_NONBLOCK);
+       //snd_rawmidi_open(&midi_in, &midi_out, "virtual", 0);
+
+
+       if (ftdi_init( &ftdi )) {
+               fprintf(stderr,  "usb - init error !\n");
+               return 1;
+       }
+
+       if (ftdi_usb_open(&ftdi, 0x0403, 0x6001)) {
+               fprintf(stderr,  "usb - open error (cannot find?) !\n");
+               fprintf(stderr, "ftdi_usb_open failed, error (%s)\n", ftdi_get_error_string(&ftdi));
+               ftdi_deinit( &ftdi );
+               return 2;
+       }
+
+       if (ftdi_usb_reset( &ftdi )) {
+               fprintf(stderr,  "usb - reset error !\n");
+               ftdi_usb_close( &ftdi );
+               ftdi_deinit( &ftdi );
+               return 3;
+       }
+
+       ftdi_disable_bitbang( &ftdi );
+       ftdi_set_baudrate(&ftdi, BAUD);
+
+       unsigned char buf;
+       int ret;
+       while(1) {
+               //FTDI2MIDI
+               ret = ftdi_read_data(&ftdi,&buf,1);
+               if(ret < 0) break;
+               if(ret > 0) snd_rawmidi_write(midi_out, &buf, 1);
+
+               //MIDI2FTDI
+               ret = snd_rawmidi_read(midi_in,&buf,1);
+               if(ret < 0 && ret != -EAGAIN) break;
+               if(ret > 0) ftdi_write_data(&ftdi, &buf, 1);
+
+               usleep(1000);
+       }
+       exit(0);
+}
This page took 0.194778 seconds and 4 git commands to generate.