1992-04-22 13:24:13 +00:00
|
|
|
# Class interface to the CD module.
|
|
|
|
|
|
|
|
import cd, CD
|
|
|
|
|
2000-08-18 14:59:33 +00:00
|
|
|
class Error(Exception):
|
|
|
|
pass
|
|
|
|
class _Stop(Exception):
|
|
|
|
pass
|
1992-04-22 13:24:13 +00:00
|
|
|
|
1993-03-29 10:50:29 +00:00
|
|
|
def _doatime(self, cb_type, data):
|
1992-04-22 13:24:13 +00:00
|
|
|
if ((data[0] * 60) + data[1]) * 75 + data[2] > self.end:
|
1992-06-03 16:47:24 +00:00
|
|
|
## print 'done with list entry',`self.listindex`
|
1992-04-22 13:24:13 +00:00
|
|
|
raise _Stop
|
1993-03-29 10:50:29 +00:00
|
|
|
func, arg = self.callbacks[cb_type]
|
1992-04-22 13:24:13 +00:00
|
|
|
if func:
|
1993-03-29 10:50:29 +00:00
|
|
|
func(arg, cb_type, data)
|
1992-04-22 13:24:13 +00:00
|
|
|
|
1993-03-29 10:50:29 +00:00
|
|
|
def _dopnum(self, cb_type, data):
|
1992-04-22 13:24:13 +00:00
|
|
|
if data > self.end:
|
1992-06-03 16:47:24 +00:00
|
|
|
## print 'done with list entry',`self.listindex`
|
1992-04-22 13:24:13 +00:00
|
|
|
raise _Stop
|
1993-03-29 10:50:29 +00:00
|
|
|
func, arg = self.callbacks[cb_type]
|
1992-04-22 13:24:13 +00:00
|
|
|
if func:
|
1993-03-29 10:50:29 +00:00
|
|
|
func(arg, cb_type, data)
|
1992-04-22 13:24:13 +00:00
|
|
|
|
1993-05-24 14:16:22 +00:00
|
|
|
class Readcd:
|
1993-11-08 15:10:43 +00:00
|
|
|
def __init__(self, *arg):
|
1992-04-22 13:24:13 +00:00
|
|
|
if len(arg) == 0:
|
|
|
|
self.player = cd.open()
|
|
|
|
elif len(arg) == 1:
|
|
|
|
self.player = cd.open(arg[0])
|
|
|
|
elif len(arg) == 2:
|
|
|
|
self.player = cd.open(arg[0], arg[1])
|
|
|
|
else:
|
1993-12-17 15:25:27 +00:00
|
|
|
raise Error, 'bad __init__ call'
|
1992-04-22 13:24:13 +00:00
|
|
|
self.list = []
|
|
|
|
self.callbacks = [(None, None)] * 8
|
|
|
|
self.parser = cd.createparser()
|
|
|
|
self.playing = 0
|
|
|
|
self.end = 0
|
|
|
|
self.status = None
|
|
|
|
self.trackinfo = None
|
|
|
|
|
|
|
|
def eject(self):
|
|
|
|
self.player.eject()
|
|
|
|
self.list = []
|
|
|
|
self.end = 0
|
|
|
|
self.listindex = 0
|
|
|
|
self.status = None
|
|
|
|
self.trackinfo = None
|
|
|
|
if self.playing:
|
1992-06-03 16:47:24 +00:00
|
|
|
## print 'stop playing from eject'
|
1992-04-22 13:24:13 +00:00
|
|
|
raise _Stop
|
|
|
|
|
|
|
|
def pmsf2msf(self, track, min, sec, frame):
|
|
|
|
if not self.status:
|
1992-04-22 14:33:26 +00:00
|
|
|
self.cachestatus()
|
1992-04-22 13:24:13 +00:00
|
|
|
if track < self.status[5] or track > self.status[6]:
|
|
|
|
raise Error, 'track number out of range'
|
1992-04-22 14:33:26 +00:00
|
|
|
if not self.trackinfo:
|
|
|
|
self.cacheinfo()
|
1992-04-22 13:24:13 +00:00
|
|
|
start, total = self.trackinfo[track]
|
|
|
|
start = ((start[0] * 60) + start[1]) * 75 + start[2]
|
|
|
|
total = ((total[0] * 60) + total[1]) * 75 + total[2]
|
|
|
|
block = ((min * 60) + sec) * 75 + frame
|
|
|
|
if block > total:
|
|
|
|
raise Error, 'out of range'
|
|
|
|
block = start + block
|
|
|
|
min, block = divmod(block, 75*60)
|
|
|
|
sec, frame = divmod(block, 75)
|
|
|
|
return min, sec, frame
|
|
|
|
|
|
|
|
def reset(self):
|
|
|
|
self.list = []
|
|
|
|
|
|
|
|
def appendtrack(self, track):
|
|
|
|
self.appendstretch(track, track)
|
|
|
|
|
|
|
|
def appendstretch(self, start, end):
|
|
|
|
if not self.status:
|
1992-04-22 14:33:26 +00:00
|
|
|
self.cachestatus()
|
1992-04-22 13:24:13 +00:00
|
|
|
if not start:
|
|
|
|
start = 1
|
|
|
|
if not end:
|
|
|
|
end = self.status[6]
|
1993-03-29 10:50:29 +00:00
|
|
|
if type(end) == type(0):
|
|
|
|
if end < self.status[5] or end > self.status[6]:
|
|
|
|
raise Error, 'range error'
|
|
|
|
else:
|
1992-04-22 13:24:13 +00:00
|
|
|
l = len(end)
|
|
|
|
if l == 4:
|
|
|
|
prog, min, sec, frame = end
|
|
|
|
if prog < self.status[5] or prog > self.status[6]:
|
|
|
|
raise Error, 'range error'
|
|
|
|
end = self.pmsf2msf(prog, min, sec, frame)
|
|
|
|
elif l <> 3:
|
|
|
|
raise Error, 'syntax error'
|
1993-03-29 10:50:29 +00:00
|
|
|
if type(start) == type(0):
|
|
|
|
if start < self.status[5] or start > self.status[6]:
|
1992-04-22 13:24:13 +00:00
|
|
|
raise Error, 'range error'
|
1993-03-29 10:50:29 +00:00
|
|
|
if len(self.list) > 0:
|
|
|
|
s, e = self.list[-1]
|
|
|
|
if type(e) == type(0):
|
|
|
|
if start == e+1:
|
|
|
|
start = s
|
|
|
|
del self.list[-1]
|
|
|
|
else:
|
1992-04-22 13:24:13 +00:00
|
|
|
l = len(start)
|
|
|
|
if l == 4:
|
|
|
|
prog, min, sec, frame = start
|
|
|
|
if prog < self.status[5] or prog > self.status[6]:
|
|
|
|
raise Error, 'range error'
|
|
|
|
start = self.pmsf2msf(prog, min, sec, frame)
|
|
|
|
elif l <> 3:
|
|
|
|
raise Error, 'syntax error'
|
|
|
|
self.list.append((start, end))
|
|
|
|
|
|
|
|
def settracks(self, list):
|
|
|
|
self.list = []
|
|
|
|
for track in list:
|
|
|
|
self.appendtrack(track)
|
|
|
|
|
1993-03-29 10:50:29 +00:00
|
|
|
def setcallback(self, cb_type, func, arg):
|
|
|
|
if cb_type < 0 or cb_type >= 8:
|
1992-04-22 13:24:13 +00:00
|
|
|
raise Error, 'type out of range'
|
1993-03-29 10:50:29 +00:00
|
|
|
self.callbacks[cb_type] = (func, arg)
|
1992-04-22 13:24:13 +00:00
|
|
|
if self.playing:
|
1993-03-29 10:50:29 +00:00
|
|
|
start, end = self.list[self.listindex]
|
|
|
|
if type(end) == type(0):
|
|
|
|
if cb_type <> CD.PNUM:
|
|
|
|
self.parser.setcallback(cb_type, func, arg)
|
|
|
|
else:
|
|
|
|
if cb_type <> CD.ATIME:
|
|
|
|
self.parser.setcallback(cb_type, func, arg)
|
|
|
|
|
|
|
|
def removecallback(self, cb_type):
|
|
|
|
if cb_type < 0 or cb_type >= 8:
|
1992-04-22 13:24:13 +00:00
|
|
|
raise Error, 'type out of range'
|
1993-03-29 10:50:29 +00:00
|
|
|
self.callbacks[cb_type] = (None, None)
|
1992-04-22 13:24:13 +00:00
|
|
|
if self.playing:
|
1993-03-29 10:50:29 +00:00
|
|
|
start, end = self.list[self.listindex]
|
|
|
|
if type(end) == type(0):
|
|
|
|
if cb_type <> CD.PNUM:
|
|
|
|
self.parser.removecallback(cb_type)
|
|
|
|
else:
|
|
|
|
if cb_type <> CD.ATIME:
|
|
|
|
self.parser.removecallback(cb_type)
|
1992-04-22 13:24:13 +00:00
|
|
|
|
|
|
|
def gettrackinfo(self, *arg):
|
|
|
|
if not self.status:
|
1992-04-22 14:33:26 +00:00
|
|
|
self.cachestatus()
|
1992-04-22 13:24:13 +00:00
|
|
|
if not self.trackinfo:
|
1992-04-22 14:33:26 +00:00
|
|
|
self.cacheinfo()
|
1992-04-22 13:24:13 +00:00
|
|
|
if len(arg) == 0:
|
|
|
|
return self.trackinfo[self.status[5]:self.status[6]+1]
|
|
|
|
result = []
|
|
|
|
for i in arg:
|
|
|
|
if i < self.status[5] or i > self.status[6]:
|
|
|
|
raise Error, 'range error'
|
|
|
|
result.append(self.trackinfo[i])
|
|
|
|
return result
|
|
|
|
|
1992-04-22 14:33:26 +00:00
|
|
|
def cacheinfo(self):
|
|
|
|
if not self.status:
|
|
|
|
self.cachestatus()
|
|
|
|
self.trackinfo = []
|
|
|
|
for i in range(self.status[5]):
|
|
|
|
self.trackinfo.append(None)
|
|
|
|
for i in range(self.status[5], self.status[6]+1):
|
|
|
|
self.trackinfo.append(self.player.gettrackinfo(i))
|
|
|
|
|
|
|
|
def cachestatus(self):
|
1992-04-22 13:44:48 +00:00
|
|
|
self.status = self.player.getstatus()
|
1992-04-23 11:34:39 +00:00
|
|
|
if self.status[0] == CD.NODISC:
|
1992-04-22 14:33:26 +00:00
|
|
|
self.status = None
|
1992-04-23 11:34:39 +00:00
|
|
|
raise Error, 'no disc in player'
|
1992-04-22 14:33:26 +00:00
|
|
|
|
|
|
|
def getstatus(self):
|
|
|
|
return self.player.getstatus()
|
1992-04-22 13:24:13 +00:00
|
|
|
|
|
|
|
def play(self):
|
|
|
|
if not self.status:
|
1992-04-22 14:33:26 +00:00
|
|
|
self.cachestatus()
|
1992-04-22 13:24:13 +00:00
|
|
|
size = self.player.bestreadsize()
|
|
|
|
self.listindex = 0
|
|
|
|
self.playing = 0
|
|
|
|
for i in range(8):
|
|
|
|
func, arg = self.callbacks[i]
|
|
|
|
if func:
|
|
|
|
self.parser.setcallback(i, func, arg)
|
|
|
|
else:
|
|
|
|
self.parser.removecallback(i)
|
|
|
|
if len(self.list) == 0:
|
|
|
|
for i in range(self.status[5], self.status[6]+1):
|
|
|
|
self.appendtrack(i)
|
1995-02-01 14:24:32 +00:00
|
|
|
try:
|
|
|
|
while 1:
|
|
|
|
if not self.playing:
|
|
|
|
if self.listindex >= len(self.list):
|
|
|
|
return
|
|
|
|
start, end = self.list[self.listindex]
|
|
|
|
if type(start) == type(0):
|
|
|
|
dummy = self.player.seektrack(
|
|
|
|
start)
|
1993-03-29 10:50:29 +00:00
|
|
|
else:
|
1995-02-01 14:24:32 +00:00
|
|
|
min, sec, frame = start
|
|
|
|
dummy = self.player.seek(
|
|
|
|
min, sec, frame)
|
|
|
|
if type(end) == type(0):
|
|
|
|
self.parser.setcallback(
|
|
|
|
CD.PNUM, _dopnum, self)
|
|
|
|
self.end = end
|
|
|
|
func, arg = \
|
|
|
|
self.callbacks[CD.ATIME]
|
|
|
|
if func:
|
|
|
|
self.parser.setcallback(CD.ATIME, func, arg)
|
|
|
|
else:
|
|
|
|
self.parser.removecallback(CD.ATIME)
|
1992-04-22 13:24:13 +00:00
|
|
|
else:
|
1995-02-01 14:24:32 +00:00
|
|
|
min, sec, frame = end
|
|
|
|
self.parser.setcallback(
|
|
|
|
CD.ATIME, _doatime,
|
|
|
|
self)
|
|
|
|
self.end = (min * 60 + sec) * \
|
|
|
|
75 + frame
|
|
|
|
func, arg = \
|
|
|
|
self.callbacks[CD.PNUM]
|
|
|
|
if func:
|
|
|
|
self.parser.setcallback(CD.PNUM, func, arg)
|
|
|
|
else:
|
|
|
|
self.parser.removecallback(CD.PNUM)
|
|
|
|
self.playing = 1
|
|
|
|
data = self.player.readda(size)
|
|
|
|
if data == '':
|
|
|
|
self.playing = 0
|
|
|
|
self.listindex = self.listindex + 1
|
|
|
|
continue
|
|
|
|
try:
|
|
|
|
self.parser.parseframe(data)
|
|
|
|
except _Stop:
|
|
|
|
self.playing = 0
|
|
|
|
self.listindex = self.listindex + 1
|
|
|
|
finally:
|
|
|
|
self.playing = 0
|