Date:2011-11-27 14:56:40 (12 years 4 months ago)
Author:Werner Almesberger
Commit:af0c0ab5c8fdf09df3b120a584e934c1f5614e0c
Message:midi2osc: added channel wildcard and overriding the value

Files: midi2osc/README (2 diffs)
midi2osc/midi2osc.c (9 diffs)

Change Details

midi2osc/README
2222
2323midi2osc can also remap MIDI controls. The syntax is
2424
25c<chan>[.<control>]=c<chan>[.<control>]
25c[<chan>][.<control>]=c<chan>[.<control>[=<value>]]
2626
2727where <chan> is a channel number and <control> is the optional control
2828number. For example,
...... 
3232would map the joysticks and two faders of a Faderfox LV3 to the
3333controls 1 through 6 on channel 0, and send the OSC messages to
3434a host called "m1".
35
36If <value> is given, that value will be assigned for any control
37message that matches the input pattern, irrespective of the input
38value.
midi2osc/midi2osc.c
2727static struct map {
2828    int chan_in, ctrl_in;
2929    int chan_out, ctrl_out;
30    int value;
3031    struct map *next;
3132} *mappings = NULL;
3233
3334
34static void add(int chan_in, int ctrl_in, int chan_out, int ctrl_out)
35static void add(int chan_in, int ctrl_in, int chan_out, int ctrl_out,
36    int value)
3537{
3638    struct map *new;
3739
...... 
4042    new->ctrl_in = ctrl_in;
4143    new->chan_out = chan_out;
4244    new->ctrl_out = ctrl_out;
45    new->value = value;
4346    new->next = mappings;
4447    mappings = new;
4548}
...... 
4750
4851static void add_mapping(const char *s)
4952{
50    unsigned chan_in, ctrl_in, chan_out, ctrl_out;
53    unsigned chan_in, ctrl_in, chan_out, ctrl_out, value;
5154
52    if (sscanf(s, "c%u.%u=c%u.%u",
55    if (sscanf(s, "c%u.%u=c%u.%u=%u",
56        &chan_in, &ctrl_in, &chan_out, &ctrl_out, &value) == 5)
57        add(chan_in, ctrl_in, chan_out, ctrl_out, value);
58    else if (sscanf(s, "c%u.%u=c%u.%u",
5359        &chan_in, &ctrl_in, &chan_out, &ctrl_out) == 4)
54        add(chan_in, ctrl_in, chan_out, ctrl_out);
60        add(chan_in, ctrl_in, chan_out, ctrl_out, -1);
5561    else if (sscanf(s, "c%u.%u=c%u", &chan_in, &ctrl_in, &chan_out) == 3)
56        add(chan_in, ctrl_in, chan_out, -1);
62        add(chan_in, ctrl_in, chan_out, -1, -1);
5763    else if (sscanf(s, "c%u=c%u.%u", &chan_in, &chan_out, &ctrl_out) == 3)
58        add(chan_in, -1, chan_out, ctrl_out);
64        add(chan_in, -1, chan_out, ctrl_out, -1);
65    else if (sscanf(s, "c.%u=c%u.%u", &ctrl_in, &chan_out, &ctrl_out) == 3)
66        add(-1, ctrl_in, chan_out, ctrl_out, -1);
5967    else if (sscanf(s, "c%u=c%u", &chan_in, &chan_out) == 2)
60        add(chan_in, -1, chan_out, -1);
68        add(chan_in, -1, chan_out, -1, -1);
6169    else {
6270        fprintf(stderr, "unrecognized mapping syntax\n");
6371        exit(1);
...... 
6573}
6674
6775
68static void map(uint8_t *chan, uint8_t *ctrl)
76static void map(uint8_t *chan, uint8_t *ctrl, uint8_t *value)
6977{
7078    const struct map *m;
7179
...... 
7684                *chan = m->chan_out;
7785            if (m->ctrl_out != -1)
7886                *ctrl = m->ctrl_out;
87            if (m->value != -1)
88                *value = m->value;
7989            return;
8090        }
8191}
...... 
8595{
8696    snd_seq_event_t *ev;
8797    uint8_t msg[4] = { 0, };
88    uint8_t chan, ctrl;
98    uint8_t chan, ctrl, value;
8999
90100    while (snd_seq_event_input(midi, &ev)) {
91101        switch (ev->type) {
...... 
97107        case SND_SEQ_EVENT_CONTROLLER:
98108            chan = ev->data.control.channel;
99109            ctrl = ev->data.control.param;
100            map(&chan, &ctrl);
101            msg[3] = ev->data.control.value;
110            value = ev->data.control.value;
111            map(&chan, &ctrl, &value);
102112            if (debug)
103                fprintf(stderr, "c%u.%u(%u) -> c%u.%u\n",
113                fprintf(stderr, "c%u.%u=%u -> c%u.%u=%u\n",
104114                    ev->data.control.channel,
105                    ev->data.control.param, msg[3],
106                    chan, ctrl);
115                    ev->data.control.param,
116                    ev->data.control.value,
117                    chan, ctrl, value);
107118            msg[1] = 0xb0 | chan;
108119            msg[2] = ctrl;
120            msg[3] = value;
109121            break;
110122        case SND_SEQ_EVENT_PITCHBEND:
111123            msg[1] = 0xe0 | ev->data.control.channel;
...... 
114126            break;
115127        default:
116128            /* Flickernoise currently doesn't support any others */
129            if (debug)
130                fprintf(stderr, "unrecognized MIDI event\n");
117131            snd_seq_free_event(ev);
118132            continue;
119133        }
...... 
128142{
129143    fprintf(stderr,
130144"usage: %s hostname [-d] [mapping ...] [port]\n\n"
131" mappings are of the form c<chan>[.<control>]=c<chan>[.<control>]\n\n"
145" mappings are of the form\n"
146" c[<chan>][.<control>]=c<chan>[.<control>[=<value>]]\n\n"
132147" -d debug mode: print all MIDI messages\n",
133148    name);
134149    exit(1);

Archive Download the corresponding diff file

Branches:
master



interactive