forked from vgmrips/vgmtools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdro2vgm.c
447 lines (391 loc) · 9.34 KB
/
dro2vgm.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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
// dro2vgm.c - DosBox RAW OPL -> VGM Converter
//
// TODO: - option to disable the DROv1 Delay Hack
// - detect Reg 0x105 = 1 with DualOPL2 and set to OPL3
// (not really neccessary, as my DosBOX patch alredy does that)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "stdtype.h"
#include "stdbool.h"
#include "VGMFile.h"
#include "common.h"
static bool OpenDROFile(const char* FileName);
static void WriteVGMFile(const char* FileName);
static void ConvertDRO2VGM(void);
typedef struct dro_file_header
{
char cSignature[0x08];
UINT16 iVersionMajor;
UINT16 iVersionMinor;
} DRO_HEADER;
typedef struct dro_version_header_1
{
UINT32 iLengthMS;
UINT32 iLengthBytes;
UINT32 iHardwareType;
} DRO_VER_HEADER_1;
typedef struct dro_version_header_2
{
UINT32 iLengthPairs;
UINT32 iLengthMS;
UINT8 iHardwareType;
UINT8 iFormat;
UINT8 iCompression;
UINT8 iShortDelayCode;
UINT8 iLongDelayCode;
UINT8 iCodemapLength;
} DRO_VER_HEADER_2;
#define FCC_DRO1 0x41524244 // 'DBRA'
#define FCC_DRO2 0x4C504F57 // 'ROPL'
// -> "DBRAWOPL"
DRO_HEADER DROHead;
DRO_VER_HEADER_2 DROInf;
UINT8* DROCodemap;
VGM_HEADER VGMHead;
UINT32 DRODataLen;
UINT8* DROData;
UINT32 DROPos;
UINT32 DRODataStart;
UINT32 VGMDataLen;
UINT8* VGMData;
UINT32 VGMPos;
char FileBase[0x100];
int main(int argc, char* argv[])
{
int argbase;
int ErrVal;
char FileName[0x100];
printf("DRO to VGM Converter\n--------------------\n\n");
ErrVal = 0;
argbase = 1;
printf("File Name:\t");
if (argc <= argbase + 0)
{
ReadFilename(FileName, sizeof(FileName));
}
else
{
strcpy(FileName, argv[argbase + 0]);
printf("%s\n", FileName);
}
if (! strlen(FileName))
return 0;
if (! OpenDROFile(FileName))
{
printf("Error opening the file!\n");
ErrVal = 1;
goto EndProgram;
}
printf("\n");
ConvertDRO2VGM();
strcpy(FileName, FileBase);
strcat(FileName, ".vgm");
WriteVGMFile(FileName);
free(DROData);
free(VGMData);
EndProgram:
DblClickWait(argv[0]);
return ErrVal;
}
static bool OpenDROFile(const char* FileName)
{
FILE* hFile;
UINT32 fccHeader;
UINT32 CurPos;
UINT16 TempSht;
UINT32 TempLng;
char* TempPnt;
DRO_VER_HEADER_1 DRO_V1;
hFile = fopen(FileName, "rb");
if (hFile == NULL)
return false;
fseek(hFile, 0x00, SEEK_SET);
fread(&fccHeader, 0x01, 0x04, hFile);
if (fccHeader != FCC_DRO1)
goto OpenErr;
fread(&fccHeader, 0x01, 0x04, hFile);
if (fccHeader != FCC_DRO2)
goto OpenErr;
fseek(hFile, 0x00, SEEK_END);
DRODataLen = ftell(hFile);
// Read Data
DROData = (UINT8*)malloc(DRODataLen);
if (DROData == NULL)
goto OpenErr;
fseek(hFile, 0x00, SEEK_SET);
DRODataLen = fread(DROData, 0x01, DRODataLen, hFile);
CurPos = 0x00;
memcpy(&DROHead, &DROData[0x00], sizeof(DRO_HEADER));
CurPos += sizeof(DRO_HEADER);
memcpy(&TempLng, &DROData[0x08], 0x04);
if (TempLng & 0xFF00FF00)
{
// DosBox Version 0.61
// this version didn't contain Version Bytes
CurPos = 0x08;
DROHead.iVersionMajor = 0x00;
DROHead.iVersionMinor = 0x00;
}
else if (! (TempLng & 0x0000FFFF))
{
// DosBox Version 0.63
// the order of the Version Bytes is swapped in this version
memcpy(&TempSht, &DROData[0x0A], 0x02);
if (TempSht == 0x01)
{
memcpy(&DROHead.iVersionMinor, &DROData[0x08], 0x02);
DROHead.iVersionMajor = TempSht;
}
}
switch(DROHead.iVersionMajor)
{
case 0: // Version 0 (DosBox Version 0.61)
case 1: // Version 1 (DosBox Version 0.63)
switch(DROHead.iVersionMajor)
{
case 0: // Version 0
memcpy(&DRO_V1, &DROData[CurPos + 0x00], 0x08);
DRO_V1.iHardwareType = DROData[CurPos + 0x08];
CurPos += 0x09;
break;
case 1: // Version 1
memcpy(&DRO_V1, &DROData[CurPos], sizeof(DRO_VER_HEADER_1));
CurPos += sizeof(DRO_VER_HEADER_1);
break;
}
DROInf.iLengthPairs = DRO_V1.iLengthBytes >> 1;
DROInf.iLengthMS = DRO_V1.iLengthMS;
switch(DRO_V1.iHardwareType)
{
case 0x01: // OPL3
DROInf.iHardwareType = 0x02;
break;
case 0x02: // Dual OPL2
DROInf.iHardwareType = 0x01;
break;
default:
DROInf.iHardwareType = (UINT8)DRO_V1.iHardwareType;
break;
}
DROInf.iFormat = 0x00;
DROInf.iCompression = 0x00;
DROInf.iShortDelayCode = 0x00;
DROInf.iLongDelayCode = 0x01;
DROInf.iCodemapLength = 0x00;
break;
case 2: // Version 2 (DosBox Version 0.73)
// sizeof(DRO_VER_HEADER_2) returns 0x10, but the exact size is 0x0E
memcpy(&DROInf, &DROData[CurPos], 0x0E);
CurPos += 0x0E;
break;
default:
goto OpenErr;
}
if (DROInf.iCodemapLength)
{
DROCodemap = (UINT8*)malloc(DROInf.iCodemapLength * sizeof(UINT8));
memcpy(DROCodemap, &DROData[CurPos], DROInf.iCodemapLength);
CurPos += DROInf.iCodemapLength;
}
else
{
DROCodemap = NULL;
}
DRODataStart = CurPos;
CurPos += DROInf.iLengthPairs << 1;
if (CurPos < DRODataLen)
DRODataLen = CurPos;
// Generate VGM Header
memset(&VGMHead, 0x00, sizeof(VGM_HEADER));
VGMHead.fccVGM = FCC_VGM;
VGMHead.lngVersion = 0x00000151;
VGMHead.lngTotalSamples = (UINT32)(DROInf.iLengthMS * 44.1f + 0.5f);
VGMHead.lngRate = 1000;
VGMHead.lngDataOffset = 0x80;
switch(DROInf.iHardwareType)
{
case 0x00: // Single OPL2 Chip
VGMHead.lngHzYM3812 = 3579545;
break;
case 0x01: // Dual OPL2 Chip
VGMHead.lngHzYM3812 = 3579545 | 0xC0000000;
break;
case 0x02: // Single OPL3 Chip
VGMHead.lngHzYMF262 = 14318180;
break;
default:
VGMHead.lngHzYM3812 = 3579545 | 0x40000000;
break;
}
fclose(hFile);
strcpy(FileBase, FileName);
TempPnt = strrchr(FileBase, '.');
if (TempPnt != NULL)
*TempPnt = 0x00;
return true;
OpenErr:
fclose(hFile);
return false;
}
static void WriteVGMFile(const char* FileName)
{
FILE* hFile;
hFile = fopen(FileName, "wb");
fwrite(VGMData, 0x01, VGMDataLen, hFile);
fclose(hFile);
printf("File written.\n");
return;
}
static void ConvertDRO2VGM(void)
{
UINT16 TempSht;
UINT8 CurChip;
UINT8 ChipCmd;
UINT8 CurCmd;
UINT8 CurVal;
UINT32 CurMS;
UINT32 VGMSmplL;
UINT32 VGMSmplC;
UINT32 SmplVal;
VGMDataLen = DRODataLen * 0x04; // this should be 200% safe
VGMData = (UINT8*)malloc(VGMDataLen);
printf("DRO File Version: %u.%02u\n", DROHead.iVersionMajor, DROHead.iVersionMinor);
DROPos = DRODataStart;
VGMPos = VGMHead.lngDataOffset;
VGMDataLen = DROPos + VGMDataLen;
CurMS = 0x00;
VGMSmplL = 0x00;
VGMSmplC = 0x00;
CurChip = 0x00;
while(DROPos < DRODataLen)
{
CurCmd = DROData[DROPos + 0x00];
if (DROHead.iVersionMajor <= 0x01)
{
if (CurCmd <= 0x04 && DROPos < 0x20)
CurCmd = 0x04; // Fix Dro-Initialisation Bug
if (CurCmd == 0x01)
{
// fix these quite common bugs of v1-files
if (! (DROData[DROPos + 0x01] & ~0x20))
{
if (DROData[DROPos + 0x02] && /*DROData[DROPos + 0x02] <= 0x04 ||*/
DROData[DROPos + 0x02] >= 0x20)
CurCmd = 0x04;
}
if (DROData[DROPos + 0x02] == 0xBD)
CurCmd = 0x04;
}
}
if (CurCmd == DROInf.iShortDelayCode || CurCmd == DROInf.iLongDelayCode)
{
switch(DROHead.iVersionMajor)
{
case 0x00:
case 0x01:
if (CurCmd == DROInf.iShortDelayCode) // == 0x00
{
CurMS += 0x01 + DROData[DROPos + 0x01];
DROPos += 0x02;
}
else if (CurCmd == DROInf.iLongDelayCode) // == 0x01
{
memcpy(&TempSht, &DROData[DROPos + 0x01], 0x02);
CurMS += 0x01 + TempSht;
DROPos += 0x03;
}
break;
case 0x02:
TempSht = 0x01 + DROData[DROPos + 0x01];
if (CurCmd == DROInf.iShortDelayCode)
CurMS += TempSht;
else if (CurCmd == DROInf.iLongDelayCode)
CurMS += TempSht << 8;
DROPos += 0x02;
break;
}
VGMSmplC = (UINT32)(CurMS * 44.1f + 0.5f);
}
else if (DROHead.iVersionMajor <= 0x01 && (CurCmd == 0x02 || CurCmd == 0x03))
{
CurChip = CurCmd & 0x01;
DROPos += 0x01;
}
else
{
if (VGMSmplL < VGMSmplC)
{
SmplVal = VGMSmplC - VGMSmplL;
while(SmplVal)
{
if (SmplVal <= 0xFFFF)
TempSht = (UINT16)SmplVal;
else
TempSht = 0xFFFF;
VGMData[VGMPos + 0x00] = 0x61;
memcpy(&VGMData[VGMPos + 0x01], &TempSht, 0x02);
VGMPos += 0x03;
SmplVal -= TempSht;
}
VGMSmplL = VGMSmplC;
}
switch(DROHead.iVersionMajor)
{
case 0x00:
case 0x01:
if (CurCmd == 0x04)
{
if (CurCmd == DROData[DROPos + 0x00] && DROPos >= 0x20)
DROPos += 0x01;
CurCmd = DROData[DROPos + 0x00];
}
break;
case 0x02:
CurChip = (CurCmd & 0x80) >> 7;
CurCmd = DROCodemap[CurCmd & 0x7F];
break;
}
CurVal = DROData[DROPos + 0x01];
DROPos += 0x02;
ChipCmd = 0x00;
switch(DROInf.iHardwareType)
{
case 0x00: // OPL2
if (! CurChip) // ignore writes to invalid OPL chip
ChipCmd = 0x5A;
break;
case 0x01: // Dual OPL2
ChipCmd = 0x5A + CurChip * 0x50;
break;
case 0x02: // OPL3
ChipCmd = 0x5E | CurChip;
break;
}
if (ChipCmd)
{
VGMData[VGMPos + 0x00] = ChipCmd;
VGMData[VGMPos + 0x01] = CurCmd;
VGMData[VGMPos + 0x02] = CurVal;
VGMPos += 0x03;
}
}
}
VGMData[VGMPos + 0x00] = 0x66;
VGMPos += 0x01;
if (VGMSmplL != VGMHead.lngTotalSamples)
{
printf("Warning! There was an error during delay calculations!\n");
printf("DRO ms Header: %u, counted: %u\n", DROInf.iLengthMS, CurMS);
printf("Please relog the file with DosBox 0.73 or higher.\n");
}
VGMDataLen = VGMPos;
VGMHead.lngEOFOffset = VGMDataLen;
SmplVal = VGMHead.lngDataOffset;
if (SmplVal > sizeof(VGM_HEADER))
SmplVal = sizeof(VGM_HEADER);
VGMHead.lngDataOffset -= 0x34;
VGMHead.lngEOFOffset -= 0x04;
memcpy(&VGMData[0x00], &VGMHead, SmplVal);
return;
}