-
Notifications
You must be signed in to change notification settings - Fork 15
/
cue.py
executable file
·295 lines (251 loc) · 10.2 KB
/
cue.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
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
#!/usr/bin/env python
# coding: utf-8
#
# A utility to automate building PSP Document files
# Based on PSP Docmaker GUI by takedown psp.in.th
import argparse
import io
import glob
import os
import struct
verbose = False
SECTLEN = 2352
def fixup_cue(cue, raw=False, psxtruncate=False):
file = None
filesize = None
_t = -1
_i = -1
for track in range(1, len(cue['TRACKS']) +1):
if file != cue['TRACKS'][track]['FILE']:
file = cue['TRACKS'][track]['FILE']
_t = -1
_i = -1
filesize = os.stat(file).st_size
for idx in cue['TRACKS'][track]['INDEX']:
cue['TRACKS'][track]['INDEX'][idx]['STOPSECT'] = int(filesize / SECTLEN) - 1
if _t >=0 and _i >= 0:
cue['TRACKS'][_t]['INDEX'][_i]['STOPSECT'] = cue['TRACKS'][track]['INDEX'][idx]['STARTSECT'] - 1
_t = track
_i = idx
mode = cue['TRACKS'][track]['MODE']
if mode == 'MODE1/2352':
cue['TRACKS'][track]['BSTART'] = 16
cue['TRACKS'][track]['BSIZE'] = 2048
if mode == 'MODE2/2352':
cue['TRACKS'][track]['BSTART'] = 24
cue['TRACKS'][track]['BSIZE'] = 2048
if raw:
cue['TRACKS'][track]['BSTART'] = 0
cue['TRACKS'][track]['BSIZE'] = 2352
if psxtruncate:
cue['TRACKS'][track]['BSIZE'] = 2336
if mode == 'MODE2/2336':
cue['TRACKS'][track]['BSTART'] = 16
cue['TRACKS'][track]['BSIZE'] = 2336
if mode == 'AUDIO':
cue['TRACKS'][track]['BSTART'] = 0
cue['TRACKS'][track]['BSIZE'] = 2352
def parse_cue(cuefile, raw=False, psxtruncate=False):
def strip_line(line):
# strip leading/trailing whitespace and ignore blank lines
while line and line[0] and line[0] in [' ', '\t', '\r', '\n', '"']:
line = line[1:]
while line and line[-1] and line[-1] in [' ', '\t', '\r', '\n', '"']:
line = line[:-1]
return line
section = None
cue = {}
file = None
filesize = 0
cue['TRACKS'] = {}
track = None
with open(cuefile, 'r') as f:
for line in f.readlines():
line = strip_line(line)
if not line:
continue
if line.upper()[:5] == 'FILE ':
file = strip_line(line[5:line.rindex(' ')])
try:
# Windows absolute paths are ?:\
if file[1:3] != ':\\' and file[0] != '/' and cuefile.rindex('/') >= 0:
file = cuefile[:cuefile.rindex('/') + 1] + file
except:
True
if line.upper()[:6] == 'TRACK ':
track = int(strip_line(line[6:line.rindex(' ')]))
mode = strip_line(line[line.rindex(' '):]).upper()
cue['TRACKS'][track] = {}
cue['TRACKS'][track]['MODE'] = mode
cue['TRACKS'][track]['FILE'] = file
cue['TRACKS'][track]['INDEX'] = {}
if line.upper()[:6] == 'INDEX ':
idx = int(strip_line(line[6:line.rindex(' ')]))
min, sec, frame = strip_line(line[line.rindex(' '):]).split(':')
min = int(min)
sec = int(sec)
frame = int(frame)
sector = 75 * ( int(min) * 60 + int(sec) ) + int(frame)
if idx == 1 and cue['TRACKS'][track]['MODE'] == 'AUDIO' and len(cue['TRACKS'][track]['INDEX']) == 0:
cue['TRACKS'][track]['INDEX'][0] = {}
cue['TRACKS'][track]['INDEX'][0]['STARTSECT'] = sector - 150
cue['TRACKS'][track]['INDEX'][idx] = {}
cue['TRACKS'][track]['INDEX'][idx]['STARTSECT'] = sector
if idx > 1:
raise Exception('Can not handle TRACK INDEX > 1 yet')
fixup_cue(cue, raw=raw, psxtruncate=psxtruncate)
return cue
def write_cue(cue, file):
with open(file, 'w') as o:
file = None
_t = -1
_i = -1
for track in range(1, len(cue['TRACKS']) +1):
if file != cue['TRACKS'][track]['FILE']:
file = cue['TRACKS'][track]['FILE']
_t = -1
_i = -1
o.write("FILE \"%s\" BINARY\n" % file)
o.write(" TRACK %d %s\n" % (track, cue['TRACKS'][track]['MODE']))
if cue['TRACKS'][track]['MODE'] == 'AUDIO' and 0 not in cue['TRACKS'][track]['INDEX']:
# add preamble
sector = cue['TRACKS'][track]['INDEX'][idx]['STARTSECT'] - 150
frames = int(sector % 75)
sector = sector - frames
secs = int((sector / 75) % 60)
sector = sector - secs * 75
mins = int(sector / 75 / 60)
o.write(" INDEX 00 %02d:%02d:%02d\n" % (mins, secs, frames))
for idx in cue['TRACKS'][track]['INDEX']:
sector = cue['TRACKS'][track]['INDEX'][idx]['STARTSECT']
frames = int(sector % 75)
sector = sector - frames
secs = int((sector / 75) % 60)
sector = sector - secs * 75
mins = int(sector / 75 / 60)
o.write(" INDEX %02d %02d:%02d:%02d\n" % (idx, mins, secs, frames))
def parse_ccd(ccdfile):
f = open(ccdfile, 'r')
section = None
ccd = {}
ccd['FILE'] = os.path.abspath(ccdfile)[:-4] + '.img'
for line in f.readlines():
# strip leading/trailing whitespace and ignore blank lines
while line and line[0] and line[0] in [' ', '\t', '\r', '\n']:
line = line[1:]
while line and line[-1] and line[-1] in [' ', '\t', '\r', '\n']:
line = line[:-1]
if not line:
continue
if line[0] == '[':
section = line[1:-1]
ccd[section] = {}
continue
# Read individual entries, we must be in a section by now
if not section:
print('ERROR: key=value outside of section')
return None
kv = line.split('=')
try:
ccd[section][kv[0]] = int(kv[1], base=16) if kv[1][:2] == '0x' else int(kv[1])
except:
ccd[section][kv[0]] = kv[1]
# Fixup the TRACKS
ccd['TRACKS'] = {}
_r = []
for k in ccd:
if k[:6].upper() == 'TRACK ':
idx = int(k[6:])
ccd['TRACKS'][idx] = ccd[k]
_r.append(k)
for i in _r:
del ccd[i]
_r = []
for track in ccd['TRACKS']:
ccd['TRACKS'][track]['INDEX'] = {}
for k in ccd['TRACKS'][track]:
if k[:6].upper() == 'INDEX ':
idx = int(k[6:])
ccd['TRACKS'][track]['INDEX'][idx] = ccd['TRACKS'][track][k]
_r.append((track,k))
for i in _r:
del ccd['TRACKS'][i[0]][i[1]]
# Fixup the ENTRIES
ccd['ENTRIES'] = {}
_r = []
for k in ccd:
if k[:6].upper() == 'ENTRY ':
idx = int(k[6:])
ccd['ENTRIES'][idx] = ccd[k]
_r.append(k)
for i in _r:
del ccd[i]
return ccd
def ccd2cue(ccd):
cue = {}
if 'CATALOG' in ccd:
cue['CATALOG'] = ccd['CATALOG']
cue['TRACKS'] = {}
for track in ccd['TRACKS']:
cue['TRACKS'][track] = {}
cue['TRACKS'][track]['FILE'] = ccd['FILE']
if ccd['TRACKS'][track]['MODE'] == 0:
cue['TRACKS'][track]['MODE'] = 'AUDIO'
if ccd['TRACKS'][track]['MODE'] == 1:
cue['TRACKS'][track]['MODE'] = 'MODE1/2352'
if ccd['TRACKS'][track]['MODE'] == 2:
cue['TRACKS'][track]['MODE'] = 'MODE2/2352'
if 'MODE' not in cue['TRACKS'][track]:
raise Exception('Can not handle MODE %d yet' % ccd['TRACKS'][track]['MODE'])
if 'FLAGS' in ccd['TRACKS'][track]:
cue['TRACKS'][track]['FLAGS'] = ccd['TRACKS'][track]['FLAGS']
if 'ISRC' in ccd['TRACKS'][track]:
cue['TRACKS'][track]['ISRC'] = ccd['TRACKS'][track]['ISRC']
cue['TRACKS'][track]['INDEX'] = {}
for idx in ccd['TRACKS'][track]['INDEX']:
cue['TRACKS'][track]['INDEX'][idx] = {}
cue['TRACKS'][track]['INDEX'][idx]['STARTSECT'] = ccd['TRACKS'][track]['INDEX'][idx]
#print(track, cue['TRACKS'][track])
fixup_cue(cue)
return cue
#CUE {
# CUE
# 'TRACKS': {1: {'MODE': 'MODE2/2352', 'FILE': 'tmp/PSX ...',
# 'INDEX': {1: {'STARTSECT': 0, 'STOPSECT': 127658}},
# 'BSTART': 24, 'BSIZE': 2048},
# 2: {'MODE': 'AUDIO', 'FILE': 'tmp/PSX ...',
# 'INDEX': {0: {'STARTSECT': 127658, 'STOPSECT': 127808},
# 1: {'STARTSECT': 127808, 'STOPSECT': 139329}},
# 'BSTART': 0, 'BSIZE': 2352},
#
# CCD
# 'FILE': 'tmp/PSX ...',
# 'CloneCD': {'Version': 3},
# 'Disc': {'TocEntries': 21, 'Sessions': 1,
# 'DataTracksScrambled': 0, 'CDTextLength': 0},
# 'Session 1': {'PreGapMode': 2, 'PreGapSubC': 0},
# 'TRACKS': {1: {'MODE': 2, 'INDEX': {1: 0}},
# 2: {'MODE': 0, 'INDEX': {1: 127808}},
# 3: {'MODE': 0, 'INDEX': {1: 139479}},
# ...
# 'ENTRIES': {0: {'Session': 1, 'Point': 160, 'ADR': 1, 'Control': 4,
# 'TrackNo': 0, 'AMin': 0, 'ASec': 0, 'AFrame': 0,
# 'ALBA': -150, 'Zero': 0, 'PMin': 1, 'PSec': 32,
# 'PFrame': 0, 'PLBA': 6750},
# 1: {'Session': 1, 'Point': 161, 'ADR': 1, 'Control': 0,
# 'TrackNo': 0, 'AMin': 0, 'ASec': 0, 'AFrame': 0,
# 'ALBA': -150, 'Zero': 0, 'PMin': 18, 'PSec': 0,
# 'PFrame': 0, 'PLBA': 80850},
# ...
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('-v', action='store_true', help='Verbose')
parser.add_argument('ccd', help='CCD file')
parser.add_argument('cue', help='CUE file')
args = parser.parse_args()
if args.v:
verbose = True
ccd = parse_ccd(args.ccd)
cue = ccd2cue(ccd)
write_cue(cue, args.cue)
print('Wrote', args.cue)