-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmidi.cpp
336 lines (275 loc) · 6.81 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
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
/*
Copyright (c) 2014 Auston Sterling
See LICENSE for copying permissions.
-----MIDI Class Implementation-----
Auston Sterling
Contains the header for the MIDI class, representing a MIDI file.
*/
#include "midi.hpp"
namespace midi
{
//MIDI Class Functions
//Writes the data to a file
void MIDI::write(std::string filename) const
{
//Open the file
std::ofstream fout(filename.c_str(), std::ios_base::out |
std::ios_base::trunc |
std::ios_base::binary);
//Verify that it opened
if (!fout) return;
//Write data
fout.write((const char*)(&(data()[0])), size());
//Close file stream
fout.close();
}
//Time division setter
void MIDI::setTimeDivision(const TimeDivision & td)
{
td_ = td;
}
//MIDI_Type0 Class Functions
//Constructor
MIDI_Type0::MIDI_Type0(const Track & tr, const TimeDivision & td)
{
track_ = tr.clone();
td_ = td;
}
//Destructor
MIDI_Type0::~MIDI_Type0()
{
clear();
}
//Size
std::size_t MIDI_Type0::size() const
{
return 14 + track_->size();
}
//Data
std::vector<std::uint8_t> MIDI_Type0::data() const
{
//Create the output vector
std::vector<std::uint8_t> out;
//MIDI header
out.reserve(14);
out.push_back(0x4D);
out.push_back(0x54);
out.push_back(0x68);
out.push_back(0x64);
out.push_back(0);
out.push_back(0);
out.push_back(0);
out.push_back(6);
out.push_back(0);
out.push_back(0);
out.push_back(0);
out.push_back(1);
std::vector<std::uint8_t> td = td_.data();
out.insert(out.end(), td.begin(), td.end());
//Track data
std::vector<std::uint8_t> tdata = track_->data();
out.insert(out.end(), tdata.begin(), tdata.end());
//Return the output
return out;
}
//Track setter
void MIDI_Type0::setTrack(const Track & tr)
{
clear();
track_ = tr.clone();
}
//Clears the track
void MIDI_Type0::clear()
{
if (track_ != NULL) delete track_;
track_ = NULL;
}
//MIDI_Type1 Functions
//Constructor
MIDI_Type1::MIDI_Type1(const std::vector<Track*> & tr, const TimeDivision & td)
{
track_ = tr;
td_ = td;
}
MIDI_Type1::MIDI_Type1(const TimeDivision & td)
{
td_ = td;
}
//Destructor
MIDI_Type1::~MIDI_Type1()
{
clear();
}
//Size accessor
std::size_t MIDI_Type1::size() const
{
//Start with header
std::size_t out = 14;
//Add each track
for (std::size_t i = 0; i < track_.size(); i++)
{
out += track_[i]->size();
}
return out;
}
//Data
std::vector<std::uint8_t> MIDI_Type1::data() const
{
//Create the output vector
std::vector<std::uint8_t> out;
//MIDI header
out.reserve(14);
out.push_back(0x4D);
out.push_back(0x54);
out.push_back(0x68);
out.push_back(0x64);
out.push_back(0);
out.push_back(0);
out.push_back(0);
out.push_back(6);
out.push_back(0);
out.push_back(1);
out.push_back(track_.size() >> 8);
out.push_back(track_.size() & 0xFF);
std::vector<std::uint8_t> td = td_.data();
out.insert(out.end(), td.begin(), td.end());
//Track data
std::vector<std::uint8_t> tdata;
for (std::size_t i = 0; i < track_.size(); i++)
{
tdata = track_[i]->data();
out.insert(out.end(), tdata.begin(), tdata.end());
}
//Return the output
return out;
}
//Adds a track to the MIDI
void MIDI_Type1::addTrack(const Track & tr)
{
track_.push_back(tr.clone());
}
//Clears all tracks from the MIDI
void MIDI_Type1::clear()
{
for (std::size_t i = 0; i < track_.size(); i++)
{
delete track_[i];
}
track_.resize(0);
}
//MIDI_Type2 Functions
//Constructor
MIDI_Type2::MIDI_Type2(const std::vector<Track*> & tr, const TimeDivision & td)
{
track_ = tr;
td_ = td;
}
MIDI_Type2::MIDI_Type2(const TimeDivision & td)
{
td_ = td;
}
//Destructor
MIDI_Type2::~MIDI_Type2()
{
clear();
}
//Size accessor
std::size_t MIDI_Type2::size() const
{
//Start with header
std::size_t out = 14;
//Add each track
for (std::size_t i = 0; i < track_.size(); i++)
{
out += track_[i]->size();
}
return out;
}
//Data
std::vector<std::uint8_t> MIDI_Type2::data() const
{
//Create the output vector
std::vector<std::uint8_t> out;
//MIDI header
out.reserve(14);
out.push_back(0x4D);
out.push_back(0x54);
out.push_back(0x68);
out.push_back(0x64);
out.push_back(0);
out.push_back(0);
out.push_back(0);
out.push_back(6);
out.push_back(0);
out.push_back(2);
out.push_back(track_.size() >> 8);
out.push_back(track_.size() & 0xFF);
std::vector<std::uint8_t> td = td_.data();
out.insert(out.end(), td.begin(), td.end());
//Track data
std::vector<std::uint8_t> tdata;
for (std::size_t i = 0; i < track_.size(); i++)
{
tdata = track_[i]->data();
out.insert(out.end(), tdata.begin(), tdata.end());
}
//Return the output
return out;
}
//Adds a track to the MIDI
void MIDI_Type2::addTrack(const Track & tr)
{
track_.push_back(tr.clone());
}
//Clears all tracks from the MIDI
void MIDI_Type2::clear()
{
for (std::size_t i = 0; i < track_.size(); i++)
{
delete track_[i];
}
track_.resize(0);
}
//Loads a midi file into the proper structure, returning a pointer to it.
//Returns NULL if the loading failed
//THIS MUST BE MANUALLY FREED BY THE USER WITH freeLoadedMIDI
/*MIDI* load(std::string filename)
{
//Open the file if possible
std::ifstream fin(filename.c_str(), std::ios::in|std::ios::binary);
if (!fin) return NULL;
//Read the header
std::uint8_t header[14];
fin.seekg(0, ios::beg);
fin.read(header, 14);
//Check the type, time division, and number of tracks
std::uint8_t type = header[9];
std::uint16_t numTracks = std::uint16_t(header[10])<<8 | header[11];
TimeDivision td(std::uint16_t(header[12])<<8 | header[13]);
//Read in each track;
std::vector<Track*> tracks;
tracks.resize(numTracks);
for (std::uint16_t i = 0; i < numTracks; i++)
{
//Read the track header
std::uint8_t t_head[8];
file.read(t_head, 8);
//Find the track size
std::uint32_t trackSize = 0;
for (int j = 0; j < 4; j++)
{
trackSize << 8;
trackSize |= t_head[4+j];
}
//Read in the track
std::vector<std::uint8_t> tr;
tr.resize(trackSize);
fin.read(&(tr[0]), trackSize);
//Put together the delta time
}
//Close the file
fin.close();
} //IT AINT DONE YET FINISH IT UP*/
inline void freeLoadedMIDI(MIDI* mid) {delete mid;}
} //Namespace