123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224 |
- #ifndef MIDIMESSAGE_H
- #define MIDIMESSAGE_H
- #include "utils.h"
- #include <sstream>
- class MidiException
- {
- public:
- MidiException(int c=0, const std::string& m="Success") :
- code(c), message(m){}
- MidiException(const std::string& m, const char* t) :
- code(-1), message(m){}
- virtual ~MidiException(){}
- MidiException(const MidiException& m){
- code=m.code;
- message=m.message;
- }
- int code;
- std::string message;
- const int SUCCESS=0;
- const int OVERFLOW_EXCEPTION=1;
- const int PARSE_EXCEPTION=2;
- };
- class MidiOverflowException : public MidiException
- {
- public:
- MidiOverflowException(const std::string& m, const char* t) :
- MidiException(PARSE_EXCEPTION, "ParseError: "+m){}
- MidiOverflowException() : MidiException(OVERFLOW_EXCEPTION, "Overflow") {}
- virtual ~MidiOverflowException(){}
- };
- class MidiParseException : public MidiException
- {
- public:
- MidiParseException(const std::string& m, const char* t) :
- MidiException(PARSE_EXCEPTION, "ParseError: "+m){}
- MidiParseException(const std::string& m) : MidiException(PARSE_EXCEPTION, "ParseError: "+m) {}
- virtual ~MidiParseException(){}
- };
- class MidiMessage
- {
- public:
- // types de trames
- static const uint8_t NOTE_ON = 0x90;
- static const uint8_t NOTE_OFF = 0x80;
- static const uint8_t CONTROLLER_CHANGE = 0xB0;
- //constructors / destructors & operators
- MidiMessage(){}
- MidiMessage(int _type) : type(_type) {}
- MidiMessage(const MidiMessage& m ) : type(m.type) {}
- virtual ~MidiMessage(){}
- // functions
- virtual int get_bytes(uint8_t* buffer) const =0;
- virtual std::string to_string() const = 0;
- bool is_note_on() const { return (type&0xf0)==NOTE_ON; }
- bool is_note_off() const { return (type&0xf0)==NOTE_OFF; }
- virtual MidiMessage* copy() const
- {
- uint8_t tmp[3];
- int n;
- n=get_bytes(tmp);
- return MidiMessage::parse(tmp, n);
- }
- //data
- uint8_t type;
- static MidiMessage* parse(const MidiRawMessage&);
- static MidiMessage* parse(uint8_t * c, int len)
- {
- MidiRawMessage msg(c, c+len);
- return MidiMessage::parse(msg);
- }
- static MidiMessage* parse(Json::Value& v);
- };
- class MidiMessageList : public std::vector<MidiMessage*>
- {
- public:
- MidiMessageList() : std::vector<MidiMessage*>() {}
- MidiMessageList(const MidiMessageList& mml) : std::vector<MidiMessage*>(mml){
- }
- void clear()
- {
- //for(int i=0; i<size(); i++) delete at(i);
- std::vector<MidiMessage*>::clear();
- }
- const MidiMessageList& operator=(const MidiMessageList& m)
- {
- int s = m.size();
- clear();
- for(int i=0; i<s; i++)
- push_back(m[i]->copy());
- return m;
- }
- MidiMessageList(MidiMessage* mm) : std::vector<MidiMessage*>()
- {
- push_back(mm);
- }
- virtual ~MidiMessageList(){
- }
- void free_content(){
- int s = size();
- for(int i=0; i<s; i++)
- delete at(i);
- clear();
- }
- static MidiMessageList* parse(Json::Value& msg){
- MidiMessageList* m = new MidiMessageList();
- int l = msg.size();
- if(!msg.isArray() || !l){
- std::cerr << "Erreur array attendu dans pad::input(JSon::Value&)\n";
- return NULL;
- }
- if(msg[0].isString()){
- m->push_back(MidiMessage::parse(msg));
- }else{
- for(int i=0; i<l ;i++)
- {
- m->push_back(MidiMessage::parse(msg[i]));
- }
- }
- return m;
- }
- static MidiMessageList* parse(const std::string& msg){
- Json::Value v;
- std::istringstream iss(msg);
- iss >> v;
- return parse(v);
- }
- };
- class Note : public MidiMessage
- {
- public:
- Note() : MidiMessage() {}
- Note(int _type, int ch, int key, int vel) :
- MidiMessage(_type), channel(ch), key(key), velocity(vel) {}
- Note(const Note& n) : MidiMessage(n.type),
- channel(n.channel), key(n.key), velocity(n.velocity) {}
- virtual ~Note(){}
- uint8_t channel;
- uint8_t key;
- uint8_t velocity;
- virtual Note* copy() const
- {
- uint8_t tmp[3];
- int n;
- n=get_bytes(tmp);
- Note* m = dynamic_cast<Note*>(MidiMessage::parse(tmp, n));
- m->channel=channel;
- m->key=key;
- m->velocity=velocity;
- return m;
- }
- virtual int get_bytes(uint8_t* buffer) const {
- buffer[0]=type|((channel-1)%16);
- buffer[1]=(key%128);
- buffer[2]=(velocity%128);
- return 3;
- }
- };
- class NoteOn : public Note {
- public:
- NoteOn(int ch, int k, int vel) : Note(MidiMessage::NOTE_ON, ch, k, vel) {}
- virtual ~NoteOn() {}
- virtual std::string to_string() const{
- return "NoteOn("+str(channel)+", "+str(key)+", "+str(velocity)+")";
- }
- };
- class NoteOff : public Note {
- public:
- NoteOff(int ch, int k, int vel) : Note(MidiMessage::NOTE_OFF, ch, k, vel) {}
- virtual ~NoteOff() {}
- virtual std::string to_string() const{
- return "NoteOn("+str(channel)+", "+str(key)+", "+str(velocity)+")";
- }
- };
- class ControllerChange : public Note {
- public:
- ControllerChange(int ch, int k, int vel) : Note(MidiMessage::CONTROLLER_CHANGE, ch, k, vel) {}
- virtual ~ControllerChange() {}
- virtual std::string to_string() const{
- return "ControllerChange("+str(channel)+", "+str(key)+", "+str(velocity)+")";
- }
- };
- #endif // MIDIMESSAGE_H
|