-
Notifications
You must be signed in to change notification settings - Fork 3
/
wwdumpsnd.c
1717 lines (1507 loc) · 43.4 KB
/
wwdumpsnd.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
/*
* wwdumpsnd 0.4 by hcs
* some stuff added by impiaaa
* dump audio from Wind Waker or Super Mario Sunshine
* needs JaiInit.aaf or msound.aaf, and *.aw in current directory, Banks/, or
* Waves/
*/
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#ifdef _MSC_VER
#include <intrin.h>
#else
#include <x86intrin.h>
#endif
typedef signed short s16;
/* read big endian */
unsigned int read32(unsigned char * buf) {
return (buf[0]<<24) | (buf[1]<<16) | (buf[2]<<8) | buf[3];
}
float readfloat(unsigned char * buf) {
union {
float f;
unsigned int ui32;
} x;
x.ui32 = read32(buf);
return x.f;
}
unsigned int read16(unsigned char * buf) {
return (buf[0]<<8) | buf[1];
}
/* write little endian (for WAV) */
void write32le(int in, unsigned char * buf) {
buf[0]=in&0xff;
buf[1]=(in>>8)&0xff;
buf[2]=(in>>16)&0xff;
buf[3]=(in>>24)&0xff;
}
void write16le(int in, unsigned char * buf) {
buf[0]=in&0xff;
buf[1]=(in>>8)&0xff;
}
/* AFC decoder */
const short afccoef[] __attribute__ ((aligned (16))) = {
0x0000,0x0000,
0x0800,0x0000,
0x0000,0x0800,
0x0400,0x0400,
0x1000,0xF800,
0x0E00,0xFA00,
0x0C00,0xFC00,
0x1200,0xF600,
0x1068,0xF738,
0x12C0,0xF704,
0x1400,0xF400,
0x0800,0xF800,
0x0400,0xFC00,
0xFC00,0x0400,
0xFC00,0x0000,
0xF800,0x0000
};
enum SamplesSourceType
{
// Samples stored in ARAM at a rate of 16 samples/5 bytes, AFC encoded
SRC_AFC_LQ_FROM_ARAM = 5,
// Samples stored in ARAM in PCM8 format
SRC_PCM8_FROM_ARAM = 8,
// Samples stored in ARAM at a rate of 16 samples/9 bytes, AFC encoded
SRC_AFC_HQ_FROM_ARAM = 9,
// Samples stored in ARAM in PCM16 format
SRC_PCM16_FROM_ARAM = 16,
};
/* from Dolphin's "UCode_Zelda_ADPCM.cpp" */
void AFCdecodebuffer(const s16 *coef, const char *src, signed short *out, short *histp, short *hist2p, enum SamplesSourceType type)
{
short nibbles[16] __attribute__ ((aligned (16)));
short delta = 1 << ((*src >> 4) & 0xF);
short idx = (*src & 0xF);
src++;
if (type == SRC_AFC_HQ_FROM_ARAM)
{
for (size_t i = 0; i < 16; i += 2)
{
nibbles[i + 0] = *src >> 4;
nibbles[i + 1] = *src & 0xF;
src++;
}
for (size_t i = 0; i < 16; i++) {
if (nibbles[i] >= 8)
nibbles[i] -= 16;
nibbles[i] <<= 11;
}
}
else if (type == SRC_AFC_LQ_FROM_ARAM)
{
// In Pikmin, Dolphin's engine sound is using AFC type 5, even though such a sound is hard
// to compare, it seems like to sound exactly like a real GC
// In Super Mario Sunshine, you can get such a sound by talking to/jumping on anyone
for (size_t i = 0; i < 16; i += 4)
{
nibbles[i + 0] = (*src >> 6) & 3;
nibbles[i + 1] = (*src >> 4) & 3;
nibbles[i + 2] = (*src >> 2) & 3;
nibbles[i + 3] = (*src >> 0) & 3;
src++;
}
for (size_t i = 0; i < 16; i++)
{
// 0 -> 0x0000
// 1 -> 0x2000
// 2 -> 0xc000
// 3 -> 0xe000
if (nibbles[i] >= 2)
nibbles[i] -= 4;
nibbles[i] <<= 13;
}
}
else
{
fprintf(stderr, "Invalid format %d\n", type);
return;
}
short yn1 = *histp;
short yn2 = *hist2p;
for (size_t i = 0; i < 16; ++i)
{
int sample = delta * nibbles[i] +
(int)yn1 * coef[idx * 2] +
(int)yn2 * coef[idx * 2 + 1];
sample >>= 11;
if (sample > 0x7fff)
sample = 0x7fff;
if (sample < -0x8000)
sample = -0x8000;
out[i] = sample;
yn2 = yn1;
yn1 = (short)sample;
}
*histp = yn1;
*hist2p = yn2;
}
unsigned char wavhead[] = {
'R', 'I', 'F', 'F', 0x00, 0x00, 0x00, 0x00, 'W', 'A', 'V', 'E',
// compression channels sample rate
'f', 'm', 't', ' ', 0x10, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
// bytes/sec block align bits/sample
0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x10, 0x00,
};
int writeWAVHead(FILE * outfile, const int total_size, const int srate, const int bytes_per_sample) {
write32le(total_size-8, wavhead+0x04);
write32le(srate, wavhead+0x18);
write32le(srate*bytes_per_sample, wavhead+0x1C);
write16le(bytes_per_sample, wavhead+0x20);
write16le(bytes_per_sample*8, wavhead+0x22);
if (fwrite(wavhead,1,sizeof(wavhead),outfile)!=sizeof(wavhead)) return 1;
return 0;
}
unsigned char datahead[] = {
'd', 'a', 't', 'a', 0x00, 0x00, 0x00, 0x00
};
/* dump a WAV, decoding AFC */
/* return 0 on success, 1 on failure */
int dumpAFC(FILE * const infile, const int size, enum SamplesSourceType type, FILE * outfile) {
char inbuf[9] __attribute__ ((aligned (16)));
short outbuf[16] __attribute__ ((aligned (16)));
int sizeToDo;
int framesize;
int datasize;
short hist=0,hist2=0;
framesize = (type==SRC_AFC_LQ_FROM_ARAM) ? 5 : 9;
datasize = size/framesize*16*2;
write32le(datasize,datahead+4);
if (fwrite(datahead,1,sizeof(datahead),outfile)!=sizeof(datahead)) return 1;
for (sizeToDo = 0; sizeToDo < size; sizeToDo += framesize) {
if (fread(inbuf,1,framesize,infile) != framesize)
return 2;
AFCdecodebuffer((s16*)afccoef,inbuf,outbuf,&hist,&hist2,type);
if (fwrite(outbuf,1,16*2,outfile) != 16*2)
return 3;
}
return 0;
}
int dumpPCM(FILE * const infile, const int size, enum SamplesSourceType type, FILE * outfile) {
char inbuf[4];
char outbuf[4];
int sizeleft;
int framesize;
framesize = (type==SRC_PCM8_FROM_ARAM) ? 1 : 2;
write32le(size,datahead+4);
if (fwrite(datahead,1,sizeof(datahead),outfile)!=sizeof(datahead)) return 1;
for (sizeleft=size;sizeleft>=framesize;sizeleft-=framesize) {
if (fread(inbuf,1,framesize,infile) != framesize)
return 1;
for (int i = 0; i < framesize; i++) {
outbuf[framesize-i-1] = inbuf[i];
}
if (fwrite(outbuf,1,framesize,outfile) != framesize)
return 1;
}
return 0;
}
unsigned char smplhead[] = {
's', 'm', 'p', 'l', 0, 0, 0, 0,
// manfctr product
0, 0, 0, 0, 0, 0, 0, 0,
// smplper unity note tuning
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
// smptefmt smpteoffset
0, 0, 0, 0, 0, 0, 0, 0,
// numloops smpldata
0, 0, 0, 0, 0, 0, 0, 0
};
unsigned char smplloop[] = {
// cueptid type
0, 0, 0, 0, 1, 0, 0, 0,
// start end
0, 0, 0, 0, 0, 0, 0, 0,
// fraction play count
0, 0, 0, 0, 0, 0, 0, 0
};
int writeSMPL(FILE * outfile, int srate, unsigned char root_key, int loop_start_sample, int loop_end_sample) {
write32le(1000000000/srate, smplhead+0x10);
write32le(root_key, smplhead+0x14);
if (loop_start_sample > 0)
{
write32le(36+24, smplhead+0x04);
write32le(1, smplhead+0x24);
}
else
{
write32le(36, smplhead+0x04);
write32le(0, smplhead+0x24);
}
if (fwrite(smplhead,1,sizeof(smplhead),outfile)!=sizeof(smplhead)) return 1;
if (loop_start_sample > 0)
{
// TODO: Sometimes the start is still off by one!?
write32le(loop_start_sample, smplloop+0x08);
write32le(loop_end_sample-1, smplloop+0x0C);
if (fwrite(smplloop,1,sizeof(smplloop),outfile)!=sizeof(smplloop)) return 1;
}
return 0;
}
int verbose = 0;
char output_dir[256] = ".";
char *aw_dir;
int doaw(FILE *infile, const int offset, int dump) {
FILE * awfile;
int next_aw_offset;
unsigned char buf[4];
int aw_name;
int table_offset;
int wav_count;
int i;
char fname[113]={0};
enum SamplesSourceType type;
/* offset to list of wave table entry offsets */
if (fread(buf,1,4,infile)!=4) return 1;
aw_name = read32(buf) + offset;
table_offset = aw_name+112;
next_aw_offset = ftell(infile);
if (next_aw_offset<0) return 2;
if (fseek(infile,aw_name,SEEK_SET)<0) return 3;
/* aw file name */
if (fread(fname,1,112,infile)!=112) return 4;
if (dump) {
char inname[sizeof(fname)+6];
snprintf(inname, sizeof(inname), "%s/%s", aw_dir, fname);
awfile = fopen(inname,"rb");
if (!awfile) {
fprintf(stderr, "Couldn't open %s\n", inname);
return 5;
}
}
/* number of waves */
if (fread(buf,1,4,infile)!=4) return 6;
wav_count = read32(buf);
if (verbose) {
printf("aw=%s\n",fname);
printf("table at %x, wav_count=%x\n",table_offset,wav_count);
}
// null out file extension
char *extptr = strrchr(fname, '.');
if (extptr != NULL) *extptr = '\0';
if (verbose)
printf("n\toffset\tsize\tsrate\ttype\troot\tstart\tend\tcount\n");
for (i=0;i<wav_count;i++) {
int wav_entry_offset;
int afcoffset,afcsize,srate;
unsigned char type_id, root_key;
unsigned char type_unk0, type_unk3;
char outname[128];
int loop_start_sample, loop_end_sample, num_samples, unk0, unk1, unk2;
if (fseek(infile,table_offset+4+i*4,SEEK_SET)<0) return 7;
if (fread(buf,1,4,infile)!=4) return 8;
wav_entry_offset = read32(buf)+offset;
/* go to the entry */
if (fseek(infile,wav_entry_offset,SEEK_SET)<0) return 9;
/* contains AFC type:
0 = type 9 (9 byte frames, samples from nibbles)
1 = type 5 (5 byte frames, samples from half-nibbles) */
if (fread(buf,1,4,infile)!=4) return 10;
type_unk0 = buf[0];
type_id=buf[1];
root_key = buf[2];
type_unk3 = buf[3];
switch (type_id) {
case 0:
type=SRC_AFC_HQ_FROM_ARAM;
break;
case 1:
type=SRC_AFC_LQ_FROM_ARAM;
break;
case 2:
type=SRC_PCM8_FROM_ARAM;
break;
case 3:
type=SRC_PCM16_FROM_ARAM;
break;
default:
type=0;
break;
}
/* contains srate */
if (fread(buf,1,4,infile)!=4) return 11;
srate=((buf[1]<<8) | buf[2])/2;
// if (type==5) srate=32000; /* hack - not sure whether this is true generally */
/* offset */
if (fread(buf,1,4,infile)!=4) return 12;
afcoffset=read32(buf);
/* size */
if (fread(buf,1,4,infile)!=4) return 13;
afcsize=read32(buf);
if (fseek(infile,4,SEEK_CUR)<0) return 14;
if (fread(buf,1,4,infile)!=4) return 15;
loop_start_sample=read32(buf);
if (fread(buf,1,4,infile)!=4) return 16;
loop_end_sample=read32(buf);
if (fread(buf,1,4,infile)!=4) return 17;
num_samples=read32(buf);
if (fread(buf,1,4,infile)!=4) return 18;
unk0=read32(buf);
if (fread(buf,1,4,infile)!=4) return 19;
unk1=read32(buf);
if (fread(buf,1,4,infile)!=4) return 20;
unk2=read32(buf);
if (verbose) {
printf("%x\t%x\t%x\t%d\t%x\t%d\t%d\t%d\t%d\t%08x",i, afcoffset, afcsize, srate, type_id, root_key, loop_start_sample, loop_end_sample, num_samples, unk0);
if (type == 0) {
printf("\ttype_id=%x", type_id);
}
if (type_unk0 != 0xFF && type_unk0 != 0x70) {
printf("\ttype_unk0=%x", type_unk0);
}
if (type_unk3 != 0) {
printf("\ttype_unk3=%x", type_unk3);
}
if (unk1 != 0) {
printf("\tunk1=%x", unk1);
}
if (unk2 != 0x01D8 && unk2 != 0x0018) {
printf("\tunk2=%x", unk2);
}
printf("\n");
}
if (dump && type != 0) {
snprintf(outname,sizeof(outname),"%s/%s_%08x.wav",output_dir,fname,i);
FILE* outfile = fopen(outname,"wb");
if (!outfile) return 21;
int total_size = sizeof(wavhead)-8;
total_size += sizeof(datahead);
if (type == SRC_PCM16_FROM_ARAM || type == SRC_PCM8_FROM_ARAM) {
total_size += afcsize;
}
else {
total_size += afcsize/((type==SRC_AFC_LQ_FROM_ARAM) ? 5 : 9)*16*2;
}
int outframesize = (type==SRC_PCM8_FROM_ARAM) ? 1 : 2;
if (writeWAVHead(outfile, total_size, srate, outframesize)) return 22;
total_size += sizeof(smplhead);
if (loop_start_sample > 0) total_size += sizeof(smplloop);
writeSMPL(outfile, srate, root_key, loop_start_sample, loop_end_sample);
long oldpos = ftell(awfile);
if (oldpos < 0) return 23;
if (fseek(awfile,afcoffset,SEEK_SET)<0) return 24;
if (type == SRC_PCM16_FROM_ARAM || type == SRC_PCM8_FROM_ARAM) {
if (dumpPCM(awfile,afcsize,type,outfile)) return 25;
}
else {
if (dumpAFC(awfile,afcsize,type,outfile)) return 26;
}
if (fseek(awfile,oldpos,SEEK_SET)<0) return 27;
if (ftell(outfile) != total_size+8) {
if (verbose) printf("Miscalculated WAV size %ld vs %d\n", ftell(outfile), total_size+8);
return 28;
}
if (fclose(outfile)==EOF) return 29;
}
}
if (dump) {
if (fclose(awfile)==EOF) return 30;
}
if (fseek(infile,next_aw_offset,SEEK_SET)<0) return 31;
return 0;
}
int doOscTable(FILE *infile) {
unsigned char buf[4];
if (verbose) {
printf("OscTable@0x%lx,", ftell(infile));
}
return 0;
}
int doOsc(FILE *infile, int ibnk_offset) {
unsigned char buf[4];
// 80287204 convert to Osc pointer (+IBNK)
// 8028721c call findOscPtr
// 802879d4 read word (000016e0) at BANK+4
// 802879d8 convert to inst pointer
// 802879f4 read word (000008e0) at INST+0x10
// 802879f8 convert to osc pointer
// if 0, break
// 80287a1c call BasicBank.getInst
// 80287270 read byte (0) at IBNK+offset
if (fread(buf,1,4,infile)!=4) return 1;
unsigned char osc_1 = buf[3];
// 80287278 read float (1.0) at IBNK+offset+4
if (fread(buf,1,4,infile)!=4) return 1;
float osc_f1 = readfloat(buf);
// 80287284 read word (000006a0) at IBNK+offset+8
if (fread(buf,1,4,infile)!=4) return 1;
int osc_s_offset1 = read32(buf);
// 80287318 read word (00000560) at IBNK+offset+0xC
if (fread(buf,1,4,infile)!=4) return 1;
int osc_s_offset2 = read32(buf);
// 802873a8 load float (1.0) at IBNK+offset+0x10
if (fread(buf,1,4,infile)!=4) return 1;
float osc_f2 = readfloat(buf);
// 802873b0 load float (0.0) at IBNK+offset+0x14
if (fread(buf,1,4,infile)!=4) return 1;
float osc_f3 = readfloat(buf);
// 802873c4 call setOsc
if (verbose) {
printf("%x,%f,", osc_1, osc_f1);
}
// TODO envelope data
// 80287288 convert to s pointer (+IBNK)
// 80287294 call getOscTableEndPtr
// 80287aec load half (0000,0000,000e) at s pointer
// 80287af4 add 6 to pointer
// 80287af8 if half < 10, loop
// 80287298 get pointer difference (0x12)
// 802872a0 shift right 1
// 802872a4 shift left 1
int endOffset = ftell(infile);
if (verbose) {
printf("0x%x,0x%x,", osc_s_offset1, osc_s_offset2);
}
/*if (fseek(infile, osc_s_offset1+ibnk_offset, SEEK_SET)<0) return 1;
if (doOscTable(infile)) return 1;
if (fseek(infile, osc_s_offset2+ibnk_offset, SEEK_SET)<0) return 1;
if (doOscTable(infile)) return 1;
if (fseek(infile, endOffset, SEEK_SET)<0) return 1;*/
if (verbose) {
printf("%f,%f", osc_f2, osc_f3);
}
return 0;
}
int doVmap(FILE *infile) {
unsigned char buf[4];
// 8028762c read byte (7f) at 0
// 80287634 read word (00000020) at 4
// 80287640 read float (1.0) at 8
// 80287648 read float (1.0) at 0xC
if (fread(buf,1,4,infile)!=4) return 1;
char vmap1 = buf[0];
if (fread(buf,1,4,infile)!=4) return 1;
int vmap2 = read32(buf);
if (fread(buf,1,4,infile)!=4) return 1;
float vmap3 = readfloat(buf);
if (fread(buf,1,4,infile)!=4) return 1;
float vmap4 = readfloat(buf);
if (verbose) {
printf("Vmap: %x,%x,%f,%f|", vmap1, vmap2, vmap3, vmap4);
}
return 0;
}
int doKeymap(FILE *infile, const int offset) {
unsigned char buf[4];
// 802875ec read byte (3b) at 0
// 802875f8 read word (00000001) at 4
// 802875fc call setVeloRegionCount
// 80287658 read word (00000001) at 4
// 80287614 call getVeloRegion
// 80287624 read word (00000a00) at 8
// that's a pointer to a vmap (814a9f30 in mem, A530 in file)
if (fread(buf,1,4,infile)!=4) return 1;
int keymap1 = buf[0];
if (fread(buf,1,4,infile)!=4) return 1;
int keymap2 = read32(buf);
if (verbose) {
printf("Keymap: %x,%x|", keymap1, keymap2);
}
if (fread(buf,1,4,infile)!=4) return 1;
int vmap_offset = read32(buf);
if (fseek(infile, vmap_offset+offset, SEEK_SET)<0) return 1;
if (doVmap(infile)) return 1;
// 80287658 read word (00000001) at 4
// 8028766c read inst sub offset again
// 802875d0 call getKeyRegion
return 0;
}
int doInst(FILE * infile, const int offset) {
unsigned char buf[4];
if (fread(buf,1,4,infile)!=4) return 1;
if (read32(buf) != 0) {
fprintf(stderr, "Unexpected non-0 at 0x%lx\n", ftell(infile));
return 1;
}
// 802871d0 read float at INST+8
if (fread(buf,1,4,infile)!=4) return 1;
float f1 = readfloat(buf);
// 802871d8 read float at INST+0xC
if (fread(buf,1,4,infile)!=4) return 1;
float f2 = readfloat(buf);
if (verbose) {
printf("(%f,%f)|", f1, f2);
}
// 802871e8 call BasicInst.setOscCount
for (int i = 0; i < 6; i++) {
// 80287200 read word at INST+0x10
if (fread(buf,1,4,infile)!=4) return 1;
int osc_offset = read32(buf);
if (osc_offset != 0) {
int next_osc_offset = ftell(infile);
if (verbose) {
printf("Osc@0x%x:", osc_offset+offset);
}
if (fseek(infile, osc_offset+offset, SEEK_SET)<0) return 1;
if (doOsc(infile, offset)) return 1;
if (fseek(infile, next_osc_offset, SEEK_SET)<0) return 1;
if (verbose) {
printf("|");
}
}
}
if (fread(buf,1,4,infile)!=4) return 1;
int inst_sub_offset_count = read32(buf);
for (int i = 0; i < inst_sub_offset_count; i++) {
if (fread(buf,1,4,infile)!=4) return 1;
int keymap_offset = read32(buf);
int next_sub_offset = ftell(infile);
// points to a keymap, relative to IBNK start
if (fseek(infile, keymap_offset+offset, SEEK_SET)<0) return 1;
if (doKeymap(infile, offset)) return 1;
if (fseek(infile, next_sub_offset, SEEK_SET)<0) return 1;
}
if (verbose) {
printf("\n");
}
return 0;
}
int doNewInst(FILE *infile) {
unsigned char buf[4];
int i;
if (fread(buf,1,4,infile)!=4) return 1;
int n1 = read32(buf);
int nCount;
if (n1 == 1) {
if (fread(buf,1,4,infile)!=4) return 1;
int n2 = read32(buf);
if (verbose) {
printf("(%d", n2);
}
if (fread(buf,1,4,infile)!=4) return 1;
nCount = read32(buf);
}
else if (n1 == 2) {
if (fread(buf,1,4,infile)!=4) return 1;
int n2 = read32(buf);
if (verbose) {
printf("(%d", n2);
}
if (fread(buf,1,4,infile)!=4) return 1;
int n3 = read32(buf);
if (verbose) {
printf(",%d", n3);
}
if (fread(buf,1,4,infile)!=4) return 1;
nCount = read32(buf);
}
else {
fprintf(stderr, "0x%lX: Expected 1 or 2 got %x\n", ftell(infile)-4, n1);
return 1;
}
for (i = 0; i < nCount; i++) {
if (fread(buf,1,4,infile)!=4) return 1;
int n5 = read32(buf);
if (verbose) {
printf(",%d", n5);
}
}
if (verbose) {
printf(")");
}
if (fread(buf,1,4,infile)!=4) return 1;
int inst_sub_count = read32(buf);
for (i = 0; i < inst_sub_count; i++) {
// TODO
if (fseek(infile,0x04,SEEK_CUR)<0) return 1;
if (fread(buf,1,4,infile)!=4) return 1;
int sz = read32(buf);
if (verbose) printf("|%d:",sz);
if (sz == 1) {
// TODO
if (fseek(infile,0x08,SEEK_CUR)<0) return 1;
}
else if (sz == 0) {
}
if (sz != 0) {
if (fread(buf,1,4,infile)!=4) return 1;
float sf1 = readfloat(buf);
if (fread(buf,1,4,infile)!=4) return 1;
float sf2 = readfloat(buf);
if (verbose) {
printf("%f,%f", sf1, sf2);
}
}
}
if (fread(buf,1,4,infile)!=4) return 1;
float f1 = readfloat(buf);
if (fread(buf,1,4,infile)!=4) return 1;
float f2 = readfloat(buf);
if (verbose) {
printf("|(%f,%f)\n", f1, f2);
}
return 0;
}
int doPmap(FILE *infile) {
unsigned char buf[4];
if (fread(buf,1,4,infile)!=4) return 1;
if (memcmp(buf,"Pmap",4)) {
fprintf(stderr,"Pmap expected at 0x%lx\n",ftell(infile)-4);
return 1;
}
// TODO
if (fseek(infile, 0x28, SEEK_CUR)<0) return 1;
return 0;
}
int doPerc(FILE *infile, const int offset) {
unsigned char buf[4];
if (fread(buf,1,4,infile)!=4) return 1;
int pmapCount = read32(buf);
int i;
for (i = 0; i < pmapCount; i++) {
if (fread(buf,1,4,infile)!=4) return 1;
int pmapOffset = read32(buf);
if (pmapOffset == 0) continue;
int nextOffset = ftell(infile);
if (fseek(infile, pmapOffset+offset, SEEK_SET)<0) return 1;
if (doPmap(infile)) return 1;
if (fseek(infile, nextOffset, SEEK_SET)<0) return 1;
}
return 0;
}
static inline const int roundUp(const int numToRound, const int multiple) {
if (multiple == 0) {
return numToRound;
}
const int remainder = numToRound % multiple;
if (remainder == 0) {
return numToRound;
}
return numToRound + multiple - remainder;
}
int doIBNK(FILE * infile, const int offset, int size) {
unsigned char buf[4];
int old_offset;
old_offset = ftell(infile);
if (old_offset<0) return 1;
if (fseek(infile,offset,SEEK_SET)<0) return 1;
/* IBNK tag */
if (fread(buf,1,4,infile)!=4) return 1;
if (memcmp(buf,"IBNK",4)) {
fprintf(stderr,"IBNK file expected at 0x%x\n",offset);
return 1;
}
if (fread(buf,1,4,infile)!=4) return 1;
if (size == 0) {
size = read32(buf);
}
else if (read32(buf) != size) {
fprintf(stderr, "Incorrect IBNK size\n");
return 1;
}
if (fread(buf,1,4,infile)!=4) return 1;
if (verbose) {
printf("IBNK #%d\n", read32(buf));
}
int newstyle=0;
//if (fseek(infile,24,SEEK_CUR)<0) return 1; /* skip stuff I don't use */
for (int j = 0; j < 5; j++) {
if (fread(buf,1,4,infile)!=4) return 1;
if (read32(buf) != 0) {
//fprintf(stderr, "IBNK is not all 0 at 0x%lx!\n", ftell(infile));
//return 1;
newstyle=1;
}
}
if (newstyle) {
int readingChunks = 1;
while (ftell(infile) < offset+size && readingChunks) {
if (fread(buf,1,4,infile)!=4) return 1;
int chunkId = read32(buf);
if (fread(buf,1,4,infile)!=4) return 1;
int chunkSize = read32(buf);
switch (chunkId) {
case 0:
readingChunks = 0;
break;
/*case 0x494E5354: // INST
{
if (fread(buf,1,4,infile)!=4) return 1;
int instCount = read32(buf);
for (int i = 0; i < instCount; i++) {
if (fread(buf,1,4,infile)!=4) return 1;
if (memcmp(buf,"Inst",4) != 0) {
fprintf(stderr, "Unknown block %s at 0x%lx\n", buf, ftell(infile)-4);
return 1;
}
if (verbose) {
printf("Inst at 0x%lx", ftell(infile)-4);
}
if (doNewInst(infile)) return 1;
}
}
break;*/
case 0x4F534354: // OSCT
{
if (fread(buf,1,4,infile)!=4) return 1;
int oscCount = read32(buf);
for (int i = 0; i < oscCount; i++) {
if (fread(buf,1,4,infile)!=4) return 1;
if (memcmp(buf,"Osci",4) != 0) {
fprintf(stderr, "Unknown block %s at 0x%lx\n", buf, ftell(infile)-4);
return 1;
}
if (verbose) {
printf("Osci@0x%lx:", ftell(infile)-4);
}
if (doOsc(infile, offset)) return 1;
if (verbose) {
printf("\n");
}
}
}
break;
case 0x4C495354: // LIST
{
if (fread(buf,1,4,infile)!=4) return 1;
int offsCount = read32(buf);
for (int i = 0; i < offsCount; i++) {
if (fread(buf,1,4,infile)!=4) return 1;
int listItemOffset = read32(buf);
if (listItemOffset == 0) continue;
int nextOffset = ftell(infile);
if (fseek(infile, listItemOffset+offset, SEEK_SET)<0) return 1;
if (fread(buf,1,4,infile)!=4) return 1;
if (memcmp(buf,"Inst",4) == 0) {
if (verbose) {
printf("Inst at 0x%lx", ftell(infile)-4);
}
if (doNewInst(infile)) return 1;
}
else if (memcmp(buf,"Perc",4) == 0) {
if (verbose) {
printf("Perc at 0x%lx", ftell(infile)-4);
}
if (doPerc(infile, offset)) return 1;
}
else {
fprintf(stderr, "Unknown chunk %c%c%c%c at 0x%lx\n", buf[0], buf[1], buf[2], buf[3], ftell(infile)-4);
}
if (fseek(infile, nextOffset, SEEK_SET)<0) return 1;
}
}
default:
if (fseek(infile,roundUp(chunkSize,4),SEEK_CUR)<0) return 1;
break;
}
}
}
else {
/* BANK tag */
if (fread(buf,1,4,infile)!=4) return 1;
if (memcmp(buf,"BANK",4)) {
fprintf(stderr,"BANK file expected at 0x%x\n",offset+0x20);
return 1;
}
for (int chunk_offset_pos = offset+0x24; chunk_offset_pos < offset+0x390; chunk_offset_pos += 4) {
if (fseek(infile,chunk_offset_pos,SEEK_SET)<0) return 1;
// 80287168 read word (000016e0) at IBNK+0x24 (BANK+4)
if (fread(buf,1,4,infile)!=4) return 1;
int chunk_offset = read32(buf);
// 8028716c convert to inst pointer
if (chunk_offset == 0) continue;
if (chunk_offset > size) {
fprintf(stderr, "Invalid BANK chunk pos %x\n", chunk_offset);
return 1;
}
// 80287190 construct BasicInst
if (fseek(infile,chunk_offset+offset,SEEK_SET)<0) return 1;
if (fread(buf,1,4,infile)!=4) return 1;
if (memcmp(buf,"INST",4) != 0) {
fprintf(stderr, "Unknown block %s at 0x%x (from 0x%x)\n", buf, chunk_offset+offset, chunk_offset_pos);
return 1;
}
if (verbose) {
printf("INST at 0x%x ", chunk_offset+offset);
}
if (doInst(infile, offset)) return 1;
}
for (int chunk_offset_pos = offset+0x390; chunk_offset_pos < offset+0x400; chunk_offset_pos += 4) {
if (fseek(infile,chunk_offset_pos,SEEK_SET)<0) return 1;
if (fread(buf,1,4,infile)!=4) return 1;
int chunk_offset = read32(buf);
if (chunk_offset == 0) continue;
if (chunk_offset > size) {
fprintf(stderr, "Invalid BANK chunk pos %x\n", chunk_offset);
return 1;
}
// 802876b4
if (fseek(infile,chunk_offset+offset,SEEK_SET)<0) return 1;
if (fread(buf,1,4,infile)!=4) return 1;
if (memcmp(buf,"PER2",4) != 0) {
fprintf(stderr, "Unknown block %s at 0x%x (from 0x%x)\n", buf, chunk_offset+offset, chunk_offset_pos);
return 1;
}
// 80287758 read word (00000000) at PER+0x88
// 8028775c convert pointer to Pmap
if (verbose) {
printf("PER2 at 0x%x\n", chunk_offset+offset);
}
}
}
if (fseek(infile,old_offset,SEEK_SET)<0) return 1;
return 0;
}
int dostrm(FILE * infile, const int offset, const int size) {
unsigned char buf[4];
int old_offset;
char afc_name[16];
old_offset = ftell(infile);
if (old_offset<0) return 1;
if (fseek(infile,offset,SEEK_SET)<0) return 1;
if (fseek(infile,16,SEEK_CUR)<0) return 1; // TODO
while (ftell(infile) < offset+size) {
if (fread(afc_name,1,16,infile) != 16) return 1;
if (afc_name[15] != '\0') {
fprintf(stderr, "Expected NULL terminator in AFC filename\n");
return 1;
}
// Same as AFC header
if (fread(buf,1,4,infile)!=4) return 1;
int datalength = read32(buf);
if (fread(buf,1,4,infile)!=4) return 1;
int num_samples = read32(buf);
if (fread(buf,1,2,infile)!=2) return 1;
short sample_rate = read16(buf);
if (fread(buf,1,2,infile)!=2) return 1;
short unk1 = read16(buf);
if (fread(buf,1,2,infile)!=2) return 1;
short unk2 = read16(buf);
if (fread(buf,1,2,infile)!=2) return 1;