[sdiy] Tips for writing a MIDI parser wanted
Seb Francis
seb at burnit.co.uk
Tue Mar 24 13:05:48 CET 2009
Hi Tom,
Sounds like you're on the right track. I've had a quick glance through
my 4xD MIDI code (I'd almost forgotten the horrors of 16F PIC assembly
now that I've been using dsPICs for a while) ....
- Receiving a MIDI byte triggers the UART ISR
- The ISR keeps track of the progress of multi-byte messages, and the
last status byte in case of running status mode, and it handles system
realtime bytes without loosing track of these things.
- The actual message to be acted on isn't written to the ring buffer
until it is completely received, and then always a complete 1, 2 or 3
byte message is written, regardless of whether the message was received
in running status mode, etc. This greatly simplifies the job of
actually reading from and acting on the buffer.
- The ISR doesn't make any decisions about MIDI channel as it's simpler
just to wait until a full message is received, especially with running
status, etc. It also keeps the action logic separate from the receiving
logic.
- The actual acting on the buffer takes places outside of an ISR so you
can take as long as necessary doing complex calculations, etc. without
disrupting any realtime processes (as long as you don't run out of the
MIDI ring buffer of course). This was more necessary for my Fat
Controller from which this code was derived - some quite lengthy
calculations had to take place in response to MIDI messages - but I
think it's a good overall framework in any case.
The 4xD only responded to MIDI CC and certain system messages, but the
code has stub subroutines for all the other MIDI messages. The code is
well tested by quite a few different people so ought to be a good
starting point for you....
Cheers,
Seb
Tom Wiltshire wrote:
> Hi All,
>
> I'm currently putting the main microprocessor board for my monosynth
> together. One important part of this is MIDI handling. I've never
> written any MIDI code before, so I'm looking for some tips. So far,
> the board can scan a keyboard and display info on an LCD, so I have
> some basic tools for debugging MIDI.
>
> I've read through Seb Francis' 4XD code (which is very useful since it
> is in 16F PIC assembly, which is what I'm using) and looked at the
> MIDIbox code too.
>
> Dealing just with MIDI reception for a moment, I think the general
> plan is something like this:
>
> * Every time a byte arrives, an interrupt is generated. The interrupt
> routine checks whether this is a byte on our channel, and whether it
> is a byte that we're expecting (the second byte of a 2-byte message
> for example) or a new message. If it has a complete message, it puts
> either 1,2, or 3 bytes into a MIDI processing buffer.
>
> * The main code goes through the MIDI processing buffer interpreting
> and acting on the received messages.
>
> The point of this two-stage scheme is that the serious work is only
> done on relevant, correct messages. The interrupt will discard
> anything on other channels, or anything badly formed. This should both
> limit the amount of data that goes into the buffer, and also remove
> the need for complex error checking from the interpreting code.
>
> What do you think? Am I on the right track? Anything I need to know?
> Anything I'm forgetting?
>
> I spent last night doing the internet research on this topic, but
> couldn't find anything that gives an overview of the best strategy for
> writing a MIDI parser. There are some examples, but no-one talks about
> *why* they did it the way they did it, and that's the really
> interesting bit.
>
> Thanks,
> Tom
>
>
> _______________________________________________
> Synth-diy mailing list
> Synth-diy at dropmix.xs4all.nl
> http://dropmix.xs4all.nl/mailman/listinfo/synth-diy
>
>
More information about the Synth-diy
mailing list