andrew@0: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ andrew@0: andrew@0: /* andrew@0: This is a modified version of a source file from the andrew@0: Rosegarden MIDI and audio sequencer and notation editor. andrew@0: This file copyright 2000-2010 Richard Bown and Chris Cannam. andrew@0: andrew@0: Permission is hereby granted, free of charge, to any person andrew@0: obtaining a copy of this software and associated documentation andrew@0: files (the "Software"), to deal in the Software without andrew@0: restriction, including without limitation the rights to use, copy, andrew@0: modify, merge, publish, distribute, sublicense, and/or sell copies andrew@0: of the Software, and to permit persons to whom the Software is andrew@0: furnished to do so, subject to the following conditions: andrew@0: andrew@0: The above copyright notice and this permission notice shall be andrew@0: included in all copies or substantial portions of the Software. andrew@0: andrew@0: THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, andrew@0: EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF andrew@0: MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND andrew@0: NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR andrew@0: ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF andrew@0: CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION andrew@0: WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. andrew@0: andrew@0: Except as contained in this notice, the names of the authors andrew@0: shall not be used in advertising or otherwise to promote the sale, andrew@0: use or other dealings in this Software without prior written andrew@0: authorization. andrew@0: */ andrew@0: andrew@0: #ifndef _MIDI_EVENT_H_ andrew@0: #define _MIDI_EVENT_H_ andrew@0: andrew@0: #include andrew@0: #include andrew@0: andrew@0: typedef unsigned char MIDIByte; andrew@0: andrew@0: namespace MIDIConstants andrew@0: { andrew@0: static const char *const MIDI_FILE_HEADER = "MThd"; andrew@0: static const char *const MIDI_TRACK_HEADER = "MTrk"; andrew@0: andrew@0: static const MIDIByte MIDI_STATUS_BYTE_MASK = 0x80; andrew@0: static const MIDIByte MIDI_MESSAGE_TYPE_MASK = 0xF0; andrew@0: static const MIDIByte MIDI_CHANNEL_NUM_MASK = 0x0F; andrew@0: andrew@0: static const MIDIByte MIDI_NOTE_OFF = 0x80; andrew@0: static const MIDIByte MIDI_NOTE_ON = 0x90; andrew@0: static const MIDIByte MIDI_POLY_AFTERTOUCH = 0xA0; andrew@0: static const MIDIByte MIDI_CTRL_CHANGE = 0xB0; andrew@0: static const MIDIByte MIDI_PROG_CHANGE = 0xC0; andrew@0: static const MIDIByte MIDI_CHNL_AFTERTOUCH = 0xD0; andrew@0: static const MIDIByte MIDI_PITCH_BEND = 0xE0; andrew@0: static const MIDIByte MIDI_SELECT_CHNL_MODE = 0xB0; andrew@0: static const MIDIByte MIDI_SYSTEM_EXCLUSIVE = 0xF0; andrew@0: static const MIDIByte MIDI_TC_QUARTER_FRAME = 0xF1; andrew@0: static const MIDIByte MIDI_SONG_POSITION_PTR = 0xF2; andrew@0: static const MIDIByte MIDI_SONG_SELECT = 0xF3; andrew@0: static const MIDIByte MIDI_TUNE_REQUEST = 0xF6; andrew@0: static const MIDIByte MIDI_END_OF_EXCLUSIVE = 0xF7; andrew@0: static const MIDIByte MIDI_TIMING_CLOCK = 0xF8; andrew@0: static const MIDIByte MIDI_START = 0xFA; andrew@0: static const MIDIByte MIDI_CONTINUE = 0xFB; andrew@0: static const MIDIByte MIDI_STOP = 0xFC; andrew@0: static const MIDIByte MIDI_ACTIVE_SENSING = 0xFE; andrew@0: static const MIDIByte MIDI_SYSTEM_RESET = 0xFF; andrew@0: static const MIDIByte MIDI_SYSEX_NONCOMMERCIAL = 0x7D; andrew@0: static const MIDIByte MIDI_SYSEX_NON_RT = 0x7E; andrew@0: static const MIDIByte MIDI_SYSEX_RT = 0x7F; andrew@0: static const MIDIByte MIDI_SYSEX_RT_COMMAND = 0x06; andrew@0: static const MIDIByte MIDI_SYSEX_RT_RESPONSE = 0x07; andrew@0: static const MIDIByte MIDI_MMC_STOP = 0x01; andrew@0: static const MIDIByte MIDI_MMC_PLAY = 0x02; andrew@0: static const MIDIByte MIDI_MMC_DEFERRED_PLAY = 0x03; andrew@0: static const MIDIByte MIDI_MMC_FAST_FORWARD = 0x04; andrew@0: static const MIDIByte MIDI_MMC_REWIND = 0x05; andrew@0: static const MIDIByte MIDI_MMC_RECORD_STROBE = 0x06; andrew@0: static const MIDIByte MIDI_MMC_RECORD_EXIT = 0x07; andrew@0: static const MIDIByte MIDI_MMC_RECORD_PAUSE = 0x08; andrew@0: static const MIDIByte MIDI_MMC_PAUSE = 0x08; andrew@0: static const MIDIByte MIDI_MMC_EJECT = 0x0A; andrew@0: static const MIDIByte MIDI_MMC_LOCATE = 0x44; andrew@0: static const MIDIByte MIDI_FILE_META_EVENT = 0xFF; andrew@0: static const MIDIByte MIDI_SEQUENCE_NUMBER = 0x00; andrew@0: static const MIDIByte MIDI_TEXT_EVENT = 0x01; andrew@0: static const MIDIByte MIDI_COPYRIGHT_NOTICE = 0x02; andrew@0: static const MIDIByte MIDI_TRACK_NAME = 0x03; andrew@0: static const MIDIByte MIDI_INSTRUMENT_NAME = 0x04; andrew@0: static const MIDIByte MIDI_LYRIC = 0x05; andrew@0: static const MIDIByte MIDI_TEXT_MARKER = 0x06; andrew@0: static const MIDIByte MIDI_CUE_POINT = 0x07; andrew@0: static const MIDIByte MIDI_CHANNEL_PREFIX = 0x20; andrew@0: static const MIDIByte MIDI_CHANNEL_PREFIX_OR_PORT = 0x21; andrew@0: static const MIDIByte MIDI_END_OF_TRACK = 0x2F; andrew@0: static const MIDIByte MIDI_SET_TEMPO = 0x51; andrew@0: static const MIDIByte MIDI_SMPTE_OFFSET = 0x54; andrew@0: static const MIDIByte MIDI_TIME_SIGNATURE = 0x58; andrew@0: static const MIDIByte MIDI_KEY_SIGNATURE = 0x59; andrew@0: static const MIDIByte MIDI_SEQUENCER_SPECIFIC = 0x7F; andrew@0: static const MIDIByte MIDI_CONTROLLER_BANK_MSB = 0x00; andrew@0: static const MIDIByte MIDI_CONTROLLER_VOLUME = 0x07; andrew@0: static const MIDIByte MIDI_CONTROLLER_BANK_LSB = 0x20; andrew@0: static const MIDIByte MIDI_CONTROLLER_MODULATION = 0x01; andrew@0: static const MIDIByte MIDI_CONTROLLER_PAN = 0x0A; andrew@0: static const MIDIByte MIDI_CONTROLLER_SUSTAIN = 0x40; andrew@0: static const MIDIByte MIDI_CONTROLLER_RESONANCE = 0x47; andrew@0: static const MIDIByte MIDI_CONTROLLER_RELEASE = 0x48; andrew@0: static const MIDIByte MIDI_CONTROLLER_ATTACK = 0x49; andrew@0: static const MIDIByte MIDI_CONTROLLER_FILTER = 0x4A; andrew@0: static const MIDIByte MIDI_CONTROLLER_REVERB = 0x5B; andrew@0: static const MIDIByte MIDI_CONTROLLER_CHORUS = 0x5D; andrew@0: static const MIDIByte MIDI_CONTROLLER_NRPN_1 = 0x62; andrew@0: static const MIDIByte MIDI_CONTROLLER_NRPN_2 = 0x63; andrew@0: static const MIDIByte MIDI_CONTROLLER_RPN_1 = 0x64; andrew@0: static const MIDIByte MIDI_CONTROLLER_RPN_2 = 0x65; andrew@0: static const MIDIByte MIDI_CONTROLLER_SOUNDS_OFF = 0x78; andrew@0: static const MIDIByte MIDI_CONTROLLER_RESET = 0x79; andrew@0: static const MIDIByte MIDI_CONTROLLER_LOCAL = 0x7A; andrew@0: static const MIDIByte MIDI_CONTROLLER_ALL_NOTES_OFF = 0x7B; andrew@0: static const MIDIByte MIDI_PERCUSSION_CHANNEL = 9; andrew@0: andrew@0: typedef enum { andrew@0: MIDI_SINGLE_TRACK_FILE = 0x00, andrew@0: MIDI_SIMULTANEOUS_TRACK_FILE = 0x01, andrew@0: MIDI_SEQUENTIAL_TRACK_FILE = 0x02, andrew@0: MIDI_FILE_BAD_FORMAT = 0xFF andrew@0: } MIDIFileFormatType; andrew@0: } andrew@0: andrew@0: class MIDIEvent andrew@0: { andrew@0: public: andrew@0: MIDIEvent(unsigned long deltaTime, andrew@0: MIDIByte eventCode, andrew@0: MIDIByte data1 = 0, andrew@0: MIDIByte data2 = 0) : andrew@0: m_deltaTime(deltaTime), andrew@0: m_duration(0), andrew@0: m_eventCode(eventCode), andrew@0: m_data1(data1), andrew@0: m_data2(data2), andrew@0: m_metaEventCode(0) andrew@0: { } andrew@0: andrew@0: MIDIEvent(unsigned long deltaTime, andrew@0: MIDIByte eventCode, andrew@0: MIDIByte metaEventCode, andrew@0: const std::string &metaMessage) : andrew@0: m_deltaTime(deltaTime), andrew@0: m_duration(0), andrew@0: m_eventCode(eventCode), andrew@0: m_data1(0), andrew@0: m_data2(0), andrew@0: m_metaEventCode(metaEventCode), andrew@0: m_metaMessage(metaMessage) andrew@0: { } andrew@0: andrew@0: MIDIEvent(unsigned long deltaTime, andrew@0: MIDIByte eventCode, andrew@0: const std::string &sysEx) : andrew@0: m_deltaTime(deltaTime), andrew@0: m_duration(0), andrew@0: m_eventCode(eventCode), andrew@0: m_data1(0), andrew@0: m_data2(0), andrew@0: m_metaEventCode(0), andrew@0: m_metaMessage(sysEx) andrew@0: { } andrew@0: andrew@0: ~MIDIEvent() { } andrew@0: andrew@0: void setTime(const unsigned long &time) { m_deltaTime = time; } andrew@0: void setDuration(const unsigned long& duration) { m_duration = duration;} andrew@0: unsigned long addTime(const unsigned long &time) { andrew@0: m_deltaTime += time; andrew@0: return m_deltaTime; andrew@0: } andrew@0: andrew@0: int getMessageType() const andrew@0: { return (m_eventCode & MIDIConstants::MIDI_MESSAGE_TYPE_MASK); } andrew@0: andrew@0: int getChannelNumber() const andrew@0: { return (m_eventCode & MIDIConstants::MIDI_CHANNEL_NUM_MASK); } andrew@0: andrew@0: unsigned long getTime() const { return m_deltaTime; } andrew@0: unsigned long getDuration() const { return m_duration; } andrew@0: andrew@0: int getPitch() const { return m_data1; } andrew@0: int getVelocity() const { return m_data2; } andrew@0: int getData1() const { return m_data1; } andrew@0: int getData2() const { return m_data2; } andrew@0: int getEventCode() const { return m_eventCode; } andrew@0: andrew@0: bool isMeta() const { return (m_eventCode == MIDIConstants::MIDI_FILE_META_EVENT); } andrew@0: andrew@0: int getMetaEventCode() const { return m_metaEventCode; } andrew@0: std::string getMetaMessage() const { return m_metaMessage; } andrew@0: void setMetaMessage(const std::string &meta) { m_metaMessage = meta; } andrew@0: andrew@0: friend bool operator<(const MIDIEvent &a, const MIDIEvent &b); andrew@0: andrew@0: private: andrew@0: unsigned long m_deltaTime; andrew@0: unsigned long m_duration; andrew@0: MIDIByte m_eventCode; andrew@0: MIDIByte m_data1; // or Note andrew@0: MIDIByte m_data2; // or Velocity andrew@0: MIDIByte m_metaEventCode; andrew@0: std::string m_metaMessage; andrew@0: }; andrew@0: andrew@0: // Comparator for sorting andrew@0: // andrew@0: struct MIDIEventCmp andrew@0: { andrew@0: bool operator()(const MIDIEvent &mE1, const MIDIEvent &mE2) const andrew@0: { return mE1.getTime() < mE2.getTime(); } andrew@0: andrew@0: bool operator()(const MIDIEvent *mE1, const MIDIEvent *mE2) const andrew@0: { return mE1->getTime() < mE2->getTime(); } andrew@0: }; andrew@0: andrew@0: class MIDIException : virtual public std::exception andrew@0: { andrew@0: public: andrew@0: MIDIException(std::string message) throw() : m_message(message) { andrew@0: std::cerr << "WARNING: MIDI exception: " << message.c_str() << std::endl; andrew@0: } andrew@0: virtual ~MIDIException() throw() { } andrew@0: andrew@0: virtual const char *what() const throw() { andrew@0: return m_message.c_str(); andrew@0: } andrew@0: andrew@0: protected: andrew@0: std::string m_message; andrew@0: }; andrew@0: andrew@0: #endif