-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsound_replace.py
More file actions
447 lines (414 loc) · 23.7 KB
/
sound_replace.py
File metadata and controls
447 lines (414 loc) · 23.7 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
import os, re, struct, shutil, sys, codecs
def wwisefnv(buf: bytes) -> int:
h = 2166136261
for b in buf:
h *= 16777619
if b >= 65 and b <= 90:
b = b - 65 + 97
h ^= b
h = (h & 0xFFFFFFFF)
return h
def run(bank_name):
shutil.copy(bank_name + ".soundbank_vanilla", bank_name + ".soundbank")
bank = open(bank_name + ".soundbank", "rb+")
bank.seek(0,2)
bankSize = bank.tell()
tracks = []
trackData = []
trackList = []
segments = []
segmentData = []
segmentList = []
checked = []
normalizeList = []
sounds = []
soundData = []
soundList = []
MusicRanSeqs = []
MusicRanSeqData = []
MusicRanSeqList = []
GameSysSrc = []
GameSysDst = []
GameSysDstOld = []
for file in sorted(os.listdir("txtp" + os.path.sep + bank_name)):
if file.endswith(".txt"):
lst = open("txtp" + os.path.sep + bank_name + os.path.sep + file, "r")
for line in lst.readlines():
if ":" in line:
SrcValue = line.split(":")[0]
SrV = wwisefnv(SrcValue.encode("utf-8"))
#print("New hash: " + str(SrV))
GameSysSrc.append(int(SrV).to_bytes(4, "little"))
if len(line.split(":")) > 2:
GameSysDstOld.append(int(line.split(":")[2]).to_bytes(4, "little"))
else:
GameSysDstOld.append(b'\xFF')
GameSysDst.append(int(line.split(":")[1]).to_bytes(4, "little"))
lst.close()
if file.endswith(".txtp"):
txtp = open("txtp" + os.path.sep + bank_name + os.path.sep + file, "r")
txtpRead = txtp.read()
txtp.close()
listFind = re.findall('wem/.+', txtpRead)
soundPrep = re.findall('CAkSound[\\[]\\d+[\\]]\\s+(\\d+)', txtpRead)
for count in range(len(soundPrep)):
soundList.append(int(soundPrep[count]))
MusicRanSeqPrep = re.findall('CAkMusicRanSeqCntr[\\[]\\d+[\\]]\\s+(\\d+)', txtpRead)
for count in range(len(MusicRanSeqPrep)):
MusicRanSeqList.append(int(MusicRanSeqPrep[count]))
segmentPrep = re.findall('CAkMusicSegment[\\[]\\d+[\\]]\\s+(\\d+)', txtpRead)
for count in range(len(segmentPrep)):
segmentList.append(int(segmentPrep[count]))
trackPrep = re.findall('CAkMusicTrack[\\[]\\d+[\\]]\\s+(\\d+)', txtpRead)
for count in range(len(trackPrep)):
trackList.append(int(trackPrep[count]))
soundOff = []
soundFind = re.findall('CAkSound[\\[]\\d+[\\]]\\s+(\\d+)', txtpRead)
if len(soundFind) > 0:
for count in range(len(soundFind)):
soundOff.append(txtpRead.find(soundFind[count]))
for count in range(len(soundFind)):
resultLength = txtpRead[soundOff[count]:]
resultLength = resultLength.split(" ")[1]
sounds.append(soundFind[count])
soundData.append((txtpRead[soundOff[count]:].split("\n#\n#\n")[0]).split("CAk")[0])
segmentFind = re.findall('CAkMusicSegment[\\[]\\d+[\\]]\\s+\\d+', txtpRead)
if len(segmentFind) <= 0:
continue
segmentOff = []
for count in range(len(segmentFind)):
segmentOff.append(txtpRead.find(segmentFind[count]))
for count in range(len(segmentFind)):
resultLength = txtpRead[segmentOff[count]:]
resultLength = resultLength.split(" ")[1].split("\n#\n")[0]
segments.append(resultLength)
segmentData.append(txtpRead[segmentOff[count]:])
trackFind = re.findall('CAkMusicTrack[\\[]\\d+[\\]]\\s+\\d+', txtpRead)
if len(trackFind) <= 0:
continue
trackOff = []
for count in range(len(trackFind)):
trackOff.append(txtpRead.find(trackFind[count]))
autoFind = -1
for count in range(len(trackFind)):
resultLength = txtpRead[trackOff[count]:]
resultLength = resultLength.split(" ")[1]
if resultLength.count('CAkMusicTrack') > 1:
resultLength = 'CAkMusicTrack' + resultLength.split('CAkMusicTrack')[1]
if 'CAkMusicSegment' in resultLength:
resultLength = resultLength.split('CAkMusicSegment')[0]
if 'AkMusicRanSeqPlaylistItem' in resultLength:
resultLength = resultLength.split('AkMusicRanSeqPlaylistItem')[0]
tracks.append(resultLength)
trackData.append(txtpRead[trackOff[count]:])
MusicRanSeqOff = []
MusicRanSeqFind = re.findall('CAkMusicRanSeqCntr[\\[]\\d+[\\]]\\s+(\\d+)', txtpRead)
if len(MusicRanSeqFind) > 0:
for count in range(len(MusicRanSeqFind)):
MusicRanSeqOff.append(txtpRead.find(MusicRanSeqFind[count]))
for count in range(len(MusicRanSeqFind)):
resultLength = txtpRead[MusicRanSeqOff[count]:]
resultLength = resultLength.split(" ")[1]
MusicRanSeqs.append(MusicRanSeqFind[count])
MusicRanSeqData.append((txtpRead[MusicRanSeqOff[count]:].split("\n#\n#\n")[0]).split("CAk")[0].split("AkMusic")[0])
bank.seek(0)
HIRCStart = 0
while bank.tell() < bankSize - 256:
bankStart = bank.tell()
bankRead = bank.read(65536)
if b'HIRC' in bankRead:
HIRCStart = bankStart + bankRead.find(b'HIRC')
bank.seek(HIRCStart + 4)
HIRCSize = int.from_bytes(bank.read(4), "little")
HIRCCount = int.from_bytes(bank.read(4), "little")
for i in range(HIRCCount - 32):
CurrOff = bank.tell()
HIRCItem = int.from_bytes(bank.read(1), "little")
HIRCItemSize = int.from_bytes(bank.read(4), "little")
if HIRCItem == 2:
HIRCItemData = bank.read(HIRCItemSize)
IDA = int.from_bytes(HIRCItemData[:4], "little")
for j in range(len(sounds)):
soundCheck = int(sounds[j].split("\n")[0])
if soundCheck == IDA:
bank.seek(CurrOff + 9)
#formatCheck = int((soundData[j].split("ulPluginID: 0x")[1]).split(" ")[0]).to_bytes(4, "little")
formatCheckPrep = (soundData[j].split("ulPluginID: 0x")[1]).split(" ")[0]
formatCheck = int(formatCheckPrep[0]) << 28
formatCheck += int(formatCheckPrep[1]) << 24
formatCheck += int(formatCheckPrep[2]) << 20
formatCheck += int(formatCheckPrep[3]) << 16
formatCheck += int(formatCheckPrep[4]) << 12
formatCheck += int(formatCheckPrep[5]) << 8
formatCheck += int(formatCheckPrep[6]) << 4
formatCheck += int(formatCheckPrep[7])
if formatCheck != int.from_bytes(HIRCItemData[4:8], "little"):
if formatCheck == 262145:
bank.seek(CurrOff + 9)
bank.write(b'\x01\x00\x04\x00')
print("Sound " + str(int.from_bytes(HIRCItemData[9:13], "little")) + " format now Vorbis!")
elif formatCheck == 1245185:
bank.seek(CurrOff + 9)
bank.write(b'\x01\x00\x13\x00')
print("Sound " + str(int.from_bytes(HIRCItemData[9:13], "little")) + " format now Opus!")
bank.seek(CurrOff + 13)
soundTypeCheck = int((soundData[j].split("StreamType: 0x0")[1]).split(" ")[0]).to_bytes(1, "little")
if soundTypeCheck != HIRCItemData[8].to_bytes(1,"little") and soundTypeCheck == b'\x02':
bank.seek(CurrOff + 13)
bank.write(soundTypeCheck)
print("Sound " + str(int.from_bytes(HIRCItemData[9:13], "little")) + " now streamed!")
bank.seek(CurrOff + 14)
soundSourceCheck = int((soundData[j].split("Source ")[1]).split("\n")[0]).to_bytes(4, "little")
if soundSourceCheck != HIRCItemData[9:13]:
bank.seek(CurrOff + 13)
bank.write(b'\x02')
bank.write(soundSourceCheck)
print("Sound " + str(int.from_bytes(HIRCItemData[9:13], "little")) + " replaced with " + str(int.from_bytes(soundSourceCheck, "little")) + "!")
elif HIRCItem == 10:
HIRCItemData = bank.read(HIRCItemSize)
IDA = int.from_bytes(HIRCItemData[:4], "little")
#if IDA not in segmentList:
#bank.seek(CurrOff + HIRCItemSize + 5)
#continue
for j in range(len(segments)):
segCheck = int(segments[j].split("\n")[0]).to_bytes(4, "little")
if segCheck == HIRCItemData[:4]:
fVolume = 0
bank.seek(CurrOff + 22)
propCheck = int.from_bytes(bank.read(1), "little")
if propCheck > 0:
volumeProp = 0
for iteration in range(propCheck):
propCheck2 = int.from_bytes(bank.read(1), "little")
if propCheck2 == 0:
volumeProp = iteration
bank.seek(CurrOff + 23 + propCheck)
break
for iter2 in range(volumeProp):
bank.seek(4,1)
fVolumeFinds = re.findall('\\* \\[Volume\\]:\\s+([-]?\\d+[.]?\\d*)',segmentData[j])
if len(fVolumeFinds) > 0:
fVolume = float(fVolumeFinds[0])
outputOff = struct.unpack('f',bank.read(4))
if fVolume != outputOff:
bank.seek(-4,1)
bank.write(struct.pack('<f', float(fVolume)))
print("Music Segment " + str(IDA) + " attenuated!")
bank.seek(CurrOff + 5)
fTempo = 0
uTimeSigNumBeatsBar = 0
uTimeSigBeatValue = 0
if segmentData[j].find('fTempo: ') > 0:
fTempo = float(segmentData[j][segmentData[j].find('fTempo: ') + 8:].split('\n')[0])
if segmentData[j].find('uTimeSigNumBeatsBar: ') > 0 and segmentData[j].find('uTimeSigBeatValue: ') > 0:
uTimeSigNumBeatsBar = int(re.findall('uTimeSigNumBeatsBar:\\s(\\d+)', segmentData[j])[0])
uTimeSigBeatValue = int(re.findall('uTimeSigBeatValue:\\s(\\d+)', segmentData[j])[0])
if uTimeSigBeatValue != 2 and uTimeSigBeatValue != 4 and uTimeSigBeatValue != 8 and uTimeSigBeatValue != 16:
uTimeSigNumBeatsBar = 4
uTimeSigBeatValue = 4
fDuration = float(segmentData[j][segmentData[j].find('fDuration: ') + 11:].split('\n')[0])
fEndMark = re.findall('AkMusicMarkerWwise:\\s+(\\d+[.]?\\d*)',segmentData[j])
offFix = HIRCItemSize - 50
if offFix > 0:
bank.seek(CurrOff + offFix)
if fTempo > 0:
bank.write(struct.pack('<f', fTempo))
bank.seek(CurrOff + offFix + 4)
if uTimeSigNumBeatsBar > 0 and uTimeSigBeatValue > 0:
bank.write(uTimeSigNumBeatsBar.to_bytes(1,"little"))
bank.write(uTimeSigBeatValue.to_bytes(1,"little"))
bank.seek(CurrOff + offFix + 11)
bank.write(struct.pack('<d', fDuration))
bank.seek(CurrOff + offFix + 27)
bank.write(struct.pack('<d', float(fEndMark[0])))
bank.seek(CurrOff + offFix + 43)
bank.write(struct.pack('<d', float(fEndMark[1])))
else:
print("Address later!")
elif HIRCItem == 11:
HIRCItemData = bank.read(HIRCItemSize)
IDB = int.from_bytes(HIRCItemData[:4], "little")
if IDB not in trackList:
bank.seek(CurrOff + HIRCItemSize + 5)
continue
for j in range(len(tracks)):
trackCheck = (int(tracks[j].split("\n")[0])).to_bytes(4, "little")
if trackCheck == HIRCItemData[:4]:
bank.seek(CurrOff + 10)
numSubTrack = int.from_bytes(bank.read(1), "little")
bank.seek(CurrOff + 14)
for k in range(numSubTrack):
ulPluginIDIn = int.from_bytes(bank.read(4), "little")
ulPluginIDOut = int(re.findall('ulPluginID\\: 0x(\\d+)', trackData[j])[k])
if ulPluginIDIn != ulPluginIDOut:
if ulPluginIDOut == 262145 or ulPluginIDOut == 1245185:
bank.seek(-4,1)
bank.write(ulPluginIDOut.to_bytes(4,"little"))
if ulPluginIDOut == 262145:
print("Format changed to Vorbis!")
else:
print("Format changed to Opus!")
StreamTypeIn = int.from_bytes(bank.read(1), "little")
StreamTypeOut = int(re.findall('StreamType\\: 0x(\\d+)', trackData[j])[k])
if StreamTypeIn != StreamTypeOut:
if StreamTypeOut == 2:
bank.seek(-1,1)
bank.write(StreamTypeOut.to_bytes(1,"little"))
print("Sound now streamed!")
IDCheck = int.from_bytes(bank.read(4),"little")
sourceID = int(re.findall('sourceID\\: (\\d+)', trackData[j])[k])
if IDCheck != sourceID:
bank.seek(-4,1)
bank.write(sourceID.to_bytes(4,"little"))
bank.seek(5,1)
playlistItems = int.from_bytes(bank.read(1), "little")
bank.seek(CurrOff + 22 + (14 * numSubTrack))
print(playlistItems)
print(bank.tell())
for k in range(playlistItems):
IDCheck = int.from_bytes(bank.read(4),"little")
if IDCheck == 0:
break
sourceID = int(re.findall('sourceID\\: (\\d+)', trackData[j])[k])
if IDCheck != sourceID:
bank.seek(-4,1)
bank.write(sourceID.to_bytes(4,"little"))
print("Music " + str(IDCheck) + " replaced with " + str(sourceID) + "!")
bank.seek(4,1)
fPlayAt = float(re.findall('fPlayAt\\: ([-]?\\d+)', trackData[j])[k])
bank.write(struct.pack('<d', fPlayAt))
fBeginTrimOffset = float(re.findall('fBeginTrimOffset\\: ([-]?\\d+)', trackData[j])[k])
bank.write(struct.pack('<d', fBeginTrimOffset))
fEndTrimOffset = float(re.findall('fEndTrimOffset\\: ([-]?\\d+)', trackData[j])[k])
bank.write(struct.pack('<d', fEndTrimOffset))
fSrcDuration = float(re.findall('fSrcDuration\\: ([-]?\\d+)', trackData[j])[k])
bank.write(struct.pack('<d', fSrcDuration))
bank.seek(4,1)
numAutos = int.from_bytes(bank.read(4), "little")
if numAutos > 8:
continue
if numAutos > 0: print(numAutos)
repeatAuto = -1
autoClip = 0
for autoCount in range(numAutos):
repeatAuto += 1
autoClipNew = int.from_bytes(bank.read(4), "little")
if autoClip != autoClipNew:
repeatAuto = 0
autoClip = autoClipNew
bank.seek(4,1)
numAutoSteps = int.from_bytes(bank.read(4), "little")
for n in range(numAutoSteps):
[fTimeIn] = struct.unpack('f',bank.read(4))
[fValIn] = struct.unpack('f',bank.read(4))
fCurveType = int.from_bytes(bank.read(4),"little")
if ('Automation ' + str(autoClip) + ' Point ' + str(n) + ' Time') in trackData[j]:
fTimeOut = float(re.findall('Automation ' + str(autoClip) + ' Point ' + str(n) + ' Time\\: ([-]?\\d+[.]?\\d*)', trackData[j])[repeatAuto])
if fTimeIn != fTimeOut:
bank.seek(-12,1)
bank.write(struct.pack('<f',fTimeOut))
bank.seek(8,1)
print("Reset time of automation " + str(autoCount) + " + " + str(n) + " from " + str(fTimeIn) + " to " + str(fTimeOut))
if ('Automation ' + str(autoClip) + ' Point ' + str(n) + ' Value') in trackData[j]:
fValOut = float(re.findall('Automation ' + str(autoClip) + ' Point ' + str(n) + ' Value\\: ([-]?\\d+[.]?\\d*)', trackData[j])[repeatAuto])
if fValIn != fValOut:
bank.seek(-8,1)
bank.write(struct.pack('<f',fValOut))
bank.seek(4,1)
print("Reset value of automation " + str(autoClip) + " + " + str(n) + " from " + str(fValIn) + " to " + str(fValOut))
elif HIRCItem == 12:
HIRCItemData = bank.read(HIRCItemSize)
IDC = int.from_bytes(HIRCItemData[:4], "little")
for IDD in range(len(GameSysSrc)):
if GameSysSrc[IDD] in HIRCItemData:
if GameSysDstOld[IDD] in HIRCItemData and GameSysDstOld[IDD] != b'\xFF':
bank.seek(CurrOff + 5 + HIRCItemData.find(GameSysSrc[IDD]) + 4)
print(GameSysSrc[IDD])
print(GameSysDstOld[IDD])
print("Check above!")
bank.write(GameSysDst[IDD])
elif GameSysDstOld[IDD] == b'\xFF':
bank.seek(CurrOff + 5 + HIRCItemData.find(GameSysSrc[IDD]) + 4)
bank.write(GameSysDst[IDD])
elif HIRCItem == 13:
HIRCItemData = bank.read(HIRCItemSize)
IDC = int.from_bytes(HIRCItemData[:4], "little")
if IDC not in MusicRanSeqList:
bank.seek(CurrOff + HIRCItemSize + 5)
continue
fVolume = 0
bank.seek(CurrOff + 22)
j = MusicRanSeqList.index(IDC)
propCheck = int.from_bytes(bank.read(1), "little")
if propCheck > 0:
volumeProp = 0
for iteration in range(propCheck):
propCheck2 = int.from_bytes(bank.read(1), "little")
if propCheck2 == 0:
volumeProp = iteration
bank.seek(CurrOff + 23 + propCheck)
break
for iter2 in range(volumeProp):
bank.seek(4,1)
fVolumeFinds = re.findall('\\* \\[Volume\\]:\\s+([-]?\\d+[.]?\\d*)',MusicRanSeqData[j])
if len(fVolumeFinds) > 0:
fVolume = float(fVolumeFinds[0])
outputOff = struct.unpack('f',bank.read(4))
if fVolume != outputOff:
bank.seek(-4,1)
bank.write(struct.pack('<f', float(fVolume)))
print("Music Playlist " + str(IDC) + " attenuated!")
ranCheck = int(MusicRanSeqs[j].split("\n")[0]).to_bytes(4, "little")
timeFound = HIRCItemData.find(b'\x04\x04\x01')
if timeFound == -1:
timeFound = HIRCItemData.find(b'\x03\x04\x01')
if timeFound == -1:
timeFound = HIRCItemData.find(b'\x05\x04\x01')
if timeFound == -1:
timeFound = HIRCItemData.find(b'\x07\x04\x01')
if timeFound == -1:
timeFound = HIRCItemData.find(b'\x06\x08\x01')
if timeFound == -1:
timeFound = HIRCItemData.find(b'\x05\x08\x01')
if timeFound == -1:
timeFound = HIRCItemData.find(b'\x07\x08\x01')
if timeFound == -1:
timeFound = HIRCItemData.find(b'\x13\x16\x01')
if ranCheck == HIRCItemData[:4] and timeFound > -1:
print("Checking")
bank.seek(CurrOff + 5 + timeFound - 20)
if MusicRanSeqData[j].find('fGridPeriod: ') >= 0:
fGridPeriod = float(MusicRanSeqData[j][MusicRanSeqData[j].find('fGridPeriod: ') + 13:].split('\n')[0])
if fGridPeriod > 0:
bank.write(struct.pack('<d', fGridPeriod))
bank.seek(CurrOff + 5 + timeFound - 12)
if MusicRanSeqData[j].find('fGridOffset: ') >= 0:
fGridOffset = float(MusicRanSeqData[j][MusicRanSeqData[j].find('fGridOffset: ') + 13:].split('\n')[0])
if fGridOffset != 0:
bank.write(struct.pack('<d', fGridOffset))
bank.seek(CurrOff + 5 + timeFound - 4)
if MusicRanSeqData[j].find('fTempo: ') >= 0:
fTempo = float(MusicRanSeqData[j][MusicRanSeqData[j].find('fTempo: ') + 8:].split('\n')[0])
if fTempo > 0:
bank.write(struct.pack('<f', fTempo))
print("Playlist Tempo Changed!")
bank.seek(CurrOff + 5 + timeFound)
if MusicRanSeqData[j].find('uTimeSigNumBeatsBar: ') >= 0:
uTimeSigNumBeatsBar = int(MusicRanSeqData[j][MusicRanSeqData[j].find('uTimeSigNumBeatsBar: ') + 21:].split('\n')[0])
if fTempo > 0:
bank.write(uTimeSigNumBeatsBar.to_bytes(1,"little"))
if MusicRanSeqData[j].find('uTimeSigBeatValue: ') >= 0:
uTimeSigBeatValue = int(MusicRanSeqData[j][MusicRanSeqData[j].find('uTimeSigBeatValue: ') + 19:].split('\n')[0])
if fTempo > 0:
bank.write(uTimeSigBeatValue.to_bytes(1,"little"))
else:
bank.seek(HIRCItemSize,1)
bank.seek(CurrOff + HIRCItemSize + 5)
bank.close()
print("Done!")
if len(sys.argv) > 1:
run(sys.argv[1])
else:
print("Nothing done.")
print("Specify a WWise soundbank without the extension.")
print("An unmodifed copy with extension \"soundbank_vanilla\" should already exist!")