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
501
502
503
504
505
506
507
508
509
510
|
/*
Encode MIDI file into CSV format
Designed and implemented in December of 1995 by John Walker.
Revised and updated by John Walker in October 1998 and
February 2004.
http://www.fourmilab.ch/
This program is in the public domain.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef _WIN32
#include <fcntl.h>
#include <io.h>
#endif
#include "version.h"
#include "types.h"
#include "midifile.h"
#include "midio.h"
#include "getopt.h"
#define FALSE 0
#define TRUE 1
static char *progname; /* Program name string */
static int verbose = FALSE; /* Debug output */
/* VLENGTH -- Parse variable length item from in-memory track */
static vlint vlength(byte **trk, long *trklen)
{
vlint value;
byte ch;
byte *cp = *trk;
trklen--;
if ((value = *cp++) & 0x80) {
value &= 0x7F;
do {
value = (value << 7) | ((ch = *cp++) & 0x7F);
trklen--;
} while (ch & 0x80);
}
#ifdef DUMP
fprintf(stderr, "Time lapse: %d bytes, %d\n", cp - *trk, value);
#endif
*trk = cp;
return value;
}
/* TEXTCSV -- Convert text field to CSV, quoting as necessary.
If CSV_Quote_ISO if defined ISO 8859-1 graphic
characters are permitted in text strings
without octal quoting. Otherwise all
characters other than 7-bit ASCII graphics are
output as octal.
Output is appended directly to output stream fo.
*/
#define CSV_Quote_ISO
static void textcsv(FILE *fo, const byte *t, const int len)
{
byte c;
int i;
putc('"', fo);
for (i = 0; i < len; i++) {
c = *t++;
if ((c < ' ') ||
#ifdef CSV_Quote_ISO
((c > '~') && (c <= 160))
#else
(c > '~')
#endif
) {
putc('\\', fo);
fprintf(fo, "%03o", c);
} else {
if (c == '"') {
putc('"', fo);
} else if (c == '\\') {
putc('\\', fo);
}
putc(c, fo);
}
}
putc('"', fo);
}
/* TRACKCSV -- Compile track into CSV written to fo. */
static void trackcsv(FILE *fo, const int trackno,
byte *trk, long trklen, const int ppq)
{
int levt = 0, evt, channel, note, vel, control, value,
type;
vlint len;
byte *titem;
vlint abstime = 0; /* Absolute time in track */
#ifdef XDD
byte *strk = trk;
#endif
while (trklen > 0) {
vlint tlapse = vlength(&trk, &trklen);
abstime += tlapse;
fprintf(fo, "%d, %ld, ", trackno, abstime);
/* Handle running status; if the next byte is a data byte,
reuse the last command seen in the track. */
if (*trk & 0x80) {
#ifdef XDD
fprintf(fo, " (Trk: %02x NS: %02X : %d) ", *trk, evt, trk - strk);
#endif
evt = *trk++;
/* One subtlety: we only save channel voice messages
for running status. System messages and file
meta-events (all of which are in the 0xF0-0xFF
range) are not saved, as it is possible to carry a
running status across them. You may have never seen
this done in a MIDI file, but I have. */
if ((evt & 0xF0) != 0xF0) {
levt = evt;
}
trklen--;
} else {
evt = levt;
#ifdef XDD
fprintf(fo, " (Trk: %02x RS: %02X : %d) ", *trk, evt, trk - strk);
#endif
}
channel = evt & 0xF;
/* Channel messages */
switch (evt & 0xF0) {
case NoteOff: /* Note off */
if (trklen < 2) {
return;
}
trklen -= 2;
note = *trk++;
vel = *trk++;
fprintf(fo, "Note_off_c, %d, %d, %d\n", channel, note, vel);
continue;
case NoteOn: /* Note on */
if (trklen < 2) {
return;
}
trklen -= 2;
note = *trk++;
vel = *trk++;
/* A note on with a velocity of 0 is actually a note
off. We do not translate it to a Note_off record
in order to preserve the original structure of the
MIDI file. */
fprintf(fo, "Note_on_c, %d, %d, %d\n", channel, note, vel);
continue;
case PolyphonicKeyPressure: /* Aftertouch */
if (trklen < 2) {
return;
}
trklen -= 2;
note = *trk++;
vel = *trk++;
fprintf(fo, "Poly_aftertouch_c, %d, %d, %d\n", channel, note, vel);
continue;
case ControlChange: /* Control change */
if (trklen < 2) {
return;
}
trklen -= 2;
control = *trk++;
value = *trk++;
fprintf(fo, "Control_c, %d, %d, %d\n", channel, control, value);
continue;
case ProgramChange: /* Program change */
if (trklen < 1) {
return;
}
trklen--;
note = *trk++;
fprintf(fo, "Program_c, %d, %d\n", channel, note);
continue;
case ChannelPressure: /* Channel pressure (aftertouch) */
if (trklen < 1) {
return;
}
trklen--;
vel = *trk++;
fprintf(fo, "Channel_aftertouch_c, %d, %d\n", channel, vel);
continue;
case PitchBend: /* Pitch bend */
if (trklen < 1) {
return;
}
trklen--;
value = *trk++;
value = value | ((*trk++) << 7);
fprintf(fo, "Pitch_bend_c, %d, %d\n", channel, value);
continue;
default:
break;
}
switch (evt) {
/* System exclusive messages */
case SystemExclusive:
case SystemExclusivePacket:
len = vlength(&trk, &trklen);
fprintf(fo, "System_exclusive%s, %lu",
evt == SystemExclusivePacket ? "_packet" : "",
len);
{
vlint i;
for (i = 0; i < len; i++) {
fprintf(fo, ", %d", *trk++);
}
fprintf(fo, "\n");
}
break;
/* File meta-events */
case FileMetaEvent:
if (trklen < 2) {
return;
}
trklen -= 2;
type = *trk++;
len = vlength(&trk, &trklen);
titem = trk;
trk += len;
trklen -= len;
switch (type) {
case SequenceNumberMetaEvent:
fprintf(fo, "Sequence_number, %d\n", (titem[0] << 8) | titem[1]);
break;
case TextMetaEvent:
#ifdef XDD
fprintf(fo, " (Len=%ld Trk=%02x) ", len, *trk);
#endif
fputs("Text_t, ", fo);
textcsv(fo, titem, len);
putc('\n', fo);
break;
case CopyrightMetaEvent:
fputs("Copyright_t, ", fo);
textcsv(fo, titem, len);
putc('\n', fo);
break;
case TrackTitleMetaEvent:
fputs("Title_t, ", fo);
textcsv(fo, titem, len);
putc('\n', fo);
break;
case TrackInstrumentNameMetaEvent:
fputs("Instrument_name_t, ", fo);
textcsv(fo, titem, len);
putc('\n', fo);
break;
case LyricMetaEvent:
fputs("Lyric_t, ", fo);
textcsv(fo, titem, len);
putc('\n', fo);
break;
case MarkerMetaEvent:
fputs("Marker_t, ", fo);
textcsv(fo, titem, len);
putc('\n', fo);
break;
case CuePointMetaEvent:
fputs("Cue_point_t, ", fo);
textcsv(fo, titem, len);
putc('\n', fo);
break;
case ChannelPrefixMetaEvent:
fprintf(fo, "Channel_prefix, %d\n", titem[0]);
break;
case PortMetaEvent:
fprintf(fo, "MIDI_port, %d\n", titem[0]);
break;
case EndTrackMetaEvent:
fprintf(fo, "End_track\n");
trklen = -1;
break;
case SetTempoMetaEvent:
fprintf(fo, "Tempo, %d\n", (titem[0] << 16) |
(titem[1] << 8) | titem[2]);
break;
case SMPTEOffsetMetaEvent:
fprintf(fo, "SMPTE_offset, %d, %d, %d, %d, %d\n",
titem[0], titem[1], titem[2], titem[3], titem[4]);
break;
case TimeSignatureMetaEvent:
fprintf(fo, "Time_signature, %d, %d, %d, %d\n",
titem[0], titem[1], titem[2], titem[3]);
break;
case KeySignatureMetaEvent:
fprintf(fo, "Key_signature, %d, \"%s\"\n", ((signed char) titem[0]),
titem[1] ? "minor" : "major");
break;
case SequencerSpecificMetaEvent:
fprintf(fo, "Sequencer_specific, %lu", len);
{
vlint i;
for (i = 0; i < len; i++) {
fprintf(fo, ", %d", titem[i]);
}
fprintf(fo, "\n");
}
break;
default:
if (verbose) {
fprintf(stderr, "Unknown meta event type 0x%02X, %ld bytes of data.\n",
type, len);
}
fprintf(fo, "Unknown_meta_event, %d, %lu", type, len);
{
vlint i;
for (i = 0; i < len; i++) {
fprintf(fo, ", %d", titem[i]);
}
fprintf(fo, "\n");
}
break;
}
break;
default:
if (verbose) {
fprintf(stderr, "Unknown event type 0x%02X.\n", evt);
}
fprintf(fo, "Unknown_event, %02Xx\n", evt);
break;
}
}
}
/* Main program. */
int main(int argc, char *argv[])
{
struct mhead mh;
FILE *fp, *fo;
long track1;
int i, n, track1l;
fp = stdin;
fo = stdout;
progname = argv[0];
while ((n = getopt(argc, argv, "uv")) != -1) {
switch (n) {
case 'u':
fprintf(stderr, "Usage: %s [ options ] [ midi_file ] [ csv_file ]\n", progname);
fprintf(stderr, " Options:\n");
fprintf(stderr, " -u Print this message\n");
fprintf(stderr, " -v Verbose: dump header and track information\n");
fprintf(stderr, "Version %s\n", VERSION);
return 0;
case 'v':
verbose = TRUE;
break;
case '?':
fprintf(stderr, "%s: undefined option -%c specified.\n",
progname, n);
return 2;
}
}
i = 0;
while (optind < argc) {
switch (i++) {
case 0:
if (strcmp(argv[optind], "-") != 0) {
fp = fopen(argv[optind], "rb");
if (fp == NULL) {
fprintf(stderr, "%s: Unable to to open MIDI input file %s\n",
progname, argv[optind]);
return 2;
}
}
break;
case 1:
if (strcmp(argv[optind], "-") != 0) {
fo = fopen(argv[optind], "w");
if (fo == NULL) {
fprintf(stderr, "%s: Unable to to create CSV output file %s\n",
progname, argv[optind]);
return 2;
}
}
break;
}
optind++;
}
#ifdef _WIN32
/* If input is from standard input, set the input file
mode to binary. */
if (fp == stdin) {
_setmode(_fileno(fp), _O_BINARY);
}
#endif
/* Read and validate header */
readMidiFileHeader(fp, &mh);
if (memcmp(mh.chunktype, "MThd", sizeof mh.chunktype) != 0) {
fprintf(stderr, "%s is not a Standard MIDI File.\n", argv[1]);
return 2;
}
if (verbose) {
fprintf(stderr, "Format %d MIDI file. %d tracks, %d ticks per quarter note.\n",
mh.format, mh.ntrks, mh.division);
}
/* Output header */
fprintf(fo, "0, 0, Header, %d, %d, %d\n", mh.format, mh.ntrks, mh.division);
/* Process tracks */
for (i = 0; i < mh.ntrks; i++) {
struct mtrack mt;
byte *trk;
if (i == 0) {
track1 = ftell(fp);
}
readMidiTrackHeader(fp, &mt);
if (memcmp(mt.chunktype, "MTrk", sizeof mt.chunktype) != 0) {
fprintf(stderr, "Track %d header is invalid.\n", i + 1);
return 2;
}
if (verbose) {
fprintf(stderr, "Track %d: length %ld.\n", i + 1, mt.length);
}
fprintf(fo, "%d, 0, Start_track\n", i + 1);
trk = (byte *) malloc(mt.length);
if (trk == NULL) {
fprintf(stderr, "%s: Cannot allocate %ld bytes for track.\n",
progname, mt.length);
return 2;
}
fread((char *) trk, (int) mt.length, 1, fp);
if (i == 0) {
track1l = (int) (ftell(fp) - track1);
}
trackcsv(fo, i + 1, trk, mt.length, mh.division);
free(trk);
}
fprintf(fo, "0, 0, End_of_file\n");
return 0;
}
|