forked from ValleyBell/MidiConverters
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhum2mid.c
600 lines (524 loc) · 14.3 KB
/
hum2mid.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
// Hummer -> Midi Converter
// ------------------------
// Written by Valley Bell, 09 April 2015
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <memory.h>
#include "stdtype.h"
#define INLINE static __inline
int main(int argc, char* argv[]);
UINT8 ConvertHummer2MID(UINT16 SeqOfs, UINT32 ROMBankOfs);
INLINE UINT8 DB2Mid(double DB);
static void WriteEvent(UINT8* Buffer, UINT32* Pos, UINT32* Delay, UINT8 Evt, UINT8 Val1, UINT8 Val2);
static void WriteMetaEvent_Data(UINT8* Buffer, UINT32* Pos, UINT32* Delay, UINT8 MetaType, UINT32 DataLen, const UINT8* Data);
static void WriteMidiValue(UINT8* Buffer, UINT32* Pos, UINT32 Value);
INLINE UINT16 ReadLE16(const UINT8* Data);
INLINE void WriteBE16(UINT8* Buffer, UINT16 Value);
INLINE void WriteBE32(UINT8* Buffer, UINT32 Value);
const UINT8 NES_SIG[0x03] = {'N', 'E', 'S'}; // .nes: "NES\x1A", .nsf: "NESM"
//const UINT8 HUMMER_LOADSEQ[0x07] = {0xAA, 0x98, 0x48, 0x8A, 0x0A, 0xAA, 0xBD}; // works in Somari (Hummer v2)
const UINT8 HUMMER_LOADSEQ[0x06] = {0x98, 0x48, 0x8A, 0x0A, 0xAA, 0xBD};
typedef struct _track_header
{
UINT8 ChnID;
UINT16 StartOfs;
} TRK_HDR;
static UINT32 ROMSize;
static UINT8* ROMData;
static UINT32 MidAlloc;
static UINT32 MidSize;
static UINT8* MidData;
static UINT16 MIDI_RES;
static UINT16 NUM_LOOPS;
static UINT8 ACCURATE_TEMPO;
static UINT32 TEMPO_BASE;
int main(int argc, char* argv[])
{
FILE* hFile;
UINT8 FileSig[0x04];
UINT8 FileMode;
UINT8 RetVal;
UINT8 CurSng;
UINT8 SongCnt;
UINT32 CurPos;
UINT32 SeqPtrsPos;
UINT32 BankPos;
UINT16 SongOfs;
char OutFileBase[0x100];
char OutFile[0x100];
char* TempPnt;
if (argc <= 1)
{
printf("Usage: hum2mid ROM.nes/nsf\n");
#ifdef _DEBUG
_getch();
#endif
return 0;
}
MIDI_RES = 32;
NUM_LOOPS = 2;
ACCURATE_TEMPO = 0x00;
SongCnt = 0x00;
strcpy(OutFileBase, argv[1]);
TempPnt = strrchr(OutFileBase, '.');
if (TempPnt == NULL)
TempPnt = OutFileBase + strlen(OutFileBase);
*TempPnt = 0x00;
hFile = fopen(argv[1], "rb");
if (hFile == NULL)
return 1;
fread(FileSig, 0x01, 0x04, hFile);
FileMode = FileSig[0x03];
if (memcmp(FileSig, NES_SIG, 0x03) || (FileMode != 0x1A && FileMode != 'M'))
{
fclose(hFile);
printf("Invalid file type! Must be .nes or .nsf!\n");
return 2;
}
fseek(hFile, 0, SEEK_END);
ROMSize = ftell(hFile);
if (FileMode == 0x1A)
{
// NES File
fseek(hFile, 0x10, SEEK_SET);
ROMSize -= 0x10; // skip header
}
else
{
fseek(hFile, 0x80, SEEK_SET);
ROMSize -= 0x80; // skip header
}
ROMData = (UINT8*)malloc(ROMSize);
fread(ROMData, 0x01, ROMSize, hFile);
fclose(hFile);
SeqPtrsPos = 0x00;
for (CurPos = 0x00; CurPos < ROMSize - 0x09; CurPos ++)
{
if (! memcmp(ROMData + CurPos, HUMMER_LOADSEQ, sizeof(HUMMER_LOADSEQ)))
{
CurPos += sizeof(HUMMER_LOADSEQ);
SeqPtrsPos = ReadLE16(ROMData + CurPos);
if (ROMData[CurPos + 0x03] == 0xFF)
SeqPtrsPos --;
BankPos = CurPos & ~0x3FFF;
// Check the first Song (ID 20h)
SongOfs = ReadLE16(&ROMData[BankPos | (SeqPtrsPos & 0x3FFF)] + 0x40);
if ((SongOfs & 0xC000) == (SeqPtrsPos & 0xC000))
{
RetVal = ROMData[BankPos | (SongOfs & 0x3FFF)];
if (RetVal == 0xFF || (RetVal & 0x7F) < 0x05)
break;
}
// This might be a wrong bank.
SeqPtrsPos = 0x00;
}
}
if (! SeqPtrsPos)
{
printf("Unable to find sequence list!\n");
return 4;
}
printf("Sequence Loader found at offset %06X.\n", CurPos);
printf("Sequence List Pointer: %04X\n", SeqPtrsPos);
if (! SongCnt)
{
UINT16 MaxPos;
// Song Count autodetection
CurSng = 0x00;
CurPos = BankPos | (SeqPtrsPos & 0x3FFF);
MaxPos = 0x3FFF;
while(CurPos < ROMSize)
{
if ((CurPos & 0x3FFF) >= MaxPos)
break;
SongOfs = ReadLE16(ROMData + CurPos);
if ((SongOfs & 0xC000) != (SeqPtrsPos & 0xC000))
break;
SongOfs &= 0x3FFF;
RetVal = ROMData[BankPos | SongOfs];
if (RetVal != 0xFF && (RetVal & 0x7F) > 0x04) // invalid first channel - exit
break;
if (SongOfs < MaxPos)
MaxPos = SongOfs;
CurPos += 0x02;
CurSng ++;
}
SongCnt = CurSng;
printf("Songs detected: 0x%02X (%u)\n", SongCnt, SongCnt);
}
if (ACCURATE_TEMPO)
TEMPO_BASE = (UINT32)(1000000 * MIDI_RES / 60.098 + 0.5);
else
TEMPO_BASE = (UINT32)(1000000 * MIDI_RES / 60.0 + 0.5);
MidAlloc = 0x20000;
MidData = (UINT8*)malloc(MidAlloc);
CurPos = BankPos | (SeqPtrsPos & 0x3FFF);
for (CurSng = 0x00; CurSng < SongCnt; CurSng ++, CurPos += 0x02)
{
printf("File %02X / %02X ...", CurSng, SongCnt);
SongOfs = ReadLE16(ROMData + CurPos) & 0x3FFF;
MidSize = 0x00;
RetVal = ConvertHummer2MID(SongOfs, BankPos);
if (RetVal)
return RetVal;
sprintf(OutFile, "%s_%02X.mid", OutFileBase, CurSng);
hFile = fopen(OutFile, "wb");
if (hFile == NULL)
{
printf("Error saving file!\n");
continue;
}
fwrite(MidData, 0x01, MidSize, hFile);
fclose(hFile);
printf("\n");
}
printf("Done.\n");
free(MidData); MidData = NULL;
free(ROMData); ROMData = NULL;
#ifdef _DEBUG
getchar();
#endif
return 0;
}
UINT8 ConvertHummer2MID(UINT16 SeqOfs, UINT32 ROMBankOfs)
{
static const char* TRK_NAMES[5] = {"Square 1", "Square 2", "Triangle", "Noise", "DPCM"};
const UINT8* SeqData;
UINT16 SeqSize;
TRK_HDR TrkHdrs[10]; // 5x Music + 5x PCM
UINT16 SeqPos;
UINT32 MidPos;
UINT8 TrkCnt;
UINT8 CurTrk;
UINT32 MidTrkBase;
UINT32 CurDly;
UINT8 MidChn;
UINT8 TrkEnd;
UINT8 MstLoopCnt;
UINT8 TrkMode; // 00 - FM, 01 - SSG
INT8 NoteMove;
UINT8 CurCmd;
UINT8 CurNote;
UINT8 CurNoteVol;
UINT8 CurChnVol;
UINT8 CurNoteLen;
UINT8 LastNote;
UINT8 HoldNote;
UINT8 LoopCnt;
UINT16 LoopOfs;
UINT16 SubRetOfs;
UINT8 TempByt;
INT16 TempPos;
UINT32 TempLng;
UINT8 TempArr[0x20];
SeqData = ROMData + ROMBankOfs;
if (ROMSize - ROMBankOfs >= 0x4000)
SeqSize = 0x4000;
else
SeqSize = ROMSize - ROMBankOfs;
SeqPos = SeqOfs & 0x3FFF;
for (CurTrk = 0; CurTrk < 10; CurTrk ++, SeqPos += 0x03)
{
if (SeqData[SeqPos] == 0xFF)
break;
TrkHdrs[CurTrk].ChnID = SeqData[SeqPos + 0x00];
TrkHdrs[CurTrk].StartOfs = ReadLE16(&SeqData[SeqPos + 0x01]) & 0x3FFF;
}
TrkCnt = CurTrk;
printf(" %u Track%s", TrkCnt, (TrkCnt == 1) ? "" : "s");
MidPos = 0x00;
WriteBE32(&MidData[MidPos], 0x4D546864); MidPos += 0x04; // 'MThd' Signature
WriteBE32(&MidData[MidPos], 0x00000006); MidPos += 0x04; // Header Size
WriteBE16(&MidData[MidPos], 0x0001); MidPos += 0x02; // Format: 1
WriteBE16(&MidData[MidPos], TrkCnt); MidPos += 0x02; // Tracks
WriteBE16(&MidData[MidPos], MIDI_RES); MidPos += 0x02; // Ticks per Quarter
#if 0 // Every sequence contains a F5 Tick Multiplier command that writes the Tempo anyway.
WriteBE32(&MidData[MidPos], 0x4D54726B); MidPos += 0x04; // 'MTrk' Signature
WriteBE32(&MidData[MidPos], 0x00000000); MidPos += 0x04; // Track Size
MidTrkBase = MidPos;
CurDly = 0;
WriteBE32(TempArr, TEMPO_BASE);
WriteMetaEvent_Data(MidData, &MidPos, &CurDly, 0x51, 0x03, &TempArr[1]);
WriteEvent(MidData, &MidPos, &CurDly, 0xFF, 0x2F, 0x00);
WriteBE32(&MidData[MidTrkBase - 0x04], MidPos - MidTrkBase); // write Track Length
#endif
for (CurTrk = 0; CurTrk < TrkCnt; CurTrk ++)
{
WriteBE32(&MidData[MidPos], 0x4D54726B); // write 'MTrk'
MidPos += 0x08;
MidTrkBase = MidPos;
CurDly = 0;
TrkEnd = (SeqPos == 0x0000);
SeqPos = TrkHdrs[CurTrk].StartOfs;
TrkMode = TrkHdrs[CurTrk].ChnID & 0x7F;
MidChn = TrkHdrs[CurTrk].ChnID;
if (TrkHdrs[CurTrk].ChnID & 0x80)
{
MidChn = 0x0A + (TrkMode & 0x0F);
TempLng = sprintf((char*)TempArr, "SFX Track %u (%s)", TrkMode, TRK_NAMES[TrkMode]);
}
else
{
MidChn = 0x00 + (TrkMode & 0x0F);
TempLng = sprintf((char*)TempArr, "Music Track %u (%s)", TrkMode, TRK_NAMES[TrkMode]);
}
WriteMetaEvent_Data(MidData, &MidPos, &CurDly, 0x03, TempLng, TempArr);
MstLoopCnt = 0x00;
NoteMove = 0;
CurNote = 0x00;
LastNote = 0x00;
CurNoteVol = 0x7F;
CurChnVol = 0x00;
CurNoteLen = 0x00;
HoldNote = 0x00;
LoopCnt = 0x00;
LoopOfs = 0x00;
SubRetOfs = 0x00;
while(! TrkEnd && SeqPos)
{
/*if (! MstLoopCnt && SeqPos == TrkHdrs[CurTrk].LoopOfs)
{
MstLoopCnt ++;
WriteEvent(MidData, &MidPos, &CurDly, 0xB0 | MidChn, 0x6F, 0x00);
}*/
CurCmd = SeqData[SeqPos]; SeqPos ++;
if (CurCmd < 0x80)
{
CurNote = CurCmd;
if (CurNote)
CurNote += NoteMove + 24;
if (LastNote != CurNote || ! HoldNote)
{
if (HoldNote)
{
if (CurDly >= 1)
{
CurDly --;
WriteEvent(MidData, &MidPos, &CurDly, 0xB0 | MidChn, 0x41, 0x7F);
CurDly ++;
}
else
{
WriteEvent(MidData, &MidPos, &CurDly, 0xB0 | MidChn, 0x41, 0x7F);
}
}
if (LastNote)
WriteEvent(MidData, &MidPos, &CurDly, 0x90 | MidChn, LastNote, 0x00);
if (CurNote)
WriteEvent(MidData, &MidPos, &CurDly, 0x90 | MidChn, CurNote, CurNoteVol);
LastNote = CurNote;
if (HoldNote)
WriteEvent(MidData, &MidPos, &CurDly, 0xB0 | MidChn, 0x41, 0x00);
}
HoldNote = 0x00;
CurDly += CurNoteLen;
}
else if (CurCmd < 0xF0)
{
CurNoteLen = CurCmd & 0x7F;
}
else
{
switch(CurCmd)
{
case 0xF0: // GoSub
TempPos = ReadLE16(&SeqData[SeqPos]) & 0x3FFF;
SeqPos += 0x02;
SubRetOfs = SeqPos;
SeqPos = TempPos;
break;
case 0xF1: // Return;
SeqPos = SubRetOfs;
SubRetOfs = 0x0000;
break;
case 0xF2: // Loop Start
printf("Command %02X at Offset %04X!\n", CurCmd, 0x8000 | SeqPos);
WriteEvent(MidData, &MidPos, &CurDly, 0xB0 | MidChn, 0x6F, CurCmd);
LoopCnt = SeqData[SeqPos];
SeqPos ++;
LoopOfs = SeqPos;
break;
case 0xF3: // Loop End
LoopCnt --;
if (LoopCnt)
SeqPos = LoopOfs;
else
LoopOfs = 0x0000;
break;
case 0xF4: // GoTo
TempPos = ReadLE16(&SeqData[SeqPos]) & 0x3FFF;
SeqPos = TempPos;
MstLoopCnt ++;
WriteEvent(MidData, &MidPos, &CurDly, 0xB0 | MidChn, 0x6F, MstLoopCnt);
if (MstLoopCnt >= NUM_LOOPS)
TrkEnd = 0x01;
break;
case 0xF5: // Tick Multiplier
TempByt = SeqData[SeqPos];
SeqPos ++;
WriteBE32(TempArr, TEMPO_BASE * TempByt);
WriteMetaEvent_Data(MidData, &MidPos, &CurDly, 0x51, 0x03, &TempArr[1]);
break;
case 0xF6: // Transpose
NoteMove = (INT8)SeqData[SeqPos];
SeqPos ++;
break;
case 0xF7: // broken/dummy
printf("Command %02X at Offset %04X!\n", CurCmd, 0x8000 | SeqPos);
WriteEvent(MidData, &MidPos, &CurDly, 0xB0 | MidChn, 0x6F, CurCmd);
break;
case 0xF8: // Set Instrument
TempByt = SeqData[SeqPos] & 0x7F;
WriteEvent(MidData, &MidPos, &CurDly, 0xC0 | MidChn, TempByt, 0x00);
SeqPos ++;
break;
case 0xF9: // Set Volume Envelope
TempByt = SeqData[SeqPos];
WriteEvent(MidData, &MidPos, &CurDly, 0xB0 | MidChn, 0x27, TempByt);
SeqPos ++;
break;
case 0xFA: // Set Modulation Envelope
TempByt = SeqData[SeqPos];
WriteEvent(MidData, &MidPos, &CurDly, 0xB0 | MidChn, 0x21, TempByt);
SeqPos ++;
break;
case 0xFB: // alternative Loop
printf("Command %02X at Offset %04X!\n", CurCmd, 0x8000 | SeqPos);
WriteEvent(MidData, &MidPos, &CurDly, 0xB0 | MidChn, 0x6F, CurCmd);
TempByt = SeqData[SeqPos + 0x00];
TempPos = ReadLE16(&SeqData[SeqPos + 0x02]) & 0x3FFF;
if (! LoopCnt)
LoopCnt = TempByt;
LoopCnt --;
if (LoopCnt)
SeqPos = TempPos;
break;
case 0xFC: // Hold
SeqPos ++;
printf("Command %02X at Offset %04X!\n", CurCmd, 0x8000 | SeqPos);
WriteEvent(MidData, &MidPos, &CurDly, 0xB0 | MidChn, 0x6F, CurCmd);
CurDly += CurNoteLen;
break;
case 0xFD: // broken
printf("Command %02X at Offset %04X!\n", CurCmd, 0x8000 | SeqPos);
WriteEvent(MidData, &MidPos, &CurDly, 0xB0 | MidChn, 0x6F, CurCmd);
SeqPos ++;
break;
case 0xFE: // broken
printf("Command %02X at Offset %04X!\n", CurCmd, 0x8000 | SeqPos);
WriteEvent(MidData, &MidPos, &CurDly, 0xB0 | MidChn, 0x6F, CurCmd);
break;
case 0xFF: // Track End
TrkEnd = 0x01;
break;
}
}
}
if (LastNote)
WriteEvent(MidData, &MidPos, &CurDly, 0x90 | MidChn, LastNote, 0x00);
WriteEvent(MidData, &MidPos, &CurDly, 0xFF, 0x2F, 0x00);
WriteBE32(&MidData[MidTrkBase - 0x04], MidPos - MidTrkBase); // write Track Length
}
MidSize = MidPos;
return 0x00;
}
INLINE UINT8 DB2Mid(double DB)
{
DB += 6.0;
if (DB > 0.0)
DB = 0.0;
return (UINT8)(pow(10.0, DB / 40.0) * 0x7F + 0.5);
}
static void WriteEvent(UINT8* Buffer, UINT32* Pos, UINT32* Delay, UINT8 Evt, UINT8 Val1, UINT8 Val2)
{
if (! (Evt & 0x80))
return;
WriteMidiValue(Buffer, Pos, *Delay);
*Delay = 0x00;
switch(Evt & 0xF0)
{
case 0x80:
case 0x90:
case 0xA0:
case 0xB0:
case 0xE0:
MidData[*Pos + 0x00] = Evt;
MidData[*Pos + 0x01] = Val1;
MidData[*Pos + 0x02] = Val2;
*Pos += 0x03;
break;
case 0xC0:
case 0xD0:
MidData[*Pos + 0x00] = Evt;
MidData[*Pos + 0x01] = Val1;
*Pos += 0x02;
break;
case 0xF0: // for Meta Event: Track End
MidData[*Pos + 0x00] = Evt;
MidData[*Pos + 0x01] = Val1;
MidData[*Pos + 0x02] = Val2;
*Pos += 0x03;
break;
default:
break;
}
return;
}
static void WriteMetaEvent_Data(UINT8* Buffer, UINT32* Pos, UINT32* Delay, UINT8 MetaType, UINT32 DataLen, const UINT8* Data)
{
WriteMidiValue(Buffer, Pos, *Delay);
*Delay = 0x00;
MidData[*Pos + 0x00] = 0xFF;
MidData[*Pos + 0x01] = MetaType;
*Pos += 0x02;
WriteMidiValue(Buffer, Pos, DataLen);
memcpy(MidData + *Pos, Data, DataLen);
*Pos += DataLen;
return;
}
static void WriteMidiValue(UINT8* Buffer, UINT32* Pos, UINT32 Value)
{
UINT8 ValSize;
UINT8* ValData;
UINT32 TempLng;
UINT32 CurPos;
ValSize = 0x00;
TempLng = Value;
do
{
TempLng >>= 7;
ValSize ++;
} while(TempLng);
ValData = &Buffer[*Pos];
CurPos = ValSize;
TempLng = Value;
do
{
CurPos --;
ValData[CurPos] = 0x80 | (TempLng & 0x7F);
TempLng >>= 7;
} while(TempLng);
ValData[ValSize - 1] &= 0x7F;
*Pos += ValSize;
return;
}
INLINE UINT16 ReadLE16(const UINT8* Data)
{
return (Data[0x00] << 0) | (Data[0x01] << 8);
}
INLINE void WriteBE16(UINT8* Buffer, UINT16 Value)
{
Buffer[0x00] = (Value & 0xFF00) >> 8;
Buffer[0x01] = (Value & 0x00FF) >> 0;
return;
}
INLINE void WriteBE32(UINT8* Buffer, UINT32 Value)
{
Buffer[0x00] = (Value & 0xFF000000) >> 24;
Buffer[0x01] = (Value & 0x00FF0000) >> 16;
Buffer[0x02] = (Value & 0x0000FF00) >> 8;
Buffer[0x03] = (Value & 0x000000FF) >> 0;
return;
}