forked from yuukawahiroshi/ddb-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpack_ddb.py
234 lines (169 loc) · 8.55 KB
/
pack_ddb.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
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
#!/bin/env python3
# I thought what I'd do was, I'd pretend I was one of those deaf-mutes.
import argparse
import io
import re
import os
import os.path
from utils.ddi_utils import DDIModel, reverse_search
def escape_filename(filename: str):
escaped = ""
for char in filename:
if char >= 'a' and char <= 'z':
escaped += char
else:
escaped += "%" + str(ord(char)) + "%"
return escaped
def parse_args(args=None): # : list[str]
# initialize parser
parser = argparse.ArgumentParser()
parser.add_argument('--src_path', required=True,
help='singer tree file path')
parser.add_argument('--dst_path',
help='destination path, '
'default to be "./[singer name]"')
# parse args
args = parser.parse_args(args)
src_path: str = os.path.normpath(args.src_path)
if not os.path.exists(src_path):
raise Exception("singer tree file not exists")
singer_path = re.sub(r"\.tree$", "", src_path)
singer_name = os.path.basename(singer_path)
dst_path: str = args.dst_path
if dst_path is None:
src_dir, src_filename = os.path.split(src_path)
src_name, src_ext = os.path.splitext(src_filename)
dst_filename = singer_name
dst_path = os.path.join(src_dir, src_name, dst_filename)
dst_path: str = os.path.normpath(dst_path)
# make dirs
if not os.path.exists(dst_path):
os.makedirs(dst_path)
return singer_path, singer_name, dst_path
def main():
singer_path, singer_name, dst_path = parse_args()
singer_tree_path = singer_path + ".tree"
if not os.path.exists(singer_tree_path):
raise Exception("Singer tree file not exists.")
with open(singer_tree_path, "rb") as f:
ddi_bytes = f.read()
ddi_data = io.BytesIO(ddi_bytes)
ddi_path = dst_path + "/" + singer_name + ".ddi"
ddb_path = dst_path + "/" + singer_name + ".ddb"
if os.path.exists(ddi_path):
os.remove(ddi_path)
if os.path.exists(ddb_path):
os.remove(ddb_path)
# Read DDI file
print("Reading DDI...")
ddi_model = DDIModel(ddi_bytes)
ddi_model.read()
print("Creating DDB...")
with open(ddb_path, "wb") as ddb_f:
for cvvc, art_items in ddi_model.ddi_data_dict["art"].items():
phonemes = cvvc.split(' ')
if len(phonemes) == 3: # vcv
art_file = singer_path + "/voice/articulation/" + escape_filename(phonemes[0]) + "/" + escape_filename(phonemes[1]) + "/" + escape_filename(phonemes[2])
elif len(phonemes) == 2: #cvvc
art_file = singer_path + "/voice/articulation/" + escape_filename(phonemes[0]) + "/" + escape_filename(phonemes[1])
print("Adding art file: %s" % art_file)
if not os.path.exists(art_file):
raise Exception("Articulation file \"%s\" not found" % art_file)
with open(art_file, "rb") as art_f:
art_bytes = art_f.read()
with io.BytesIO(art_bytes) as art_data:
for i in range(0, len(art_items)):
art_item = art_items[i]
# Add Articulation EpR to ddb
for epr_info in art_item["epr"]:
ddi_epr_pos, epr_offset = epr_info.split("=")
ddb_epr_offset = ddb_f.tell()
ddi_epr_pos = int(ddi_epr_pos, 16)
epr_offset = int(epr_offset, 16)
art_data.seek(epr_offset)
hed = art_data.read(4)
if hed != b"FRM2":
raise Exception("Articulation file \"%s\" is broken" % art_file)
frm_len = int.from_bytes(art_data.read(4), byteorder='little')
epr_cutoff = epr_offset + frm_len
ddb_f.write(art_bytes[epr_offset:epr_cutoff])
# Change offset in ddi
ddi_data.seek(ddi_epr_pos)
ddi_data.write(ddb_epr_offset.to_bytes(8, byteorder="little"))
ddi_snd_pos, t = art_item["snd"].split("=")
snd_offset, _ = t.split("_")
ddi_snd_pos2, t = art_item["snd_start"].split("=")
snd_offset2, _ = t.split("_")
ddi_snd_pos = int(ddi_snd_pos, 16)
snd_offset = int(snd_offset, 16)
ddi_snd_pos2 = int(ddi_snd_pos2, 16)
snd_offset2 = int(snd_offset2, 16)
offset2_delta = snd_offset2 - snd_offset
art_data.seek(snd_offset)
hed = art_data.read(4)
if hed != b"SND ":
raise Exception("Articulation file \"%s\" is broken" % art_file)
snd_len = int.from_bytes(art_data.read(4), byteorder='little')
snd_start = snd_offset + snd_len
ddb_snd_offset = ddb_f.tell() + 0x12
snd_bytes = art_bytes[snd_offset:snd_start]
hed = snd_bytes[0:4]
if hed != b"SND ":
raise Exception("Articulation file \"%s\" is broken" % art_file)
ddb_f.write(snd_bytes)
# Change offset in ddi
ddi_data.seek(ddi_snd_pos)
ddi_data.write(ddb_snd_offset.to_bytes(8, byteorder="little"))
ddi_data.write((ddb_snd_offset + offset2_delta).to_bytes(8, byteorder="little"))
for _, sta_info in ddi_model.sta_data.items():
phoneme = sta_info["phoneme"]
for sta_idx, sta_item in sta_info["stap"].items():
sta_file = singer_path + "/voice/stationary/normal/" + escape_filename(phoneme) + "/" + escape_filename(str(sta_idx))
print("Adding sta file: %s" % sta_file)
if not os.path.exists(sta_file):
raise Exception("Stationary file \"%s\" not found" % sta_file)
with open(sta_file, "rb") as sta_f:
sta_bytes = sta_f.read()
with io.BytesIO(sta_bytes) as sta_data:
# Add Stationary EpR to ddb
for epr_info in sta_item["epr"]:
ddi_epr_pos, epr_offset = epr_info.split("=")
ddb_epr_offset = ddb_f.tell()
ddi_epr_pos = int(ddi_epr_pos, 16)
epr_offset = int(epr_offset, 16)
sta_data.seek(epr_offset)
hed = sta_data.read(4)
if hed != b"FRM2":
raise Exception("Stationary file \"%s\" is broken" % sta_file)
frm_len = int.from_bytes(sta_data.read(4), byteorder='little')
epr_cutoff = epr_offset + frm_len
ddb_f.write(sta_bytes[epr_offset:epr_cutoff])
# Change offset in ddi
ddi_data.seek(ddi_epr_pos)
ddi_data.write(ddb_epr_offset.to_bytes(8, byteorder="little"))
ddi_snd_pos, snd_name = sta_item["snd"].split("=")
snd_offset, snd_id = snd_name.split("_")
ddi_snd_pos = int(ddi_snd_pos, 16)
snd_offset = int(snd_offset, 16)
# real_snd_offset = 0x3d
real_snd_offset = reverse_search(sta_bytes, b"SND ", snd_offset)
sta_data.seek(real_snd_offset)
hed = sta_data.read(4)
if hed != b"SND ":
raise Exception("Stationary file \"%s\" is broken" % sta_file)
snd_len = int.from_bytes(sta_data.read(4), byteorder='little')
snd_start = real_snd_offset + snd_len
delta_snd_offset = snd_offset - real_snd_offset
ddb_snd_offset = ddb_f.tell() + delta_snd_offset
snd_bytes = sta_bytes[real_snd_offset:snd_start]
ddb_f.write(snd_bytes)
# Change offset in ddi
ddi_data.seek(ddi_snd_pos)
ddi_data.write(ddb_snd_offset.to_bytes(8, byteorder="little"))
# Write DDI file
print("Writing DDI...")
with open(ddi_path, "wb") as f:
f.write(ddi_data.getbuffer())
print("Finished...")
if __name__ == '__main__':
main()