-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
mdxinfo.c
342 lines (309 loc) · 7.81 KB
/
mdxinfo.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <dirent.h>
#include <time.h>
#include "tools.h"
#include "mdx.h"
#include "sjis.h"
#include "utf8.h"
#include "cmdline.h"
#include "md5.h"
int opt_utf8 = 0;
char *opt_output = 0;
int opt_recursive = 0;
int opt_header = 0;
int opt_opm = 0;
int opt_pcm = 0;
FILE *of;
static void print_escaped_string_utf8(uint8_t *sjis_data, int sjis_len) {
putchar('"');
int last_byte = 0;
for(int i = 0; i < sjis_len; i++) {
uint8_t b = sjis_data[i];
if(last_byte == 0 && SJIS_FIRST_CHAR(b)) {
last_byte = b;
} else {
char buf[4];
int l = utf8_encode(sjis_char_to_unicode((last_byte << 8) | b), buf);
fwrite(buf, l, 1, stdout);
last_byte = 0;
}
}
putchar('"');
}
static void print_escaped_string(uint8_t *sjis_data, int sjis_len) {
putchar('"');
int last_byte = 0;
for(int i = 0; i < sjis_len; i++) {
uint8_t b = sjis_data[i];
if(last_byte == 0 && SJIS_FIRST_CHAR(b)) {
last_byte = b;
} else {
char buf[4];
int l = 0;
if(last_byte) {
buf[0] = last_byte;
buf[1] = b;
l = 2;
} else {
buf[0] = b;
l = 1;
}
fwrite(buf, l, 1, stdout);
last_byte = 0;
}
}
putchar('"');
}
static void hexdump(FILE *f, uint8_t *data, int len) {
for(int i = 0; i < len; i++) {
fprintf(f, "%02x", data[i]);
}
}
static void run_through_file(struct mdx_file *f, int *num_cmds_out, int *pcm8_out, int *pcm_notes_out) {
*pcm8_out = 0;
memset(num_cmds_out, 0, 16 * sizeof(int));
if(pcm_notes_out) memset(pcm_notes_out, 0, 96 * sizeof(int));
for(int i = 0; i < f->num_tracks; i++) {
struct mdx_track *chan = &f->tracks[i];
// printf("%d/%d len=%d\n", i, f->num_tracks, chan->data_len);
for(int j = 0; j < chan->data_len;) {
int l = mdx_cmd_len(chan->data, j, chan->data_len - j);
//printf("cmd=%02x j=%d len=%d calculated=%d l=%d\n", chan->data[j], j, chan->data_len, chan->data_len - j, l);
if(l < 0) break;
// stop on performance end command
num_cmds_out[i]++;
if(chan->data[j] == 0xf1 && j < chan->data_len -1 && chan->data[j+1] == 0) {
break;
}
if(chan->data[j] == 0xe8) *pcm8_out = 1;
if(pcm_notes_out && i >= 8 && chan->data[j] >= 0x80 && chan->data[j] <= 0xdf) {
pcm_notes_out[chan->data[j] - 0x80]++;
}
j += l;
}
}
}
static void mdxinfo(const char *filename) {
int len = strlen(filename);
if(strcasecmp(filename + len - 4, ".mdx")) return;
struct stat st;
stat(filename, &st);
char tmbuf[100];
strftime(tmbuf, sizeof tmbuf, "%Y-%m-%d %H:%M:%S", localtime(&(st.st_mtime)));
size_t l = 0;
uint8_t *mdx_data = load_file(filename, &l);
struct mdx_file f;
int r = mdx_file_load(&f, mdx_data, l);
if(opt_opm) {
if(r != 0) goto freemdx;
for(int i = 0; i < 256; i++) {
if(f.voices[i]) {
printf("%s\t", filename);
printf("%d\t", i);
uint8_t *v = f.voices[i] + 1;
struct md5_ctx ctx;
md5_init_ctx(&ctx);
md5_process_bytes(v, 26, &ctx);
uint8_t smplmd5buf[16];
md5_finish_ctx(&ctx, smplmd5buf);
for(int j = 0; j < 16; j++) {
printf("%02x", smplmd5buf[j]);
}
printf("\t%d", *v++); // fl con
printf("\t%d", *v++); // slot mask
for(int j = 0; j < 4; j++) {
for(int k = 0; k < 6; k++) {
printf("\t%d", v[j+k*4]);
}
}
printf("\n");
}
}
} else if(opt_pcm) {
int num_cmds[16];
int pcm8 = 0;
int pcm_notes[96]; // number of times each pcm sample was played
run_through_file(&f, num_cmds, &pcm8, pcm_notes);
for(int j = 0; j < 96; j++) {
if(pcm_notes[j] > 0)
printf("%s\t%d\t%d\n", filename, j, pcm_notes[j]);
}
} else {
printf("%s\t%zu\t%zu\t%s\t%s\t", filename, l, l - f.data_start_ofs, tmbuf, mdx_error_name(r));
struct md5_ctx ctx;
uint8_t md5buf[16];
// entire file MD5
md5_init_ctx(&ctx);
md5_process_bytes(mdx_data, l, &ctx);
md5_finish_ctx(&ctx, md5buf);
for(int j = 0; j < 16; j++) {
printf("%02x", md5buf[j]);
}
printf("\t");
// data only MD5
md5_init_ctx(&ctx);
md5_process_bytes(mdx_data + f.data_start_ofs, l - f.data_start_ofs, &ctx);
md5_finish_ctx(&ctx, md5buf);
for(int j = 0; j < 16; j++) {
printf("%02x", md5buf[j]);
}
printf("\t");
if(opt_utf8) {
print_escaped_string_utf8(f.title, f.title_len);
putchar('\t');
if(f.pdx_filename_len) {
print_escaped_string_utf8(f.pdx_filename, f.pdx_filename_len);
}
putchar('\t');
if(f.pdx_filename_len) {
char pdxbuf[512], pdxutf[512];
f.pdx_filename[f.pdx_filename_len] = 0;
int ul = sjis_to_utf8(f.pdx_filename, f.pdx_filename_len, (uint8_t *)pdxutf, sizeof(pdxutf));
pdxutf[ul] = 0;
find_pdx_file(filename, pdxutf, pdxbuf, sizeof(pdxbuf));
printf("%s\t", pdxbuf);
} else printf("\t");
} else {
print_escaped_string(f.title, f.title_len);
putchar('\t');
if(f.pdx_filename_len) {
print_escaped_string(f.pdx_filename, f.pdx_filename_len);
}
putchar('\t');
if(f.pdx_filename_len) {
char pdxbuf[512];
f.pdx_filename[f.pdx_filename_len] = 0;
find_pdx_file(filename, (char *)f.pdx_filename, pdxbuf, sizeof(pdxbuf));
printf("%s\t", pdxbuf);
} else printf("\t");
}
hexdump(of, f.title, f.title_len);
putchar('\t');
hexdump(of, f.pdx_filename, f.pdx_filename_len);
putchar('\t');
if(r != 0) {
printf("\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\n");
goto freemdx;
}
int num_cmds[16];
int pcm8 = 0;
run_through_file(&f, num_cmds, &pcm8, 0);
printf("%d\t", f.num_tracks);
printf("%d\t", pcm8);
for(int i = 0; i < 16; i++) {
if(i < f.num_tracks) {
printf("%d\t", num_cmds[i]);
} else printf("\t");
}
printf("\n");
}
freemdx:
free(mdx_data);
}
static void mdxinfo_dir(const char *name, int recurse) {
DIR *dir;
struct dirent *entry;
if(!(dir = opendir(name)))
return;
while((entry = readdir(dir)) != NULL) {
char path[1024];
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
continue;
snprintf(path, sizeof(path), "%s/%s", name, entry->d_name);
#ifdef DIRENT_HAS_D_TYPE
if (entry->d_type == DT_DIR) {
#else
struct stat st;
stat(path, &st);
if(S_ISDIR(st.st_mode)) {
#endif
if(!recurse)
continue;
mdxinfo_dir(path, recurse);
} else {
mdxinfo(path);
}
}
closedir(dir);
}
int main(int argc, char **argv) {
int optind = cmdline_parse_args(argc, argv, (struct cmdline_option[]){
{
'u', "utf8",
"Convert to UTF-8",
0,
TYPE_SWITCH,
0, &opt_utf8
},
{
'o', "output",
"Output file",
"file",
TYPE_REQUIRED,
TYPE_STRING, &opt_output
},
{
'r', "recursive",
"Recurse directories",
0,
TYPE_SWITCH,
TYPE_NONE, &opt_recursive
},
{
'H', "header",
"Print header",
0,
TYPE_SWITCH,
TYPE_NONE, &opt_header
},
{
'y', "opm",
"Print OPM voices",
0,
TYPE_SWITCH,
TYPE_NONE, &opt_opm
},
{
'p', "pcm",
"Print PCM samples",
0,
TYPE_SWITCH,
TYPE_NONE, &opt_pcm
},
CMDLINE_ARG_TERMINATOR
}, 1, -1, "<file.mdx>");
if(optind < 0) exit(-optind);
of = stdout;
if(opt_output) of = fopen(opt_output, "w");
if(opt_header) {
if(opt_opm) {
printf("File\tVoice\tMD5\tFL CON\tSlot Mask");
const char *operators[] = { "M1", "M2", "C1", "C2" };
const char *regs[] = { "DT1 MUL", "TL", "KS AR", "AME D1R", "DT2 D2R", "D1L RR" };
for(int i = 0; i < 4; i++)
for(int j = 0; j < 6; j++)
printf("\t%s %s", operators[i], regs[j]);
printf("\n");
} else if(opt_pcm) {
printf("File\tSample\tTimes Used\n");
} else {
printf("File\tSize\tData size\tDate\tError\tMD5\tData MD5\tTitle\tPCM File\tPDX file\tTitle hex\tPCM File hex\tTracks\tPCM8\tA\tB\tC\tD\tE\tF\tG\tH\tP\tQ\tR\tS\tT\tU\tV\tW\n");
}
}
for(int i = optind; i < argc; i++) {
struct stat st;
stat(argv[i], &st);
if(S_ISDIR(st.st_mode))
mdxinfo_dir(argv[i], opt_recursive);
else
mdxinfo(argv[i]);
}
if(opt_output) fclose(of);
return 0;
}