-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmmtdecod.c
More file actions
587 lines (472 loc) · 11.6 KB
/
mmtdecod.c
File metadata and controls
587 lines (472 loc) · 11.6 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
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
/* MMTDECOD.C - decode and print out mmt8 data */
/* (c) Copyright 2001 Ricard Wanderlof
/* V1.0 RW 0011?? */
/* V1.1 RW 010828 Small model version */
/* V1.2 RW 010906 Rebuilt for .mmp and .mms files */
/* V1.3 RW 010914 Reversed track order for compatibility with mmtencod */
/* V1.31 RW 010925 Fixed bug in tracknums */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <alloc.h>
#ifdef __TURBOC__
#define FAR far
#define MALLOC farmalloc
#define FREE farfree
#define CORELEFT farcoreleft
#else
#define FAR
#define MALLOC malloc
#define FREE free
#define CORELEFT coreleft
#endif
#define FAIL 1 /* general status return code */
#define OK 0
/* MMT-8 stuff */
#define SYSEX 240 /* MIDI sysex code */
#define EOX 247 /* MIDI eox code */
#define ALESIS_SYX 0x0E /* Alesis sysex ID */
#define MMT8_SYX 0x00 /* MMT8 ID */
#define MMT8_PARTS 100 /* # parts */
#define MMT8_SONGS 100 /* # songs */
#define MMT8_TRACKS 8 /* # tracks */
#define MMT8_NAMELEN 14 /* # chars */
#define MMT8_PPQN 96
#define MMT8_MEMSTART 0x0400
#define MMT8_MEMEND 0xFF00
#define MMT8_MEMSIZE (MMT8_MEMEND-MMT8_MEMSTART) /* memory / max dump */
#define ALLOCSIZE (MMT8_MEMSIZE+10) /* size of MMT8 memory + some extra */
typedef unsigned int uint;
typedef unsigned char uchar;
typedef enum { NONE, PART, SONG } nametype;
typedef struct
{
uint partptrs[MMT8_PARTS];
char dummy1[0xCF-0xC8];
uint freememptr;
char dummy2[0xD3-0xD1];
uint freememsize;
char dummy3[0x102-0xD5];
uint songptrs[MMT8_SONGS];
/* char dummy4[0x200-0x1CA]; */
} datahead;
typedef struct
{
uint bytes;
uint trackoffset[MMT8_TRACKS];
uint bcdbeats;
char m_channel[MMT8_TRACKS];
char name[MMT8_NAMELEN];
} parthead;
typedef struct
{
uchar number;
uint start;
uchar amount;
uchar ch;
uint dura;
} long_ev;
typedef struct
{
uchar number;
uchar amount;
uchar ch;
uint dura;
} short_ev;
typedef union
{
short_ev five;
long_ev seven;
} ev;
typedef struct
{
uint bytes;
uchar tempo;
char name[MMT8_NAMELEN];
} songhead;
uchar FAR *buffer;
uint bufbytes;
uint Allocsize;
FILE *infile;
FILE *outfile;
void farcpy(char FAR *dest, char FAR *src, int len)
{
while (len--)
*dest++ = *src++;
}
uint byteswap(uint d)
{
return (d << 8) | ((d >> 8) & 255);
}
char yesno(char *prompt)
{
char yes;
printf("%s (y/n) ?", prompt);
yes = (toupper(getche()) == 'Y');
printf("\n");
return yes;
}
char r_getbyte(int *c)
{
int ch;
ch = getc(infile);
if (ch == EOF)
{
printf("Unexpected EOF\n");
return FAIL;
}
*c = ch;
return OK;
}
char r_expect(int expected, const char *errstring)
{
int c;
if (!r_getbyte(&c))
{
if (c == expected)
return OK;
else
printf(errstring, c);
}
return FAIL;
}
char r_pshead(nametype *ntype)
{
static char *err = "Wrong file type!\n";
int ch;
if (r_expect('M', err)) return FAIL;
if (r_expect('M', err)) return FAIL;
if (r_expect('T', err)) return FAIL;
if (r_getbyte(&ch)) return FAIL;
switch (ch)
{
case 'P': *ntype = PART; break;
case 'S': *ntype = SONG; break;
default: printf(err); return FAIL;
}
return OK;
}
uint r_pslen(void)
{
int ch;
int len;
if (r_getbyte(&len)) return 0;
if (r_getbyte(&ch)) return 0;
len |= (ch << 8);
return len;
}
char r_psbuf(char FAR *buf, uint len)
{
int ch;
while (len--)
{
if (r_getbyte(&ch)) return FAIL;
*buf++ = ch;
}
return OK;
}
char r_openfile(char *filename, char *mode)
{
printf("Opening input file %s ...\n", filename);
infile = fopen(filename, mode);
printf(infile ? "Reading file ... \n" : "Can't open!\n");
return infile == NULL;
}
char w_openfile(char *filename, char *mode)
{
printf("Creating output file %s ...\n", filename);
outfile = fopen(filename, "rb");
if (outfile)
{
fclose(outfile);
if (!yesno("File exists, overwrite"))
return FAIL;
}
outfile = fopen(filename, mode);
printf(outfile ? "Writing file ...\n" : "Can't create file!\n");
return outfile == NULL;
}
void r_closefile(void)
{
if (infile)
fclose(infile);
infile = NULL;
}
void w_closefile(void)
{
if (outfile)
fclose(outfile);
outfile = NULL;
}
/* decode routines */
int bendamt(uint val)
{
val = (val & 0x7F) | ((val & 0x7F00) >> 1);
return (int) val - 8192;
}
char *printev(uchar number, uchar ch, uchar amount, uint dura)
{
char *retval = NULL;
if (number == 0x80) /* END = 0x80 */
{
fprintf(outfile, "END");
if (amount != 0 | ch != 0x80 | dura != 0)
retval = "Bad end format";
}
else /* regular event */
{
number &= 127; /* was if &0x80, &= start = 65k */
if (amount & 128) /* non-note */
{
amount &= 127;
if (number <= 121) /* controller */
fprintf(outfile, "CH %-2d CTRL %-3d VAL %-3d ", ch+1, number, amount);
else
switch (number)
{
case 122:
fprintf(outfile, "CH %-2d PRGM %-3d ", ch+1, amount);
break;
case 123:
fprintf(outfile, "CH %-2d AFTR %-3d ", ch+1, amount);
break;
case 124:
fprintf(outfile, "CH %-2d BEND %-5d ", ch+1, bendamt(dura));
break;
case 125:
fprintf(outfile, "SYSX %-3d ", amount);
if (ch & 128) fprintf(outfile, "EOX ");
else
{
fprintf(outfile, "%-3d ", ch & 127);
if (dura & 32768U) fprintf(outfile, "EOX ");
else fprintf(outfile, "%-3d ", (dura >> 8) & 127);
}
break;
default:
retval = "Unrecognized event type";
break;
}
}
else /* note */
{
dura = byteswap(dura);
fprintf(outfile, "CH %-2d NOTE %-3d VEL %-3d DURA %3u/%-3u ",
ch+1, number, amount, dura / MMT8_PPQN, dura % MMT8_PPQN);
}
} /* endif-else regular event */
fprintf(outfile, "\n");
return retval;
}
void printtrack(uchar FAR *trackptr, int number, int ch, uint len)
{
ev FAR *eventptr;
char *error;
uint start;
fprintf(outfile, "TRACK %d CH %d\n", number, ch);
fprintf(outfile, "; len %u bytes\n", len);
do
{
eventptr = (ev FAR *) trackptr;
if (*trackptr & 0x80) /* long event type */
{
if (len < 7)
{
fprintf(outfile, "; Not enough track length for long event.\n");
return;
}
len -= 7;
trackptr += 7;
start = eventptr->seven.start;
fprintf(outfile, "AT %3u/%-4u", start / MMT8_PPQN, start % MMT8_PPQN);
error = printev(eventptr->seven.number, eventptr->seven.ch,
eventptr->seven.amount, eventptr->seven.dura);
if (error)
{
fprintf(outfile, "; %s (%02Xh %02Xh %02Xh %02Xh %02Xh %02Xh %02Xh)\n",
trackptr[0], trackptr[1], trackptr[2], trackptr[3],
trackptr[4], trackptr[5], trackptr[6]);
}
}
else /* short event type */
{
if (len < 5)
{
fprintf(outfile, "; Not enough track length for short event.\n");
return;
}
len -= 5;
trackptr += 5;
fprintf(outfile, "%11s", "");
error = printev(eventptr->five.number, eventptr->five.ch,
eventptr->five.amount, eventptr->five.dura);
if (error)
{
fprintf(outfile, "; %s (%02Xh %02Xh %02Xh %02Xh %02Xh)\n",
trackptr[0], trackptr[1], trackptr[2], trackptr[3],
trackptr[4]);
}
}
} while (eventptr->five.number != 0x80); /* end */
}
char *namestring(char FAR *name)
{
static char str[MMT8_NAMELEN+1];
farcpy(str, name, MMT8_NAMELEN);
str[MMT8_NAMELEN] = '\0';
return str;
}
uint beats(uint bcdbeats)
{
return (bcdbeats & 0x0F) + ((bcdbeats & 0xF0) >> 4) * 10 +
((bcdbeats & 0xF00) >> 8) * 100;
}
/* Create tracks specifier string for song step */
char *tracknums(char tracks)
{
int i;
int bit = 1;
static char buf[MMT8_TRACKS+1];
for (i = 0; i < 8; i++)
{
buf[i] = (tracks & bit) ? '1'+i : '-';
bit <<= 1;
}
buf[i] = '\0';
return buf;
}
void printpart(parthead FAR *partptr)
{
int track;
fprintf(outfile, "PART NAME \"%s\" BEATS %u\n",
namestring(partptr->name), beats(partptr->bcdbeats)-1);
fprintf(outfile, "; len %u bytes\n", partptr->bytes);
for (track = MMT8_TRACKS; track > 0; track--)
printtrack(&buffer[partptr->trackoffset[MMT8_TRACKS - track]],
track, partptr->m_channel[MMT8_TRACKS - track],
((track == 1) ? partptr->bytes :
partptr->trackoffset[MMT8_TRACKS+1-track]) -
partptr->trackoffset[MMT8_TRACKS - track]);
}
void printsong(songhead FAR *songptr)
{
uchar FAR *data;
int len = songptr->bytes;
fprintf(outfile, "SONG NAME \"%s\" TEMPO %u\n",
namestring(songptr->name), songptr->tempo);
fprintf(outfile, "; len %u bytes\n", len);
len -= sizeof(songhead);
data = ((char FAR *) songptr) + sizeof(songhead);
while (len--)
{
if (data[0] == 255)
{
fprintf(outfile, "END\n");
if (len)
{
fprintf(outfile, "; premature song end\n");
break;
}
}
else
{
if (len < 1)
{
fprintf(outfile, "; not enough song length for event\n");
break;
}
len--;
fprintf(outfile, "PART %2u TRACKS %s\n", data[0], tracknums(data[1]));
data += 2;
}
}
if (data[0] != 255)
fprintf(outfile, "; song end missing\n");
}
int main(int argc, char **argv)
{
int i;
char *infilename = NULL;
char *outfilename = NULL;
nametype ntype;
long mem;
ntype = NONE;
printf("== mmtdecod - read and interpret MMT-8 data V1.31 RW 010925\n==\n");
for (i = 1; i < argc; i++)
{
if (argv[i][0] == '-')
{
switch (toupper(argv[i][1]))
{
case 'H':
fprintf(stderr, "Usage: mmtread [options] <infile> [<outfile>]\n");
fprintf(stderr, "Options: -h this list\n");
exit(0);
break;
default:
fprintf(stderr, "Unknown option: %s\n", argv[i]);
exit(1);
break;
}
}
else if (infilename == NULL)
infilename = argv[i];
else if (outfilename == NULL)
outfilename = argv[i];
} /* eofor args */
mem = CORELEFT()-1000;
if (mem < ALLOCSIZE)
{
printf("Warning: only %ld bytes for buf!\n", mem);
Allocsize = (uint) mem;
}
else
Allocsize = ALLOCSIZE;
buffer = MALLOC(Allocsize);
if (buffer == NULL)
{
fprintf(stderr, "Buffer allocation failed!\n");
exit(1);
}
if (infilename)
{
if (r_openfile(infilename, "rb"))
exit(1);
}
else
{
fprintf(stderr, "No input file specified!\n");
exit(1);
}
if (r_pshead(&ntype) || (bufbytes = r_pslen()) == 0)
exit(2);
if (bufbytes > Allocsize)
{
printf("File size (%u) too long for buffer (%u)!\n",
bufbytes, Allocsize);
exit(2);
}
((parthead FAR *) buffer)->bytes = bufbytes; /* same for songhead */
if (r_psbuf(buffer+2, bufbytes-2))
exit(2);
r_closefile();
printf("Read %u bytes of %s data.\n",
bufbytes, ntype == PART ? "part" : "song");
if (outfilename)
{
printf("Writing output file...\n");
if (w_openfile(outfilename, "wt"))
exit(2);
}
else
outfile = stdout;
if (ntype == PART)
printpart((parthead FAR *) buffer);
else
printsong((songhead FAR *) buffer);
w_closefile();
return 0;
}
/**************************** END OF FILE *********************************/