Hi, First of all I would like to thank you for this! I use VMPK quite a lot and it is nice to see a "simpler" alternative without all the dependencies.
> The biggest missing feature is ALSA support, which I can't do > since I have no linux box, but might be extremely simple task. > Once the support is added, the software can be quite useful, in > such a small piece of code. I added basic support for ALSA, which works on my machine at least. See attached patch. (fair warning: I haven't used the ALSA sequencer API before) There seems to be some BSD-specific things in your code (u_char, for instance) which I had to change to make svmidi compile, but I haven't included this in the patch since it would distract from the ALSA support itself. Cheers, Johnny
>From 4f341bd11a76d8b92fcc954ba0e072af2b0e62d4 Mon Sep 17 00:00:00 2001 From: Johnny Oskarsson <jos...@joskar.se> Date: Wed, 6 Apr 2016 22:09:17 +0200 Subject: [PATCH] Added rudimentary ALSA support. Port routing currently needs to be done using external tools, such as aconnect(1). --- Makefile | 10 +++++++--- alsa.c | 45 +++++++++++++++++++++++++++++++++++++++++++++ alsa.h | 10 ++++++++++ 3 files changed, 62 insertions(+), 3 deletions(-) create mode 100644 alsa.c create mode 100644 alsa.h diff --git a/Makefile b/Makefile index a4a15fd..b8fe4c7 100644 --- a/Makefile +++ b/Makefile @@ -2,8 +2,8 @@ # VERSION = 0 # SOURCE -SOURCE = main.c sndio.c -OBJ = main.o sndio.o +SOURCE = main.c sndio.c alsa.c +OBJ = main.o HDR = midi.h config.h PREFIX = /usr/local @@ -15,10 +15,14 @@ XFLAGS = `pkg-config --cflags x11` SOUNDLIBS = -lsndio SOUNDFLAGS = -DSNDIO +SOUNDOBJ = sndio.o # TODO: implement # #uncomment for ALSA -#SOUNDLIBS = -lalsa +#SOUNDLIBS = -lasound #SOUNDFLAGS = -DALSA +#SOUNDOBJ = alsa.o + +OBJ += $(SOUNDOBJ) LIBS = ${XLIBS} ${SOUNDLIBS} CFLAGS = -g -std=c99 -pedantic -Wall -O0 ${XFLAGS} ${SOUNDFLAGS} diff --git a/alsa.c b/alsa.c new file mode 100644 index 0000000..48ecc3d --- /dev/null +++ b/alsa.c @@ -0,0 +1,45 @@ +#include <alsa/asoundlib.h> + +snd_seq_t *hdl; +static snd_midi_event_t *mbuf; +static int midiport; + +/* open */ +int +midiopen(void) +{ + snd_midi_event_new(32, &mbuf); + if (snd_seq_open(&hdl, "default", SND_SEQ_OPEN_OUTPUT, 0) != 0) { + return 1; + } else { + snd_seq_set_client_name(hdl, "svmidi"); + if ((midiport = snd_seq_create_simple_port(hdl, "svmidi", + SND_SEQ_PORT_CAP_READ|SND_SEQ_PORT_CAP_SUBS_READ, + SND_SEQ_PORT_TYPE_APPLICATION)) < 0) { + return 1; + } + return 0; + } +} + +/* send message */ +void +_midisend(unsigned char message[], size_t count) +{ + snd_seq_event_t ev; + + snd_midi_event_encode(mbuf, message, count, &ev); + + snd_seq_ev_set_subs(&ev); + snd_seq_ev_set_direct(&ev); + snd_seq_ev_set_source(&ev, midiport); + snd_seq_event_output_direct(hdl, &ev); +} + +/* close */ +void +midiclose(void) +{ + snd_midi_event_free(mbuf); + snd_seq_close(hdl); +} diff --git a/alsa.h b/alsa.h new file mode 100644 index 0000000..c870308 --- /dev/null +++ b/alsa.h @@ -0,0 +1,10 @@ +#include <alsa/asoundlib.h> + +/* device */ +extern snd_seq_t *hdl; + +/* functions */ +void _midisend(unsigned char [], size_t); +#define midisend(msg) _midisend(msg, sizeof(msg)) +int midiopen(void); +void midiclose(void); -- 2.4.6