The MIDI stream was completely unsynchronized. It's almost a miracle
that MIDI worked at all :-)

This patch synchronizes the first (status) byte of each message and
it also ignores real-time messages which can appear _inside_ the
data of other messages.

I didn't make provisions for doing anything clever about SysEx,
because we don't handle them anyway.

These videos show the effect of the patch. The dot is supposed to
follow the button press (which lights the red LED of the button).

Before:
http://downloads.qi-hardware.com/people/werner/m1/midi/sync-before.mov

After:
http://downloads.qi-hardware.com/people/werner/m1/midi/sync-after.mov

A warning: while this makes the M1 lose MIDI messages a lot less
often, it does greatly increase the risk of running into the MIDI
overrun hang. It now takes only a few rapid movements of a slider
to freeze the M1.

- Werner

---
 src/input.c |   18 +++++++++++++-----
 1 files changed, 13 insertions(+), 5 deletions(-)

diff --git a/src/input.c b/src/input.c
index 6c7e5c8..32647f4 100644
--- a/src/input.c
+++ b/src/input.c
@@ -255,7 +255,7 @@ static int handle_midi_msg(mtk_event *e, unsigned char *msg)
 
 #define MIDI_TIMEOUT 20
 
-static int midi_p;
+static unsigned char *midi_p = NULL;
 static rtems_interval midi_last;
 static unsigned char midi_msg[3];
 
@@ -269,13 +269,21 @@ static int handle_midi_event(mtk_event *e, unsigned char 
*msg)
                midi_p = 0;
        midi_last = t;
 
-       midi_msg[midi_p] = msg[0];
-       midi_p++;
+       if ((*msg & 0xf8) == 0xf8)
+               return 0; /* ignore system real-time */
 
-       if(midi_p == 3) {
+       if (*msg & 0x80)
+               midi_p = midi_msg; /* status byte */
+
+       if (!midi_p)
+               return 0; /* ignore extra or unsynchronized data */
+
+       *midi_p++ = *msg;
+
+       if(midi_p == midi_msg+3) {
                /* received a complete MIDI message */
                r = handle_midi_msg(e, midi_msg);
-               midi_p = 0;
+               midi_p = NULL;
                return r;
        }
        
-- 
1.7.0.4

_______________________________________________
http://lists.milkymist.org/listinfo.cgi/devel-milkymist.org
IRC: #milkymist@Freenode

Reply via email to