-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmidi.cpp
311 lines (253 loc) · 10.3 KB
/
midi.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
#include "midi.hpp"
Midi::Midi() : pos(0), unitOfTime(60000.0f) {}
Midi::~Midi(){
if (in.is_open()) in.close();
tracks.clear();
}
bool Midi::parse(const char *name) {
in.open(name, std::ios::in | std::ios::binary);
if (!in.is_open()) return false;
if (readStr(4).compare("MThd") != 0) return false;
uint32_t chunkSize = readBytes(4);
if (chunkSize != 6) return false;
//uint16_t formatType = readBytes(2);
seekg(2);
uint16_t numOfTracks = readBytes(2);
uint32_t timeDivision = readBytes(2);
if (timeDivision & 0x8000)
return false; //15 bit set SMPTE format
uint8_t previousEvent = 0;
uint32_t maxTicks = 0;
for (uint16_t i = 0; i < numOfTracks; ++i) {
//bool EOT = false;
//std::string mtrkHeader = readStr(4);
seekg(4); // MTrk header
uint32_t length = pos + readBytes(4);
//seekg(8); // Mtrk header 4 + trackLength 4
uint32_t tick = 0;
tracks.push_back(Track());
for (;pos <= length/*!EOT*/;) {
tick += readVarLen();
uint8_t event = readByte();
if (event < 0x80) {
event = previousEvent;
seekg(-1);
if(event == 0) continue; // SysEx event
} else
previousEvent = ((event & 0xF0) != 0xF0) ? event : 0;
switch(event & 0xF0) {
case 0x80: { // note off
int8_t note = readByte();
int8_t velocity = readByte();
tracks.back().notes.push_back({.state=false, .channel=(uint8_t)(event & 0xF),
.key = note, .velocity = velocity, .startTick = tick});
}
break;
case 0x90: { // note on
int8_t note = readByte();
int8_t velocity = readByte();
tracks.back().notes.push_back({.state=(bool)velocity, .channel=(uint8_t)(event & 0xF),
.key = note, .velocity = velocity, .startTick = tick});
}
break;
case 0xA0: { //Polyphonic Pressure
//int8_t note = readByte();
//int8_t pressure = readByte();
seekg(2);
}
break;
case 0xB0: { // controller
//int8_t controller = readByte();
//int8_t value = readByte();
seekg(2);
}
break;
case 0xC0: { // program change
//int8_t program = readByte();
seekg(1);
}
break;
case 0xD0: { // Channel Pressure
//int8_t pressure = readByte();
seekg(1);
}
break;
case 0xE0: { // pitch bend
//int8_t lsb = readByte();
//int8_t msb = readByte();
seekg(2);
}
break;
case 0xF0: { // SysEx event
if (event == 0xF0) // System Exclusive Message Begin
seekg(readVarLen());
else if (event == 0xF7) // System Exclusive Message End
seekg(readVarLen());
else if (event == 0xFF) {
uint8_t type = readByte();
uint32_t value = readVarLen();
switch(type) {
case 0x00: { // MetaSequence
seekg(2);
}
break;
case 0x01: { // MetaText
seekg(value);
}
break;
case 0x02: { // MetaCopyright
seekg(value);
}
break;
case 0x03: { // MetaTrackName
seekg(value);
}
break;
case 0x04: { // MetaInstrumentName
seekg(value);
}
break;
case 0x05: { // MetaLyrics
seekg(value);
}
break;
case 0x06: { // MetaMarker
seekg(value);
}
break;
case 0x07: { // MetaCuePoint
seekg(value);
}
break;
case 0x20: { // MetaChannelPrefix
seekg(1);
}
break;
case 0x2F: { // END OF TRACK
//EOT = true;
}
break;
case 0x51: { // MetaSetTempo
const uint32_t BPM = 60000000 / readBytes(3);
tempo_map.push_back({tick, BPM});
}
break;
case 0x54: { // MetaSMPTEOffset
seekg(5);
}
break;
case 0x58: { // MetaTimeSignature
//uint8_t numenator = readByte();
//uint8_t denominator = readByte();
//seekg(2);
seekg(4);
//std::cout << "time signature" << (uint16_t)numenator << "/" << (uint32_t)(1 << denominator) << std::endl;
}
break;
case 0x59: { // MetaKeySignature
seekg(2);
}
break;
case 0x7F: { // MetaSequencerSpecific
seekg(value);
}
break;
default:break;
}
}
}
break;
default:break;
}
}
if (tick > maxTicks)
maxTicks = tick;
}
std::sort(tempo_map.begin(), tempo_map.end(), [&](const Tempo &tempo1, const Tempo &tempo2) {
return tempo1.tick < tempo2.tick;
});
int32_t i = 0;
for (float elapsedTicks = 0.0f, time = 0.0f; i < tempo_map.size(); ++i) {
time += (unitOfTime / tempo_map[i ? i - 1 : i].tempo) * ((tempo_map[i].tick - elapsedTicks) / timeDivision);
elapsedTicks = tempo_map[i].tick;
tempo_map[i].time = time;
}
//float maxTime = 0;
for (auto& track : tracks)
for (auto& note : track.notes)
if(note.state == true) {
auto noteOff = std::find_if(track.notes.begin(), track.notes.end(), [&](const Track::Note &foundNote){
return (bool)((note.key == foundNote.key) &&
(note.channel == foundNote.channel) && foundNote.state == false);
});
if (noteOff != track.notes.end()) {
auto tempo = std::find_if(tempo_map.begin(), tempo_map.end(), [&](const Tempo &bpm) {
return (note.startTick < bpm.tick);
}) - 1;
if (tempo != tempo_map.end()) {
const float TEMPO = ((unitOfTime / tempo->tempo) / timeDivision);
note.durationTick = (noteOff->startTick - note.startTick);
note.durationTime = note.durationTick * TEMPO;
note.startTime = tempo->time + ((note.startTick - tempo->tick) * TEMPO);
track.notes.erase(noteOff);
/*
if (note.startTime > maxTime)
maxTime = note.startTime;
*/
}
}
}
//maxTime = tempo_map.back().time + (maxTicks - tempo_map.back().tick) * (unitOfTime / tempo_map.back().tempo) / timeDivision;
//std::cout << "minutes " << ((uint32_t)maxTime / 1000) / 60 << " seconds " << ((uint32_t)maxTime / 1000) % 60 << std::endl;
in.close();
return true;
}
const std::vector<Midi::Track>& Midi::getTracks() const {
return tracks;
}
void Midi::setUnitOfTime(float unit) {
unitOfTime = unit;
}
float Midi::getUnitOfTime() const {
return unitOfTime;
}
uint32_t Midi::readBytes(uint16_t length) {
uint32_t value = 0;
for (uint8_t i = 0; i < length; ++i)
value = (value << 8) | readByte();
return value;
}
uint8_t Midi::readByte() {
++pos;
return in.get();
}
void Midi::seekg(int32_t cursor) {
pos += cursor;
//in.seekg(pos, std::ios::beg);
in.seekg(cursor, std::ios::cur);
}
std::string Midi::readStr(uint16_t length) {
std::string str = "";
for (uint16_t i = 0; i < length; ++i)
str += readByte();
return str;
}
uint32_t Midi::readVarLen() {
uint32_t value = readByte();
uint8_t byteRead = 0;
if (value & 0x80) {
value &= 0x7F;
do {
byteRead = readByte();
value = (value << 7) | (byteRead & 0x7F);
} while (byteRead & 0x80);
}
return value;
}
uint32_t Midi::swap32(uint32_t dword) const {
return ((dword>>24)&0xff) | ((dword<<8)&0xff0000) |
((dword>>8)&0xff00) | ((dword<<24)&0xff000000);
}
uint16_t Midi::swap16(uint16_t word) const {
return ((word >> 8) | (word << 8));
}