forked from vgmrips/vgmtools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vgm_smp1.c
608 lines (555 loc) · 11.8 KB
/
vgm_smp1.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
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
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
// vgm_smp1.c - Remove 1 Sample Delays
//
// TODO: Make it remove 2x 7# delays.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <zlib.h>
#include "stdtype.h"
#include "stdbool.h"
#include "VGMFile.h"
#include "common.h"
static bool OpenVGMFile(const char* FileName);
static void WriteVGMFile(const char* FileName);
static void RoundVGMData(void);
static void WriteVGMHeader(UINT8* DstData, const UINT8* SrcData, const UINT32 EOFPos,
const UINT32 LoopPos);
// recommend values: 1 for vgms with PSG, 45 for dro2vgm
UINT16 MAX_SMALL_DELAY; // for standard VGMs
VGM_HEADER VGMHead;
UINT32 VGMDataLen;
UINT8* VGMData;
UINT32 VGMPos;
UINT32 VGMSmplPos;
UINT8* DstData;
UINT32 DstDataLen;
char FileBase[0x100];
bool DidSomething;
int main(int argc, char* argv[])
{
int argbase;
int ErrVal;
char FileName[0x100];
printf("Remove 1 Sample Delays\n----------------------\n\n");
ErrVal = 0;
MAX_SMALL_DELAY = 0x0000;
argbase = 1;
while(argbase < argc && argv[argbase][0] == '-')
{
if (! strncmp(argv[argbase], "-delay:", 7))
{
MAX_SMALL_DELAY = (UINT16)strtoul(argv[argbase] + 7, NULL, 0);
argbase ++;
}
else
{
break;
}
}
if (! MAX_SMALL_DELAY)
MAX_SMALL_DELAY = 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 (! OpenVGMFile(FileName))
{
printf("Error opening the file!\n");
ErrVal = 1;
goto EndProgram;
}
printf("\n");
RoundVGMData();
if (DidSomething)
{
if (argc > argbase + 1)
strcpy(FileName, argv[argbase + 1]);
else
strcpy(FileName, "");
if (FileName[0] == '\0')
{
strcpy(FileName, FileBase);
strcat(FileName, "_no1smpl.vgm");
}
WriteVGMFile(FileName);
}
free(VGMData);
free(DstData);
EndProgram:
DblClickWait(argv[0]);
return ErrVal;
}
static bool OpenVGMFile(const char* FileName)
{
gzFile hFile;
UINT32 CurPos;
UINT32 TempLng;
char* TempPnt;
hFile = gzopen(FileName, "rb");
if (hFile == NULL)
return false;
gzseek(hFile, 0x00, SEEK_SET);
gzread(hFile, &TempLng, 0x04);
if (TempLng != FCC_VGM)
goto OpenErr;
gzseek(hFile, 0x00, SEEK_SET);
gzread(hFile, &VGMHead, sizeof(VGM_HEADER));
ZLIB_SEEKBUG_CHECK(VGMHead);
// Header preperations
if (VGMHead.lngVersion < 0x00000150)
{
VGMHead.lngDataOffset = 0x00000000;
}
if (VGMHead.lngVersion < 0x00000151)
{
VGMHead.lngHzSPCM = 0x0000;
VGMHead.lngSPCMIntf = 0x00000000;
// all others are zeroed by memset
}
// relative -> absolute addresses
VGMHead.lngEOFOffset += 0x00000004;
if (VGMHead.lngGD3Offset)
VGMHead.lngGD3Offset += 0x00000014;
if (VGMHead.lngLoopOffset)
VGMHead.lngLoopOffset += 0x0000001C;
if (! VGMHead.lngDataOffset)
VGMHead.lngDataOffset = 0x0000000C;
VGMHead.lngDataOffset += 0x00000034;
CurPos = VGMHead.lngDataOffset;
if (VGMHead.lngVersion < 0x00000150)
CurPos = 0x40;
TempLng = sizeof(VGM_HEADER);
if (TempLng > CurPos)
TempLng -= CurPos;
else
TempLng = 0x00;
memset((UINT8*)&VGMHead + CurPos, 0x00, TempLng);
// Read Data
VGMDataLen = VGMHead.lngEOFOffset;
VGMData = (UINT8*)malloc(VGMDataLen);
if (VGMData == NULL)
goto OpenErr;
gzseek(hFile, 0x00, SEEK_SET);
gzread(hFile, VGMData, VGMDataLen);
gzclose(hFile);
strcpy(FileBase, FileName);
TempPnt = strrchr(FileBase, '.');
if (TempPnt != NULL)
*TempPnt = 0x00;
return true;
OpenErr:
gzclose(hFile);
return false;
}
static void WriteVGMFile(const char* FileName)
{
FILE* hFile;
const char* FileTitle;
printf("\t\t\t\t\t\t\t\t\r");
FileTitle = strrchr(FileName, '\\');
if (FileTitle == NULL)
FileTitle = FileName;
else
FileTitle ++;
hFile = fopen(FileName, "wb");
if (hFile == NULL)
{
printf("Error writing %s!\n", FileTitle);
return;
}
fwrite(DstData, 0x01, DstDataLen, hFile);
fclose(hFile);
printf("%s written.\n", FileTitle);
return;
}
static void RoundVGMData(void)
{
UINT32 DstPos;
UINT8 ChipID;
UINT8 Command;
UINT32 CmdDelay;
UINT8 TempByt;
UINT16 TempSht;
UINT32 TempLng;
#ifdef WIN32
UINT32 CmdTimer;
char TempStr[0x80];
char MinSecStr[0x80];
#endif
UINT32 CmdLen;
bool StopVGM;
bool IsDelay;
bool WasDelay;
bool WriteEvent;
UINT32 LoopPos;
UINT32 DelayQueue;
DstData = (UINT8*)malloc(VGMDataLen);
VGMPos = VGMHead.lngDataOffset;
DstPos = VGMHead.lngDataOffset;
DidSomething = false;
#ifdef WIN32
CmdTimer = 0;
#endif
VGMSmplPos = 0x00;
DelayQueue = 0x00;
LoopPos = 0x00;
StopVGM = false;
IsDelay = false;
while(VGMPos < VGMHead.lngEOFOffset)
{
CmdLen = 0x00;
Command = VGMData[VGMPos + 0x00];
WasDelay = IsDelay;
IsDelay = false;
CmdDelay = 0x00;
if (Command >= 0x70 && Command <= 0x8F)
{
switch(Command & 0xF0)
{
case 0x70:
TempSht = (Command & 0x0F) + 0x01;
VGMSmplPos += TempSht;
CmdDelay = TempSht;
IsDelay = true;
break;
case 0x80:
TempSht = Command & 0x0F;
VGMSmplPos += TempSht;
CmdDelay = TempSht;
break;
}
CmdLen = 0x01;
}
else
{
// Cheat Mode (to use 2 instances of 1 chip)
ChipID = 0x00;
switch(Command)
{
case 0x30:
if (VGMHead.lngHzPSG & 0x40000000)
{
Command += 0x20;
ChipID = 0x01;
}
break;
case 0xA1:
if (VGMHead.lngHzYM2413 & 0x40000000)
{
Command -= 0x50;
ChipID = 0x01;
}
break;
case 0xA2:
case 0xA3:
if (VGMHead.lngHzYM2612 & 0x40000000)
{
Command -= 0x50;
ChipID = 0x01;
}
break;
case 0xA4:
if (VGMHead.lngHzYM2151 & 0x40000000)
{
Command -= 0x50;
ChipID = 0x01;
}
break;
case 0xA5:
if (VGMHead.lngHzYM2203 & 0x40000000)
{
Command -= 0x50;
ChipID = 0x01;
}
break;
case 0xA6:
case 0xA7:
if (VGMHead.lngHzYM2608 & 0x40000000)
{
Command -= 0x50;
ChipID = 0x01;
}
break;
case 0xA8:
case 0xA9:
if (VGMHead.lngHzYM2610 & 0x40000000)
{
Command -= 0x50;
ChipID = 0x01;
}
break;
case 0xAA:
if (VGMHead.lngHzYM3812 & 0x40000000)
{
Command -= 0x50;
ChipID = 0x01;
}
break;
case 0xAB:
if (VGMHead.lngHzYM3526 & 0x40000000)
{
Command -= 0x50;
ChipID = 0x01;
}
break;
case 0xAC:
if (VGMHead.lngHzY8950 & 0x40000000)
{
Command -= 0x50;
ChipID = 0x01;
}
break;
}
switch(Command)
{
case 0x66: // End Of File
CmdLen = 0x01;
StopVGM = true;
break;
case 0x62: // 1/60s delay
TempSht = 735;
VGMSmplPos += TempSht;
CmdDelay = TempSht;
CmdLen = 0x01;
IsDelay = true;
break;
case 0x63: // 1/50s delay
TempSht = 882;
VGMSmplPos += TempSht;
CmdDelay = TempSht;
CmdLen = 0x01;
IsDelay = true;
break;
case 0x61: // xx Sample Delay
memcpy(&TempSht, &VGMData[VGMPos + 0x01], 0x02);
VGMSmplPos += TempSht;
CmdDelay = TempSht;
CmdLen = 0x03;
IsDelay = true;
break;
case 0x50: // SN76496 write
CmdLen = 0x02;
break;
case 0x51: // YM2413 write
CmdLen = 0x03;
break;
case 0x52: // YM2612 write port 0
case 0x53: // YM2612 write port 1
CmdLen = 0x03;
break;
case 0x67: // PCM Data Stream
TempByt = VGMData[VGMPos + 0x02];
memcpy(&TempLng, &VGMData[VGMPos + 0x03], 0x04);
TempLng &= 0x7FFFFFFF;
CmdLen = 0x07 + TempLng;
break;
case 0xE0: // Seek to PCM Data Bank Pos
CmdLen = 0x05;
break;
case 0x4F: // GG Stereo
CmdLen = 0x02;
break;
case 0x54: // YM2151 write
CmdLen = 0x03;
break;
case 0xC0: // Sega PCM memory write
CmdLen = 0x04;
break;
case 0xB0: // RF5C68 register write
CmdLen = 0x03;
break;
case 0xC1: // RF5C68 memory write
CmdLen = 0x04;
break;
case 0x55: // YM2203
CmdLen = 0x03;
break;
case 0x56: // YM2608 write port 0
case 0x57: // YM2608 write port 1
CmdLen = 0x03;
break;
case 0x58: // YM2610 write port 0
case 0x59: // YM2610 write port 1
CmdLen = 0x03;
break;
case 0x5A: // YM3812 write
CmdLen = 0x03;
break;
case 0x5B: // YM3526 write
CmdLen = 0x03;
break;
case 0x5C: // Y8950 write
CmdLen = 0x03;
break;
case 0x5E: // YMF262 write port 0
case 0x5F: // YMF262 write port 1
CmdLen = 0x03;
break;
case 0x5D: // YMZ280B write
CmdLen = 0x03;
break;
case 0x68: // PCM RAM write
CmdLen = 0x0C;
break;
case 0x90: // DAC Ctrl: Setup Chip
CmdLen = 0x05;
break;
case 0x91: // DAC Ctrl: Set Data
CmdLen = 0x05;
break;
case 0x92: // DAC Ctrl: Set Freq
CmdLen = 0x06;
break;
case 0x93: // DAC Ctrl: Play from Start Pos
CmdLen = 0x0B;
break;
case 0x94: // DAC Ctrl: Stop immediately
CmdLen = 0x02;
break;
case 0x95: // DAC Ctrl: Play Block (small)
CmdLen = 0x05;
break;
default:
switch(Command & 0xF0)
{
case 0x30:
case 0x40:
CmdLen = 0x02;
break;
case 0x50:
case 0xA0:
case 0xB0:
CmdLen = 0x03;
break;
case 0xC0:
case 0xD0:
CmdLen = 0x04;
break;
case 0xE0:
case 0xF0:
CmdLen = 0x05;
break;
default:
printf("Unknown Command: %X\n", Command);
CmdLen = 0x01;
//StopVGM = true;
break;
}
break;
}
}
WriteEvent = ! IsDelay;
if (VGMHead.lngLoopOffset && VGMPos == VGMHead.lngLoopOffset)
{
LoopPos = DstPos;
WasDelay = true;
}
if (IsDelay)
{
if (! DelayQueue && CmdDelay > MAX_SMALL_DELAY)
{
WriteEvent = true;
}
else if (CmdDelay <= MAX_SMALL_DELAY && ! WasDelay)
{
DelayQueue += CmdDelay;
DidSomething = true;
}
else
{
if (WasDelay)
{
CmdDelay = 0x00;
WriteEvent = true;
}
CmdDelay += DelayQueue;
while(CmdDelay)
{
if (CmdDelay <= 0xFFFF)
TempSht = (UINT16)CmdDelay;
else
TempSht = 0xFFFF;
if (TempSht <= 0x10)
{
DstData[DstPos] = 0x70 | (TempSht - 0x01);
DstPos ++;
}
else
{
DstData[DstPos + 0x00] = 0x61;
memcpy(&DstData[DstPos + 0x01], &TempSht, 0x02);
DstPos += 0x03;
}
CmdDelay -= TempSht;
}
DelayQueue = 0x00;
}
}
if (WriteEvent)
{
memcpy(&DstData[DstPos], &VGMData[VGMPos], CmdLen);
DstPos += CmdLen;
}
VGMPos += CmdLen;
if (StopVGM)
break;
#ifdef WIN32
if (CmdTimer < GetTickCount())
{
PrintMinSec(VGMSmplPos, MinSecStr);
PrintMinSec(VGMHead.lngTotalSamples, TempStr);
TempLng = VGMPos - VGMHead.lngDataOffset;
CmdLen = VGMHead.lngEOFOffset - VGMHead.lngDataOffset;
printf("%04.3f %% - %s / %s (%08X / %08X) ...\r", (float)TempLng / CmdLen * 100,
MinSecStr, TempStr, VGMPos, VGMHead.lngEOFOffset);
CmdTimer = GetTickCount() + 200;
}
#endif
}
VGMHead.lngTotalSamples = VGMSmplPos - DelayQueue;
if (VGMHead.lngLoopOffset && ! LoopPos)
printf("Warning! Failed to relocate Loop Point!\n");
WriteVGMHeader(DstData, VGMData, DstPos, LoopPos);
return;
}
static void WriteVGMHeader(UINT8* DstData, const UINT8* SrcData, const UINT32 EOFPos,
const UINT32 LoopPos)
{
UINT32 CurPos;
UINT32 DstPos;
UINT32 CmdLen;
UINT32 TempLng;
memcpy(DstData, VGMData, VGMHead.lngDataOffset); // Copy Header
memcpy(&DstData[0x18], &VGMHead.lngTotalSamples, 0x04);
TempLng = LoopPos;
if (TempLng)
TempLng -= 0x1C;
memcpy(&DstData[0x1C], &TempLng, 0x04);
DstPos = EOFPos;
if (VGMHead.lngGD3Offset && VGMHead.lngGD3Offset + 0x0B < VGMHead.lngEOFOffset)
{
CurPos = VGMHead.lngGD3Offset;
memcpy(&TempLng, &VGMData[CurPos + 0x00], 0x04);
if (TempLng == FCC_GD3)
{
memcpy(&CmdLen, &VGMData[CurPos + 0x08], 0x04);
CmdLen += 0x0C;
TempLng = DstPos - 0x14;
memcpy(&DstData[0x14], &TempLng, 0x04);
memcpy(&DstData[DstPos], &VGMData[CurPos], CmdLen); // Copy GD3 Tag
DstPos += CmdLen;
}
}
DstDataLen = DstPos;
TempLng = DstDataLen - 0x04;
memcpy(&DstData[0x04], &TempLng, 0x04); // Write EOF Position
return;
}