Retab midi.c and fix obvious bugs in MIDI state machine