Add resume support in do_getalbumart()
[abcde.git] / examples / cue2discid
1 #!/usr/bin/python
2
3 """Outputs a long-form cddb discid given a cue file as input"""
4
5 import fileinput
6 import re
7 import sys
8
9 if sys.version_info > (3,):
10     long = int
11
12 FRAMES_PER_SECOND = 75
13 SAMPLES_PER_SECOND = 44100
14 SAMPLES_PER_FRAME = SAMPLES_PER_SECOND // FRAMES_PER_SECOND
15
16 pregap = 0
17 currentTrackFrame = 0
18 pregapRegex = re.compile(r'PREGAP (?P<min>\d+):(?P<sec>\d\d):(?P<frame>\d\d)')
19 startTrackRegex = re.compile(r'INDEX 01 (?P<min>\d+):(?P<sec>\d\d):(?P<frame>\d\d)')
20 leadInRegex = re.compile(r'REM FLAC__lead-in (?P<sample>\d+)')
21 leadOutRegex = re.compile(r'REM FLAC__lead-out 170 (?P<sample>\d+)')
22 raw_framelist = list()
23 leadInFrame = 0
24 leadOutFrame_raw = 0
25 numTracks = 0
26
27 def cddb_sum(trackseconds):
28     def digits(number):
29         tmp = number
30         while tmp:
31             yield tmp % 10
32             tmp = tmp // 10
33
34     return sum(digits(trackseconds))
35
36 for line in fileinput.input():
37     pregapMatch = pregapRegex.search(line)
38     if pregapMatch:
39         pregap = (int((pregapMatch.group('min')) * 60) + int(pregapMatch.group('sec'))) * FRAMES_PER_SECOND + int(pregapMatch.group('frame'))
40         continue
41
42     startTrackMatch = startTrackRegex.search(line)
43     if startTrackMatch:
44         numTracks += 1
45         currentTrackFrame = ((int(startTrackMatch.group('min')) * 60) + int(startTrackMatch.group('sec'))) * FRAMES_PER_SECOND + int(startTrackMatch.group('frame'))
46         raw_framelist.append(currentTrackFrame)
47         continue
48
49     leadInMatch = leadInRegex.search(line)
50     if leadInMatch:
51         leadInFrame = long(leadInMatch.group('sample')) // SAMPLES_PER_FRAME
52         continue
53
54     leadOutMatch = leadOutRegex.search(line)
55     if leadOutMatch:
56         leadOutFrame_raw = long(leadOutMatch.group('sample')) // SAMPLES_PER_FRAME
57         continue
58
59 leadOutFrame = leadOutFrame_raw + leadInFrame
60
61 framelist = [i + leadInFrame + pregap for i in raw_framelist]
62
63 secondslist = [i//FRAMES_PER_SECOND for i in framelist]
64
65 n = long(sum(map(cddb_sum, secondslist)))
66 t = int(leadOutFrame/FRAMES_PER_SECOND - framelist[0]/FRAMES_PER_SECOND)
67 print("%08x %s %s %s" % ((((n % 0xff) << 24) | (t << 8) | numTracks), numTracks,
68                          " ".join([str(i) for i in framelist]),
69                          int(leadOutFrame/FRAMES_PER_SECOND)))