source: trunk/src/testing/bin/fileServer/misc/mmpython/disc/DiscID.py @ 4

Revision 4, 2.0 KB checked in by ajaworski, 13 years ago (diff)

Added modified SAGE sources

Line 
1#!/usr/bin/env python
2
3# Module for fetching information about an audio compact disc and
4# returning it in a format friendly to CDDB.
5
6# If called from the command line, will print out disc info in a
7# format identical to Robert Woodcock's 'cd-discid' program.
8
9# Written 17 Nov 1999 by Ben Gertzfield <che@debian.org>
10# This work is released under the GNU GPL, version 2 or later.
11
12# Release version 1.3
13
14
15# changes for mmpython:
16#
17# $Log: DiscID.py,v $
18# Revision 1.4  2004/11/14 19:26:38  dischi
19# fix future warning
20#
21# Revision 1.3  2003/06/23 19:26:16  dischi
22# Fixed bug in the cdrommodule that the file was not closed after usage.
23# The result was a drive you can't eject while the program (e.g. Freevo)
24# is running. Added cvs log for DiscID and cdrommodule to keep track of
25# all changes we did for mmpython.
26#
27#
28
29try:
30    import cdrom, sys
31except ImportError:
32    # Seems cdrom has either not been compiler or is not supported
33    # on this System
34    pass
35   
36
37def cddb_sum(n):
38    ret = 0
39   
40    while n > 0:
41        ret = ret + (n % 10)
42        n = n / 10
43
44    return ret
45
46def disc_id(device):
47    device = cdrom.open(device)
48    (first, last) = cdrom.toc_header(device)
49
50    track_frames = []
51    checksum = 0
52   
53    for i in range(first, last + 1):
54        (min, sec, frame) = cdrom.toc_entry(device, i)
55        checksum = checksum + cddb_sum(min*60 + sec)
56        track_frames.append(min*60*75 + sec*75 + frame)
57
58    (min, sec, frame) = cdrom.leadout(device)
59    track_frames.append(min*60*75 + sec*75 + frame)
60
61    total_time = (track_frames[-1] / 75) - (track_frames[0] / 75)
62               
63    discid = ((long(checksum) % 0xff) << 24 | total_time << 8 | last)
64    cdrom.close(device)
65    return [discid, last] + track_frames[:-1] + [ track_frames[-1] / 75 ]
66
67if __name__ == '__main__':
68
69    dev_name = None
70    device = None
71   
72    if len(sys.argv) >= 2:
73        dev_name = sys.argv[1]
74
75    if dev_name:
76        device = open(dev_name)
77    else:
78        device = open()
79       
80    disc_info = disc_id(device)
81
82    print ('%08lx' % disc_info[0]),
83
84    for i in disc_info[1:]:
85        print ('%d' % i),
Note: See TracBrowser for help on using the repository browser.