1
0
mirror of git://projects.qi-hardware.com/wernermisc.git synced 2024-11-15 11:30:37 +02:00

midi2osc: added channel wildcard and overriding the value

This commit is contained in:
Werner Almesberger 2011-11-27 10:56:40 -03:00
parent 3a82787c6c
commit af0c0ab5c8
2 changed files with 35 additions and 16 deletions

View File

@ -22,7 +22,7 @@ To use it with the Milkymist One,
midi2osc can also remap MIDI controls. The syntax is midi2osc can also remap MIDI controls. The syntax is
c<chan>[.<control>]=c<chan>[.<control>] c[<chan>][.<control>]=c<chan>[.<control>[=<value>]]
where <chan> is a channel number and <control> is the optional control where <chan> is a channel number and <control> is the optional control
number. For example, number. For example,
@ -32,3 +32,7 @@ number. For example,
would map the joysticks and two faders of a Faderfox LV3 to the would map the joysticks and two faders of a Faderfox LV3 to the
controls 1 through 6 on channel 0, and send the OSC messages to controls 1 through 6 on channel 0, and send the OSC messages to
a host called "m1". a host called "m1".
If <value> is given, that value will be assigned for any control
message that matches the input pattern, irrespective of the input
value.

View File

@ -27,11 +27,13 @@ static int debug = 0;
static struct map { static struct map {
int chan_in, ctrl_in; int chan_in, ctrl_in;
int chan_out, ctrl_out; int chan_out, ctrl_out;
int value;
struct map *next; struct map *next;
} *mappings = NULL; } *mappings = NULL;
static void add(int chan_in, int ctrl_in, int chan_out, int ctrl_out) static void add(int chan_in, int ctrl_in, int chan_out, int ctrl_out,
int value)
{ {
struct map *new; struct map *new;
@ -40,6 +42,7 @@ static void add(int chan_in, int ctrl_in, int chan_out, int ctrl_out)
new->ctrl_in = ctrl_in; new->ctrl_in = ctrl_in;
new->chan_out = chan_out; new->chan_out = chan_out;
new->ctrl_out = ctrl_out; new->ctrl_out = ctrl_out;
new->value = value;
new->next = mappings; new->next = mappings;
mappings = new; mappings = new;
} }
@ -47,17 +50,22 @@ static void add(int chan_in, int ctrl_in, int chan_out, int ctrl_out)
static void add_mapping(const char *s) static void add_mapping(const char *s)
{ {
unsigned chan_in, ctrl_in, chan_out, ctrl_out; unsigned chan_in, ctrl_in, chan_out, ctrl_out, value;
if (sscanf(s, "c%u.%u=c%u.%u", if (sscanf(s, "c%u.%u=c%u.%u=%u",
&chan_in, &ctrl_in, &chan_out, &ctrl_out, &value) == 5)
add(chan_in, ctrl_in, chan_out, ctrl_out, value);
else if (sscanf(s, "c%u.%u=c%u.%u",
&chan_in, &ctrl_in, &chan_out, &ctrl_out) == 4) &chan_in, &ctrl_in, &chan_out, &ctrl_out) == 4)
add(chan_in, ctrl_in, chan_out, ctrl_out); add(chan_in, ctrl_in, chan_out, ctrl_out, -1);
else if (sscanf(s, "c%u.%u=c%u", &chan_in, &ctrl_in, &chan_out) == 3) else if (sscanf(s, "c%u.%u=c%u", &chan_in, &ctrl_in, &chan_out) == 3)
add(chan_in, ctrl_in, chan_out, -1); add(chan_in, ctrl_in, chan_out, -1, -1);
else if (sscanf(s, "c%u=c%u.%u", &chan_in, &chan_out, &ctrl_out) == 3) else if (sscanf(s, "c%u=c%u.%u", &chan_in, &chan_out, &ctrl_out) == 3)
add(chan_in, -1, chan_out, ctrl_out); add(chan_in, -1, chan_out, ctrl_out, -1);
else if (sscanf(s, "c.%u=c%u.%u", &ctrl_in, &chan_out, &ctrl_out) == 3)
add(-1, ctrl_in, chan_out, ctrl_out, -1);
else if (sscanf(s, "c%u=c%u", &chan_in, &chan_out) == 2) else if (sscanf(s, "c%u=c%u", &chan_in, &chan_out) == 2)
add(chan_in, -1, chan_out, -1); add(chan_in, -1, chan_out, -1, -1);
else { else {
fprintf(stderr, "unrecognized mapping syntax\n"); fprintf(stderr, "unrecognized mapping syntax\n");
exit(1); exit(1);
@ -65,7 +73,7 @@ static void add_mapping(const char *s)
} }
static void map(uint8_t *chan, uint8_t *ctrl) static void map(uint8_t *chan, uint8_t *ctrl, uint8_t *value)
{ {
const struct map *m; const struct map *m;
@ -76,6 +84,8 @@ static void map(uint8_t *chan, uint8_t *ctrl)
*chan = m->chan_out; *chan = m->chan_out;
if (m->ctrl_out != -1) if (m->ctrl_out != -1)
*ctrl = m->ctrl_out; *ctrl = m->ctrl_out;
if (m->value != -1)
*value = m->value;
return; return;
} }
} }
@ -85,7 +95,7 @@ static void forward(snd_seq_t *midi, lo_address osc)
{ {
snd_seq_event_t *ev; snd_seq_event_t *ev;
uint8_t msg[4] = { 0, }; uint8_t msg[4] = { 0, };
uint8_t chan, ctrl; uint8_t chan, ctrl, value;
while (snd_seq_event_input(midi, &ev)) { while (snd_seq_event_input(midi, &ev)) {
switch (ev->type) { switch (ev->type) {
@ -97,15 +107,17 @@ static void forward(snd_seq_t *midi, lo_address osc)
case SND_SEQ_EVENT_CONTROLLER: case SND_SEQ_EVENT_CONTROLLER:
chan = ev->data.control.channel; chan = ev->data.control.channel;
ctrl = ev->data.control.param; ctrl = ev->data.control.param;
map(&chan, &ctrl); value = ev->data.control.value;
msg[3] = ev->data.control.value; map(&chan, &ctrl, &value);
if (debug) if (debug)
fprintf(stderr, "c%u.%u(%u) -> c%u.%u\n", fprintf(stderr, "c%u.%u=%u -> c%u.%u=%u\n",
ev->data.control.channel, ev->data.control.channel,
ev->data.control.param, msg[3], ev->data.control.param,
chan, ctrl); ev->data.control.value,
chan, ctrl, value);
msg[1] = 0xb0 | chan; msg[1] = 0xb0 | chan;
msg[2] = ctrl; msg[2] = ctrl;
msg[3] = value;
break; break;
case SND_SEQ_EVENT_PITCHBEND: case SND_SEQ_EVENT_PITCHBEND:
msg[1] = 0xe0 | ev->data.control.channel; msg[1] = 0xe0 | ev->data.control.channel;
@ -114,6 +126,8 @@ static void forward(snd_seq_t *midi, lo_address osc)
break; break;
default: default:
/* Flickernoise currently doesn't support any others */ /* Flickernoise currently doesn't support any others */
if (debug)
fprintf(stderr, "unrecognized MIDI event\n");
snd_seq_free_event(ev); snd_seq_free_event(ev);
continue; continue;
} }
@ -128,7 +142,8 @@ static void usage(const char *name)
{ {
fprintf(stderr, fprintf(stderr,
"usage: %s hostname [-d] [mapping ...] [port]\n\n" "usage: %s hostname [-d] [mapping ...] [port]\n\n"
" mappings are of the form c<chan>[.<control>]=c<chan>[.<control>]\n\n" " mappings are of the form\n"
" c[<chan>][.<control>]=c<chan>[.<control>[=<value>]]\n\n"
" -d debug mode: print all MIDI messages\n", " -d debug mode: print all MIDI messages\n",
name); name);
exit(1); exit(1);