[sdiy] Tips for writing a MIDI parser wanted

ASSI Stromeko at nexgo.de
Tue Mar 24 21:13:28 CET 2009


On Dienstag, 24. März 2009, Tom Wiltshire wrote:
[...]
> What do you think? Am I on the right track? Anything I need to
> know? Anything I'm forgetting?

As much as you would wish, there is no shortcut to MIDI parsing: you 
need to deal with everything from system realtime (this is sidechannel 
data that can come anywhere inbetween other messages!) to running 
status to sysex to note off (as note on w/ velocity zero or as proper 
note off with some other velocity value).  This means you need to keep 
quite a bit of state information.  Once you have figured out what 
you're looking at you will probably throw away most of the data, but 
you have to maintain the correct state to make any sense of it at all.  
Of the many ways to implement such a parser, there really isn't a 
preferrable style, IMHO - although you should pay attention to do it 
in a robust fashion, so that a mangled or missing byte on the wire 
doesn't freak out your parser for longer than necessary.

Personally I'd have an ISR take care of feeding a ringbuffer with raw 
MIDI bytes from the wire (when the write pointer chases the read you 
do at least know that you've missed something and need to reset the 
state machine), then have a event or timer triggered routine fend off 
the realtime messages and whatever is left over to go into a state 
machine that keeps running status.  Most of the "receive data" 
transistions on this state machine will throw away the payload as 
you're not interested in the particular message.  The rest would be 
normalized (only one type of note off, for instance) and sent to 
another buffer for pickup by the engine.  If you need to process 
realtime messages, this would be separate state machine and buffer.  
This keeps the timing critical code nicely isolated and you can easily 
rig a debugging scaffold around each part to test everything.


Achim.
-- 
+<[Q+ Matrix-12 WAVE#46+305 Neuron microQkb Andromeda XTk]>+

SD adaptations for KORG EX-800 and Poly-800MkII V0.9:
http://Synth.Stromeko.net/Downloads.html#KorgSDada






More information about the Synth-diy mailing list