-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathevent.cpp
427 lines (380 loc) · 9.92 KB
/
event.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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
/*
Copyright (c) 2014 Auston Sterling
See LICENSE for copying permissions.
-----Event Class Implementation-----
Auston Sterling
Contains the implementation for the Event class, referring to MIDI events on a track.
*/
#include "event.hpp"
namespace midi
{
//Event functions
//All events must have a delta time
std::uint32_t Event::dt() const
{
return deltaTime_;
}
void Event::setdt(std::uint32_t indt)
{
deltaTime_ = indt;
}
//Channel event functions
std::vector<std::uint8_t> ChannelEvent::data() const
{
//Create the output vector
std::vector<std::uint8_t> out;
//Start adding things to it
//Delta Time
for (std::size_t i = 0; i < deltaTime_.size(); i++)
{
out.push_back(deltaTime_[i]);
}
//Type and MIDI channel
out.push_back((type_ << 4) | (channel_ & 0x0F));
//Parameter 1
out.push_back(param1_);
//Parameter 2
if (param2_ != 0) out.push_back(param2_);
//Return the completed data
return out;
}
std::size_t ChannelEvent::size() const
{
if (param2_ != 0) return deltaTime_.size() + 3;
return deltaTime_.size() + 2;
}
std::uint16_t ChannelEvent::getNote() const
{
if (usesNote_ == 1)
{
return param1_;
}
else if (usesNote_ == 2)
{
return param1_+128;
}
return 256;
}
//Note Off event
NoteOffEvent::NoteOffEvent(std::uint32_t deltaTime, std::uint8_t channel, std::uint8_t note, std::uint8_t velocity)
{
deltaTime_ = deltaTime;
type_ = 0x08;
channel_ = channel;
param1_ = note;
param2_ = velocity;
usesNote_ = 2;
}
//Note On event
NoteOnEvent::NoteOnEvent(std::uint32_t deltaTime, std::uint8_t channel, std::uint8_t note, std::uint8_t velocity)
{
deltaTime_ = deltaTime;
type_ = 0x09;
channel_ = channel;
param1_ = note;
param2_ = velocity;
usesNote_ = 1;
}
//Note Aftertouch event
NoteAftertouchEvent::NoteAftertouchEvent(std::uint32_t deltaTime, std::uint8_t channel, std::uint8_t note, std::uint8_t amount)
{
deltaTime_ = deltaTime;
type_ = 0x0A;
channel_ = channel;
param1_ = note;
param2_ = amount;
usesNote_ = 0;
}
//Controller event
ControllerEvent::ControllerEvent(std::uint32_t deltaTime, std::uint8_t channel, std::uint8_t type, std::uint8_t value)
{
deltaTime_ = deltaTime;
type_ = 0x0B;
channel_ = channel;
param1_ = type;
param2_ = value;
usesNote_ = 0;
}
//Program Change event
ProgramChangeEvent::ProgramChangeEvent(std::uint32_t deltaTime, std::uint8_t channel, Instrument number)
{
deltaTime_ = deltaTime;
type_ = 0x0C;
channel_ = channel;
param1_ = static_cast<std::uint8_t>(number);
param2_ = 0;
usesNote_ = 0;
}
//Channel Aftertouch event
ChannelAftertouchEvent::ChannelAftertouchEvent(std::uint32_t deltaTime, std::uint8_t channel, std::uint8_t amount)
{
deltaTime_ = deltaTime;
type_ = 0x0D;
channel_ = channel;
param1_ = amount;
param2_ = 0;
usesNote_ = 0;
}
//Pitch Bend event
PitchBendEvent::PitchBendEvent(std::uint32_t deltaTime, std::uint8_t channel, std::uint16_t value)
{
deltaTime_ = deltaTime;
type_ = 0x0E;
channel_ = channel;
param1_ = value >> 8;
param2_ = value & 0x00FF;
usesNote_ = 0;
}
//MetaEvent functions
std::vector<std::uint8_t> MetaEvent::data() const
{
//Create the output vector
std::vector<std::uint8_t> out;
//Fill it up
for (std::size_t i = 0; i < deltaTime_.size(); i++)
{
out.push_back(deltaTime_[i]);
}
out.push_back(0xFF);
out.push_back(type_);
for (std::size_t i = 0; i < length_.size(); i++)
{
out.push_back(length_[i]);
}
out.insert(out.end(), data_.begin(), data_.end());
//Return
return out;
}
std::size_t MetaEvent::size() const
{
return deltaTime_.size() + 2 + length_.size() + data_.size();
}
std::uint16_t MetaEvent::getNote() const
{
return 256;
}
//Sequence Number event
SequenceNumberEvent::SequenceNumberEvent(std::uint16_t number)
{
deltaTime_ = 0;
type_ = 0x00;
length_ = 2;
data_.push_back(number >> 8);
data_.push_back(number & 0x00FF);
usesNote_ = 0;
}
//Text event
TextEvent::TextEvent(std::string text)
{
deltaTime_ = 0;
type_ = 0x01;
length_ = text.size();
for (std::size_t i = 0; i < text.size(); i++)
{
data_.push_back(text[i]);
}
usesNote_ = 0;
}
//Copyright Notice event
CopyrightNoticeEvent::CopyrightNoticeEvent(std::string text)
{
deltaTime_ = 0;
type_ = 0x02;
length_ = text.size();
for (std::size_t i = 0; i < text.size(); i++)
{
data_.push_back(text[i]);
}
usesNote_ = 0;
}
//Sequence/Track Name event
SequenceTrackNameEvent::SequenceTrackNameEvent(std::string text)
{
deltaTime_ = 0;
type_ = 0x03;
length_ = text.size();
for (std::size_t i = 0; i < text.size(); i++)
{
data_.push_back(text[i]);
}
usesNote_ = 0;
}
//Instrument Name event
InstrumentNameEvent::InstrumentNameEvent(std::string text)
{
deltaTime_ = 0;
type_ = 0x04;
length_ = text.size();
for (std::size_t i = 0; i < text.size(); i++)
{
data_.push_back(text[i]);
}
usesNote_ = 0;
}
//Lyrics event
LyricsEvent::LyricsEvent(std::uint32_t deltaTime, std::string text)
{
deltaTime_ = deltaTime;
type_ = 0x05;
length_ = text.size();
for (std::size_t i = 0; i < text.size(); i++)
{
data_.push_back(text[i]);
}
usesNote_ = 0;
}
//Marker event
MarkerEvent::MarkerEvent(std::uint32_t deltaTime, std::string text)
{
deltaTime_ = deltaTime;
type_ = 0x06;
length_ = text.size();
for (std::size_t i = 0; i < text.size(); i++)
{
data_.push_back(text[i]);
}
usesNote_ = 0;
}
//Cue Point event
CuePointEvent::CuePointEvent(std::uint32_t deltaTime, std::string text)
{
deltaTime_ = deltaTime;
type_ = 0x07;
length_ = text.size();
for (std::size_t i = 0; i < text.size(); i++)
{
data_.push_back(text[i]);
}
usesNote_ = 0;
}
//MIDI Channel Prefix event
MIDIChannelPrefixEvent::MIDIChannelPrefixEvent(std::uint32_t deltaTime, std::uint8_t channel)
{
deltaTime_ = deltaTime;
type_ = 0x20;
length_ = 1;
data_.push_back(channel);
usesNote_ = 0;
}
//End Of Track event
EndOfTrackEvent::EndOfTrackEvent(std::uint32_t deltaTime)
{
deltaTime_ = deltaTime;
type_ = 0x2F;
length_ = 0;
usesNote_ = 0;
}
//Set Tempo event
SetTempoEvent::SetTempoEvent(std::uint32_t deltaTime, std::uint32_t mspq)
{
deltaTime_ = deltaTime;
type_ = 0x51;
length_ = 3;
data_.push_back((mspq & 0xFF0000) >> 16);
data_.push_back((mspq & 0xFF00) >> 8);
data_.push_back(mspq & 0xFF);
usesNote_ = 0;
}
//SMPTE Offset event
SMPTEOffsetEvent::SMPTEOffsetEvent(std::uint32_t deltaTime, std::uint8_t hour, std::uint8_t minute, std::uint8_t second, std::uint8_t frame, std::uint8_t sub_frame)
{
deltaTime_ = deltaTime;
type_ = 0x54;
length_ = 5;
data_.push_back(hour);
data_.push_back(minute);
data_.push_back(second);
data_.push_back(frame);
data_.push_back(sub_frame);
usesNote_ = 0;
}
//Time Signature event
TimeSignatureEvent::TimeSignatureEvent(std::uint32_t deltaTime, std::uint8_t numerator, std::uint8_t denominator, std::uint8_t metronome, std::uint8_t num32s)
{
deltaTime_ = deltaTime;
type_ = 0x58;
length_ = 4;
data_.push_back(numerator);
data_.push_back(denominator);
data_.push_back(metronome);
data_.push_back(num32s);
usesNote_ = 0;
}
//Key Signature event
KeySignatureEvent::KeySignatureEvent(std::uint32_t deltaTime, char key, bool scale)
{
deltaTime_ = deltaTime;
type_ = 0x59;
length_ = 2;
data_.push_back(key);
if (scale) data_.push_back(0x01);
else data_.push_back(0x00);
usesNote_ = 0;
}
//Sequencer Specific Event
SequencerSpecificEvent::SequencerSpecificEvent(std::uint32_t deltaTime, std::vector<std::uint8_t> input)
{
deltaTime_ = deltaTime;
type_ = 0x7F;
length_ = input.size();
data_.insert(data_.end(), input.begin(), input.end());
usesNote_ = 0;
}
//SysExEvent functions
std::vector<std::uint8_t> SysExEvent::data() const
{
//Create the output vector
std::vector<std::uint8_t> out;
//Fill it up
for (std::size_t i = 0; i < deltaTime_.size(); i++)
{
out.push_back(deltaTime_[i]);
}
out.push_back(type_);
for (std::size_t i = 0; i < length_.size(); i++)
{
out.push_back(length_[i]);
}
out.insert(out.end(), data_.begin(), data_.end());
//Return
return out;
}
std::size_t SysExEvent::size() const
{
return deltaTime_.size() + 1 + length_.size() + data_.size();
}
std::uint16_t SysExEvent::getNote() const
{
return 256;
}
//Normal SysEx event
NormalSysExEvent::NormalSysExEvent(std::uint32_t deltaTime, std::vector<std::uint8_t> data, bool startDivide)
{
deltaTime_ = deltaTime;
type_ = 0xF0;
length_ = data.size() + (startDivide?0:1);
data_.insert(data_.end(), data.begin(), data.end());
if (!startDivide) data_.push_back(0xF7);
usesNote_ = 0;
}
//Divided SysEx event
DividedSysExEvent::DividedSysExEvent(std::uint32_t deltaTime, std::vector<std::uint8_t> data, bool endDivide)
{
deltaTime_ = deltaTime;
type_ = 0xF7;
length_ = data.size() + (endDivide?1:0);
data_.insert(data_.end(), data.begin(), data.end());
if (endDivide) data_.push_back(0xF7);
usesNote_ = 0;
}
//Authorization SysEx event
AuthorizationSysExEvent::AuthorizationSysExEvent(std::uint32_t deltaTime, std::vector<std::uint8_t> data)
{
deltaTime_ = deltaTime;
type_ = 0xF7;
length_ = data.size();
data_.insert(data_.end(), data.begin(), data.end());
usesNote_ = 0;
}
} //Namespace