-
Notifications
You must be signed in to change notification settings - Fork 2
/
dw7_to_wav.py
67 lines (33 loc) · 1.2 KB
/
dw7_to_wav.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
"""
Pull out up to 8 Waves from a DW7 file
"""
import wave
import struct
import pathlib
# pip install numpy
import numpy
def dw7_to_wav(INPUT : pathlib.Path, OUTPUT : pathlib.Path):
with open(INPUT, "rb") as f1:
X = f1.read()
OVERALL_LENGTH, = struct.unpack_from("<I", X, 0x0C)
if OVERALL_LENGTH + 0x114 != len(X):
raise Exception
LENGTHS = struct.unpack_from("<65I", X, 0x10)
START = len(X) - sum(LENGTHS)
print("{0:X}".format(START))
if START < 0x114:
raise Exception
J = START
for I, L in enumerate(LENGTHS):
if L > 0:
END = J + L
Z = (numpy.frombuffer(X[J:END], dtype=numpy.int8) + 128).astype(numpy.uint8)
f2 = wave.open(str(OUTPUT.parent.joinpath(str(OUTPUT.stem) + "_{0}".format(I) + OUTPUT.suffix)), "wb")
f2.setnchannels(1)
f2.setsampwidth(1)
f2.setframerate(21410)
f2.writeframes(bytes(Z))
f2.close()
J = END
if __name__=="__main__":
raise Exception("dw7_to_wav should be called as a function only")