-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmf2t.c
427 lines (384 loc) · 9.65 KB
/
mf2t.c
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
/*
* mf2t
*
* Convert a MIDI file to text.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#if defined _WIN32 || defined MSDOS
#include <io.h>
#include <fcntl.h>
#include "getopt.h"
#else
#include <unistd.h>
#endif
#include <errno.h>
#include "midifile.h"
#include "version.h"
static int TrkNr;
static int TrksToDo = 1;
static int Measure, M0, Beat, Clicks;
static long T0;
/* options */
static int fold = 0; /* fold long lines */
static int notes = 0; /* print notes as a–g */
static int on0_to_off = 0; /* replace On vol=0 with Off or vv*/
static int times = 0; /* print times as Measure/beat/click */
static char *Onmsg = "On ch=%d n=%s v=%d\n";
static char *Offmsg = "Off ch=%d n=%s v=%d\n";
static char *PoPrmsg = "PoPr ch=%d n=%s v=%d\n";
static char *Parmsg = "Par ch=%d c=%d v=%d\n";
static char *Pbmsg = "Pb ch=%d v=%d\n";
static char *PrChmsg = "PrCh ch=%d p=%d\n";
static char *ChPrmsg = "ChPr ch=%d v=%d\n";
static void error(char *s)
{
if (TrksToDo <= 0)
fprintf(stderr, "Error: Garbage at end\n");
else
fprintf(stderr, "Error: %s\n", s);
}
static void prtime(void)
{
if (times) {
long m = (Mf_currtime-T0)/Beat;
printf("%ld:%ld:%ld ",
m/Measure+M0, m%Measure, (Mf_currtime-T0)%Beat);
} else
printf("%ld ",Mf_currtime);
}
static void prtext(unsigned char *p, int leng)
{
int n, c;
int pos = 25;
printf("\"");
for (n = 0; n < leng; n++) {
c = *p++;
if (fold && pos >= fold) {
printf("\\\n\t");
pos = 13; /* tab + \xab + \ */
if (c == ' ' || c == '\t') {
putchar('\\');
++pos;
}
}
switch (c) {
case '\\':
case '"':
printf("\\%c", c);
pos += 2;
break;
case '\r':
printf("\\r");
pos += 2;
break;
case '\n':
printf("\\n");
pos += 2;
break;
case '\0':
printf("\\0");
pos += 2;
break;
default:
if (c >= 0x20) {
putchar(c);
++pos;
} else {
printf("\\x%02x" , c);
pos += 4;
}
}
}
printf("\"\n");
}
static void prhex(unsigned char *p, int leng)
{
int n;
int pos = 25;
for (n = 0; n < leng; n++, p++) {
if (fold && pos >= fold) {
printf("\\\n\t%02x", *p);
pos = 14; /* tab + ab + " ab" + \ */
} else {
printf(" %02x" , *p);
pos += 3;
}
}
printf("\n");
}
static char *mknote(int pitch)
{
static char *Notes[] =
{ "c", "c#", "d", "d#", "e", "f", "f#", "g",
"g#", "a", "a#", "b" };
static char buf[20];
if (notes)
sprintf(buf, "%s%d", Notes[pitch % 12], pitch/12);
else
sprintf(buf, "%d", pitch);
return buf;
}
static void myheader(int format, int ntrks, int division)
{
if (division & 0x8000) { /* SMPTE */
times = 0; /* Can’t do beats */
printf("MFile %d %d %d %d\n",format,ntrks,
-((-(division>>8))&0xff), division&0xff);
} else
printf("MFile %d %d %d\n",format,ntrks,division);
if (format > 2) {
fprintf(stderr, "Can’t deal with format %d files\n", format);
exit (1);
}
Beat = Clicks = division;
TrksToDo = ntrks;
}
static void mytrstart(void)
{
printf("MTrk\n");
TrkNr ++;
}
static void mytrend(void)
{
printf("TrkEnd\n");
--TrksToDo;
}
static void mynoteoff();
static void mynoteon(int chan, int pitch, int vol)
{
if ((on0_to_off > 0) && vol == 0) {
mynoteoff(chan, pitch, vol);
return;
}
prtime();
printf(Onmsg, chan+1, mknote(pitch), vol);
}
static void mynoteoff(int chan, int pitch, int vol)
{
if (on0_to_off < 0) {
mynoteon(chan, pitch, 0);
return;
}
prtime();
printf(Offmsg, chan+1, mknote(pitch), vol);
}
static void mypressure(int chan, int pitch, int press)
{
prtime();
printf(PoPrmsg, chan+1, mknote(pitch), press);
}
static void myparameter(int chan, int control, int value)
{
prtime();
printf(Parmsg, chan+1, control, value);
}
static void mypitchbend(int chan, int lsb, int msb)
{
prtime();
printf(Pbmsg, chan+1, 128*msb+lsb);
}
static void myprogram(int chan, int program)
{
prtime();
printf(PrChmsg, chan+1, program);
}
static void mychanpressure(int chan, int press)
{
prtime();
printf(ChPrmsg, chan+1, press);
}
static void mysysex(int leng, char *mess)
{
prtime();
printf("SysEx");
prhex((unsigned char *)mess, leng);
}
static void mymmisc(int type, int leng, char *mess)
{
prtime();
printf("Meta 0x%02x",type);
prhex((unsigned char *)mess, leng);
}
static void mymspecial(int leng, char *mess)
{
prtime();
printf("SeqSpec");
prhex((unsigned char *)mess, leng);
}
static void mymtext(int type, int leng, char *mess)
{
static char *ttype[] = {
NULL,
"Text", /* type=0x01 */
"Copyright", /* type=0x02 */
"TrkName",
"InstrName", /* ... */
"Lyric",
"Marker",
"Cue", /* type=0x07 */
"Unrec"
};
int unrecognized = (sizeof(ttype)/sizeof(char *)) - 1;
prtime();
if (type < 1 || type > unrecognized)
printf("Meta 0x%02x ",type);
else if (type == 3 && TrkNr == 1)
printf("Meta SeqName ");
else
printf("Meta %s ",ttype[type]);
prtext((unsigned char *)mess, leng);
}
static void mymseq(int num)
{
prtime();
printf("SeqNr %d\n",num);
}
static void mymeot(void)
{
prtime();
printf("Meta TrkEnd\n");
}
static void mykeysig(int sf, int mi)
{
prtime();
printf("KeySig %d %s\n", (sf>127?sf-256:sf), (mi?"minor":"major"));
}
static void mytempo(long tempo)
{
prtime();
printf("Tempo %ld\n",tempo);
}
static void mytimesig(int nn, int dd, int cc, int bb)
{
int denom = 1;
while (dd-- > 0)
denom *= 2;
prtime();
printf("TimeSig %d/%d %d %d\n", nn,denom,cc,bb);
M0 += (Mf_currtime-T0)/(Beat*Measure);
T0 = Mf_currtime;
Measure = nn;
Beat = 4 * Clicks / denom;
}
static void mysmpte(int hr, int mn, int se, int fr, int ff)
{
prtime();
printf("SMPTE %d %d %d %d %d\n", hr, mn, se, fr, ff);
}
static void myarbitrary(int leng, char *mess)
{
prtime();
printf("Arb");
prhex ((unsigned char *)mess, leng);
}
static void initfuncs(void)
{
Mf_error = error;
Mf_getc = getchar;
Mf_header = myheader;
Mf_starttrack = mytrstart;
Mf_endtrack = mytrend;
Mf_on = mynoteon;
Mf_off = mynoteoff;
Mf_pressure = mypressure;
Mf_parameter = myparameter;
Mf_pitchbend = mypitchbend;
Mf_program = myprogram;
Mf_chanpressure = mychanpressure;
Mf_sysex = mysysex;
Mf_metamisc = mymmisc;
Mf_seqnum = mymseq;
Mf_eot = mymeot;
Mf_timesig = mytimesig;
Mf_smpte = mysmpte;
Mf_tempo = mytempo;
Mf_keysig = mykeysig;
Mf_sqspecific = mymspecial;
Mf_text = mymtext;
Mf_arbitrary = myarbitrary;
}
static void usage(void)
{
fprintf(stderr,
"mf2t v%s\n"
"Usage: mf2t [-mnbtv] [-on|-off] [-f n] [midifile [textfile]]\n\n"
"Options:\n"
" -m merge partial sysex into a single sysex message\n"
" -n write notes in symbolic form\n"
" -on replace Note On with vol=0 by Note Off\n"
" -off replace Note Off by Note On with vol=0\n"
" -b|-t write event times as bar:beat:click\n"
" -v use slightly more verbose output\n"
" -f n fold long text and hex entries at n characters\n", VERSION);
exit(1);
}
int main(int argc, char **argv)
{
int c;
Mf_nomerge = 1;
while ((c = getopt(argc, argv, "mno:btvf:h")) != -1) {
switch (c) {
case 'm':
Mf_nomerge = 0;
break;
case 'n':
notes++;
break;
case 'o':
if (*optarg == 'n')
on0_to_off = 1;
else if (*optarg == 'f')
on0_to_off = -1;
else {
fprintf(stderr, "illegal option -o%s\n", optarg);
}
break;
case 'b':
case 't':
times++;
break;
case 'v':
Onmsg = "On ch=%d note=%s vol=%d\n";
Offmsg = "Off ch=%d note=%s vol=%d\n";
PoPrmsg = "PolyPr ch=%d note=%s val=%d\n";
Parmsg = "Param ch=%d con=%d val=%d\n";
Pbmsg = "Pb ch=%d val=%d\n";
PrChmsg = "ProgCh ch=%d prog=%d\n";
ChPrmsg = "ChanPr ch=%d val=%d\n";
break;
case 'f':
fold = atoi(optarg);
break;
case 'h':
case '?':
default:
usage();
}
}
/* Filename "-" indicates stdin */
if (optind < argc && strcmp(argv[optind++], "-") != 0
&& !freopen(argv[optind - 1], "rb", stdin)) {
perror(argv[optind - 1]);
exit(1);
}
#if defined _WIN32 || defined MSDOS
setmode(fileno(stdin),O_BINARY);
#endif
/* Filename "-" indicates stdout */
if (optind < argc && strcmp(argv[optind], "-") != 0
&& !freopen(argv[optind], "w", stdout)) {
perror(argv[optind]);
exit(1);
}
initfuncs();
TrkNr = 0;
Measure = 4;
Beat = 96;
Clicks = 96;
T0 = 0;
M0 = 0;
mfread();
return 0;
}