1
0
mirror of git://projects.qi-hardware.com/wernermisc.git synced 2024-11-15 12:34:05 +02:00
wernermisc/midi2osc/midi2osc.c
2011-11-07 13:43:54 -03:00

103 lines
2.2 KiB
C

/*
* midi2osc.c - MIDI to OSC forwarder
*
* Written 2011 by Werner Almesberger
* Copyright 2011 Werner Almesberger
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*/
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <alsa/asoundlib.h>
#include "lo/lo.h"
#define NAME "midi2osc"
static void forward(snd_seq_t *midi, lo_address osc)
{
snd_seq_event_t *ev;
uint8_t msg[4] = { 0, };
while (snd_seq_event_input(midi, &ev)) {
switch (ev->type) {
case SND_SEQ_EVENT_NOTEON:
msg[1] = 0x90 | ev->data.control.channel;
msg[2] = ev->data.note.note;
msg[3] = ev->data.note.velocity;
break;
case SND_SEQ_EVENT_CONTROLLER:
msg[1] = 0xb0 | ev->data.control.channel;
msg[2] = ev->data.control.param;
msg[3] = ev->data.control.value;
break;
case SND_SEQ_EVENT_PITCHBEND:
msg[1] = 0xe0 | ev->data.control.channel;
msg[2] = ev->data.control.value;
msg[3] = 0;
break;
default:
/* Flickernoise currently doesn't support any others */
snd_seq_free_event(ev);
continue;
}
if (lo_send(osc, "/midi", "m", msg) < 0)
fprintf(stderr, "%s\n", lo_address_errstr(osc));
snd_seq_free_event(ev);
}
}
static void usage(const char *name)
{
fprintf(stderr, "usage: %s hostname [port]\n", name);
exit(1);
}
int main(int argc, char **argv)
{
const char *port = "4444"; /* Milkymist One OSC port */
lo_address osc;
snd_seq_t *midi;
switch (argc) {
case 2:
break;
case 3:
port = argv[2];
break;
default:
usage(*argv);
}
osc = lo_address_new(argv[1], port);
if (!osc) {
fprintf(stderr, "invalid address %s %s\n", argv[1], port);
exit(1);
}
if (snd_seq_open(&midi, "hw", SND_SEQ_OPEN_INPUT,0) < 0) {
fprintf(stderr, "can't open ALSA sequencer\n");
exit(1);
}
snd_seq_set_client_name(midi, NAME);
if (snd_seq_create_simple_port(midi, NAME,
SND_SEQ_PORT_CAP_WRITE | SND_SEQ_PORT_CAP_SUBS_WRITE,
SND_SEQ_PORT_TYPE_APPLICATION) < 0) {
fprintf(stderr, "can't create sequencer port\n");
exit(1);
}
forward(midi, osc);
return 0;
}