-
Notifications
You must be signed in to change notification settings - Fork 0
/
MidiEventFactory.cpp
393 lines (339 loc) · 12.8 KB
/
MidiEventFactory.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
//
// Created by madsy on 19.03.17.
//
#include <iterator>
#include "MidiEventFactory.h"
unsigned int MidiEventFactory::previousChannelEventCode = 0;
//Treat bytes as 8-bit and read until we encounter a 0xF7 terminator
static unsigned int readBuffer8BitsUntilF7(std::vector<unsigned char>& buffer, std::ifstream& strm){
unsigned char data8bits = 0;
unsigned int bytesread = 1;
strm.read((char *) &data8bits, 1);
do {
buffer.push_back(data8bits);
strm.read((char *) &data8bits, 1);
bytesread++;
//TODO: Assemble the bits (remove the MSB)
} while(data8bits != 0xF7u);
return bytesread;
}
static unsigned int readBuffer8BitsWithLength(std::vector<unsigned char>& buffer, std::ifstream& strm){
unsigned char data8bits = 0;
unsigned int bytesread = 0;
unsigned char len = 0;
strm.read((char *) &len, 1);
for(unsigned int i = 0; i < len; i++){
strm.read((char *) &data8bits, 1);
buffer.push_back(data8bits);
}
return len + 1;
}
static unsigned int readMetaEvtString(std::string& str, std::ifstream& strm){
unsigned char len = 0;
char ch = 0;
strm.read((char *)&len, 1);
for(unsigned int i = 0; i < len; i++){
strm.read(&ch, 1);
str += ch;
}
return len + 1;
}
std::shared_ptr<MidiEvent>
MidiEventFactory::createChannelEvent(
unsigned int& bytesread,
std::ifstream& strm,
MidiEvent::ChannelEventType type,
unsigned int timestamp,
unsigned int absolutetime,
int channel){
MidiEvent* result = nullptr;
unsigned char arg1, arg2;
switch(type){
case MidiEvent::ChannelEventType::EVT_NOTE_OFF: {
strm.read((char *) &arg1, 1);
strm.read((char *) &arg2, 1);
result = new ChEvtNoteOff(timestamp, channel, arg1, arg2);
bytesread += 2;
break;
}
case MidiEvent::ChannelEventType::EVT_NOTE_ON:{
strm.read((char *) &arg1, 1);
strm.read((char *) &arg2, 1);
result = new ChEvtNoteOn(timestamp, channel, arg1, arg2);
bytesread += 2;
break;
}
case MidiEvent::ChannelEventType::EVT_AFTERTOUCH1:{
strm.read((char *) &arg1, 1);
strm.read((char *) &arg2, 1);
result = new ChEvtAfterTouch1(timestamp, channel, arg1, arg2);
bytesread += 2;
break;
}
case MidiEvent::ChannelEventType::EVT_CONTROL_CHANGE:{
strm.read((char *) &arg1, 1);
strm.read((char *) &arg2, 1);
result = new ChEvtControlChange(timestamp, channel, arg1, arg2);
bytesread += 2;
break;
}
case MidiEvent::ChannelEventType::EVT_PROGRAM_CHANGE:{
strm.read((char *) &arg1, 1);
result = new ChEvtProgramChange(timestamp, channel, arg1);
bytesread +=1;
break;
}
case MidiEvent::ChannelEventType::EVT_AFTERTOUCH2:{
strm.read((char *) &arg1, 1);
result = new ChEvtAfterTouch2(timestamp, channel, arg1);
bytesread += 1;
break;
}
case MidiEvent::ChannelEventType::EVT_PITCH_WHEEL:{
strm.read((char *) &arg1, 1);
strm.read((char *) &arg2, 1);
unsigned short wheel = (arg1<<7)|arg2;
result = new ChEvtPitchWheel(timestamp, channel, (int)wheel);
bytesread += 2;
break;
}
}
return std::shared_ptr<MidiEvent>(result);
}
std::shared_ptr<MidiEvent>
MidiEventFactory::createSystemCommonEvent(
unsigned int& bytesread,
std::ifstream& strm,
MidiEvent::SystemCommonEventType type,
unsigned int timestamp,
unsigned int absolutetime){
MidiEvent* result = nullptr;
unsigned char arg1, arg2, data7bits;
switch(type){
case MidiEvent::SystemCommonEventType::EVT_SYSTEM_EXCLUSIVE:{
std::vector<unsigned char> data;
strm.read((char *) &arg1, 1);
do {
strm.read((char *) &data7bits, 1);
//TODO: Assemble the bits (remove the MSB)
data.push_back(data7bits);
} while(data7bits != 0xF7u);
result = new SysCmnEvtExclusive(timestamp, arg1, data);
bytesread += (data.size() + 1);
break;
}
case MidiEvent::SystemCommonEventType::EVT_SONG_POSITION_POINTER:{
strm.read((char *) &arg1, 1);
strm.read((char *) &arg2, 1);
int timecounter = (arg1<<7)|arg2;
//14-bit song position
result = new SysCmnEvtSongPositionCounter(timestamp, timecounter);
bytesread += 2;
break;
}
case MidiEvent::SystemCommonEventType::EVT_SONG_SELECT:{
strm.read((char *) &arg1, 1);
//7-bit song select value
result = new SysCmnEvtSongSelect(timestamp, arg1);
bytesread += 1;
break;
}
case MidiEvent::SystemCommonEventType::EVT_TUNE_REQUEST:{
result = new SysCmnEvtTuneRequest(timestamp);
break;
}
//Not really an independent event, so kept unimplemented.
//Used by EVT_SYSTEM_EXCLUSIVE
case MidiEvent::SystemCommonEventType::EVT_END_OF_EXCLUSIVE:{
break;
}
}
return std::shared_ptr<MidiEvent>(result);
}
std::shared_ptr<MidiEvent>
MidiEventFactory::createSystemRealtimeEvent(
unsigned int& bytesread,
std::ifstream& strm,
MidiEvent::SystemRealtimeEventType type,
unsigned int timestamp,
unsigned int absolutetime){
MidiEvent* result = nullptr;
switch(type){
case MidiEvent::SystemRealtimeEventType::EVT_TIMING_CLOCK:{
result = new SysRtEvtTimingClock(timestamp);
break;
}
case MidiEvent::SystemRealtimeEventType::EVT_START:{
result = new SysRtEvtStart(timestamp);
break;
}
case MidiEvent::SystemRealtimeEventType::EVT_CONTINUE:{
result = new SysRtEvtContinue(timestamp);
break;
}
case MidiEvent::SystemRealtimeEventType::EVT_STOP:{
result = new SysRtEvtStop(timestamp);
break;
}
case MidiEvent::SystemRealtimeEventType::EVT_ACTIVE_SENSING:{
result = new SysRtEvtActiveSensing(timestamp);
break;
}
}
return std::shared_ptr<MidiEvent>(result);
}
//readBuffer8BitsUntilF7
std::shared_ptr<MidiEvent>
MidiEventFactory::createMetaEvent(
unsigned int &bytesread,
std::ifstream &strm,
MidiEvent::MetaEventType type,
unsigned int timestamp,
unsigned int absolutetime) {
std::string s;
MidiEvent* result = nullptr;
unsigned char arg1, arg2, arg3, arg4, arg5, data7bits;
switch(type) {
case MidiEvent::MetaEventType::EVT_SEQUENCE_NUMBER:{
strm.read((char *) &arg1, 1);
strm.read((char *) &arg2, 1);
int timecounter = (arg1<<7)|arg2;
//16-bit sequence number
result = new MetaEvtSequenceNumber(timestamp, timecounter);
bytesread += 2;
break;
}
case MidiEvent::MetaEventType::EVT_TEXT:{
bytesread += readMetaEvtString(s, strm);
result = new MetaEvtText(timestamp, s);
break;
}
case MidiEvent::MetaEventType::EVT_COPYRIGHT:{
bytesread += readMetaEvtString(s, strm);
result = new MetaEvtCopyright(timestamp, s);
break;
}
case MidiEvent::MetaEventType::EVT_TRACK_NAME:{
bytesread += readMetaEvtString(s, strm);
result = new MetaEvtTrackName(timestamp, s);
break;
}
case MidiEvent::MetaEventType::EVT_INSTRUMENT_NAME:{
bytesread += readMetaEvtString(s, strm);
result = new MetaEvtInstrumentName(timestamp, s);
break;
}
case MidiEvent::MetaEventType::EVT_LYRICS:{
bytesread += readMetaEvtString(s, strm);
result = new MetaEvtLyrics(timestamp, s);
break;
}
case MidiEvent::MetaEventType::EVT_MARKER:{
bytesread += readMetaEvtString(s, strm);
result = new MetaEvtMarker(timestamp, s);
break;
}
case MidiEvent::MetaEventType::EVT_CUE_POINT:{
bytesread += readMetaEvtString(s, strm);
result = new MetaEvtCuePoint(timestamp, s);
break;
}
case MidiEvent::MetaEventType::EVT_MIDI_CHANNEL_PREFIX:{
strm.read((char *) &arg1, 1);
result = new MetaEvtMidiChannelPrefix(timestamp, arg1);
bytesread += 1;
break;
}
case MidiEvent::MetaEventType::EVT_END_OF_TRACK:{
result = new MetaEvtEndOfTrack(timestamp);
break;
}
case MidiEvent::MetaEventType::EVT_SET_TEMPO:{
strm.read((char *) &arg1, 1);
strm.read((char *) &arg2, 1);
strm.read((char *) &arg3, 1);
unsigned int tempo = (arg1<<14)|(arg2<<7)|arg3;
result = new MetaEvtSetTempo(timestamp, tempo);
bytesread += 3;
break;
}
case MidiEvent::MetaEventType::EVT_SMPTE_OFFSET:{
strm.read((char *) &arg1, 1);
strm.read((char *) &arg2, 1);
strm.read((char *) &arg3, 1);
strm.read((char *) &arg4, 1);
strm.read((char *) &arg5, 1);
result = new MetaEvtSMPTEOffset(timestamp, arg1, arg2, arg3, arg4, arg5);
bytesread += 5;
break;
}
case MidiEvent::MetaEventType::EVT_TIME_SIGNATURE:{
strm.read((char *) &arg1, 1);
strm.read((char *) &arg2, 1);
strm.read((char *) &arg3, 1);
strm.read((char *) &arg4, 1);
result = new MetaEvtTimeSignature(timestamp, arg1, arg2, arg3, arg4);
bytesread += 4;
break;
}
case MidiEvent::MetaEventType::EVT_KEY_SIGNATURE:{
strm.read((char *) &arg1, 1);
strm.read((char *) &arg2, 1);
result = new MetaEvtKeySignature(timestamp, arg1, arg2);
bytesread += 2;
break;
}
case MidiEvent::MetaEventType::EVT_SEQUENCER_SPECIFIC:{
std::vector<unsigned char> data;
bytesread += readBuffer8BitsWithLength(data, strm);
result = new MetaEvtSequencerSpecific(timestamp, data);
break;
}
}
return std::shared_ptr<MidiEvent>(result);
}
//Event factory function
std::shared_ptr<MidiEvent>
MidiEventFactory::createMidiEvent(
unsigned int absolutetime,
unsigned int& bytesread,
std::ifstream& strm){
unsigned int timestamp = 0;
unsigned char event_type_byte = 0;
//Read timestamp before the event
bytesread = readMidiVarLenInteger(timestamp, strm);
strm.read((char*)&event_type_byte, 1);
bytesread++;
//first code
MidiEvent::MidiEventType evt = MidiEvent::getEventType(event_type_byte);
switch(evt){
case MidiEvent::MidiEventType::EVT_CHANNEL: {
MidiEvent::ChannelEventType ChEv = MidiEvent::getChannelEventType(event_type_byte);
MidiEventFactory::previousChannelEventCode = event_type_byte;
return createChannelEvent(bytesread, strm, ChEv, absolutetime + timestamp, absolutetime, (event_type_byte & 0xF));
}
case MidiEvent::MidiEventType::EVT_SYSCOMMON: {
MidiEvent::SystemCommonEventType SysCmn = MidiEvent::getSystemCommonEventType(event_type_byte);
return createSystemCommonEvent(bytesread, strm, SysCmn, absolutetime + timestamp, absolutetime);
}
case MidiEvent::MidiEventType::EVT_SYSRT: {
MidiEvent::SystemRealtimeEventType SysRt = MidiEvent::getSystemRealtimeEventType(event_type_byte);
return createSystemRealtimeEvent(bytesread, strm, SysRt, absolutetime + timestamp, absolutetime);
}
case MidiEvent::MidiEventType::EVT_META:{
MidiEvent::MetaEventType Meta = MidiEvent::getMetaEventType(bytesread, strm);
return createMetaEvent(bytesread, strm, Meta, absolutetime + timestamp, absolutetime);
}
case MidiEvent::MidiEventType::EVT_RUNNING_STATUS: {
//MSB not set, so this is a running status without a code
//use the previous code (and channel)
//Go back one byte. The byte we read is a data byte
bytesread--;
strm.seekg(-1, strm.cur);
MidiEvent::ChannelEventType ChEv = MidiEvent::getChannelEventType(MidiEventFactory::previousChannelEventCode);
return createChannelEvent(bytesread, strm, ChEv, absolutetime + timestamp, absolutetime, (MidiEventFactory::previousChannelEventCode & 0xF));
}
}
}
MidiEventFactory::~MidiEventFactory() {
}