detokenizing stream of numbers

    Feb 14 2013 | 11:20 pm
    Hello Max Wizards,
    I'm struggling with what seems like a fairly simple problem.
    I am wanting to de-tokenize (?) a stream of 7-bit integers coming to me via a Sysex stream from a MIDI device. The stream contains delimiters (tokens) which denote which command/opcode to execute, however each opcode has a discreet discrete number of variables. Here's an example input stream:
    08 12 09 00 72 101 108 108 111 08 14 09 30 08 12 32 32 32 32 32 10 65 65 127 97
    Opcode 08 has a single parameter that follows Opcode 09 has a single parameter that follows Opcode 10 has four parameters that follow ALL other data should be sent as is, but the ABOVE data should be filtered.
    Ideally, I'd like something that does:
    Read stream of data; Get an 08, read in the next digit and send it out OUT 1 Get an 09, read in the next digit and send it out OUT 2 Get a 10, read in the next FOUR digits, and send them out OUT 3 Get any other digits, send them out OUT 4.
    I have all this built with a state machine, but I'm doing kludgy things like watching for a token, and using that to close a gate, but then having a delay in place to prevent the following digit from going through (i.e. it gets "eaten" by the gate). It works, but it feels wrong.
    I have the data in a coll, but finding the logic to, say, find where a "10" token exists, and delete the next 4 digits is not apparent to me.
    I also tried using zl objects, but, again trying to use 'sub' to locate the tokens was proving quite difficult.
    Any suggestions? I'm a little rusty with Max, but have been using it since 1993 on and off!
    Thanks, Dave

    • Feb 15 2013 | 4:53 pm
      Got this figured out. Solutions often fall into your lap when you ask others for help :)
    • Feb 16 2013 | 6:09 am
      This is one of those situations when Java or Javascript might be more straightforward than Max. But it's not all that ghastly in Max. Here's what I think is about the simplest way (which I think also pretty much illustrates how one might do it in a text-based language).
    • Feb 21 2013 | 5:27 am
      Thanks! I had forgotten that gate has arguments, turning them into multi-output gates. Pretty slick!