-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMidiEvent.cpp
284 lines (215 loc) · 5.52 KB
/
MidiEvent.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
//
// Programmer: Craig Stuart Sapp <[email protected]>
// Creation Date: Sat Feb 14 21:40:14 PST 2015
// Last Modified: Sat Apr 21 10:52:19 PDT 2018 Removed using namespace std;
// Filename: midifile/src/MidiEvent.cpp
// Website: http://midifile.sapp.org
// Syntax: C++11
// vim: ts=3 noexpandtab
//
// Description: A class which stores a MidiMessage and a timestamp
// for the MidiFile class.
//
#include "MidiEvent.h"
#include <stdlib.h>
namespace smf {
//////////////////////////////
//
// MidiEvent::MidiEvent -- Constructor classes
//
MidiEvent::MidiEvent(void) : MidiMessage() {
clearVariables();
}
MidiEvent::MidiEvent(int command) : MidiMessage(command) {
clearVariables();
}
MidiEvent::MidiEvent(int command, int p1) : MidiMessage(command, p1) {
clearVariables();
}
MidiEvent::MidiEvent(int command, int p1, int p2)
: MidiMessage(command, p1, p2) {
clearVariables();
}
MidiEvent::MidiEvent(int aTime, int aTrack, vector<uchar>& message)
: MidiMessage(message) {
track = aTrack;
tick = aTime;
seconds = 0.0;
seq = 0;
m_eventlink = NULL;
}
MidiEvent::MidiEvent(const MidiEvent& mfevent) : MidiMessage() {
track = mfevent.track;
tick = mfevent.tick;
seconds = mfevent.seconds;
seq = mfevent.seq;
m_eventlink = NULL;
this->resize(mfevent.size());
for (int i=0; i<(int)this->size(); i++) {
(*this)[i] = mfevent[i];
}
}
//////////////////////////////
//
// MidiEvent::~MidiEvent -- MidiFile Event destructor
//
MidiEvent::~MidiEvent() {
track = -1;
tick = -1;
seconds = -1.0;
seq = -1;
this->resize(0);
m_eventlink = NULL;
}
//////////////////////////////
//
// MidiEvent::clearVariables -- Clear everything except MidiMessage data.
//
void MidiEvent::clearVariables(void) {
track = 0;
tick = 0;
seconds = 0.0;
seq = 0;
m_eventlink = NULL;
}
//////////////////////////////
//
// MidiEvent::operator= -- Copy the contents of another MidiEvent.
//
MidiEvent& MidiEvent::operator=(const MidiEvent& mfevent) {
if (this == &mfevent) {
return *this;
}
tick = mfevent.tick;
track = mfevent.track;
seconds = mfevent.seconds;
seq = mfevent.seq;
m_eventlink = NULL;
this->resize(mfevent.size());
for (int i=0; i<(int)this->size(); i++) {
(*this)[i] = mfevent[i];
}
return *this;
}
MidiEvent& MidiEvent::operator=(const MidiMessage& message) {
if (this == &message) {
return *this;
}
clearVariables();
this->resize(message.size());
for (int i=0; i<(int)this->size(); i++) {
(*this)[i] = message[i];
}
return *this;
}
MidiEvent& MidiEvent::operator=(const vector<uchar>& bytes) {
clearVariables();
this->resize(bytes.size());
for (int i=0; i<(int)this->size(); i++) {
(*this)[i] = bytes[i];
}
return *this;
}
MidiEvent& MidiEvent::operator=(const vector<char>& bytes) {
clearVariables();
setMessage(bytes);
return *this;
}
MidiEvent& MidiEvent::operator=(const vector<int>& bytes) {
clearVariables();
setMessage(bytes);
return *this;
}
//////////////////////////////
//
// MidiEvent::unlinkEvent -- Disassociate this event with another.
// Also tell the other event to disassociate from this event.
//
void MidiEvent::unlinkEvent(void) {
if (m_eventlink == NULL) {
return;
}
MidiEvent* mev = m_eventlink;
m_eventlink = NULL;
mev->unlinkEvent();
}
//////////////////////////////
//
// MidiEvent::linkEvent -- Make a link between two messages.
// Unlinking
//
void MidiEvent::linkEvent(MidiEvent* mev) {
if (mev->m_eventlink != NULL) {
// unlink other event if it is linked to something else;
mev->unlinkEvent();
}
// if this is already linked to something else, then unlink:
if (m_eventlink != NULL) {
m_eventlink->unlinkEvent();
}
unlinkEvent();
mev->m_eventlink = this;
m_eventlink = mev;
}
void MidiEvent::linkEvent(MidiEvent& mev) {
linkEvent(&mev);
}
//////////////////////////////
//
// MidiEvent::getLinkedEvent -- Returns a linked event. Usually
// this is the note-off message for a note-on message and vice-versa.
// Returns null if there are no links.
//
MidiEvent* MidiEvent::getLinkedEvent(void) {
return m_eventlink;
}
const MidiEvent* MidiEvent::getLinkedEvent(void) const {
return m_eventlink;
}
//////////////////////////////
//
// MidiEvent::isLinked -- Returns true if there is an event which is not
// NULL. This function is similar to getLinkedEvent().
//
int MidiEvent::isLinked(void) const {
return m_eventlink == NULL ? 0 : 1;
}
//////////////////////////////
//
// MidiEvent::getTickDuration -- For linked events (note-ons and note-offs),
// return the absolute tick time difference between the two events.
// The tick values are presumed to be in absolute tick mode rather than
// delta tick mode. Returns 0 if not linked.
//
int MidiEvent::getTickDuration(void) const {
const MidiEvent* mev = getLinkedEvent();
if (mev == NULL) {
return 0;
}
int tick2 = mev->tick;
if (tick2 > tick) {
return tick2 - tick;
} else {
return tick - tick2;
}
}
//////////////////////////////
//
// MidiEvent::getDurationInSeconds -- For linked events (note-ons and
// note-offs), return the duration of the note in seconds. The
// seconds analysis must be done first; otherwise the duration will be
// reported as zero.
//
double MidiEvent::getDurationInSeconds(void) const {
const MidiEvent* mev = getLinkedEvent();
if (mev == NULL) {
return 0;
}
double seconds2 = mev->seconds;
if (seconds2 > seconds) {
return seconds2 - seconds;
} else {
return seconds - seconds2;
}
}
} // end namespace smf