Tagged 2.3.99.6
[clinton/abcde.git] / examples / cue2discid
CommitLineData
27fdb84b 1#!/usr/bin/python
2
3"""Outputs a long-form cddb discid given a cue file as input"""
4
5import fileinput
6import re
7
8FRAMES_PER_SECOND = 75
9SAMPLES_PER_SECOND = 44100
10SAMPLES_PER_FRAME = SAMPLES_PER_SECOND // FRAMES_PER_SECOND
11
12pregap = 0
13currentTrackFrame = 0
14pregapRegex = re.compile(r'PREGAP (?P<min>\d+):(?P<sec>\d\d):(?P<frame>\d\d)')
15startTrackRegex = re.compile(r'INDEX 01 (?P<min>\d+):(?P<sec>\d\d):(?P<frame>\d\d)')
16leadInRegex = re.compile(r'REM FLAC__lead-in (?P<sample>\d+)')
17leadOutRegex = re.compile(r'REM FLAC__lead-out 170 (?P<sample>\d+)')
18raw_framelist = list()
19
20numTracks = 0
21
22def 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
31for 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
54leadOutFrame = leadOutFrame_raw + leadInFrame
55
56framelist = [i + leadInFrame + pregap for i in raw_framelist]
57
58secondslist = [i//FRAMES_PER_SECOND for i in framelist]
59
60n = long(sum(map(cddb_sum, secondslist)))
61t = leadOutFrame/FRAMES_PER_SECOND - framelist[0]/FRAMES_PER_SECOND
62print "%08x" % (((n % 0xff) << 24) | (t << 8) | numTracks), numTracks,
63print " ".join([str(i) for i in framelist]), leadOutFrame/FRAMES_PER_SECOND