-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdisassemble_sound.py
More file actions
executable file
·887 lines (769 loc) · 29.9 KB
/
Copy pathdisassemble_sound.py
File metadata and controls
executable file
·887 lines (769 loc) · 29.9 KB
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
# From the SM64 decompilation project, modified.
from typing import Any, List, Optional, Tuple
#!/usr/bin/env python3
from collections import namedtuple, defaultdict
from aifc_decode import decode_aifc_to_aiff
import tempfile
import uuid
import json
import os
import re
import struct
import sys
from utils import debug_print
TYPE_CTL = 1
TYPE_TBL = 2
# whether ext bank C is used or not
ExtC = False
class AifcEntry:
def __init__(self, data, book, loop):
self.name = None
self.data = data
self.book = book
self.loop = loop
self.tunings = []
class SampleBank:
def __init__(self, name, data, offset):
self.offset = offset
self.name = name
self.data = data
self.entries = {}
def add_sample(self, offset, sample_size, book, loop):
if sample_size % 2 != 0:
from utils import debug_print
debug_print(
f"Warning: Audio sample at 0x{offset:X} has unaligned size {sample_size}. Fixing."
)
sample_size &= ~1
if sample_size % 9 != 0:
if sample_size % 9 != 1:
debug_print(
f"Warning: Audio sample at 0x{offset:X} has size {sample_size} "
f"not matching VADPCM block alignment (expected mod 9 == 0 or 1)"
)
sample_size -= sample_size % 9
if offset in self.entries:
entry = self.entries[offset]
# assert entry.book == book
# assert entry.loop == loop
# assert len(entry.data) == sample_size
else:
entry = AifcEntry(self.data[offset : offset + sample_size], book, loop)
self.entries[offset] = entry
return entry
Sound = namedtuple("Sound", ["sample_addr", "tuning"])
Drum = namedtuple("Drum", ["name", "addr", "release_rate", "pan", "envelope", "sound"])
Inst = namedtuple(
"Inst",
[
"name",
"addr",
"release_rate",
"normal_range_lo",
"normal_range_hi",
"envelope",
"sound_lo",
"sound_med",
"sound_hi",
],
)
Book = namedtuple("Book", ["order", "npredictors", "table"])
Loop = namedtuple("Loop", ["start", "end", "count", "state"])
Envelope = namedtuple("Envelope", ["name", "entries"])
Bank = namedtuple(
"Bank",
[
"name",
"iso_date",
"sample_bank",
"insts",
"drums",
"all_insts",
"inst_list",
"envelopes",
"samples",
],
)
def align(val, al):
return (val + (al - 1)) & -al
name_tbl = {}
def gen_name(prefix, name_table=[]):
if prefix not in name_tbl:
name_tbl[prefix] = 0
ind = name_tbl[prefix]
name_tbl[prefix] += 1
if ind < len(name_table):
return name_table[ind]
return prefix + str(ind)
def parse_bcd(data):
ret = 0
for c in data:
ret *= 10
ret += c >> 4
ret *= 10
ret += c & 15
return ret
def serialize_f80(num):
num = float(num)
(f64,) = struct.unpack(">Q", struct.pack(">d", num))
f64_sign_bit = f64 & 2**63
if num == 0.0:
if f64_sign_bit:
return b"\x80" + b"\0" * 9
else:
return b"\0" * 10
exponent = (f64 ^ f64_sign_bit) >> 52
assert exponent != 0, "can't handle denormals"
assert exponent != 0x7FF, "can't handle infinity/nan"
exponent -= 1023
f64_mantissa_bits = f64 & (2**52 - 1)
f80_sign_bit = f64_sign_bit << (80 - 64)
f80_exponent = (exponent + 0x3FFF) << 64
f80_mantissa_bits = 2**63 | (f64_mantissa_bits << (63 - 52))
f80 = f80_sign_bit | f80_exponent | f80_mantissa_bits
return struct.pack(">HQ", f80 >> 64, f80 & (2**64 - 1))
def round_f32(num):
enc = struct.pack(">f", num)
for decimals in range(5, 20):
num2 = round(num, decimals)
if struct.pack(">f", num2) == enc:
return num2
return num
def parse_sound(data):
sample_addr, tuning = struct.unpack(">If", data)
if sample_addr == 0:
if tuning != 0:
debug_print(f"WARNING: tuning is {tuning} (expected 0) for null sample")
return None
return Sound(sample_addr, tuning)
def parse_drum(data, addr):
name = gen_name("drum")
release_rate, pan, loaded, pad = struct.unpack(">BBBB", data[:4])
if loaded != 0:
debug_print(f"WARNING: loaded is {loaded} (expected 0) for drum at 0x{addr:08X}")
if pad != 0:
debug_print(f"WARNING: pad is {pad} (expected 0) for drum at 0x{addr:08X}")
sound = parse_sound(data[4:12])
(env_addr,) = struct.unpack(">I", data[12:])
assert env_addr != 0
return Drum(name, addr, release_rate, pan, env_addr, sound)
def parse_inst(data, addr):
name = gen_name("inst")
loaded, normal_range_lo, normal_range_hi, release_rate, env_addr = struct.unpack(
">BBBBI", data[:8]
)
assert env_addr != 0
sound_lo = parse_sound(data[8:16])
sound_med = parse_sound(data[16:24])
sound_hi = parse_sound(data[24:])
if sound_lo is None:
if normal_range_lo != 0:
debug_print(
f"WARNING: normal_range_lo is {normal_range_lo} (expected 0) at 0x{addr:08X}"
)
if sound_hi is None:
if normal_range_hi != 127:
debug_print(
f"WARNING: normal_range_hi is {normal_range_hi} (expected 127) at 0x{addr:08X}"
)
return Inst(
name,
addr,
release_rate,
normal_range_lo,
normal_range_hi,
env_addr,
sound_lo,
sound_med,
sound_hi,
)
def parse_loop(addr, bank_data):
start, end, count, pad = struct.unpack(">IIiI", bank_data[addr : addr + 16])
if pad != 0:
debug_print(f"WARNING: loop pad is {pad} (expected 0) at 0x{addr:08X}")
if count != 0:
state = struct.unpack(">16h", bank_data[addr + 16 : addr + 48])
else:
state = None
return Loop(start, end, count, state)
def parse_book(addr, bank_data):
order, npredictors = struct.unpack(">ii", bank_data[addr : addr + 8])
if order != 2:
debug_print(f"WARNING: VADPCM order is {order} (expected 2) at 0x{addr:08X}")
# assert npredictors == 2
table_data = bank_data[addr + 8 : addr + 8 + 16 * order * npredictors]
table = []
for i in range(0, 16 * order * npredictors, 2):
table.append(struct.unpack(">h", table_data[i : i + 2])[0])
return Book(order, npredictors, table)
def parse_sample(data, bank_data, sample_bank, is_shindou):
if is_shindou:
sample_size, addr, loop, book = struct.unpack(">IIII", data)
else:
zero, addr, loop, book, sample_size = struct.unpack(">IIIII", data)
assert zero == 0
assert loop != 0
assert book != 0
loop = parse_loop(loop, bank_data)
book = parse_book(book, bank_data)
return sample_bank.add_sample(addr, sample_size, book, loop)
def parse_envelope(addr, data_bank):
entries = []
while True:
if addr + 4 > len(data_bank):
break
try:
delay, arg = struct.unpack(">HH", data_bank[addr : addr + 4])
except struct.error:
break
entries.append((delay, arg))
addr += 4
# Terminators: 0 (stop), 0xFFFF (none), 0xFFFE (goto), 0xFFFD (restart)
if delay == 0 or 1 <= (-delay) % 2**16 <= 3:
break
if len(entries) > 1000:
break
return entries
def parse_ctl_header(header):
num_instruments, num_drums, shared = struct.unpack(">III", header[:12])
date = parse_bcd(header[12:])
y = date // 10000
m = date // 100 % 100
d = date % 100
iso_date = "{:02}-{:02}-{:02}".format(y, m, d)
debug_print(
f"parse_ctl_header: num_instruments={num_instruments}, num_drums={num_drums}, shared={shared}, date={iso_date}, raw_header={header[:].hex()}"
)
if shared not in [0, 1]:
debug_print(f"WARNING: shared is {shared} (expected 0 or 1)")
return num_instruments, num_drums, iso_date
def parse_ctl(parsed_header, data, sample_bank, index, is_shindou):
name_tbl.clear()
name = "{:02X}".format(index)
num_instruments, num_drums, iso_date = parsed_header
(drum_base_addr,) = struct.unpack(">I", data[:4])
drum_addrs = []
if num_drums != 0:
if drum_base_addr == 0:
debug_print(f"WARNING: drum_base_addr is 0 but num_drums is {num_drums}")
else:
for i in range(num_drums):
(drum_addr,) = struct.unpack(
">I", data[drum_base_addr + i * 4 : drum_base_addr + i * 4 + 4]
)
if drum_addr == 0:
debug_print(f"WARNING: drum_addr is 0 for drum {i}")
drum_addrs.append(drum_addr)
else:
if drum_base_addr != 0:
debug_print(f"WARNING: drum_base_addr is 0x{drum_base_addr:X} but num_drums is 0")
inst_base_addr = 4
inst_addrs = []
inst_list: List[Optional[int]] = []
for i in range(num_instruments):
(inst_addr,) = struct.unpack(
">I", data[inst_base_addr + i * 4 : inst_base_addr + i * 4 + 4]
)
if inst_addr == 0:
inst_list.append(None)
else:
inst_list.append(inst_addr)
inst_addrs.append(inst_addr)
inst_addrs.sort()
if drum_addrs != sorted(drum_addrs):
debug_print("WARNING: drum_addrs are not sorted")
if drum_addrs and inst_addrs:
if max(inst_addrs) >= min(drum_addrs):
debug_print(
f"WARNING: max(inst_addrs) 0x{max(inst_addrs):X} >= min(drum_addrs) 0x{min(drum_addrs):X}"
)
if len(set(inst_addrs)) != len(inst_addrs):
debug_print("WARNING: duplicate inst_addrs found")
if len(set(drum_addrs)) != len(drum_addrs):
debug_print("WARNING: duplicate drum_addrs found")
insts = []
for inst_addr in inst_addrs:
insts.append(parse_inst(data[inst_addr : inst_addr + 32], inst_addr))
drums = []
for drum_addr in drum_addrs:
drums.append(parse_drum(data[drum_addr : drum_addr + 16], drum_addr))
env_addrs = set()
sample_addrs = set()
tunings = defaultdict(lambda: [])
for inst in insts:
for sound in [inst.sound_lo, inst.sound_med, inst.sound_hi]:
if sound is not None:
sample_addrs.add(sound.sample_addr)
tunings[sound.sample_addr].append(sound.tuning)
env_addrs.add(inst.envelope)
for drum in drums:
sample_addrs.add(drum.sound.sample_addr)
tunings[drum.sound.sample_addr].append(drum.sound.tuning)
env_addrs.add(drum.envelope)
# Put drums somewhere in the middle of the instruments to make sample
# addresses come in increasing order. (This logic isn't totally right,
# but it works for our purposes.)
all_insts = []
need_drums = len(drums) > 0
for inst in insts:
if need_drums and any(
s.sample_addr > drums[0].sound.sample_addr
for s in [inst.sound_lo, inst.sound_med, inst.sound_hi]
if s is not None
):
all_insts.append(drums)
need_drums = False
all_insts.append(inst)
if need_drums:
all_insts.append(drums)
samples = {}
for addr in sorted(sample_addrs):
sample_size = 16 if is_shindou else 20
sample_data = data[addr : addr + sample_size]
samples[addr] = parse_sample(sample_data, data, sample_bank, is_shindou)
samples[addr].tunings.extend(tunings[addr])
env_data = {}
used_env_addrs = set()
for addr in sorted(env_addrs):
env = parse_envelope(addr, data)
env_data[addr] = env
for i in range(len(env)):
used_env_addrs.add(addr + i * 4)
# Unused envelopes
unused_envs = set()
if used_env_addrs:
for addr in range(min(used_env_addrs) + 4, max(used_env_addrs), 4):
if addr not in used_env_addrs:
unused_envs.add(addr)
(stub_marker,) = struct.unpack(">I", data[addr : addr + 4])
# assert stub_marker == 0
env = parse_envelope(addr, data)
env_data[addr] = env
for i in range(align(len(env), 4)):
used_env_addrs.add(addr + i * 4)
envelopes = {}
for addr in sorted(env_data.keys()):
env_name = gen_name("envelope")
if addr in unused_envs:
env_name += "_unused"
envelopes[addr] = Envelope(env_name, env_data[addr])
return Bank(
name,
iso_date,
sample_bank,
insts,
drums,
all_insts,
inst_list,
envelopes,
samples,
)
def parse_seqfile(data, filetype):
magic, num_entries = struct.unpack(">HH", data[:4])
if magic != filetype:
debug_print(f"WARNING: seqfile magic is {magic:04X} (expected {filetype:04X})")
prev = align(4 + num_entries * 8, 16)
entries = []
for i in range(num_entries):
offset, length = struct.unpack(">II", data[4 + i * 8 : 4 + i * 8 + 8])
if filetype == TYPE_CTL:
# this is not true in ext bank 12 hacks
# so if this condition is true, then I must stop at bank 12
if not offset == prev:
global ExtC
if not ExtC:
# TODO: implement this
print("Extended Bank C detected. No .AIFF files will be exported")
ExtC = True
else:
if offset > prev:
debug_print(f"WARNING: seqfile offset 0x{offset:X} > prev 0x{prev:X}")
prev = max(prev, offset + length)
entries.append((offset, length))
# idk why but it don't work
# assert all(x == 0 for x in data[prev:])
return entries
def parse_sh_header(data, filetype):
(num_entries,) = struct.unpack(">H", data[:2])
if data[2:16] != b"\0" * 14:
debug_print(f"WARNING: sh_header padding is not zero: {data[2:16].hex()}")
prev = 0
entries: List[Tuple[int, int, Optional[Tuple[int, int, int]]]] = []
for i in range(num_entries):
subdata = data[16 + 16 * i : 32 + 16 * i]
offset, length, magic = struct.unpack(">IIH", subdata[:10])
if offset != prev:
debug_print(f"WARNING: sh_header offset 0x{offset:X} != prev 0x{prev:X}")
expected_magic = 0x0204 if filetype == TYPE_TBL else 0x0203
if magic != expected_magic:
debug_print(f"WARNING: sh_header magic is {magic:04X} (expected {expected_magic:04X})")
prev = offset + length
if filetype == TYPE_CTL:
if subdata[14:16] != b"\0" * 2:
debug_print(
f"WARNING: sh_header subdata padding is not zero: {subdata[14:16].hex()}"
)
sample_bank_index, magic2, num_instruments, num_drums = struct.unpack(
">BBBB", subdata[10:14]
)
if magic2 != 0xFF:
debug_print(f"WARNING: sh_header magic2 is {magic2:02X} (expected FF)")
entries.append((offset, length, (sample_bank_index, num_instruments, num_drums)))
else:
if subdata[10:16] != b"\0" * 6:
debug_print(f"WARNING: sh_header subdata 10:16 is not zero: {subdata[10:16].hex()}")
entries.append((offset, length, None))
return entries
def parse_tbl(data, entries):
seen = {}
tbls = []
sample_banks = []
sample_bank_map = {}
for offset, length in entries:
if offset not in seen:
name = gen_name("sample_bank")
seen[offset] = name
sample_bank = SampleBank(name, data[offset : offset + length], offset)
sample_banks.append(sample_bank)
sample_bank_map[name] = sample_bank
tbls.append(seen[offset])
return tbls, sample_banks, sample_bank_map
class AifcWriter:
def __init__(self, out):
self.out = out
self.sections = []
self.total_size = 0
def add_section(self, tp, data):
assert isinstance(tp, bytes)
assert isinstance(data, bytes)
self.sections.append((tp, data))
self.total_size += align(len(data), 2) + 8
def add_custom_section(self, tp, data):
self.add_section(b"APPL", b"stoc" + self.pstring(tp) + data)
def pstring(self, data):
return bytes([len(data)]) + data + (b"" if len(data) % 2 else b"\0")
def finish(self):
# total_size isn't used, and is regularly wrong. In particular, vadpcm_enc
# preserves the size of the input file...
self.total_size += 4
self.out.write(b"FORM" + struct.pack(">I", self.total_size) + b"AIFC")
for tp, data in self.sections:
self.out.write(tp + struct.pack(">I", len(data)))
self.out.write(data)
if len(data) % 2:
self.out.write(b"\0")
def write_aifc(entry, out):
writer = AifcWriter(out)
num_channels = 1
data = entry.data
assert len(data) % 9 == 0
if len(data) % 2 == 1:
data += b"\0"
# (Computing num_frames this way makes it off by one when the data length
# is odd. It matches vadpcm_enc, though.)
num_frames = len(data) * 16 // 9
sample_size = 16 # bits per sample
if len(set(entry.tunings)) == 1:
sample_rate = 32000 * entry.tunings[0]
else:
# Some drum sounds in sample bank B don't have unique sample rates, so
# we have to guess. This doesn't matter for matching, it's just to make
# the sounds easy to listen to.
if min(entry.tunings) <= 0.5 <= max(entry.tunings):
sample_rate = 16000
elif min(entry.tunings) <= 1.0 <= max(entry.tunings):
sample_rate = 32000
elif min(entry.tunings) <= 1.5 <= max(entry.tunings):
sample_rate = 48000
elif min(entry.tunings) <= 2.5 <= max(entry.tunings):
sample_rate = 80000
else:
sample_rate = 16000 * (min(entry.tunings) + max(entry.tunings))
writer.add_section(
b"COMM",
struct.pack(">hIh", num_channels, num_frames, sample_size)
+ serialize_f80(sample_rate)
+ b"VAPC"
+ writer.pstring(b"VADPCM ~4-1"),
)
writer.add_section(b"INST", b"\0" * 20)
table_data = b"".join(struct.pack(">h", x) for x in entry.book.table)
writer.add_custom_section(
b"VADPCMCODES",
struct.pack(">hhh", 1, entry.book.order, entry.book.npredictors) + table_data,
)
writer.add_section(b"SSND", struct.pack(">II", 0, 0) + data)
if entry.loop.count != 0:
writer.add_custom_section(
b"VADPCMLOOPS",
struct.pack(
">HHIIi16h",
1,
1,
entry.loop.start,
entry.loop.end,
entry.loop.count,
*entry.loop.state,
),
)
writer.finish()
def write_aiff(entry, filename):
temp = tempfile.NamedTemporaryFile(suffix=".aifc", delete=False)
try:
write_aifc(entry, temp)
temp.flush()
temp.close()
try:
decode_aifc_to_aiff(temp.name, filename, fast=True)
except Exception as e:
print(f"Warning: aifc_decode failed on {filename}: {e}, retrying with --fast")
try:
decode_aifc_to_aiff(temp.name, filename, fast=True)
except Exception as e2:
print(f"Error: aifc_decode failed on {filename} even with --fast: {e2}")
finally:
temp.close()
if os.path.exists(temp.name):
os.remove(temp.name)
# Modified from https://stackoverflow.com/a/25935321/1359139, cc by-sa 3.0
class NoIndent(object):
def __init__(self, value):
self.value = value
class NoIndentEncoder(json.JSONEncoder):
def __init__(self, *args, **kwargs):
super(NoIndentEncoder, self).__init__(*args, **kwargs)
self._replacement_map = {}
def default(self, o):
def ignore_noindent(o):
if isinstance(o, NoIndent):
return o.value
return self.default(o)
if isinstance(o, NoIndent):
key = uuid.uuid4().hex
self._replacement_map[key] = json.dumps(o.value, default=ignore_noindent)
return "@@%s@@" % (key,)
else:
return super(NoIndentEncoder, self).default(o)
def encode(self, o):
result = super(NoIndentEncoder, self).encode(o)
repl_map = self._replacement_map
def repl(m):
key = m.group()[3:-3]
return repl_map[key]
return re.sub(r"\"@@[0-9a-f]*?@@\"", repl, result)
def inst_ifdef_json(bank_index, inst_index):
if bank_index == 7 and inst_index >= 13:
return NoIndent(["VERSION_US", "VERSION_EU"])
if bank_index == 8 and inst_index >= 16:
return NoIndent(["VERSION_US", "VERSION_EU"])
if bank_index == 10 and inst_index >= 14:
return NoIndent(["VERSION_US", "VERSION_EU"])
return None
def main(*Fargs):
args = []
need_help = False
only_samples = False
only_samples_list: List[str] = []
shindou_headers = None
for i, a in enumerate(Fargs, 1):
# if skip_next > 0:
# skip_next -= 1
# continue
# if a == "--help" or a == "-h":
# need_help = True
# elif a == "--only-samples":
# only_samples = True
# elif a == "--shindou-headers":
# shindou_headers = sys.argv[i + 1 : i + 5]
# skip_next = 4
# elif a.startswith("-"):
# print("Unrecognized option " + a)
# sys.exit(1)
# elif only_samples:
# only_samples_list.append(a)
# else:
args.append(a)
expected_num_args = 6 + (0 if only_samples else 2)
if len(args) != expected_num_args or (shindou_headers and len(shindou_headers) != 4):
print(
"Usage: {}"
" <.z64 rom read> <ctl offset> <ctl size> <tbl offset> <tbl size>"
" [--shindou-headers <ctl header offset> <ctl header size>"
" <tbl header offset> <tbl header size>]"
" (<samples outdir> <sound bank outdir> |"
" --only-samples file:index ...)".format(sys.argv[0])
)
sys.exit(0 if need_help else 1)
rom_file = args[0]
def read_at(offset, size):
rom_file.seek(int(offset))
return rom_file.read(int(size))
ctl_data = read_at(args[1], args[2])
tbl_data = read_at(args[3], args[4])
ctl_header_data = None
tbl_header_data = None
if shindou_headers:
ctl_header_data = read_at(shindou_headers[0], shindou_headers[1])
tbl_header_data = read_at(shindou_headers[2], shindou_headers[3])
if not only_samples:
samples_out_dir = args[5]
banks_out_dir = args[6]
banks = []
if shindou_headers:
ctl_entries = parse_sh_header(ctl_header_data, TYPE_CTL)
tbl_entries = parse_sh_header(tbl_header_data, TYPE_TBL)
sample_banks = parse_tbl(tbl_data, tbl_entries)[1]
for index, (offset, length, sh_meta) in enumerate(ctl_entries):
sample_bank = sample_banks[sh_meta[0]]
entry = ctl_data[offset : offset + length]
header = (sh_meta[1], sh_meta[2], "0000-00-00")
banks.append(parse_ctl(header, entry, sample_bank, index, True))
else:
ctl_entries = parse_seqfile(ctl_data, TYPE_CTL)
tbl_entries = parse_seqfile(tbl_data, TYPE_TBL)
assert len(ctl_entries) == len(tbl_entries)
tbls, sample_banks, sample_bank_map = parse_tbl(tbl_data, tbl_entries)
for index, (offset, length), sample_bank_name in zip(
range(len(ctl_entries)), ctl_entries, tbls
):
if index == 13 and ExtC:
break
sample_bank = sample_bank_map[sample_bank_name]
entry = ctl_data[offset : offset + length]
header = parse_ctl_header(entry[:16])
banks.append(parse_ctl(header, entry[16:], sample_bank, index, False))
# Special mode used for asset extraction: generate aifc files, with paths
# given by command line arguments
if only_samples:
index_to_filename = {}
created_dirs = set()
for arg in only_samples_list:
filename, idx_str = arg.rsplit(":", 1)
index_to_filename[int(idx_str)] = filename
index = -1
for sample_bank in sample_banks:
offsets = sorted(set(sample_bank.entries.keys()))
for offset in offsets:
entry = sample_bank.entries[offset]
index += 1
if index in index_to_filename:
filename = index_to_filename[index]
dir = os.path.dirname(filename)
if dir not in created_dirs:
os.makedirs(dir, exist_ok=True)
created_dirs.add(dir)
write_aiff(entry, filename)
return
# Yeppers
if not ExtC:
# Generate aiff files
for sample_bank in sample_banks:
dir = os.path.join(samples_out_dir, sample_bank.name)
os.makedirs(dir, exist_ok=True)
offsets = sorted(set(sample_bank.entries.keys()))
# print(sample_bank.name, len(offsets), 'entries')
offsets.append(len(sample_bank.data))
# assert 0 in offsets
for offset, next_offset, index in zip(offsets, offsets[1:], range(len(offsets))):
entry = sample_bank.entries[offset]
entry.name = "{:02X}".format(index)
size = next_offset - offset
# assert size % 16 == 0
# assert size - 15 <= len(entry.data) <= size
garbage = sample_bank.data[offset + len(entry.data) : offset + size]
if len(entry.data) % 2 == 1:
assert garbage[0] == 0
if next_offset != offsets[-1]:
# (The last chunk follows a more complex garbage pattern)
assert all(x == 0 for x in garbage)
filename = os.path.join(dir, entry.name + ".aiff")
write_aiff(entry, filename)
# Generate sound bank .json files
os.makedirs(banks_out_dir, exist_ok=True)
sound_bank_register_lines: list[str] = []
for bank_index, bank in enumerate(banks):
filename = os.path.join(banks_out_dir, bank.name + ".json")
sound_bank_register_lines.append(
f"smlua_audio_utils_replace_bank({bank_index}, '{bank.name}.json')\n"
)
with open(filename, "w") as out:
def sound_to_json(sound):
entry = bank.samples[sound.sample_addr]
if len(set(entry.tunings)) == 1:
return entry.name
return {"sample": entry.name, "tuning": round_f32(sound.tuning)}
bank_json = {
"date": bank.iso_date,
"sample_bank": bank.sample_bank.name,
"envelopes": {},
"instruments": {},
"instrument_list": [],
}
addr_to_name = {}
# Envelopes
for env in bank.envelopes.values():
env_json: List[Any] = []
ins: Any = None
for delay, arg in env.entries:
if delay == 0:
ins = "stop"
# assert arg == 0
elif delay == 2**16 - 1:
ins = "hang"
# assert arg == 0
elif delay == 2**16 - 2:
ins = ["goto", arg]
elif delay == 2**16 - 3:
ins = "restart"
# assert arg == 0
else:
ins = [delay, arg]
env_json.append(NoIndent(ins))
bank_json["envelopes"][env.name] = env_json
# Instruments/drums
for inst_index, inst in enumerate(bank.all_insts):
if isinstance(inst, Inst):
inst_json = {
"ifdef": inst_ifdef_json(bank_index, inst_index),
"release_rate": inst.release_rate,
"normal_range_lo": inst.normal_range_lo,
"normal_range_hi": inst.normal_range_hi,
"envelope": bank.envelopes[inst.envelope].name,
}
if inst_json["ifdef"] is None:
del inst_json["ifdef"]
if inst.sound_lo is not None:
inst_json["sound_lo"] = NoIndent(sound_to_json(inst.sound_lo))
else:
del inst_json["normal_range_lo"]
inst_json["sound"] = NoIndent(sound_to_json(inst.sound_med))
if inst.sound_hi is not None:
inst_json["sound_hi"] = NoIndent(sound_to_json(inst.sound_hi))
else:
del inst_json["normal_range_hi"]
bank_json["instruments"][inst.name] = inst_json
addr_to_name[inst.addr] = inst.name
else:
assert isinstance(inst, list)
drums_list_json = []
for drum in inst:
drum_json = {
"release_rate": drum.release_rate,
"pan": drum.pan,
"envelope": bank.envelopes[drum.envelope].name,
"sound": sound_to_json(drum.sound),
}
drums_list_json.append(NoIndent(drum_json))
bank_json["instruments"]["percussion"] = drums_list_json
# Instrument lists
for addr in bank.inst_list:
if addr is None:
bank_json["instrument_list"].append(None)
else:
bank_json["instrument_list"].append(addr_to_name[addr])
out.write(json.dumps(bank_json, indent=4, cls=NoIndentEncoder))
out.write("\n")
# TODO: Fix coop's implementation of smlua_audio_utils_replace_bank and restore this.
# txt.write_lua_append(sound_bank_register_lines, "main.lua")
if __name__ == "__main__":
main()