From: Tomas Mudrunka Date: Tue, 16 Apr 2013 08:09:35 +0000 (+0200) Subject: FTDI USB MIDI connection X-Git-Url: https://git.harvie.cz/?a=commitdiff_plain;h=233e34bb486439ff24580eb34362591d22289b81;p=mirrors%2FPrograms.git FTDI USB MIDI connection --- diff --git a/c/ghetto-sound-system/.gitignore b/c/ghetto-sound-system/.gitignore new file mode 100644 index 0000000..b9d7d15 --- /dev/null +++ b/c/ghetto-sound-system/.gitignore @@ -0,0 +1,6 @@ +.gitignore +sampler +synth +beatdetect +midi-rx +ftdi-midi diff --git a/c/ghetto-sound-system/Makefile b/c/ghetto-sound-system/Makefile index 260a436..d99fdb5 100644 --- a/c/ghetto-sound-system/Makefile +++ b/c/ghetto-sound-system/Makefile @@ -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 index 0000000..652141e --- /dev/null +++ b/c/ghetto-sound-system/ftdi-midi.c @@ -0,0 +1,57 @@ +#include +#include +#include +#include + + +#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); +}