-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMidiFile.cpp
More file actions
500 lines (448 loc) · 16.2 KB
/
MidiFile.cpp
File metadata and controls
500 lines (448 loc) · 16.2 KB
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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
#include <stdio.h>
#include <string.h>
#include "MidiFile.h"
MidiFile::MidiFile()
{
}
unsigned short MidiFile::read_2b_integer(FILE *fp)
{
unsigned char buf[2];
unsigned char bufb[2];
int r = fread(buf, 1, 2, fp);
if (r == 2)
{
if (debug) printf("integer in 2 bytes: %x %x\r\n", buf[0], buf[1]);
bufb[0] = buf[1];
bufb[1] = buf[0];
unsigned short *ps = (unsigned short *) bufb;
if (debug) printf("integer %u\n", *ps);
return *ps;
}
return 0;
}
unsigned long MidiFile::read_3b_integer(FILE *fp)
{
unsigned char buf[3];
//unsigned char bufb[4];
int r = fread(buf, 1, 3, fp);
if (r == 3)
{
if (debug) printf("length in 3 bytes: %x %x %x\r\n", buf[0], buf[1], buf[2]);
/*
bufb[0] = buf[2];
bufb[1] = buf[1];
bufb[2] = buf[0];
bufb[3] = 0;
unsigned long *pl = (unsigned long *) bufb;
*/
// This conversion is architecture safe.
unsigned long pl = 256*256*buf[0] + 256*buf[1] + buf[2];
if (debug) printf("length %lu\n", pl);
return pl;
}
return 0;
}
unsigned long MidiFile::read_4b_length(FILE *fp)
{
char buf[4];
char bufb[4];
int r = fread(buf, 1, 4, fp);
if (r == 4)
{
if (debug) printf("length in 4 bytes: %x %x %x %x\r\n", buf[0], buf[1], buf[2], buf[3]);
bufb[0] = buf[3];
bufb[1] = buf[2];
bufb[2] = buf[1];
bufb[3] = buf[0];
unsigned long *pl = (unsigned long *) bufb;
if (debug) printf("length %lu\n", *pl);
return *pl;
}
return 0;
}
unsigned char MidiFile::read_char(FILE *fp)
{
unsigned char c;
int r = 0;
if (stabuf.get_size() > 0)
{
c = stabuf.pop();
r = 1;
}
else
{
r = fread(&c, 1, 1, fp);
}
if (debug) printf("char in %d byte: %x\r\n", r, c);
return c;
}
unsigned int MidiFile::read_var_len_val(FILE *fp)
{
unsigned int value = 0;
unsigned char c;
bool reading = true;
fread(&c, 1, 1, fp);
if (debug) printf(" bytea %x\r\n", c);
while (reading)
{
value = (value << 7) + (c & 0x7f);
if ((c & 0x80) != 0)
{
fread(&c, 1, 1, fp);
if (debug) printf(" byteb %x\r\n", c);
}
else
{
reading = false;
}
}
if (debug) printf("read var len value %d\r\n", value);
return value;
}
unsigned int MidiFile::delta_to_ms(unsigned int dlt)
{
//printf("tempo upb %d %d\r\n", tempo, upb);
double tick_ms = ((double) tempo)/((double) upb);
tick_ms /= 1000.0; // convert µs to ms
return (unsigned int)(((double) dlt) * tick_ms);
}
ParserResult MidiFile::parse(const char *fn, std::function<void(unsigned int, unsigned char, unsigned char, unsigned char)> notefu)
{
if (debug) printf("start midireader\r\n");
FILE *fp = fopen(fn, "r");
if (fp == NULL)
{
if (debug) printf("error at fopen\r\n");
return parser_openerror;
}
if (debug) printf("fopen ok\r\n");
char buf[20];
int r = fread(buf, 1, 4, fp);
if (r == 4)
{
if (debug) printf("fread first 4 bytes ok\r\n");
buf[4] = '\0';
if (debug) printf("type %s\r\n", buf);
if (strcmp(buf, "MThd") == 0)
{
if (debug) printf("is header\r\n");
total_length = read_4b_length(fp);
format = read_2b_integer(fp);
ntracks = read_2b_integer(fp);
upb = read_2b_integer(fp); // units per beat
if (debug) printf("format %d\r\n", format);
if (debug) printf("ntracks %d\r\n", ntracks);
if (debug) printf("upb %d\r\n", upb);
int r = fread(buf, 1, 4, fp);
if (r == 4)
{
if (debug) printf("fread track 4 bytes ok\r\n");
buf[4] = '\0';
if (debug) printf("type %s\r\n", buf);
if (strcmp(buf, "MTrk") == 0)
{
if (debug) printf("is track\r\n");
unsigned long trlen = read_4b_length(fp);
if (debug) printf("track length in bytes %ld\r\n", trlen);
unsigned int delta = read_var_len_val(fp);
unsigned char type = read_char(fp);
bool reading = true;
while (reading)
{
if (debug) printf("\r\n\r\ntype %x\r\n", type);
if (type == 0xff)
{
stabuf.clear();
if (debug) printf("meta type\r\n");
unsigned char subtype = read_char(fp);
if (debug) printf("subtype %x\r\n", subtype);
unsigned int melen;
melen = read_var_len_val(fp);
if (debug) printf("melen %d\r\n", melen);
switch (subtype)
{
case 0x00:
// sequence number
if (melen == 2)
{
// read only if present
seqnr = read_2b_integer(fp);
}
notefu(delta, type, 0, 0);
break;
case 0x01:
// text
for (unsigned int i = 0; i<melen; i++)
{
text[i] = read_char(fp);
}
text[melen] = '\0';
if (debug) printf("text %s\r\n", text);
notefu(delta, type, 0, 0);
break;
case 0x02:
// copyright notice
for (unsigned int i = 0; i<melen; i++)
{
read_char(fp);
}
notefu(delta, type, 0, 0);
break;
case 0x03:
// track name
for (unsigned int i = 0; i<melen; i++)
{
track_name[i] = read_char(fp);
}
track_name[melen] = '\0';
if (debug) printf("track name %s\r\n", track_name);
notefu(delta, type, 0, 0);
break;
case 0x04:
// instrument name
for (unsigned int i = 0; i<melen; i++)
{
read_char(fp);
}
notefu(delta, type, 0, 0);
break;
case 0x05:
// lyric text
for (unsigned int i = 0; i<melen; i++)
{
read_char(fp);
}
notefu(delta, type, 0, 0);
break;
case 0x06:
// marker text
for (unsigned int i = 0; i<melen; i++)
{
read_char(fp);
}
notefu(delta, type, 0, 0);
break;
case 0x07:
// cue point
for (unsigned int i = 0; i<melen; i++)
{
read_char(fp);
}
notefu(delta, type, 0, 0);
break;
case 0x08:
// Program name
for (unsigned int i = 0; i<melen; i++)
{
read_char(fp);
}
notefu(delta, type, 0, 0);
break;
case 0x09:
// Device name
for (unsigned int i = 0; i<melen; i++)
{
read_char(fp);
}
notefu(delta, type, 0, 0);
break;
case 0x20:
// midi channel prefix assignment
read_char(fp);
notefu(delta, type, 0, 0);
break;
case 0x21:
// midi port
read_char(fp);
notefu(delta, type, 0, 0);
break;
case 0x2f:
// track end
reading = false;
break;
case 0x51:
// tempo
tempo = read_3b_integer(fp);
if (debug) printf("tempo %lu\r\n", tempo);
// calculate the bpm only for test
//if (debug) printf(" bpm %3.1lf\r\n", 1.0/tempo*1000000.0*60.0);
notefu(delta, type, 0, 0);
break;
case 0x54:
// SMPTE offset, fixed size = 5 bytes
for (unsigned int i = 0; i<melen; i++)
{
read_char(fp);
}
notefu(delta, type, 0, 0);
break;
case 0x58:
// time signature
if (debug) printf("time signature\r\n");
tsig_num = read_char(fp);
tsig_den = read_char(fp);
mtro_tic = read_char(fp); // number of clock ticks per metronome
not_32nd = read_char(fp);
if (debug) printf("time signature %d %d %d %d\r\n",
tsig_num, tsig_den, mtro_tic, not_32nd);
notefu(delta, type, 0, 0);
break;
case 0x59:
// key signature
sf = read_char(fp);
mi = read_char(fp);
break;
notefu(delta, type, 0, 0);
case 0x7f:
// Sequencer specific event
for (unsigned int i = 0; i<melen; i++)
{
read_char(fp);
}
notefu(delta, type, 0, 0);
break;
default:
// wrong subtype
fclose(fp);
return parser_subtypeerror;
}
}
else
if ((type & 0xf0) == 0x80 || (type & 0xf0) == 0x90)
{
// note on/off
unsigned char note = read_char(fp);
unsigned char velo = read_char(fp);
if (debug) printf("note on/off %d %d\r\n", note, velo);
notefu(delta, type, note, velo);
stabuf.clear_set(type);
}
else
if ((type & 0xf0) == 0xa0)
{
// polyphonic pressure
read_char(fp);
read_char(fp);
notefu(delta, type, 0, 0);
stabuf.clear_set(type);
}
else
if ((type & 0xf0) == 0xb0)
{
// controller
unsigned char note = read_char(fp);
unsigned char velo = read_char(fp);
if (debug) printf("controller %d %d\r\n", note, velo);
notefu(delta, type, note, velo);
stabuf.clear_set(type);
}
else
if ((type & 0xf0) == 0xc0)
{
// program change
unsigned char cha = read_char(fp);
if (debug) printf("program change %x\r\n", cha);
notefu(delta, type, 0, 0);
stabuf.clear_set(type);
}
else
if ((type & 0xf0) == 0xd0)
{
// channel pressure
read_char(fp);
notefu(delta, type, 0, 0);
stabuf.clear_set(type);
}
else
if ((type & 0xf0) == 0xe0)
{
// pitch bend
read_char(fp);
read_char(fp);
notefu(delta, type, 0, 0);
stabuf.clear_set(type);
}
else
if ((type & 0xf0) == 0xf0)
{
// single SysEx
unsigned int melen;
melen = read_var_len_val(fp);
if (debug) printf("melen %d\r\n", melen);
for (unsigned int i = 0; i<melen; i++)
{
read_char(fp);
}
notefu(delta, type, 0, 0);
stabuf.clear_set(type);
}
else
if ((type & 0xf0) == 0xf7)
{
// escaped single SysEx
unsigned int melen;
melen = read_var_len_val(fp);
if (debug) printf("melen %d\r\n", melen);
for (unsigned int i = 0; i<melen; i++)
{
read_char(fp);
}
notefu(delta, type, 0, 0);
stabuf.clear_set(type);
}
else
{
// This is the Running Status situation.
// Keep the type as the 1st char.
stabuf.push(type);
if (debug) printf("reuse type\r\n");
//reading = false;
}
if (reading)
{
if (stabuf.get_size() == 0)
{
// Normal situation
delta = read_var_len_val(fp);
if (debug) printf("next delta %u\r\n", delta);
type = read_char(fp);
if (debug) printf("next type %x\r\n", type);
}
else
{
// Status Running situation
// delta is not changed.
// Take the previous type.
type = stabuf.get_type();
}
}
} // while
fclose(fp);
return parser_ok;
}
else
{
return parser_trackerror;
}
}
else
{
// less than 4 bytes in file
if (debug) printf("wrong track in first 4 bytes\r\n");
return parser_shorterror;
}
}
else
{
if (debug) printf("wrong header\n");
return parser_headererror;
}
}
else
{
// file too short, less than 4 bytes
if (debug) printf("file too short\n");
fclose(fp);
return parser_shorterror;
}
}