This repository has been archived by the owner on Sep 18, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
midi.cpp
257 lines (193 loc) · 6.26 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
// DO NOT EDIT
#include "midi.hpp"
#include <cstdint>
#include <fstream>
#include <iostream>
#include <string>
#include <list>
#include <bitset>
MIDIReader::MIDIReader(): status(true){
}
bool MIDIReader::parse(std::istream & ins){
parseHeader(ins);
if(status){
for(int i = 0; i < head.n; ++i){
parseNextTrack(ins);
if(!status) break;
}
}
// TODO strictly, signature must be present in the file
return status;
}
TrackListType MIDIReader::getAllTracks(){
return std::move(tracklist);
}
void MIDIReader::parseHeader(std::istream & instream){
if(instream.good()){
// read header
char magic[4];
instream.read(reinterpret_cast<std::istream::char_type *>(&magic[0]), 4);
status = status && instream.good();
status = status && ((magic[0] == 'M') && (magic[1] == 'T') &&
(magic[2] == 'h') && (magic[3] == 'd'));
head.length = read_big32(instream);
head.format = read_big16(instream);
head.n = read_big16(instream);
head.division = read_big16(instream);
// format 2 files unsupported
status = status && (head.format < 2);
// Tempo in SMPTE format unsupported
std::bitset<16> b(head.division);
status = status && !b.test(15);
}
else{
status = false;
}
}
void MIDIReader::parseNextTrack(std::istream & instream){
// allocate a track in the list
tracklist.emplace_back(head.division);
Track& trk = tracklist.back();
bool track_end = false;
char magic[4];
instream.read(reinterpret_cast<std::istream::char_type *>(&magic[0]), 4);
status = status && instream.good();
status = status && ((magic[0] == 'M') && (magic[1] == 'T') &&
(magic[2] == 'r') && (magic[3] == 'k'));
// use this to verify the number of bytes read is correct
int32_t track_length = read_big32(instream);
// save stream position
std::istream::pos_type startpos = instream.tellg();
// ticks tracks the number of MIDI ticks accumulated since the
// track started
intmax_t ticks = 0;
// flag used for running status;
uint8_t notestatus = false;
do{
ticks += read_variable_length(instream);
uint8_t eventcode = read_byte(instream);
uint8_t data1;
uint8_t data2;
track_end = false;
if(eventcode == 0xFF){ // META event
uint8_t code = read_byte(instream);
// if at track end, add event and stop parsing
if(code == 0x2F){
track_end = true;
trk.addEndEvent(ticks);
}
intmax_t length = read_variable_length(instream);
if(code == 0x51){ // tempo
intmax_t tempo = read_tempo(instream);
trk.addTempoEvent(tempo, ticks);
}
else if(code == 0x58){ // time signature
read_signature(instream);
// TODO: For now we assume 4/4 time
}
else{
// TODO: handle others like instrument name, etc.,
// for now just skip them
instream.seekg(length, std::ios_base::cur);
}
}
else if(eventcode == 0xF0){ // sysex events not supported.
status = false;
break;
}
else if(eventcode >= 0x80){ // MIDI event
notestatus = eventcode >> 4;
data1 = read_byte(instream);
if((notestatus != 0xC) && (notestatus != 0xD)){
data2 = read_byte(instream);
trk.addNoteEvent(notestatus, data1, data2, ticks);
}
}
else{ // running status
data1 = eventcode;
if((notestatus != 0xC) && (notestatus != 0xD)){
data2 = read_byte(instream);
trk.addNoteEvent(notestatus, data1, data2, ticks);
}
}
} while(!track_end && instream.good());
// check stream pos - startpos is equal to expected track length
std::istream::pos_type endpos = instream.tellg();
status = status && ((endpos - startpos) == track_length);
}
// read bigendian bytes as long as most-signifigant bit is 1,
// using lower 7 bits
intmax_t MIDIReader::read_variable_length(std::istream & instream){
auto startpos = instream.tellg();
intmax_t value;
uint8_t temp;
instream.read(reinterpret_cast<std::istream::char_type *>(&temp), 1);
status = status && instream.good();
value = temp;
if(value & 0x80){
value &= 0x7f;
do{
instream.read(reinterpret_cast<std::istream::char_type *>(&temp), 1);
value = (value << 7) + (temp & 0x7f);
}while(temp & 0x80);
}
// check we did not read more bytes than is in intmax_t
auto endpos = instream.tellg();
status = status && ((endpos - startpos) <
static_cast<std::istream::pos_type>(sizeof(intmax_t)));
return value;
}
int32_t MIDIReader::read_big32(std::istream &instream) {
std::istream::char_type raw[4];
instream.read(reinterpret_cast<std::istream::char_type *>(&raw[0]), 4);
status = status && instream.good();
uint8_t data0 = raw[0];
uint8_t data1 = raw[1];
uint8_t data2 = raw[2];
uint8_t data3 = raw[3];
int32_t temp = (data0 << 24) | (data1 << 16) | (data2 << 8) | (data3 << 0);
return temp;
}
int16_t MIDIReader::read_big16(std::istream &instream) {
std::istream::char_type raw[2];
instream.read(reinterpret_cast<std::istream::char_type *>(&raw[0]), 2);
status = status && instream.good();
uint8_t data0 = raw[0];
uint8_t data1 = raw[1];
int16_t temp = (data0 << 8) | (data1 << 0);
return temp;
}
uint8_t MIDIReader::read_byte(std::istream & instream){
uint8_t temp;
instream.read(reinterpret_cast<std::istream::char_type *>(&temp), 1);
status = status && instream.good();
return temp;
}
// tempo is 3 bytes
intmax_t MIDIReader::read_tempo(std::istream & instream){
std::istream::char_type raw[3];
instream.read(reinterpret_cast<std::istream::char_type *>(&raw[0]), 3);
status = status && instream.good();
uint8_t data0 = raw[0];
uint8_t data1 = raw[1];
uint8_t data2 = raw[2];
int32_t temp = (data0 << 16) | (data1 << 8) | (data2 << 0);
return temp;
}
// signature is 4 bytes
void MIDIReader::read_signature(std::istream & instream){
std::istream::char_type raw[4];
instream.read(reinterpret_cast<std::istream::char_type *>(&raw[0]), 4);
status = status && instream.good();
}
TrackListType readMIDIFromFile(const std::string & infilename){
std::ifstream ifs(infilename, std::ios::binary);
MIDIReader reader;
if(reader.parse(ifs)){
std::cout << "Parsing MIDI file succeeded.\n";
}
else{
std::cout << "Parsing MIDI file failed.\n";
}
return reader.getAllTracks();
}