MIDIEvent.h

Go to the documentation of this file.
00001 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */
00002 
00003 /*
00004     Sonic Visualiser
00005     An audio file viewer and annotation editor.
00006     Centre for Digital Music, Queen Mary, University of London.
00007     
00008     This program is free software; you can redistribute it and/or
00009     modify it under the terms of the GNU General Public License as
00010     published by the Free Software Foundation; either version 2 of the
00011     License, or (at your option) any later version.  See the file
00012     COPYING included with this distribution for more information.
00013 */
00014 
00015 
00016 /*
00017    This is a modified version of a source file from the 
00018    Rosegarden MIDI and audio sequencer and notation editor.
00019    This file copyright 2000-2006 Richard Bown and Chris Cannam.
00020 */
00021 
00022 #ifndef _MIDI_EVENT_H_
00023 #define _MIDI_EVENT_H_
00024 
00025 #include <QString>
00026 #include <string>
00027 #include <iostream>
00028 
00029 typedef unsigned char MIDIByte;
00030 
00031 namespace MIDIConstants
00032 {
00033     static const char *const MIDI_FILE_HEADER         = "MThd";
00034     static const char *const MIDI_TRACK_HEADER        = "MTrk";
00035 
00036     static const MIDIByte MIDI_STATUS_BYTE_MASK       = 0x80;
00037     static const MIDIByte MIDI_MESSAGE_TYPE_MASK      = 0xF0;
00038     static const MIDIByte MIDI_CHANNEL_NUM_MASK       = 0x0F;
00039 
00040     static const MIDIByte MIDI_NOTE_OFF               = 0x80;
00041     static const MIDIByte MIDI_NOTE_ON                = 0x90;
00042     static const MIDIByte MIDI_POLY_AFTERTOUCH        = 0xA0;
00043     static const MIDIByte MIDI_CTRL_CHANGE            = 0xB0;
00044     static const MIDIByte MIDI_PROG_CHANGE            = 0xC0;
00045     static const MIDIByte MIDI_CHNL_AFTERTOUCH        = 0xD0;
00046     static const MIDIByte MIDI_PITCH_BEND             = 0xE0;
00047     static const MIDIByte MIDI_SELECT_CHNL_MODE       = 0xB0;
00048     static const MIDIByte MIDI_SYSTEM_EXCLUSIVE       = 0xF0;
00049     static const MIDIByte MIDI_TC_QUARTER_FRAME       = 0xF1;
00050     static const MIDIByte MIDI_SONG_POSITION_PTR      = 0xF2;
00051     static const MIDIByte MIDI_SONG_SELECT            = 0xF3;
00052     static const MIDIByte MIDI_TUNE_REQUEST           = 0xF6;
00053     static const MIDIByte MIDI_END_OF_EXCLUSIVE       = 0xF7;
00054     static const MIDIByte MIDI_TIMING_CLOCK           = 0xF8;
00055     static const MIDIByte MIDI_START                  = 0xFA;
00056     static const MIDIByte MIDI_CONTINUE               = 0xFB;
00057     static const MIDIByte MIDI_STOP                   = 0xFC;
00058     static const MIDIByte MIDI_ACTIVE_SENSING         = 0xFE;
00059     static const MIDIByte MIDI_SYSTEM_RESET           = 0xFF;
00060     static const MIDIByte MIDI_SYSEX_NONCOMMERCIAL    = 0x7D;
00061     static const MIDIByte MIDI_SYSEX_NON_RT           = 0x7E;
00062     static const MIDIByte MIDI_SYSEX_RT               = 0x7F;
00063     static const MIDIByte MIDI_SYSEX_RT_COMMAND       = 0x06;
00064     static const MIDIByte MIDI_SYSEX_RT_RESPONSE      = 0x07;
00065     static const MIDIByte MIDI_MMC_STOP               = 0x01;
00066     static const MIDIByte MIDI_MMC_PLAY               = 0x02;
00067     static const MIDIByte MIDI_MMC_DEFERRED_PLAY      = 0x03;
00068     static const MIDIByte MIDI_MMC_FAST_FORWARD       = 0x04;
00069     static const MIDIByte MIDI_MMC_REWIND             = 0x05;
00070     static const MIDIByte MIDI_MMC_RECORD_STROBE      = 0x06;
00071     static const MIDIByte MIDI_MMC_RECORD_EXIT        = 0x07;
00072     static const MIDIByte MIDI_MMC_RECORD_PAUSE       = 0x08;
00073     static const MIDIByte MIDI_MMC_PAUSE              = 0x08;
00074     static const MIDIByte MIDI_MMC_EJECT              = 0x0A;
00075     static const MIDIByte MIDI_MMC_LOCATE             = 0x44;
00076     static const MIDIByte MIDI_FILE_META_EVENT        = 0xFF;
00077     static const MIDIByte MIDI_SEQUENCE_NUMBER        = 0x00;
00078     static const MIDIByte MIDI_TEXT_EVENT             = 0x01;
00079     static const MIDIByte MIDI_COPYRIGHT_NOTICE       = 0x02;
00080     static const MIDIByte MIDI_TRACK_NAME             = 0x03;
00081     static const MIDIByte MIDI_INSTRUMENT_NAME        = 0x04;
00082     static const MIDIByte MIDI_LYRIC                  = 0x05;
00083     static const MIDIByte MIDI_TEXT_MARKER            = 0x06;
00084     static const MIDIByte MIDI_CUE_POINT              = 0x07;
00085     static const MIDIByte MIDI_CHANNEL_PREFIX         = 0x20;
00086     static const MIDIByte MIDI_CHANNEL_PREFIX_OR_PORT = 0x21;
00087     static const MIDIByte MIDI_END_OF_TRACK           = 0x2F;
00088     static const MIDIByte MIDI_SET_TEMPO              = 0x51;
00089     static const MIDIByte MIDI_SMPTE_OFFSET           = 0x54;
00090     static const MIDIByte MIDI_TIME_SIGNATURE         = 0x58;
00091     static const MIDIByte MIDI_KEY_SIGNATURE          = 0x59;
00092     static const MIDIByte MIDI_SEQUENCER_SPECIFIC     = 0x7F;
00093     static const MIDIByte MIDI_CONTROLLER_BANK_MSB      = 0x00;
00094     static const MIDIByte MIDI_CONTROLLER_VOLUME        = 0x07;
00095     static const MIDIByte MIDI_CONTROLLER_BANK_LSB      = 0x20;
00096     static const MIDIByte MIDI_CONTROLLER_MODULATION    = 0x01;
00097     static const MIDIByte MIDI_CONTROLLER_PAN           = 0x0A;
00098     static const MIDIByte MIDI_CONTROLLER_SUSTAIN       = 0x40;
00099     static const MIDIByte MIDI_CONTROLLER_RESONANCE     = 0x47;
00100     static const MIDIByte MIDI_CONTROLLER_RELEASE       = 0x48;
00101     static const MIDIByte MIDI_CONTROLLER_ATTACK        = 0x49;
00102     static const MIDIByte MIDI_CONTROLLER_FILTER        = 0x4A;
00103     static const MIDIByte MIDI_CONTROLLER_REVERB        = 0x5B;
00104     static const MIDIByte MIDI_CONTROLLER_CHORUS        = 0x5D;
00105     static const MIDIByte MIDI_CONTROLLER_NRPN_1        = 0x62;
00106     static const MIDIByte MIDI_CONTROLLER_NRPN_2        = 0x63;
00107     static const MIDIByte MIDI_CONTROLLER_RPN_1         = 0x64;
00108     static const MIDIByte MIDI_CONTROLLER_RPN_2         = 0x65;
00109     static const MIDIByte MIDI_CONTROLLER_SOUNDS_OFF    = 0x78;
00110     static const MIDIByte MIDI_CONTROLLER_RESET         = 0x79;
00111     static const MIDIByte MIDI_CONTROLLER_LOCAL         = 0x7A;
00112     static const MIDIByte MIDI_CONTROLLER_ALL_NOTES_OFF = 0x7B;
00113     static const MIDIByte MIDI_PERCUSSION_CHANNEL       = 9;
00114 }
00115 
00116 class MIDIEvent
00117 {
00118 public:
00119     MIDIEvent(unsigned long deltaTime,
00120               MIDIByte eventCode,
00121               MIDIByte data1 = 0,
00122               MIDIByte data2 = 0) :
00123         m_deltaTime(deltaTime),
00124         m_duration(0),
00125         m_eventCode(eventCode),
00126         m_data1(data1),
00127         m_data2(data2),
00128         m_metaEventCode(0)
00129     { }
00130 
00131     MIDIEvent(unsigned long deltaTime,
00132               MIDIByte eventCode,
00133               MIDIByte metaEventCode,
00134               const std::string &metaMessage) :
00135         m_deltaTime(deltaTime),
00136         m_duration(0),
00137         m_eventCode(eventCode),
00138         m_data1(0),
00139         m_data2(0),
00140         m_metaEventCode(metaEventCode),
00141         m_metaMessage(metaMessage)
00142     { }
00143 
00144     MIDIEvent(unsigned long deltaTime,
00145               MIDIByte eventCode,
00146               const std::string &sysEx) :
00147         m_deltaTime(deltaTime),
00148         m_duration(0),
00149         m_eventCode(eventCode),
00150         m_data1(0),
00151         m_data2(0),
00152         m_metaEventCode(0),
00153         m_metaMessage(sysEx)
00154     { }
00155 
00156     ~MIDIEvent() { }
00157 
00158     void setTime(const unsigned long &time) { m_deltaTime = time; }
00159     void setDuration(const unsigned long& duration) { m_duration = duration;}
00160     unsigned long addTime(const unsigned long &time) {
00161         m_deltaTime += time;
00162         return m_deltaTime;
00163     }
00164 
00165     MIDIByte getMessageType() const
00166         { return (m_eventCode & MIDIConstants::MIDI_MESSAGE_TYPE_MASK); }
00167 
00168     MIDIByte getChannelNumber() const
00169         { return (m_eventCode & MIDIConstants::MIDI_CHANNEL_NUM_MASK); }
00170 
00171     unsigned long getTime() const { return m_deltaTime; }
00172     unsigned long getDuration() const { return m_duration; }
00173 
00174     MIDIByte getPitch() const { return m_data1; }
00175     MIDIByte getVelocity() const { return m_data2; }
00176     MIDIByte getData1() const { return m_data1; }
00177     MIDIByte getData2() const { return m_data2; }
00178     MIDIByte getEventCode() const { return m_eventCode; }
00179 
00180     bool isMeta() const { return (m_eventCode == MIDIConstants::MIDI_FILE_META_EVENT); }
00181 
00182     MIDIByte getMetaEventCode() const { return m_metaEventCode; }
00183     std::string getMetaMessage() const { return m_metaMessage; }
00184     void setMetaMessage(const std::string &meta) { m_metaMessage = meta; }
00185 
00186     friend bool operator<(const MIDIEvent &a, const MIDIEvent &b);
00187 
00188 private:
00189     MIDIEvent& operator=(const MIDIEvent);
00190 
00191     unsigned long  m_deltaTime;
00192     unsigned long  m_duration;
00193     MIDIByte       m_eventCode;
00194     MIDIByte       m_data1;         // or Note
00195     MIDIByte       m_data2;         // or Velocity
00196     MIDIByte       m_metaEventCode;
00197     std::string    m_metaMessage;
00198 };
00199 
00200 // Comparator for sorting
00201 //
00202 struct MIDIEventCmp
00203 {
00204     bool operator()(const MIDIEvent &mE1, const MIDIEvent &mE2) const
00205     { return mE1.getTime() < mE2.getTime(); }
00206 
00207     bool operator()(const MIDIEvent *mE1, const MIDIEvent *mE2) const
00208     { return mE1->getTime() < mE2->getTime(); }
00209 };
00210 
00211 class MIDIException : virtual public std::exception
00212 {
00213 public:
00214     MIDIException(QString message) throw() : m_message(message) {
00215         std::cerr << "WARNING: MIDI exception: "
00216                   << message.toLocal8Bit().data() << std::endl;
00217     }
00218     virtual ~MIDIException() throw() { }
00219 
00220     virtual const char *what() const throw() {
00221         return m_message.toLocal8Bit().data();
00222     }
00223 
00224 protected:
00225     QString m_message;
00226 };
00227 
00228 #endif

Generated on Wed Feb 20 15:45:26 2008 for SonicVisualiser by  doxygen 1.5.1