svcore  1.9
MIDIEvent.h
Go to the documentation of this file.
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
2 
3 /*
4  Sonic Visualiser
5  An audio file viewer and annotation editor.
6  Centre for Digital Music, Queen Mary, University of London.
7 
8  This program is free software; you can redistribute it and/or
9  modify it under the terms of the GNU General Public License as
10  published by the Free Software Foundation; either version 2 of the
11  License, or (at your option) any later version. See the file
12  COPYING included with this distribution for more information.
13 */
14 
15 
16 /*
17  This is a modified version of a source file from the
18  Rosegarden MIDI and audio sequencer and notation editor.
19  This file copyright 2000-2006 Richard Bown and Chris Cannam.
20 */
21 
22 #ifndef _MIDI_EVENT_H_
23 #define _MIDI_EVENT_H_
24 
25 #include <QString>
26 #include <string>
27 #include <iostream>
28 #include "base/Debug.h"
29 
30 typedef unsigned char MIDIByte;
31 
32 namespace MIDIConstants
33 {
34  static const char *const MIDI_FILE_HEADER = "MThd";
35  static const char *const MIDI_TRACK_HEADER = "MTrk";
36 
37  static const MIDIByte MIDI_STATUS_BYTE_MASK = 0x80;
38  static const MIDIByte MIDI_MESSAGE_TYPE_MASK = 0xF0;
39  static const MIDIByte MIDI_CHANNEL_NUM_MASK = 0x0F;
40 
41  static const MIDIByte MIDI_NOTE_OFF = 0x80;
42  static const MIDIByte MIDI_NOTE_ON = 0x90;
43  static const MIDIByte MIDI_POLY_AFTERTOUCH = 0xA0;
44  static const MIDIByte MIDI_CTRL_CHANGE = 0xB0;
45  static const MIDIByte MIDI_PROG_CHANGE = 0xC0;
46  static const MIDIByte MIDI_CHNL_AFTERTOUCH = 0xD0;
47  static const MIDIByte MIDI_PITCH_BEND = 0xE0;
48  static const MIDIByte MIDI_SELECT_CHNL_MODE = 0xB0;
49  static const MIDIByte MIDI_SYSTEM_EXCLUSIVE = 0xF0;
50  static const MIDIByte MIDI_TC_QUARTER_FRAME = 0xF1;
51  static const MIDIByte MIDI_SONG_POSITION_PTR = 0xF2;
52  static const MIDIByte MIDI_SONG_SELECT = 0xF3;
53  static const MIDIByte MIDI_TUNE_REQUEST = 0xF6;
54  static const MIDIByte MIDI_END_OF_EXCLUSIVE = 0xF7;
55  static const MIDIByte MIDI_TIMING_CLOCK = 0xF8;
56  static const MIDIByte MIDI_START = 0xFA;
57  static const MIDIByte MIDI_CONTINUE = 0xFB;
58  static const MIDIByte MIDI_STOP = 0xFC;
59  static const MIDIByte MIDI_ACTIVE_SENSING = 0xFE;
60  static const MIDIByte MIDI_SYSTEM_RESET = 0xFF;
61  static const MIDIByte MIDI_SYSEX_NONCOMMERCIAL = 0x7D;
62  static const MIDIByte MIDI_SYSEX_NON_RT = 0x7E;
63  static const MIDIByte MIDI_SYSEX_RT = 0x7F;
64  static const MIDIByte MIDI_SYSEX_RT_COMMAND = 0x06;
65  static const MIDIByte MIDI_SYSEX_RT_RESPONSE = 0x07;
66  static const MIDIByte MIDI_MMC_STOP = 0x01;
67  static const MIDIByte MIDI_MMC_PLAY = 0x02;
68  static const MIDIByte MIDI_MMC_DEFERRED_PLAY = 0x03;
69  static const MIDIByte MIDI_MMC_FAST_FORWARD = 0x04;
70  static const MIDIByte MIDI_MMC_REWIND = 0x05;
71  static const MIDIByte MIDI_MMC_RECORD_STROBE = 0x06;
72  static const MIDIByte MIDI_MMC_RECORD_EXIT = 0x07;
73  static const MIDIByte MIDI_MMC_RECORD_PAUSE = 0x08;
74  static const MIDIByte MIDI_MMC_PAUSE = 0x08;
75  static const MIDIByte MIDI_MMC_EJECT = 0x0A;
76  static const MIDIByte MIDI_MMC_LOCATE = 0x44;
77  static const MIDIByte MIDI_FILE_META_EVENT = 0xFF;
78  static const MIDIByte MIDI_SEQUENCE_NUMBER = 0x00;
79  static const MIDIByte MIDI_TEXT_EVENT = 0x01;
80  static const MIDIByte MIDI_COPYRIGHT_NOTICE = 0x02;
81  static const MIDIByte MIDI_TRACK_NAME = 0x03;
82  static const MIDIByte MIDI_INSTRUMENT_NAME = 0x04;
83  static const MIDIByte MIDI_LYRIC = 0x05;
84  static const MIDIByte MIDI_TEXT_MARKER = 0x06;
85  static const MIDIByte MIDI_CUE_POINT = 0x07;
86  static const MIDIByte MIDI_CHANNEL_PREFIX = 0x20;
88  static const MIDIByte MIDI_END_OF_TRACK = 0x2F;
89  static const MIDIByte MIDI_SET_TEMPO = 0x51;
90  static const MIDIByte MIDI_SMPTE_OFFSET = 0x54;
91  static const MIDIByte MIDI_TIME_SIGNATURE = 0x58;
92  static const MIDIByte MIDI_KEY_SIGNATURE = 0x59;
93  static const MIDIByte MIDI_SEQUENCER_SPECIFIC = 0x7F;
94  static const MIDIByte MIDI_CONTROLLER_BANK_MSB = 0x00;
95  static const MIDIByte MIDI_CONTROLLER_VOLUME = 0x07;
96  static const MIDIByte MIDI_CONTROLLER_BANK_LSB = 0x20;
97  static const MIDIByte MIDI_CONTROLLER_MODULATION = 0x01;
98  static const MIDIByte MIDI_CONTROLLER_PAN = 0x0A;
99  static const MIDIByte MIDI_CONTROLLER_SUSTAIN = 0x40;
100  static const MIDIByte MIDI_CONTROLLER_RESONANCE = 0x47;
101  static const MIDIByte MIDI_CONTROLLER_RELEASE = 0x48;
102  static const MIDIByte MIDI_CONTROLLER_ATTACK = 0x49;
103  static const MIDIByte MIDI_CONTROLLER_FILTER = 0x4A;
104  static const MIDIByte MIDI_CONTROLLER_REVERB = 0x5B;
105  static const MIDIByte MIDI_CONTROLLER_CHORUS = 0x5D;
106  static const MIDIByte MIDI_CONTROLLER_NRPN_1 = 0x62;
107  static const MIDIByte MIDI_CONTROLLER_NRPN_2 = 0x63;
108  static const MIDIByte MIDI_CONTROLLER_RPN_1 = 0x64;
109  static const MIDIByte MIDI_CONTROLLER_RPN_2 = 0x65;
111  static const MIDIByte MIDI_CONTROLLER_RESET = 0x79;
112  static const MIDIByte MIDI_CONTROLLER_LOCAL = 0x7A;
115 }
116 
118 {
119 public:
120  MIDIEvent(unsigned long deltaTime,
121  MIDIByte eventCode,
122  MIDIByte data1 = 0,
123  MIDIByte data2 = 0) :
124  m_deltaTime(deltaTime),
125  m_duration(0),
126  m_eventCode(eventCode),
127  m_data1(data1),
128  m_data2(data2),
129  m_metaEventCode(0)
130  { }
131 
132  MIDIEvent(unsigned long deltaTime,
133  MIDIByte eventCode,
134  MIDIByte metaEventCode,
135  const std::string &metaMessage) :
136  m_deltaTime(deltaTime),
137  m_duration(0),
138  m_eventCode(eventCode),
139  m_data1(0),
140  m_data2(0),
141  m_metaEventCode(metaEventCode),
142  m_metaMessage(metaMessage)
143  { }
144 
145  MIDIEvent(unsigned long deltaTime,
146  MIDIByte eventCode,
147  const std::string &sysEx) :
148  m_deltaTime(deltaTime),
149  m_duration(0),
150  m_eventCode(eventCode),
151  m_data1(0),
152  m_data2(0),
153  m_metaEventCode(0),
154  m_metaMessage(sysEx)
155  { }
156 
158 
159  void setTime(const unsigned long &time) { m_deltaTime = time; }
160  void setDuration(const unsigned long& duration) { m_duration = duration;}
161  unsigned long addTime(const unsigned long &time) {
162  m_deltaTime += time;
163  return m_deltaTime;
164  }
165 
168 
171 
172  unsigned long getTime() const { return m_deltaTime; }
173  unsigned long getDuration() const { return m_duration; }
174 
175  MIDIByte getPitch() const { return m_data1; }
176  MIDIByte getVelocity() const { return m_data2; }
177  MIDIByte getData1() const { return m_data1; }
178  MIDIByte getData2() const { return m_data2; }
179  MIDIByte getEventCode() const { return m_eventCode; }
180 
182 
184  std::string getMetaMessage() const { return m_metaMessage; }
185  void setMetaMessage(const std::string &meta) { m_metaMessage = meta; }
186 
187  friend bool operator<(const MIDIEvent &a, const MIDIEvent &b);
188 
189 private:
190  MIDIEvent& operator=(const MIDIEvent);
191 
192  unsigned long m_deltaTime;
193  unsigned long m_duration;
195  MIDIByte m_data1; // or Note
196  MIDIByte m_data2; // or Velocity
198  std::string m_metaMessage;
199 };
200 
201 // Comparator for sorting
202 //
204 {
205  bool operator()(const MIDIEvent &mE1, const MIDIEvent &mE2) const
206  { return mE1.getTime() < mE2.getTime(); }
207 
208  bool operator()(const MIDIEvent *mE1, const MIDIEvent *mE2) const
209  { return mE1->getTime() < mE2->getTime(); }
210 };
211 
212 class MIDIException : virtual public std::exception
213 {
214 public:
215  MIDIException(QString message) throw() : m_message(message) {
216  std::cerr << "WARNING: MIDI exception: "
217  << message.toLocal8Bit().data() << std::endl;
218  }
219  virtual ~MIDIException() throw() { }
220 
221  virtual const char *what() const throw() {
222  return m_message.toLocal8Bit().data();
223  }
224 
225 protected:
226  QString m_message;
227 };
228 
229 #endif
bool operator()(const MIDIEvent &mE1, const MIDIEvent &mE2) const
Definition: MIDIEvent.h:205
MIDIEvent(unsigned long deltaTime, MIDIByte eventCode, MIDIByte data1=0, MIDIByte data2=0)
Definition: MIDIEvent.h:120
static const MIDIByte MIDI_KEY_SIGNATURE
Definition: MIDIEvent.h:92
static const MIDIByte MIDI_MMC_PLAY
Definition: MIDIEvent.h:67
static const MIDIByte MIDI_CHANNEL_PREFIX_OR_PORT
Definition: MIDIEvent.h:87
static const MIDIByte MIDI_END_OF_TRACK
Definition: MIDIEvent.h:88
static const MIDIByte MIDI_CONTROLLER_FILTER
Definition: MIDIEvent.h:103
MIDIEvent & operator=(const MIDIEvent)
MIDIByte getEventCode() const
Definition: MIDIEvent.h:179
MIDIByte getMessageType() const
Definition: MIDIEvent.h:166
static const MIDIByte MIDI_CONTROLLER_RPN_2
Definition: MIDIEvent.h:109
static const MIDIByte MIDI_COPYRIGHT_NOTICE
Definition: MIDIEvent.h:80
void setDuration(const unsigned long &duration)
Definition: MIDIEvent.h:160
MIDIByte m_data1
Definition: MIDIEvent.h:195
static const MIDIByte MIDI_CONTROLLER_RESET
Definition: MIDIEvent.h:111
unsigned long m_deltaTime
Definition: MIDIEvent.h:192
static const MIDIByte MIDI_CHANNEL_PREFIX
Definition: MIDIEvent.h:86
static const MIDIByte MIDI_MMC_RECORD_STROBE
Definition: MIDIEvent.h:71
static const MIDIByte MIDI_SYSEX_RT
Definition: MIDIEvent.h:63
static const MIDIByte MIDI_CHANNEL_NUM_MASK
Definition: MIDIEvent.h:39
static const MIDIByte MIDI_TUNE_REQUEST
Definition: MIDIEvent.h:53
~MIDIEvent()
Definition: MIDIEvent.h:157
static const MIDIByte MIDI_CONTROLLER_MODULATION
Definition: MIDIEvent.h:97
static const MIDIByte MIDI_TC_QUARTER_FRAME
Definition: MIDIEvent.h:50
static const char *const MIDI_TRACK_HEADER
Definition: MIDIEvent.h:35
static const MIDIByte MIDI_SONG_SELECT
Definition: MIDIEvent.h:52
static const MIDIByte MIDI_CONTROLLER_NRPN_2
Definition: MIDIEvent.h:107
static const MIDIByte MIDI_TRACK_NAME
Definition: MIDIEvent.h:81
static const MIDIByte MIDI_MMC_DEFERRED_PLAY
Definition: MIDIEvent.h:68
MIDIByte getData1() const
Definition: MIDIEvent.h:177
static const MIDIByte MIDI_TEXT_MARKER
Definition: MIDIEvent.h:84
void setTime(const unsigned long &time)
Definition: MIDIEvent.h:159
static const MIDIByte MIDI_CONTROLLER_ATTACK
Definition: MIDIEvent.h:102
static const MIDIByte MIDI_SELECT_CHNL_MODE
Definition: MIDIEvent.h:48
static const MIDIByte MIDI_SEQUENCE_NUMBER
Definition: MIDIEvent.h:78
static const MIDIByte MIDI_MMC_EJECT
Definition: MIDIEvent.h:75
static const MIDIByte MIDI_MMC_STOP
Definition: MIDIEvent.h:66
MIDIByte getMetaEventCode() const
Definition: MIDIEvent.h:183
static const MIDIByte MIDI_TEXT_EVENT
Definition: MIDIEvent.h:79
static const MIDIByte MIDI_CONTROLLER_BANK_LSB
Definition: MIDIEvent.h:96
static const MIDIByte MIDI_MMC_RECORD_PAUSE
Definition: MIDIEvent.h:73
MIDIByte m_eventCode
Definition: MIDIEvent.h:194
MIDIEvent(unsigned long deltaTime, MIDIByte eventCode, MIDIByte metaEventCode, const std::string &metaMessage)
Definition: MIDIEvent.h:132
static const MIDIByte MIDI_SMPTE_OFFSET
Definition: MIDIEvent.h:90
static const MIDIByte MIDI_PERCUSSION_CHANNEL
Definition: MIDIEvent.h:114
unsigned char MIDIByte
Definition: MIDIEvent.h:30
MIDIByte getPitch() const
Definition: MIDIEvent.h:175
static const MIDIByte MIDI_MMC_REWIND
Definition: MIDIEvent.h:70
static const MIDIByte MIDI_CONTROLLER_NRPN_1
Definition: MIDIEvent.h:106
static const MIDIByte MIDI_LYRIC
Definition: MIDIEvent.h:83
static const MIDIByte MIDI_SYSEX_RT_COMMAND
Definition: MIDIEvent.h:64
friend bool operator<(const MIDIEvent &a, const MIDIEvent &b)
static const MIDIByte MIDI_CUE_POINT
Definition: MIDIEvent.h:85
bool operator()(const MIDIEvent *mE1, const MIDIEvent *mE2) const
Definition: MIDIEvent.h:208
static const MIDIByte MIDI_FILE_META_EVENT
Definition: MIDIEvent.h:77
MIDIEvent(unsigned long deltaTime, MIDIByte eventCode, const std::string &sysEx)
Definition: MIDIEvent.h:145
static const MIDIByte MIDI_SET_TEMPO
Definition: MIDIEvent.h:89
static const MIDIByte MIDI_SYSEX_NON_RT
Definition: MIDIEvent.h:62
static const MIDIByte MIDI_CONTROLLER_ALL_NOTES_OFF
Definition: MIDIEvent.h:113
static const MIDIByte MIDI_CONTROLLER_RPN_1
Definition: MIDIEvent.h:108
unsigned long m_duration
Definition: MIDIEvent.h:193
static const MIDIByte MIDI_INSTRUMENT_NAME
Definition: MIDIEvent.h:82
static const MIDIByte MIDI_CONTROLLER_RESONANCE
Definition: MIDIEvent.h:100
bool isMeta() const
Definition: MIDIEvent.h:181
static const MIDIByte MIDI_SEQUENCER_SPECIFIC
Definition: MIDIEvent.h:93
static const MIDIByte MIDI_STATUS_BYTE_MASK
Definition: MIDIEvent.h:37
std::string getMetaMessage() const
Definition: MIDIEvent.h:184
static const MIDIByte MIDI_STOP
Definition: MIDIEvent.h:58
unsigned long getTime() const
Definition: MIDIEvent.h:172
static const MIDIByte MIDI_TIMING_CLOCK
Definition: MIDIEvent.h:55
virtual const char * what() const
Definition: MIDIEvent.h:221
static const MIDIByte MIDI_CONTROLLER_SOUNDS_OFF
Definition: MIDIEvent.h:110
static const MIDIByte MIDI_ACTIVE_SENSING
Definition: MIDIEvent.h:59
MIDIByte getVelocity() const
Definition: MIDIEvent.h:176
static const MIDIByte MIDI_PITCH_BEND
Definition: MIDIEvent.h:47
static const MIDIByte MIDI_MMC_RECORD_EXIT
Definition: MIDIEvent.h:72
static const MIDIByte MIDI_MMC_PAUSE
Definition: MIDIEvent.h:74
std::string m_metaMessage
Definition: MIDIEvent.h:198
MIDIByte getChannelNumber() const
Definition: MIDIEvent.h:169
MIDIException(QString message)
Definition: MIDIEvent.h:215
virtual ~MIDIException()
Definition: MIDIEvent.h:219
MIDIByte m_metaEventCode
Definition: MIDIEvent.h:197
static const MIDIByte MIDI_CONTROLLER_RELEASE
Definition: MIDIEvent.h:101
static const MIDIByte MIDI_START
Definition: MIDIEvent.h:56
static const MIDIByte MIDI_MMC_LOCATE
Definition: MIDIEvent.h:76
static const MIDIByte MIDI_MMC_FAST_FORWARD
Definition: MIDIEvent.h:69
static const MIDIByte MIDI_CTRL_CHANGE
Definition: MIDIEvent.h:44
static const MIDIByte MIDI_CONTROLLER_REVERB
Definition: MIDIEvent.h:104
static const MIDIByte MIDI_POLY_AFTERTOUCH
Definition: MIDIEvent.h:43
static const MIDIByte MIDI_SYSTEM_EXCLUSIVE
Definition: MIDIEvent.h:49
static const MIDIByte MIDI_CONTROLLER_LOCAL
Definition: MIDIEvent.h:112
static const MIDIByte MIDI_NOTE_OFF
Definition: MIDIEvent.h:41
static const char *const MIDI_FILE_HEADER
Definition: MIDIEvent.h:34
static const MIDIByte MIDI_PROG_CHANGE
Definition: MIDIEvent.h:45
MIDIByte getData2() const
Definition: MIDIEvent.h:178
static const MIDIByte MIDI_SYSTEM_RESET
Definition: MIDIEvent.h:60
static const MIDIByte MIDI_SYSEX_RT_RESPONSE
Definition: MIDIEvent.h:65
unsigned long getDuration() const
Definition: MIDIEvent.h:173
static const MIDIByte MIDI_TIME_SIGNATURE
Definition: MIDIEvent.h:91
static const MIDIByte MIDI_SONG_POSITION_PTR
Definition: MIDIEvent.h:51
static const MIDIByte MIDI_CONTINUE
Definition: MIDIEvent.h:57
static const MIDIByte MIDI_SYSEX_NONCOMMERCIAL
Definition: MIDIEvent.h:61
MIDIByte m_data2
Definition: MIDIEvent.h:196
static const MIDIByte MIDI_MESSAGE_TYPE_MASK
Definition: MIDIEvent.h:38
void setMetaMessage(const std::string &meta)
Definition: MIDIEvent.h:185
static const MIDIByte MIDI_NOTE_ON
Definition: MIDIEvent.h:42
static const MIDIByte MIDI_CONTROLLER_VOLUME
Definition: MIDIEvent.h:95
unsigned char MIDIByte
static const MIDIByte MIDI_CONTROLLER_SUSTAIN
Definition: MIDIEvent.h:99
static const MIDIByte MIDI_END_OF_EXCLUSIVE
Definition: MIDIEvent.h:54
static const MIDIByte MIDI_CHNL_AFTERTOUCH
Definition: MIDIEvent.h:46
static const MIDIByte MIDI_CONTROLLER_PAN
Definition: MIDIEvent.h:98
unsigned long addTime(const unsigned long &time)
Definition: MIDIEvent.h:161
static const MIDIByte MIDI_CONTROLLER_BANK_MSB
Definition: MIDIEvent.h:94
static const MIDIByte MIDI_CONTROLLER_CHORUS
Definition: MIDIEvent.h:105
QString m_message
Definition: MIDIEvent.h:226