|
- #!/usr/bin/env python
-
- def checksum(n):
- ret = 0
-
- while n > 0:
- n, m = divmod(n, 10)
- ret += m
-
- return ret
-
- def discid(tracks):
- '''Pass in a list of tuples. Each tuple must contain
- (minutes, seconds) of the track. The last tuple in the list should
- contain the start of lead out.'''
-
- last = len(tracks) - 1
- tracksms = map(lambda x: x[0] * 60 + x[1], tracks)
- n = sum(map(checksum, tracksms[:-1]))
-
- t = tracksms[-1] - tracksms[0]
-
- discid = (long(n) % 0xff) << 24 | t << 8 | last
-
- ret = [ discid, last ]
- try:
- tracksframes = map(lambda x: x[0] * 60 * 75 + x[1] * 75 + x[2], tracks)
- ret.extend(tracksframes[:-1])
- ret.append(tracksms[-1])
- except IndexError:
- tracksframes = map(lambda x: x[0] * 60 * 75 + x[1] * 75, tracks)
- ret.extend(tracksframes[:-1])
- ret.append(tracksms[-1])
- return ret
-
- if __name__ == '__main__':
- tracks = [ (0, 3, 71), (5, 44, 0) ]
- tracksdiskinfo = [ 0x03015501, 1, 296, 344 ]
- diskinfo = discid(tracks)
-
- assert diskinfo == tracksdiskinfo
-
- print '%08x' % diskinfo[0],
- print ' '.join(map(str, diskinfo[1:]))
|