Removed -u in favour of -U
[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
8 FRAMES_PER_SECOND = 75
9 SAMPLES_PER_SECOND = 44100
10 SAMPLES_PER_FRAME = SAMPLES_PER_SECOND // FRAMES_PER_SECOND
11
12 pregap = 0
13 currentTrackFrame = 0
14 pregapRegex = re.compile(r'PREGAP (?P<min>\d+):(?P<sec>\d\d):(?P<frame>\d\d)')
15 startTrackRegex = re.compile(r'INDEX 01 (?P<min>\d+):(?P<sec>\d\d):(?P<frame>\d\d)')
16 leadInRegex = re.compile(r'REM FLAC__lead-in (?P<sample>\d+)')
17 leadOutRegex = re.compile(r'REM FLAC__lead-out 170 (?P<sample>\d+)')
18 raw_framelist = list()
19
20 numTracks = 0
21
22 def cddb_sum(trackseconds):
23     def digits(number):
24         tmp = number
25         while tmp:
26             yield tmp % 10
27             tmp = tmp // 10
28
29     return sum(digits(trackseconds))
30
31 for line in fileinput.input():
32     pregapMatch = pregapRegex.search(line)
33     if pregapMatch:
34         pregap = (int((pregapMatch.group('min')) * 60) + int(pregapMatch.group('sec'))) * FRAMES_PER_SECOND + int(pregapMatch.group('frame'))
35         continue
36
37     startTrackMatch = startTrackRegex.search(line)
38     if startTrackMatch:
39         numTracks += 1
40         currentTrackFrame = ((int(startTrackMatch.group('min')) * 60) + int(startTrackMatch.group('sec'))) * FRAMES_PER_SECOND + int(startTrackMatch.group('frame'))
41         raw_framelist.append(currentTrackFrame)
42         continue
43
44     leadInMatch = leadInRegex.search(line)
45     if leadInMatch:
46         leadInFrame = long(leadInMatch.group('sample')) // SAMPLES_PER_FRAME
47         continue
48
49     leadOutMatch = leadOutRegex.search(line)
50     if leadOutMatch:
51         leadOutFrame_raw = long(leadOutMatch.group('sample')) // SAMPLES_PER_FRAME
52         continue
53
54 leadOutFrame = leadOutFrame_raw + leadInFrame
55
56 framelist = [i + leadInFrame + pregap for i in raw_framelist]
57
58 secondslist = [i//FRAMES_PER_SECOND for i in framelist]
59
60 n = long(sum(map(cddb_sum, secondslist)))
61 t = leadOutFrame/FRAMES_PER_SECOND - framelist[0]/FRAMES_PER_SECOND
62 print "%08x" % (((n % 0xff) << 24) | (t << 8) | numTracks), numTracks, 
63 print " ".join([str(i) for i in framelist]), leadOutFrame/FRAMES_PER_SECOND