svcore  1.9
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 #include "base/Debug.h"
00029 
00030 typedef unsigned char MIDIByte;
00031 
00032 namespace MIDIConstants
00033 {
00034     static const char *const MIDI_FILE_HEADER         = "MThd";
00035     static const char *const MIDI_TRACK_HEADER        = "MTrk";
00036 
00037     static const MIDIByte MIDI_STATUS_BYTE_MASK       = 0x80;
00038     static const MIDIByte MIDI_MESSAGE_TYPE_MASK      = 0xF0;
00039     static const MIDIByte MIDI_CHANNEL_NUM_MASK       = 0x0F;
00040 
00041     static const MIDIByte MIDI_NOTE_OFF               = 0x80;
00042     static const MIDIByte MIDI_NOTE_ON                = 0x90;
00043     static const MIDIByte MIDI_POLY_AFTERTOUCH        = 0xA0;
00044     static const MIDIByte MIDI_CTRL_CHANGE            = 0xB0;
00045     static const MIDIByte MIDI_PROG_CHANGE            = 0xC0;
00046     static const MIDIByte MIDI_CHNL_AFTERTOUCH        = 0xD0;
00047     static const MIDIByte MIDI_PITCH_BEND             = 0xE0;
00048     static const MIDIByte MIDI_SELECT_CHNL_MODE       = 0xB0;
00049     static const MIDIByte MIDI_SYSTEM_EXCLUSIVE       = 0xF0;
00050     static const MIDIByte MIDI_TC_QUARTER_FRAME       = 0xF1;
00051     static const MIDIByte MIDI_SONG_POSITION_PTR      = 0xF2;
00052     static const MIDIByte MIDI_SONG_SELECT            = 0xF3;
00053     static const MIDIByte MIDI_TUNE_REQUEST           = 0xF6;
00054     static const MIDIByte MIDI_END_OF_EXCLUSIVE       = 0xF7;
00055     static const MIDIByte MIDI_TIMING_CLOCK           = 0xF8;
00056     static const MIDIByte MIDI_START                  = 0xFA;
00057     static const MIDIByte MIDI_CONTINUE               = 0xFB;
00058     static const MIDIByte MIDI_STOP                   = 0xFC;
00059     static const MIDIByte MIDI_ACTIVE_SENSING         = 0xFE;
00060     static const MIDIByte MIDI_SYSTEM_RESET           = 0xFF;
00061     static const MIDIByte MIDI_SYSEX_NONCOMMERCIAL    = 0x7D;
00062     static const MIDIByte MIDI_SYSEX_NON_RT           = 0x7E;
00063     static const MIDIByte MIDI_SYSEX_RT               = 0x7F;
00064     static const MIDIByte MIDI_SYSEX_RT_COMMAND       = 0x06;
00065     static const MIDIByte MIDI_SYSEX_RT_RESPONSE      = 0x07;
00066     static const MIDIByte MIDI_MMC_STOP               = 0x01;
00067     static const MIDIByte MIDI_MMC_PLAY               = 0x02;
00068     static const MIDIByte MIDI_MMC_DEFERRED_PLAY      = 0x03;
00069     static const MIDIByte MIDI_MMC_FAST_FORWARD       = 0x04;
00070     static const MIDIByte MIDI_MMC_REWIND             = 0x05;
00071     static const MIDIByte MIDI_MMC_RECORD_STROBE      = 0x06;
00072     static const MIDIByte MIDI_MMC_RECORD_EXIT        = 0x07;
00073     static const MIDIByte MIDI_MMC_RECORD_PAUSE       = 0x08;
00074     static const MIDIByte MIDI_MMC_PAUSE              = 0x08;
00075     static const MIDIByte MIDI_MMC_EJECT              = 0x0A;
00076     static const MIDIByte MIDI_MMC_LOCATE             = 0x44;
00077     static const MIDIByte MIDI_FILE_META_EVENT        = 0xFF;
00078     static const MIDIByte MIDI_SEQUENCE_NUMBER        = 0x00;
00079     static const MIDIByte MIDI_TEXT_EVENT             = 0x01;
00080     static const MIDIByte MIDI_COPYRIGHT_NOTICE       = 0x02;
00081     static const MIDIByte MIDI_TRACK_NAME             = 0x03;
00082     static const MIDIByte MIDI_INSTRUMENT_NAME        = 0x04;
00083     static const MIDIByte MIDI_LYRIC                  = 0x05;
00084     static const MIDIByte MIDI_TEXT_MARKER            = 0x06;
00085     static const MIDIByte MIDI_CUE_POINT              = 0x07;
00086     static const MIDIByte MIDI_CHANNEL_PREFIX         = 0x20;
00087     static const MIDIByte MIDI_CHANNEL_PREFIX_OR_PORT = 0x21;
00088     static const MIDIByte MIDI_END_OF_TRACK           = 0x2F;
00089     static const MIDIByte MIDI_SET_TEMPO              = 0x51;
00090     static const MIDIByte MIDI_SMPTE_OFFSET           = 0x54;
00091     static const MIDIByte MIDI_TIME_SIGNATURE         = 0x58;
00092     static const MIDIByte MIDI_KEY_SIGNATURE          = 0x59;
00093     static const MIDIByte MIDI_SEQUENCER_SPECIFIC     = 0x7F;
00094     static const MIDIByte MIDI_CONTROLLER_BANK_MSB      = 0x00;
00095     static const MIDIByte MIDI_CONTROLLER_VOLUME        = 0x07;
00096     static const MIDIByte MIDI_CONTROLLER_BANK_LSB      = 0x20;
00097     static const MIDIByte MIDI_CONTROLLER_MODULATION    = 0x01;
00098     static const MIDIByte MIDI_CONTROLLER_PAN           = 0x0A;
00099     static const MIDIByte MIDI_CONTROLLER_SUSTAIN       = 0x40;
00100     static const MIDIByte MIDI_CONTROLLER_RESONANCE     = 0x47;
00101     static const MIDIByte MIDI_CONTROLLER_RELEASE       = 0x48;
00102     static const MIDIByte MIDI_CONTROLLER_ATTACK        = 0x49;
00103     static const MIDIByte MIDI_CONTROLLER_FILTER        = 0x4A;
00104     static const MIDIByte MIDI_CONTROLLER_REVERB        = 0x5B;
00105     static const MIDIByte MIDI_CONTROLLER_CHORUS        = 0x5D;
00106     static const MIDIByte MIDI_CONTROLLER_NRPN_1        = 0x62;
00107     static const MIDIByte MIDI_CONTROLLER_NRPN_2        = 0x63;
00108     static const MIDIByte MIDI_CONTROLLER_RPN_1         = 0x64;
00109     static const MIDIByte MIDI_CONTROLLER_RPN_2         = 0x65;
00110     static const MIDIByte MIDI_CONTROLLER_SOUNDS_OFF    = 0x78;
00111     static const MIDIByte MIDI_CONTROLLER_RESET         = 0x79;
00112     static const MIDIByte MIDI_CONTROLLER_LOCAL         = 0x7A;
00113     static const MIDIByte MIDI_CONTROLLER_ALL_NOTES_OFF = 0x7B;
00114     static const MIDIByte MIDI_PERCUSSION_CHANNEL       = 9;
00115 }
00116 
00117 class MIDIEvent
00118 {
00119 public:
00120     MIDIEvent(unsigned long deltaTime,
00121               MIDIByte eventCode,
00122               MIDIByte data1 = 0,
00123               MIDIByte data2 = 0) :
00124         m_deltaTime(deltaTime),
00125         m_duration(0),
00126         m_eventCode(eventCode),
00127         m_data1(data1),
00128         m_data2(data2),
00129         m_metaEventCode(0)
00130     { }
00131 
00132     MIDIEvent(unsigned long deltaTime,
00133               MIDIByte eventCode,
00134               MIDIByte metaEventCode,
00135               const std::string &metaMessage) :
00136         m_deltaTime(deltaTime),
00137         m_duration(0),
00138         m_eventCode(eventCode),
00139         m_data1(0),
00140         m_data2(0),
00141         m_metaEventCode(metaEventCode),
00142         m_metaMessage(metaMessage)
00143     { }
00144 
00145     MIDIEvent(unsigned long deltaTime,
00146               MIDIByte eventCode,
00147               const std::string &sysEx) :
00148         m_deltaTime(deltaTime),
00149         m_duration(0),
00150         m_eventCode(eventCode),
00151         m_data1(0),
00152         m_data2(0),
00153         m_metaEventCode(0),
00154         m_metaMessage(sysEx)
00155     { }
00156 
00157     ~MIDIEvent() { }
00158 
00159     void setTime(const unsigned long &time) { m_deltaTime = time; }
00160     void setDuration(const unsigned long& duration) { m_duration = duration;}
00161     unsigned long addTime(const unsigned long &time) {
00162         m_deltaTime += time;
00163         return m_deltaTime;
00164     }
00165 
00166     MIDIByte getMessageType() const
00167         { return (m_eventCode & MIDIConstants::MIDI_MESSAGE_TYPE_MASK); }
00168 
00169     MIDIByte getChannelNumber() const
00170         { return (m_eventCode & MIDIConstants::MIDI_CHANNEL_NUM_MASK); }
00171 
00172     unsigned long getTime() const { return m_deltaTime; }
00173     unsigned long getDuration() const { return m_duration; }
00174 
00175     MIDIByte getPitch() const { return m_data1; }
00176     MIDIByte getVelocity() const { return m_data2; }
00177     MIDIByte getData1() const { return m_data1; }
00178     MIDIByte getData2() const { return m_data2; }
00179     MIDIByte getEventCode() const { return m_eventCode; }
00180 
00181     bool isMeta() const { return (m_eventCode == MIDIConstants::MIDI_FILE_META_EVENT); }
00182 
00183     MIDIByte getMetaEventCode() const { return m_metaEventCode; }
00184     std::string getMetaMessage() const { return m_metaMessage; }
00185     void setMetaMessage(const std::string &meta) { m_metaMessage = meta; }
00186 
00187     friend bool operator<(const MIDIEvent &a, const MIDIEvent &b);
00188 
00189 private:
00190     MIDIEvent& operator=(const MIDIEvent);
00191 
00192     unsigned long  m_deltaTime;
00193     unsigned long  m_duration;
00194     MIDIByte       m_eventCode;
00195     MIDIByte       m_data1;         // or Note
00196     MIDIByte       m_data2;         // or Velocity
00197     MIDIByte       m_metaEventCode;
00198     std::string    m_metaMessage;
00199 };
00200 
00201 // Comparator for sorting
00202 //
00203 struct MIDIEventCmp
00204 {
00205     bool operator()(const MIDIEvent &mE1, const MIDIEvent &mE2) const
00206     { return mE1.getTime() < mE2.getTime(); }
00207 
00208     bool operator()(const MIDIEvent *mE1, const MIDIEvent *mE2) const
00209     { return mE1->getTime() < mE2->getTime(); }
00210 };
00211 
00212 class MIDIException : virtual public std::exception
00213 {
00214 public:
00215     MIDIException(QString message) throw() : m_message(message) {
00216         std::cerr << "WARNING: MIDI exception: "
00217                   << message.toLocal8Bit().data() << std::endl;
00218     }
00219     virtual ~MIDIException() throw() { }
00220 
00221     virtual const char *what() const throw() {
00222         return m_message.toLocal8Bit().data();
00223     }
00224 
00225 protected:
00226     QString m_message;
00227 };
00228 
00229 #endif