-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathtop2mid.c
1343 lines (1204 loc) · 32.4 KB
/
top2mid.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
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Tales of Phantasia SPC -> Midi Converter
// ----------------------------------------
// Written by Valley Bell, 2012, 2014, 2016, 2019
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
#include "stdtype.h"
#include <stdbool.h>
#ifdef _MSC_VER
#define stricmp _stricmp
#else
#define stricmp strcasecmp
#endif
#include "midi_funcs.h"
typedef struct _track_info
{
UINT16 startOfs;
UINT16 loopOfs;
UINT32 tickCnt;
UINT32 loopTick;
UINT16 loopTimes;
UINT8 mode;
} TRK_INF;
typedef struct running_event
{
UINT8 evt; // MIDI event + channel
UINT8 val1; // parameter 1
UINT8 val2; // parameter 2
UINT32 remLen;
// user parameters
UINT8 user1; // [note event] orignal Note value - used to extend notes
UINT8 user2; // [note event] additional flags
} RUN_EVT;
//#define RUNNING_NOTES // we need a custom implementation of this here
#define BALANCE_TRACK_TIMES
#include "midi_utils.h"
#define INS_MAX_INSTRUMENTS 0x100
#define INS_DATA_BLK_LINES 1
#define INS_LINE_CNT (INS_MAX_INSTRUMENTS * INS_DATA_BLK_LINES)
typedef struct instrument_setup
{
UINT8 MidiIns;
INT8 NoteMove;
UINT8 DrumBase;
} INS_SETUP;
typedef struct channel_data_seq
{
UINT8 Ins;
INT8 Move;
UINT8 DrmNote;
UINT8 DefaultChn;
UINT8 ModEnable;
UINT8 ModDelay;
UINT8 ModRange;
UINT8 ModSpeed;
} CHN_DATA_SEQ;
typedef struct channel_data_midi
{
UINT8 Vol;
UINT8 Pan;
UINT8 Expr;
UINT8 Mod;
UINT8 PBRange;
UINT8 RPN_MSB;
UINT8 RPN_LSB;
} CHN_DATA_MID;
typedef struct channel_data_drums
{
UINT8 Pitch;
UINT8 Volume;
UINT8 Pan;
} CHN_DATA_DRM;
void ReadInsMappingFile(const char* FileName);
static INS_SETUP* GetInsSetupData(UINT8 Ins, UINT8 Note);
static void WriteRPN(CHN_DATA_MID* MidChnData, FILE_INF* fInf, MID_TRK_STATE* MTS,
UINT8 RpnMSB, UINT8 RpnLSB, UINT8 RpnData);
UINT8 ToP2Mid(void);
static void PreparseSeq(TRK_INF* trkInf, UINT16 BasePtr);
static UINT32 GetMaxRunEventLen(UINT16 runEvtCnt, const RUN_EVT* runEvts);
static RUN_EVT* AddRunningEvent(UINT16 runEvtMax, UINT16* runEvtCnt, RUN_EVT* runEvts,
UINT8 evt, UINT8 val1, UINT8 val2, UINT32 timeout);
static void CheckRunningEvents(FILE_INF* fInf, UINT32* delay, UINT16* runEvtCnt, RUN_EVT* runEvts);
static void FlushRunningEvents(FILE_INF* fInf, UINT32* delay, UINT16* runEvtCnt, RUN_EVT* runEvts);
static UINT8 MidiDelayHandler(FILE_INF* fInf, UINT32* delay);
static UINT16 ReadLE16(const UINT8* Data);
static UINT32 Tempo2Mid(UINT16 bpm, UINT8 scale);
static double Lin2DB(UINT8 LinVol);
static UINT8 DB2Mid(double DB);
static UINT16 MIDI_RES = 24;
static UINT8 NUM_LOOPS;
static UINT8 NO_LOOP_EXT;
static UINT32 SpcLen;
static UINT8* SpcData;
static UINT32 MidLen;
static UINT8* MidData;
#define MAX_RUN_EVTS 0x20
static UINT16 RunEventCnt;
static RUN_EVT RunEvents[MAX_RUN_EVTS];
static INS_SETUP InsSetup[INS_LINE_CNT];
static bool FixInsSet;
static bool FixVolume;
static bool ConvModulation;
static bool WriteDbgCtrls;
static UINT32* midWrtTick = NULL;
int main(int argc, char* argv[])
{
FILE* hFile;
char* StrPtr;
char TempArr[0x08];
int RetVal;
printf("ToP SPC -> Midi Converter\n-------------------------\n");
if (argc < 4)
{
printf("Usage: top2mid.exe Options Song.spc Song.mid [InsMap.ini]\n");
printf("Options: (options can be combined)\n");
printf(" r Raw conversion (other options are ignored)\n");
printf(" i fix Instruments (needs InsMap.ini)\n");
printf(" v fix Volume (convert linear SNES to logarithmic MIDI)\n");
printf(" m convert vibrato into Modulation CCs\n");
printf(" d write Debug Controllers (for unknown events)\n");
printf(" x no loop extension\n");
printf("Supported games: Tales Of Phantasia SFC and Star Ocean.\n");
return 0;
}
MidiDelayCallback = MidiDelayHandler;
FixInsSet = false;
FixVolume = false;
ConvModulation = false;
WriteDbgCtrls = false;
NO_LOOP_EXT = 0;
NUM_LOOPS = 2;
StrPtr = argv[1];
while(*StrPtr != '\0')
{
switch(toupper(*StrPtr))
{
case 'R':
FixInsSet = false;
FixVolume = false;
ConvModulation = false;
break;
case 'I':
FixInsSet = true;
break;
case 'V':
FixVolume = true;
break;
case 'M':
ConvModulation = true;
break;
case 'D':
WriteDbgCtrls = true;
break;
case 'X':
NO_LOOP_EXT = 1;
break;
}
StrPtr ++;
}
if (FixInsSet)
{
if (argc <= 4)
{
printf("Insufficient arguments!\n");
return 9;
}
ReadInsMappingFile(argv[4]);
}
hFile = fopen(argv[2], "rb");
if (hFile == NULL)
{
printf("Error opening file!\n");
return 1;
}
fread(TempArr, 0x01, 0x08, hFile);
if (strncmp(TempArr, "SNES-SPC", 0x08))
{
fclose(hFile);
printf("Not an SPC file!\n");
return 2;
}
fseek(hFile, 0x100, SEEK_SET); // jump to SPC RAM dump
SpcLen = 0x10000; // 64 KB
SpcData = (UINT8*)malloc(SpcLen);
fread(SpcData, 0x01, SpcLen, hFile);
fclose(hFile);
RetVal = ToP2Mid();
if (RetVal)
return 3;
hFile = fopen(argv[3], "wb");
if (hFile == NULL)
{
printf("Error opening file!\n");
return 1;
}
fwrite(MidData, 0x01, MidLen, hFile);
fclose(hFile);
printf("Done.\n");
#ifdef _DEBUG
getchar();
#endif
return 0;
}
void ReadInsMappingFile(const char* FileName)
{
FILE* hFile;
char TempStr[0x100];
char* TempPnt;
char* EndPnt;
UINT16 InsNum;
UINT8 InsLine;
INS_SETUP* TempIns;
hFile = fopen(FileName, "rt");
if (hFile == NULL)
{
printf("Error opening %s\n", FileName);
return;
}
TempIns = InsSetup;
for (InsNum = 0x00; InsNum < INS_MAX_INSTRUMENTS; InsNum ++)
{
for (InsLine = 0x00; InsLine < INS_DATA_BLK_LINES; InsLine ++, TempIns ++)
{
TempIns->MidiIns = 0xFF/*InsNum*/;
TempIns->NoteMove = 0x00;
TempIns->DrumBase = 0xFF;
}
}
while(! feof(hFile))
{
TempPnt = fgets(TempStr, 0x100, hFile);
if (TempPnt == NULL)
break;
if (TempStr[0x00] == '\n' || TempStr[0x00] == '\0')
continue;
if (TempStr[0x00] == ';')
{
// skip comment lines
// fgets has set a null-terminator at char 0xFF
while(TempStr[strlen(TempStr) - 1] != '\n')
{
fgets(TempStr, 0x100, hFile);
if (TempStr[0x00] == '\0')
break;
}
continue;
}
TempPnt = strchr(TempStr, '\t');
if (TempPnt == NULL || TempPnt == TempStr)
continue; // invalid line
InsNum = (UINT8)strtoul(TempStr, &EndPnt, 0x10);
if (EndPnt == TempStr || InsNum >= INS_MAX_INSTRUMENTS)
continue;
if (EndPnt != NULL && *EndPnt == '-')
InsLine = (UINT8)strtoul(EndPnt + 1, NULL, 0x10);
else
InsLine = 0x00;
//InsLine &= 0x0F;
//TempIns = &InsSetup[(InsNum << 4) | InsLine];
TempIns = &InsSetup[InsNum];
TempPnt ++;
if (*TempPnt != 'D')
{
TempIns->MidiIns = (UINT8)strtoul(TempPnt, NULL, 0x10);
TempPnt = strchr(TempPnt, '\t');
if (TempPnt != NULL)
{
TempPnt ++;
TempIns->NoteMove = (UINT8)strtoul(TempPnt, NULL, 10);
}
else
{
TempIns->NoteMove = 0x00;
}
TempIns->DrumBase = 0xFF;
}
else
{
TempPnt ++;
TempIns->MidiIns = 0x80;
TempIns->NoteMove = (UINT8)strtoul(TempPnt, NULL, 0x10);
TempPnt = strchr(TempPnt, '\t');
if (TempPnt != NULL)
{
TempPnt ++;
TempIns->DrumBase = (UINT8)strtoul(TempPnt, NULL, 0x10);
}
else
{
TempIns->DrumBase = 0x00;
}
if (! TempIns->DrumBase)
TempIns->DrumBase = TempIns->NoteMove;
}
}
fclose(hFile);
return;
}
static INS_SETUP* GetInsSetupData(UINT8 Ins, UINT8 Note)
{
INS_SETUP* TempIS = &InsSetup[Ins];
if (TempIS->MidiIns == 0xFF)
{
printf("Warning: Unmapped instrument 0x%02X!\n", Ins);
TempIS->MidiIns = Ins & 0x7F;
}
return TempIS;
}
static void WriteRPN(CHN_DATA_MID* MidChnData, FILE_INF* fInf, MID_TRK_STATE* MTS,
UINT8 RpnMSB, UINT8 RpnLSB, UINT8 RpnData)
{
UINT8 RPNCtrl;
if (RpnMSB & 0x80)
RPNCtrl = 0x62; // NRPN
else
RPNCtrl = 0x64; // RPN
// sadly, this doesn't work, because I process every track separately
// if (MidChnData->RPN_MSB != RpnMSB || MidChnData->RPN_LSB != RpnLSB)
{
WriteEvent(fInf, MTS, 0xB0, RPNCtrl | 0x01, RpnMSB & 0x7F);
WriteEvent(fInf, MTS, 0xB0, RPNCtrl | 0x00, RpnLSB & 0x7F);
MidChnData->RPN_MSB = RpnMSB;
MidChnData->RPN_LSB = RpnLSB;
}
if (! (RpnData & 0x80))
WriteEvent(fInf, MTS, 0xB0, 0x06, RpnData);
return;
}
#define TRK_COUNT 0x0F
UINT8 ToP2Mid(void)
{
static const UINT8 DRIVER_SIG[0x14] =
{ 0x20, 0xE8, 0x00, 0xC4, 0xF4, 0xC4, 0xF5, 0xC4,
0xF6, 0xC4, 0xF7, 0xC4, 0x83, 0x8F, 0x30, 0xF1,
0xCD, 0xFF, 0xBD, 0x3F};
UINT16 BasePtr;
UINT8 InitTempo;
TRK_INF TrkInfo[TRK_COUNT];
TRK_INF* TempTInf;
UINT8 CurTrk;
UINT16 SegBase;
UINT16 SegIdx;
UINT16 InPos;
FILE_INF midFileInf;
MID_TRK_STATE MTS;
bool TrkEnd;
UINT8 CurCmd;
UINT8 LoopIdx;
UINT16 LoopCur[0x10]; // current loop counter (16-bit due to loop extension)
UINT16 LoopPos[0x10]; // loop offset
UINT16 LoopSeg[0x10]; // loop segment
CHN_DATA_SEQ ChnSeq;
CHN_DATA_MID ChnMid;
CHN_DATA_DRM ChnDrm[0x80];
INS_SETUP* TempIS;
CHN_DATA_DRM* TempChD;
UINT8 TempArr[0x04];
UINT32 TempLng;
INT16 TempSSht;
UINT8 TempByt;
UINT8 LastChn;
UINT8 NoteVol;
UINT8 ChnVol;
UINT32 midiTick;
UINT32 modStartTick;
UINT32 modEndTick;
UINT8 modState; // 0 - off, 1 - static, 2 - wait for on, 3 - wait for off
if (memcmp(&SpcData[0x0840], DRIVER_SIG, 0x14))
{
printf("This SPC uses an unsupported sound driver!\n");
return 0xFF;
}
BasePtr = ReadLE16(&SpcData[0x0854]);
midFileInf.alloc = 0x40000; // 256 KB should be enough (so-37 has 66.4 KB)
midFileInf.data = (UINT8*)malloc(midFileInf.alloc);
midFileInf.pos = 0x00;
midWrtTick = &midiTick;
WriteMidiHeader(&midFileInf, 0x0001, 1 + TRK_COUNT, MIDI_RES);
InPos = BasePtr + 0x0020;
WriteMidiTrackStart(&midFileInf, &MTS);
MTS.midChn = 0x00;
InPos += 0x02; // skip file size
InitTempo = SpcData[InPos];
TempLng = Tempo2Mid(InitTempo, 0x40);
WriteBE32(TempArr, TempLng);
WriteMetaEvent(&midFileInf, &MTS, 0x51, 0x03, &TempArr[0x01]);
InPos ++;
if (FixInsSet)
WriteEvent(&midFileInf, &MTS, 0xC9, 0x00, 0x00); // make sure that the drum settings are reset
WriteEvent(&midFileInf, &MTS, 0xFF, 0x2F, 0x00);
WriteMidiTrackEnd(&midFileInf, &MTS);
for (CurTrk = 0; CurTrk < TRK_COUNT; CurTrk ++, InPos += 0x03)
{
TempTInf = &TrkInfo[CurTrk];
TempTInf->mode = SpcData[InPos + 0x00];
TempTInf->startOfs = ReadLE16(&SpcData[InPos + 0x01]);
TempTInf->loopOfs = 0x0000;
TempTInf->tickCnt = 0;
TempTInf->loopTick = 0;
PreparseSeq(TempTInf, BasePtr);
TempTInf->loopTimes = TempTInf->loopOfs ? NUM_LOOPS : 0;
}
if (! NO_LOOP_EXT)
BalanceTrackTimes(TRK_COUNT, TrkInfo, MIDI_RES / 4, 0xFF);
for (CurTrk = 0; CurTrk < TRK_COUNT; CurTrk ++)
{
TempTInf = &TrkInfo[CurTrk];
SegBase = BasePtr + TempTInf->startOfs;
WriteMidiTrackStart(&midFileInf, &MTS);
TrkEnd = ! (TempTInf->mode >> 7);
LoopIdx = 0x00;
MTS.midChn = CurTrk + (CurTrk + 6) / 15;
LastChn = MTS.midChn;
NoteVol = 0x7F;
ChnVol = 0x64;
RunEventCnt = 0;
midiTick = 0;
modState = 0;
modStartTick = (UINT32)-1;
modEndTick = 0;
SegIdx = 0x00;
InPos = 0x0000;
ChnSeq.Ins = 0xFF;
ChnSeq.Move = 0;
ChnSeq.DrmNote = 0xFF;
ChnSeq.DefaultChn = MTS.midChn;
ChnSeq.ModEnable = 0;
ChnSeq.ModDelay = 0;
ChnSeq.ModRange = 0;
ChnSeq.ModSpeed = 0;
ChnMid.Mod = 0;
ChnMid.PBRange = 0x00;
ChnMid.RPN_MSB = 0x7F; // RPN Null
ChnMid.RPN_LSB = 0x7F;
for (TempByt = 0x00; TempByt < 0x80; TempByt ++)
{
TempChD = &ChnDrm[TempByt];
TempChD->Pitch = 0x40;
TempChD->Volume = 0x7F;
TempChD->Pan = 0x40;
}
while(! TrkEnd)
{
if (InPos == 0x0000)
{
//if (SegIdx == 0xFFFF)
// break;
InPos = ReadLE16(&SpcData[SegBase + SegIdx * 0x02]);
if (InPos == 0xFFFF)
break;
InPos += BasePtr;
SegIdx ++;
}
if (modState == 2 && midiTick + MTS.curDly >= modStartTick && modStartTick < modEndTick)
{
if (midiTick > modStartTick)
{
printf("Modulation bug! Please report!\n");
}
else
{
// write Modulation On at modStartTick
UINT32 remTicks = MTS.curDly;
MTS.curDly = modStartTick - midiTick;
remTicks -= MTS.curDly;
WriteEvent(&midFileInf, &MTS, 0xB0, 0x01, ChnMid.Mod);
MTS.curDly += remTicks;
modState = 3;
}
modStartTick = (UINT32)-1;
}
if (modState == 3 && midiTick + MTS.curDly >= modEndTick)
{
if (midiTick > modEndTick)
{
printf("Modulation bug! Please report!\n");
}
else
{
// write Modulation On at modEndTick
UINT32 remTicks = MTS.curDly;
MTS.curDly = modEndTick - midiTick;
remTicks -= MTS.curDly;
WriteEvent(&midFileInf, &MTS, 0xB0, 0x01, 0);
MTS.curDly += remTicks;
}
modState = 2;
}
CurCmd = SpcData[InPos];
if (CurCmd < 0x90)
{
UINT8 midiNote;
UINT8 noteLen;
UINT8 noteVel;
UINT8 cmdDelay;
UINT16 curRN;
RUN_EVT* rn;
cmdDelay = SpcData[InPos + 0x01];
noteLen = SpcData[InPos + 0x02];
noteVel = SpcData[InPos + 0x03];
InPos += 0x04;
if (ChnSeq.DrmNote == 0xFF)
{
// normal Melody instrument note
TempSSht = (INT16)CurCmd + ChnSeq.Move;
if (TempSSht < 0x00)
midiNote = 0x00;
else if (TempSSht > 0x7F)
midiNote = 0x7F;
else
midiNote = (UINT8)TempSSht;
}
else
{
TempIS = GetInsSetupData(ChnSeq.Ins, CurCmd);
if (TempIS->DrumBase != 0xFF)
{
// single Drum instrument with varying pitch
// -> convert to NRPN: Drum Pitch + Drum Note
TempChD = &ChnDrm[ChnSeq.DrmNote];
// The "default" pitch is 0x40. 00..3F is lower, 41..7F is higher.
TempSSht = (INT16)CurCmd - TempIS->DrumBase + 0x40;
if (TempSSht < 0x00)
TempByt = 0x00;
else if (TempSSht > 0x7F)
TempByt = 0x7F;
else
TempByt = (UINT8)TempSSht;
if (TempChD->Pitch != TempByt)
{
// NRPN: Drum Note Pitch
WriteRPN(&ChnMid, &midFileInf, &MTS, 0x98, ChnSeq.DrmNote, TempByt);
TempChD->Pitch = TempByt;
}
midiNote = TempIS->NoteMove;
}
else
{
// instrument with multiple drums
// This would need a lookup table.
midiNote = TempIS->NoteMove;
}
midiNote |= 0x80;
}
midiTick += MTS.curDly;
CheckRunningEvents(&midFileInf, &MTS.curDly, &RunEventCnt, RunEvents);
midiTick -= MTS.curDly;
for (curRN = 0; curRN < RunEventCnt; curRN ++)
{
if (RunEvents[curRN].user1 == CurCmd)
break;
}
if (curRN < RunEventCnt)
{
rn = &RunEvents[curRN];
rn->remLen = MTS.curDly + noteLen; // extend playing note
if (rn->user2 & 0x01) // uses "note cut"?
{
UINT16 curRNSub;
for (curRNSub = curRN + 1; curRNSub < RunEventCnt; curRNSub ++)
{
if (RunEvents[curRNSub].user1 == 0xFF)
{
// adjust length of "sound cut" note
RunEvents[curRNSub + 0].remLen = MTS.curDly + noteLen;
RunEvents[curRNSub + 1].remLen = MTS.curDly + noteLen;
break;
}
}
}
CurCmd = 0xFF;
}
if (CurCmd != 0xFF)
{
UINT32 timeout = noteLen;
UINT8 offNote = 0xFF;
if (midiNote == (0x80 | 0x2E))
offNote = 0x2C;
midiNote &= 0x7F;
if (modState == 3)
{
// When starting a new note, modulation gets reset for still-playing notes
// as well, so this behaves just like the sound driver. (see so-06.spc)
WriteEvent(&midFileInf, &MTS, 0xB0, 0x01, 0);
modState = 2;
modStartTick = (UINT32)-1;
}
if (FixVolume)
NoteVol = DB2Mid(Lin2DB(noteVel));
else
NoteVol = noteVel >> 1;
WriteEvent(&midFileInf, &MTS, 0x90, midiNote, NoteVol);
rn = AddRunningEvent(MAX_RUN_EVTS, &RunEventCnt, RunEvents, 0x90 | MTS.midChn, midiNote, 0x00, timeout);
rn->user1 = CurCmd;
if (offNote != 0xFF) // additional note to cut the sound at Note Off
{
rn->user2 |= 0x01;
rn = AddRunningEvent(MAX_RUN_EVTS, &RunEventCnt, RunEvents, 0x90 | MTS.midChn, offNote, 1, timeout);
rn->user1 = 0xFF;
rn = AddRunningEvent(MAX_RUN_EVTS, &RunEventCnt, RunEvents, 0x90 | MTS.midChn, offNote, 0, timeout + 1);
rn->user1 = 0xFF;
}
if (modState == 2)
modStartTick = midiTick + MTS.curDly + ChnSeq.ModDelay;
}
modEndTick = (RunEventCnt == 0) ? (UINT32)-1 : (midiTick + GetMaxRunEventLen(RunEventCnt, RunEvents));
MTS.curDly += cmdDelay;
}
else
{
switch(CurCmd)
{
case 0x90: // Delay
MTS.curDly += SpcData[InPos + 0x01];
InPos += 0x02;
break;
case 0x92: // Loop Start
if (InPos == TempTInf->loopOfs)
{
WriteEvent(&midFileInf, &MTS, 0xB0, 0x6F, 0x00);
}
else if (WriteDbgCtrls)
{
WriteEvent(&midFileInf, &MTS, 0xB0, 0x70, LoopIdx);
WriteEvent(&midFileInf, &MTS, 0xB0, 0x26, 0x00);
}
InPos += 0x01;
LoopSeg[LoopIdx] = SegIdx;
LoopPos[LoopIdx] = InPos;
LoopCur[LoopIdx] = 0x00;
LoopIdx ++;
break;
case 0x93: // Loop End
TempByt = SpcData[InPos + 0x01]; // loop count
InPos += 0x02;
if (! LoopIdx)
{
// missing Loop Start (see ToP 219)
printf("Trk %u: Warning: Loop End without Loop Start at 0x%04X\n", CurTrk, InPos - 0x02);
//LoopSeg[LoopIdx] = 0x01; // start from beginning of the 2nd segment
//LoopPos[LoopIdx] = 0x0000;
//LoopCur[LoopIdx] = 0x00;
//LoopIdx ++;
if (! TempByt)
{
WriteEvent(&midFileInf, &MTS, 0xB0, 0x6F, 0x01);
}
else if (WriteDbgCtrls)
{
WriteEvent(&midFileInf, &MTS, 0xB0, 0x70, LoopIdx);
WriteEvent(&midFileInf, &MTS, 0xB0, 0x26, 0x7F);
}
break;
}
LoopIdx --;
LoopCur[LoopIdx] ++;
if (! TempByt && LoopCur[LoopIdx] <= 0x7F)
{
WriteEvent(&midFileInf, &MTS, 0xB0, 0x6F, (UINT8)LoopCur[LoopIdx]);
}
else if (WriteDbgCtrls)
{
WriteEvent(&midFileInf, &MTS, 0xB0, 0x70, LoopIdx);
WriteEvent(&midFileInf, &MTS, 0xB0, 0x26, LoopCur[LoopIdx] & 0x7F);
}
if (LoopCur[LoopIdx] < TempByt ||
(! TempByt && LoopCur[LoopIdx] < TempTInf->loopTimes))
{
SegIdx = LoopSeg[LoopIdx];
InPos = LoopPos[LoopIdx];
LoopIdx ++;
}
break;
case 0x94: // Pitch Bend
TempByt = 12;
if (ChnMid.PBRange != TempByt)
{
WriteRPN(&ChnMid, &midFileInf, &MTS, 0x00, 0x00, TempByt);
ChnMid.PBRange = TempByt;
}
WriteEvent(&midFileInf, &MTS, 0xE0, 0x00, SpcData[InPos + 0x02]);
MTS.curDly += SpcData[InPos + 0x01];
InPos += 0x03;
break;
case 0x95: // Set Tempo (relative to initial tempo)
TempByt = SpcData[InPos + 0x02];
TempLng = Tempo2Mid(InitTempo, TempByt); // RCP-stype tempo
WriteBE32(TempArr, TempLng);
WriteMetaEvent(&midFileInf, &MTS, 0x51, 0x03, &TempArr[0x01]);
if (WriteDbgCtrls)
WriteEvent(&midFileInf, &MTS, 0xB0, 0x26, SpcData[InPos + 0x02]);
MTS.curDly += SpcData[InPos + 0x01];
InPos += 0x03;
break;
case 0x96: // Set Instrument
TempByt = SpcData[InPos + 0x01];
ChnSeq.Ins = TempByt;
if (FixInsSet)
{
TempIS = GetInsSetupData(ChnSeq.Ins, 0xFF);
TempByt = TempIS->MidiIns & 0x7F;
// MidiIns 80..FF -> Drum Note
if ((TempIS->MidiIns & 0x80) && TempIS->DrumBase != 0xFF)
{
// Drum Instrument: one drum with varying pitch
ChnSeq.DrmNote = (UINT8)TempIS->NoteMove;
if (MTS.midChn != 0x09)
{
MTS.midChn = 0x09;
// copy Volume/Pan to Drum Channel
WriteRPN(&ChnMid, &midFileInf, &MTS,
0x9A, ChnSeq.DrmNote, ChnMid.Vol); // Drum Volume
WriteRPN(&ChnMid, &midFileInf, &MTS,
0x9C, ChnSeq.DrmNote, ChnMid.Pan); // Drum Panorama
}
}
else
{
if ((TempIS->MidiIns & 0x80) && TempIS->DrumBase == 0xFF)
{
// generic Drum Channel
MTS.midChn = 0x09;
ChnSeq.Move = 0;
ChnSeq.DrmNote = (UINT8)TempIS->NoteMove;
}
else
{
// normal Instrument
if (MTS.midChn == 0x09)
{
// copy Volume/Pan back to Normal Channel
MTS.midChn = ChnSeq.DefaultChn;
WriteEvent(&midFileInf, &MTS, 0xB0, 0x07, ChnMid.Vol); // Volume
WriteEvent(&midFileInf, &MTS, 0xB0, 0x0A, ChnMid.Pan); // Panorama
}
MTS.midChn = ChnSeq.DefaultChn;
ChnSeq.Move = TempIS->NoteMove;
ChnSeq.DrmNote = 0xFF;
}
}
}
if (ChnSeq.DrmNote == 0xFF)
WriteEvent(&midFileInf, &MTS, 0xC0, TempByt, 0x00);
InPos += 0x02;
break;
case 0x97: // another Volume setting?
case 0x98: // Set Volume
if (FixVolume)
TempByt = DB2Mid(Lin2DB(SpcData[InPos + 0x02]));
else
TempByt = SpcData[InPos + 0x02] >> 1;
if (CurCmd == 0x97)
{
ChnMid.Vol = TempByt;
if (ChnSeq.DrmNote == 0xFF)
WriteEvent(&midFileInf, &MTS, 0xB0, 0x07, ChnMid.Vol);
else
WriteRPN(&ChnMid, &midFileInf, &MTS, 0x9A, ChnSeq.DrmNote, ChnMid.Vol);
}
else //if (CurCmd == 0x98)
{
ChnMid.Expr = TempByt;
WriteEvent(&midFileInf, &MTS, 0xB0, 0x0B, ChnMid.Expr);
}
MTS.curDly += SpcData[InPos + 0x01];
InPos += 0x03;
break;
case 0x99: // Set Pan
ChnMid.Pan = 0x80 - SpcData[InPos + 0x02];
if (ChnMid.Pan >= 0x80)
ChnMid.Pan = 0x7F;
if (ChnSeq.DrmNote == 0xFF)
WriteEvent(&midFileInf, &MTS, 0xB0, 0x0A, ChnMid.Pan);
else
WriteRPN(&ChnMid, &midFileInf, &MTS, 0x9C, ChnSeq.DrmNote, ChnMid.Pan);
MTS.curDly += SpcData[InPos + 0x01];
InPos += 0x03;
break;
case 0x9B: // Modulation Enable
ChnSeq.ModEnable = SpcData[InPos + 0x01];
if (WriteDbgCtrls)
WriteEvent(&midFileInf, &MTS, 0xB0, 0x21, SpcData[InPos + 0x01]);
InPos += 0x02;
if (ChnSeq.ModEnable && ConvModulation)
{
if (modState == 0) // activate modulation
{
if (ChnSeq.ModDelay == 0)
{
if (ChnSeq.ModRange > 0)
WriteEvent(&midFileInf, &MTS, 0xB0, 0x01, ChnMid.Mod);
modState = 1; // "static" mode
}
else
{
modState = 2; // "dynamic" mode (on/off after delay)
}
if (modState == 2 && RunEventCnt > 0)
{
modStartTick = midiTick + MTS.curDly + ChnSeq.ModDelay;
modEndTick = midiTick + GetMaxRunEventLen(RunEventCnt, RunEvents);
}
else
{
modStartTick = (UINT32)-1;
modEndTick = (UINT32)-1;
}
}
}
else //if (! ChnSeq.ModEnable)
{
if (modState == 1 || modState == 3)
WriteEvent(&midFileInf, &MTS, 0xB0, 0x01, 0);
modState = 0;
}
break;
case 0x9C: // Modulation Parameters
ChnSeq.ModDelay = SpcData[InPos + 0x01];
ChnSeq.ModRange = SpcData[InPos + 0x02]; // 0x80 = 1 semitone up + down
ChnSeq.ModSpeed = SpcData[InPos + 0x03]; // higher = faster
//printf("Trk %u: Vibrato Params: Delay %u, Range %u, Speed %u\n", CurTrk,
// ChnSeq.ModDelay, ChnSeq.ModRange, ChnSeq.ModSpeed);
if (WriteDbgCtrls)
{
WriteEvent(&midFileInf, &MTS, 0xB0, 0x10, SpcData[InPos + 0x01]);
WriteEvent(&midFileInf, &MTS, 0xB0, 0x12, SpcData[InPos + 0x02]);
WriteEvent(&midFileInf, &MTS, 0xB0, 0x13, SpcData[InPos + 0x03]);
}
InPos += 0x04;
if (0)
{
ChnMid.Mod = ChnSeq.ModRange;
}
else
{
// The sound driver's modulation range seems to be 0x80 = 1 semitone.
// General MIDI (apparently) has 0.5 semitones for value 0x80.
// So we apply a square root here to make it sound nicer.
double modVal = ChnSeq.ModRange / 128.0;
modVal = sqrt(modVal);
ChnMid.Mod = (UINT8)(modVal * 128.0 + 0.5);
}
if (ChnMid.Mod >= 0x80)
ChnMid.Mod = 0x7F;
if (ChnSeq.ModEnable && ConvModulation)
{
if (ChnSeq.ModDelay == 0)
{
WriteEvent(&midFileInf, &MTS, 0xB0, 0x01, ChnMid.Mod);
modState = 1; // "static" mode
}
else
{
if (modState == 1 || modState == 3)
WriteEvent(&midFileInf, &MTS, 0xB0, 0x01, 0);
modState = 2;
}
if (modState == 2 && RunEventCnt > 0)
{
modStartTick = midiTick + MTS.curDly + ChnSeq.ModDelay;
modEndTick = midiTick + GetMaxRunEventLen(RunEventCnt, RunEvents);
}
else
{
modStartTick = (UINT32)-1;
modEndTick = (UINT32)-1;
}
}
break;
case 0xA2: // Detune
WriteRPN(&ChnMid, &midFileInf, &MTS, 0x00, 0x01, SpcData[InPos + 0x01]);
WriteEvent(&midFileInf, &MTS, 0xB0, 0x26, 0x00);
InPos += 0x02;
break;
case 0xA3:
if (WriteDbgCtrls)
{
WriteEvent(&midFileInf, &MTS, 0xB0, 0x6D, CurCmd & 0x7F);
WriteEvent(&midFileInf, &MTS, 0xB0, 0x26, SpcData[InPos + 0x01]);
}
InPos += 0x02;
break;
case 0xAA: // set Reverb/Chorus
//WriteEvent(&midFileInf, &MTS, 0xB0, 0x6D, CurCmd & 0x7F);
if (0 || WriteDbgCtrls)
{
// These are global settings, so I write them to all channels.
UINT8 chnBak = MTS.midChn;
for (MTS.midChn = 0x00; MTS.midChn < 0x10; MTS.midChn ++)
{
WriteEvent(&midFileInf, &MTS, 0xB0, 0x5B, SpcData[InPos + 0x01]);
WriteEvent(&midFileInf, &MTS, 0xB0, 0x5D, SpcData[InPos + 0x02]);
}
MTS.midChn = chnBak;
}
InPos += 0x03;
break;