A class that converts byte streams into MIDI messages, and stores them in a FIFO. This is useful if you wish to read MIDI messages via polling instead of interrupts. The class supports every type of MIDI message, and System Realtime messages can be interleaved with regular ones.

MIDIparser.h

Committer:
Padman
Date:
2016-08-03
Revision:
0:69cbdcd5d770
Child:
1:1c3f0c6ea0fb

File content as of revision 0:69cbdcd5d770:

/**
 * @file    MIDIparser.h
 * @brief   MIDI parser - converts bytes into queued MIDI messages
 * @author  Patrick Thomas
 * @version 1.0
 * @see     
 *
 * Copyright (c) 2016
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef MIDIPARSER_MIDIPARSER_H_
#define MIDIPARSER_MIDIPARSER_H_

#include "MIDIMessage.h"
#include "MyBuffer.h"
#include <queue>

/**
 *
 * Example:
 * @code
 *  #include "mbed.h"
 *  #include "MIDIparser.h"
 *
 *  MIDIparser myParser;
 *  Serial pc(USBTX, USBRX);
 *
 *  int main()
 *  {
 *      while(1) {
 *       
 *      while(pc.readable()) {
 *       
 *          myParser.parse(pc.getc());
 *      }
 *       
 *      while (myParser.available()) {
 *           
 *          MIDIMessage myMessage = myParser.grab();
 *          pc.printf("%d %d %d %d\n", myMessage.type(), myMessage.channel(), myMessage.key(), myMessage.velocity());      
 *      } 
 *          
 *      }
 *  }
 * @endcode
 */

class MIDIparser {

private:

    MyBuffer<uint8_t> input_vector;
    queue<MIDIMessage> output_queue;

    bool sysex;
    bool dual;

public:

    MIDIparser();

    void parse(uint8_t);
    bool available();
    MIDIMessage grab();
};

#endif /* MIDIPARSER_MIDIPARSER_H_ */