-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathwwise.py
187 lines (147 loc) · 5.45 KB
/
wwise.py
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
# wwise riff header parser
# thanks to hcs and bnnm work
def parse_wwise(reader):
# default meta config
metadata = {
"format": 0,
"channels": 0,
"sampleRate": 0,
"avgBitrate": 0,
"blockSize": 0,
"bitsPerSample": 0,
"extraSize": 0,
"channelLayout": None,
"channelType": None,
"codec": None,
"codecDisplay": None,
"layoutType": None,
"interleaveBlockSize": None,
"numSamples": None,
"duration": 0
}
if reader.GetStreamLength() == 0:
print(f"[WARNING] null stream size at {reader.GetName()}, unreadable block")
return metadata
header = reader.ReadBytes(4)
# endian check header
if header == b"RIFX":
reader.endianness = "big"
elif header == b"RIFF":
reader.endianness = "little"
else:
print(f"[WARNING] invalid header {header} at {reader.GetName()}, assuming unreadable")
return metadata
# additional check
reader.SetBufferPos(0x08)
check = reader.ReadBytes(4)
if check != b"WAVE" and check != "XWMA":
print(f"[WARNING] invalid check mark {check}, assuming unreadable")
return metadata
# read chunks
reader.SetBufferPos(0x0C)
chunks = {}
while reader.GetBufferPos() < reader.GetStreamLength():
chunk_type = reader.ReadBytes(4)
if chunk_type not in [b"fmt ", b"JUNK", b"data", b"akd ", b"cue ", b"LIST", b"smpl"]:
print(f"[WARNING] unexpected chunk {chunk_type} at {reader.GetName()}")
formatted_chunk_type = chunk_type.decode("utf-8").replace(" ", "")
chunk_length = reader.ReadUInt32()
if chunk_length > reader.GetRemainingLength():
chunk_length = reader.GetRemainingLength()
chunks[formatted_chunk_type] = {
"length": chunk_length,
"offset": reader.GetBufferPos(),
"data": reader.ReadBytes(chunk_length)
}
# reader fmt header
fmt_length = chunks["fmt"]["length"]
if fmt_length < 0x10:
print(f"[WARNING] invalid fmt chunk length {fmt_length} at {reader.GetName()}, skipping")
return metadata
reader.SetBufferPos(chunks["fmt"]["offset"])
metadata["format"] = reader.ReadUInt16()
metadata["channels"] = reader.ReadUInt16()
metadata["sampleRate"] = reader.ReadUInt32()
metadata["avgBitrate"] = reader.ReadUInt32()
metadata["blockSize"] = reader.ReadUInt16()
metadata["bitsPerSample"] = reader.ReadUInt16()
if chunks["fmt"]["length"] > 0x10 and metadata["format"] != 0x0165 and metadata["format"] != 0x0166:
metadata["extraSize"] = reader.ReadUInt16()
if metadata["extraSize"] >= 0x06:
metadata["channelLayout"] = reader.ReadUInt32()
if metadata["channelLayout"] & 0xFF == metadata["channels"]:
metadata["channelType"] = (metadata["channelLayout"] >> 8) & 0x0F
metadata["channelLayout"] = metadata["channelLayout"] >> 12
if metadata["format"] == 0x0166:
print(f"[WARNING] XMA2WAVEFORMATEX in fmt at {reader.GetName()}")
return metadata
# parse codec
codecs = {
0x0001: "PCM",
0x0002: "IMA",
0x0069: "IMA",
0x0161: "XWLA",
0x0162: "XWMA",
0x0165: "XMA2",
0x0166: "XMA2",
0xAAC0: "AAC",
0xFFF0: "DSP",
0xFFFB: "HEVAG",
0xFFFC: "ATRAC9",
0xFFFE: "PCM",
0xFFFF: "VORBIS",
0x3039: "OPUSNX",
0x3040: "OPUS",
0x3041: "OPUSWW",
0x8311: "PTADPCM"
}
# genshin should be *mostly* PTADPCM
# hsr and zzz should be VORBIS
if metadata["format"] not in codecs:
print(f'[WARNING] unknown codec {metadata["format"]} at {reader.GetName()}')
return metadata
codec = codecs[metadata["format"]]
if codec not in ["PTADPCM", "VORBIS"]: # Platinum "PtADPCM" custom ADPCM for Wwise
print(f"[WARNING] unhandled codec {codec}, need to implement this later")
metadata["codec"] = codec
# codec name
codecs_names = {
"PTADPCM": "Platinum 4-bit ADPCM",
"VORBIS": "Custom Vorbis"
}
if codec in codecs_names:
metadata["codecDisplay"] = codecs_names[codec]
else:
metadata["codecDisplay"] = codec
# parse duration
if metadata["codec"] == "PTADPCM":
metadata["layoutType"] = "interleave"
metadata["interleaveBlockSize"] = metadata["blockSize"] // metadata["channels"]
metadata["numSamples"] = int((chunks["data"]["length"] / (metadata["channels"] * metadata["interleaveBlockSize"])) * (2 + (metadata["interleaveBlockSize"] - 0x05) * 2))
metadata["duration"] = metadata["numSamples"] / metadata["sampleRate"]
elif metadata["codec"] == "VORBIS":
if (metadata["blockSize"] != 0 or metadata["bitsPerSample"] != 0):
print(f"[WARNING] worbis type at {reader.GetName()}, skipping")
return metadata
if "vorb" in chunks:
# vorb chunk only in wwise earlier to 2012, therefore impossible for mihoyo games
print(f"[WARNING] found vorb chunk at {reader.GetName()}, is this the correct game ?")
return metadata
extra_offset = chunks["fmt"]["offset"] + 0x18
if metadata["extraSize"] != 0x30:
print(f"[WARNING] unknown extra wwise size at {reader.GetName()}, skipping")
return metadata
data_offset = 0x10
blocks_offset = 0x28
# define header to type 2, packet to modified and codebook to aoTuV603, required ?
metadata["numSamples"] = reader.ReadInt32(extra_offset)
setup_offset = reader.ReadUInt32(extra_offset + data_offset)
audio_offset = reader.ReadUInt32(extra_offset + data_offset + 0x04)
block_size_1_exp = reader.ReadUInt8(extra_offset + blocks_offset)
block_size_0_exp = reader.ReadUInt8(extra_offset + blocks_offset + 0x01)
# if both exp are equals and extra size is 0x30, then reset packet type to standard
chunks["data"]["offset"] -= audio_offset
# ignore packets update and codebooks parse attempts, not implemented
metadata["layoutType"] = "none"
metadata["duration"] = metadata["numSamples"] / metadata["sampleRate"]
return metadata