Guido van Rossum | f94e309 | 1992-04-22 13:24:13 +0000 | [diff] [blame] | 1 | # Class interface to the CD module. |
| 2 | |
| 3 | import cd, CD |
| 4 | |
| 5 | Error = 'Readcd.Error' |
| 6 | _Stop = 'Readcd.Stop' |
| 7 | |
Sjoerd Mullender | 04321d1 | 1993-03-29 10:50:29 +0000 | [diff] [blame] | 8 | def _doatime(self, cb_type, data): |
Guido van Rossum | f94e309 | 1992-04-22 13:24:13 +0000 | [diff] [blame] | 9 | if ((data[0] * 60) + data[1]) * 75 + data[2] > self.end: |
Guido van Rossum | ada67ec | 1992-06-03 16:47:24 +0000 | [diff] [blame] | 10 | ## print 'done with list entry',`self.listindex` |
Guido van Rossum | f94e309 | 1992-04-22 13:24:13 +0000 | [diff] [blame] | 11 | raise _Stop |
Sjoerd Mullender | 04321d1 | 1993-03-29 10:50:29 +0000 | [diff] [blame] | 12 | func, arg = self.callbacks[cb_type] |
Guido van Rossum | f94e309 | 1992-04-22 13:24:13 +0000 | [diff] [blame] | 13 | if func: |
Sjoerd Mullender | 04321d1 | 1993-03-29 10:50:29 +0000 | [diff] [blame] | 14 | func(arg, cb_type, data) |
Guido van Rossum | f94e309 | 1992-04-22 13:24:13 +0000 | [diff] [blame] | 15 | |
Sjoerd Mullender | 04321d1 | 1993-03-29 10:50:29 +0000 | [diff] [blame] | 16 | def _dopnum(self, cb_type, data): |
Guido van Rossum | f94e309 | 1992-04-22 13:24:13 +0000 | [diff] [blame] | 17 | if data > self.end: |
Guido van Rossum | ada67ec | 1992-06-03 16:47:24 +0000 | [diff] [blame] | 18 | ## print 'done with list entry',`self.listindex` |
Guido van Rossum | f94e309 | 1992-04-22 13:24:13 +0000 | [diff] [blame] | 19 | raise _Stop |
Sjoerd Mullender | 04321d1 | 1993-03-29 10:50:29 +0000 | [diff] [blame] | 20 | func, arg = self.callbacks[cb_type] |
Guido van Rossum | f94e309 | 1992-04-22 13:24:13 +0000 | [diff] [blame] | 21 | if func: |
Sjoerd Mullender | 04321d1 | 1993-03-29 10:50:29 +0000 | [diff] [blame] | 22 | func(arg, cb_type, data) |
Guido van Rossum | f94e309 | 1992-04-22 13:24:13 +0000 | [diff] [blame] | 23 | |
Guido van Rossum | d316607 | 1993-05-24 14:16:22 +0000 | [diff] [blame] | 24 | class Readcd: |
Sjoerd Mullender | b2e358d | 1993-11-08 15:10:43 +0000 | [diff] [blame] | 25 | def __init__(self, *arg): |
Guido van Rossum | f94e309 | 1992-04-22 13:24:13 +0000 | [diff] [blame] | 26 | if len(arg) == 0: |
| 27 | self.player = cd.open() |
| 28 | elif len(arg) == 1: |
| 29 | self.player = cd.open(arg[0]) |
| 30 | elif len(arg) == 2: |
| 31 | self.player = cd.open(arg[0], arg[1]) |
| 32 | else: |
Guido van Rossum | 7bc817d | 1993-12-17 15:25:27 +0000 | [diff] [blame] | 33 | raise Error, 'bad __init__ call' |
Guido van Rossum | f94e309 | 1992-04-22 13:24:13 +0000 | [diff] [blame] | 34 | self.list = [] |
| 35 | self.callbacks = [(None, None)] * 8 |
| 36 | self.parser = cd.createparser() |
| 37 | self.playing = 0 |
| 38 | self.end = 0 |
| 39 | self.status = None |
| 40 | self.trackinfo = None |
Guido van Rossum | f94e309 | 1992-04-22 13:24:13 +0000 | [diff] [blame] | 41 | |
| 42 | def eject(self): |
| 43 | self.player.eject() |
| 44 | self.list = [] |
| 45 | self.end = 0 |
| 46 | self.listindex = 0 |
| 47 | self.status = None |
| 48 | self.trackinfo = None |
| 49 | if self.playing: |
Guido van Rossum | ada67ec | 1992-06-03 16:47:24 +0000 | [diff] [blame] | 50 | ## print 'stop playing from eject' |
Guido van Rossum | f94e309 | 1992-04-22 13:24:13 +0000 | [diff] [blame] | 51 | raise _Stop |
| 52 | |
| 53 | def pmsf2msf(self, track, min, sec, frame): |
| 54 | if not self.status: |
Guido van Rossum | 58230c3 | 1992-04-22 14:33:26 +0000 | [diff] [blame] | 55 | self.cachestatus() |
Guido van Rossum | f94e309 | 1992-04-22 13:24:13 +0000 | [diff] [blame] | 56 | if track < self.status[5] or track > self.status[6]: |
| 57 | raise Error, 'track number out of range' |
Guido van Rossum | 58230c3 | 1992-04-22 14:33:26 +0000 | [diff] [blame] | 58 | if not self.trackinfo: |
| 59 | self.cacheinfo() |
Guido van Rossum | f94e309 | 1992-04-22 13:24:13 +0000 | [diff] [blame] | 60 | start, total = self.trackinfo[track] |
| 61 | start = ((start[0] * 60) + start[1]) * 75 + start[2] |
| 62 | total = ((total[0] * 60) + total[1]) * 75 + total[2] |
| 63 | block = ((min * 60) + sec) * 75 + frame |
| 64 | if block > total: |
| 65 | raise Error, 'out of range' |
| 66 | block = start + block |
| 67 | min, block = divmod(block, 75*60) |
| 68 | sec, frame = divmod(block, 75) |
| 69 | return min, sec, frame |
| 70 | |
| 71 | def reset(self): |
| 72 | self.list = [] |
| 73 | |
| 74 | def appendtrack(self, track): |
| 75 | self.appendstretch(track, track) |
| 76 | |
| 77 | def appendstretch(self, start, end): |
| 78 | if not self.status: |
Guido van Rossum | 58230c3 | 1992-04-22 14:33:26 +0000 | [diff] [blame] | 79 | self.cachestatus() |
Guido van Rossum | f94e309 | 1992-04-22 13:24:13 +0000 | [diff] [blame] | 80 | if not start: |
| 81 | start = 1 |
| 82 | if not end: |
| 83 | end = self.status[6] |
Sjoerd Mullender | 04321d1 | 1993-03-29 10:50:29 +0000 | [diff] [blame] | 84 | if type(end) == type(0): |
| 85 | if end < self.status[5] or end > self.status[6]: |
| 86 | raise Error, 'range error' |
| 87 | else: |
Guido van Rossum | f94e309 | 1992-04-22 13:24:13 +0000 | [diff] [blame] | 88 | l = len(end) |
| 89 | if l == 4: |
| 90 | prog, min, sec, frame = end |
| 91 | if prog < self.status[5] or prog > self.status[6]: |
| 92 | raise Error, 'range error' |
| 93 | end = self.pmsf2msf(prog, min, sec, frame) |
| 94 | elif l <> 3: |
| 95 | raise Error, 'syntax error' |
Sjoerd Mullender | 04321d1 | 1993-03-29 10:50:29 +0000 | [diff] [blame] | 96 | if type(start) == type(0): |
| 97 | if start < self.status[5] or start > self.status[6]: |
Guido van Rossum | f94e309 | 1992-04-22 13:24:13 +0000 | [diff] [blame] | 98 | raise Error, 'range error' |
Sjoerd Mullender | 04321d1 | 1993-03-29 10:50:29 +0000 | [diff] [blame] | 99 | if len(self.list) > 0: |
| 100 | s, e = self.list[-1] |
| 101 | if type(e) == type(0): |
| 102 | if start == e+1: |
| 103 | start = s |
| 104 | del self.list[-1] |
| 105 | else: |
Guido van Rossum | f94e309 | 1992-04-22 13:24:13 +0000 | [diff] [blame] | 106 | l = len(start) |
| 107 | if l == 4: |
| 108 | prog, min, sec, frame = start |
| 109 | if prog < self.status[5] or prog > self.status[6]: |
| 110 | raise Error, 'range error' |
| 111 | start = self.pmsf2msf(prog, min, sec, frame) |
| 112 | elif l <> 3: |
| 113 | raise Error, 'syntax error' |
Guido van Rossum | f94e309 | 1992-04-22 13:24:13 +0000 | [diff] [blame] | 114 | self.list.append((start, end)) |
| 115 | |
| 116 | def settracks(self, list): |
| 117 | self.list = [] |
| 118 | for track in list: |
| 119 | self.appendtrack(track) |
| 120 | |
Sjoerd Mullender | 04321d1 | 1993-03-29 10:50:29 +0000 | [diff] [blame] | 121 | def setcallback(self, cb_type, func, arg): |
| 122 | if cb_type < 0 or cb_type >= 8: |
Guido van Rossum | f94e309 | 1992-04-22 13:24:13 +0000 | [diff] [blame] | 123 | raise Error, 'type out of range' |
Sjoerd Mullender | 04321d1 | 1993-03-29 10:50:29 +0000 | [diff] [blame] | 124 | self.callbacks[cb_type] = (func, arg) |
Guido van Rossum | f94e309 | 1992-04-22 13:24:13 +0000 | [diff] [blame] | 125 | if self.playing: |
Sjoerd Mullender | 04321d1 | 1993-03-29 10:50:29 +0000 | [diff] [blame] | 126 | start, end = self.list[self.listindex] |
| 127 | if type(end) == type(0): |
| 128 | if cb_type <> CD.PNUM: |
| 129 | self.parser.setcallback(cb_type, func, arg) |
| 130 | else: |
| 131 | if cb_type <> CD.ATIME: |
| 132 | self.parser.setcallback(cb_type, func, arg) |
Guido van Rossum | f94e309 | 1992-04-22 13:24:13 +0000 | [diff] [blame] | 133 | |
Sjoerd Mullender | 04321d1 | 1993-03-29 10:50:29 +0000 | [diff] [blame] | 134 | def removecallback(self, cb_type): |
| 135 | if cb_type < 0 or cb_type >= 8: |
Guido van Rossum | f94e309 | 1992-04-22 13:24:13 +0000 | [diff] [blame] | 136 | raise Error, 'type out of range' |
Sjoerd Mullender | 04321d1 | 1993-03-29 10:50:29 +0000 | [diff] [blame] | 137 | self.callbacks[cb_type] = (None, None) |
Guido van Rossum | f94e309 | 1992-04-22 13:24:13 +0000 | [diff] [blame] | 138 | if self.playing: |
Sjoerd Mullender | 04321d1 | 1993-03-29 10:50:29 +0000 | [diff] [blame] | 139 | start, end = self.list[self.listindex] |
| 140 | if type(end) == type(0): |
| 141 | if cb_type <> CD.PNUM: |
| 142 | self.parser.removecallback(cb_type) |
| 143 | else: |
| 144 | if cb_type <> CD.ATIME: |
| 145 | self.parser.removecallback(cb_type) |
Guido van Rossum | f94e309 | 1992-04-22 13:24:13 +0000 | [diff] [blame] | 146 | |
| 147 | def gettrackinfo(self, *arg): |
| 148 | if not self.status: |
Guido van Rossum | 58230c3 | 1992-04-22 14:33:26 +0000 | [diff] [blame] | 149 | self.cachestatus() |
Guido van Rossum | f94e309 | 1992-04-22 13:24:13 +0000 | [diff] [blame] | 150 | if not self.trackinfo: |
Guido van Rossum | 58230c3 | 1992-04-22 14:33:26 +0000 | [diff] [blame] | 151 | self.cacheinfo() |
Guido van Rossum | f94e309 | 1992-04-22 13:24:13 +0000 | [diff] [blame] | 152 | if len(arg) == 0: |
| 153 | return self.trackinfo[self.status[5]:self.status[6]+1] |
| 154 | result = [] |
| 155 | for i in arg: |
| 156 | if i < self.status[5] or i > self.status[6]: |
| 157 | raise Error, 'range error' |
| 158 | result.append(self.trackinfo[i]) |
| 159 | return result |
| 160 | |
Guido van Rossum | 58230c3 | 1992-04-22 14:33:26 +0000 | [diff] [blame] | 161 | def cacheinfo(self): |
| 162 | if not self.status: |
| 163 | self.cachestatus() |
| 164 | self.trackinfo = [] |
| 165 | for i in range(self.status[5]): |
| 166 | self.trackinfo.append(None) |
| 167 | for i in range(self.status[5], self.status[6]+1): |
| 168 | self.trackinfo.append(self.player.gettrackinfo(i)) |
| 169 | |
| 170 | def cachestatus(self): |
Guido van Rossum | dee5d8e | 1992-04-22 13:44:48 +0000 | [diff] [blame] | 171 | self.status = self.player.getstatus() |
Guido van Rossum | a3cd071 | 1992-04-23 11:34:39 +0000 | [diff] [blame] | 172 | if self.status[0] == CD.NODISC: |
Guido van Rossum | 58230c3 | 1992-04-22 14:33:26 +0000 | [diff] [blame] | 173 | self.status = None |
Guido van Rossum | a3cd071 | 1992-04-23 11:34:39 +0000 | [diff] [blame] | 174 | raise Error, 'no disc in player' |
Guido van Rossum | 58230c3 | 1992-04-22 14:33:26 +0000 | [diff] [blame] | 175 | |
| 176 | def getstatus(self): |
| 177 | return self.player.getstatus() |
Guido van Rossum | f94e309 | 1992-04-22 13:24:13 +0000 | [diff] [blame] | 178 | |
| 179 | def play(self): |
| 180 | if not self.status: |
Guido van Rossum | 58230c3 | 1992-04-22 14:33:26 +0000 | [diff] [blame] | 181 | self.cachestatus() |
Guido van Rossum | f94e309 | 1992-04-22 13:24:13 +0000 | [diff] [blame] | 182 | size = self.player.bestreadsize() |
| 183 | self.listindex = 0 |
| 184 | self.playing = 0 |
| 185 | for i in range(8): |
| 186 | func, arg = self.callbacks[i] |
| 187 | if func: |
| 188 | self.parser.setcallback(i, func, arg) |
| 189 | else: |
| 190 | self.parser.removecallback(i) |
| 191 | if len(self.list) == 0: |
| 192 | for i in range(self.status[5], self.status[6]+1): |
| 193 | self.appendtrack(i) |
Sjoerd Mullender | 992ff06 | 1995-02-01 14:24:32 +0000 | [diff] [blame] | 194 | try: |
| 195 | while 1: |
| 196 | if not self.playing: |
| 197 | if self.listindex >= len(self.list): |
| 198 | return |
| 199 | start, end = self.list[self.listindex] |
| 200 | if type(start) == type(0): |
| 201 | dummy = self.player.seektrack( |
| 202 | start) |
Sjoerd Mullender | 04321d1 | 1993-03-29 10:50:29 +0000 | [diff] [blame] | 203 | else: |
Sjoerd Mullender | 992ff06 | 1995-02-01 14:24:32 +0000 | [diff] [blame] | 204 | min, sec, frame = start |
| 205 | dummy = self.player.seek( |
| 206 | min, sec, frame) |
| 207 | if type(end) == type(0): |
| 208 | self.parser.setcallback( |
| 209 | CD.PNUM, _dopnum, self) |
| 210 | self.end = end |
| 211 | func, arg = \ |
| 212 | self.callbacks[CD.ATIME] |
| 213 | if func: |
| 214 | self.parser.setcallback(CD.ATIME, func, arg) |
| 215 | else: |
| 216 | self.parser.removecallback(CD.ATIME) |
Guido van Rossum | f94e309 | 1992-04-22 13:24:13 +0000 | [diff] [blame] | 217 | else: |
Sjoerd Mullender | 992ff06 | 1995-02-01 14:24:32 +0000 | [diff] [blame] | 218 | min, sec, frame = end |
| 219 | self.parser.setcallback( |
| 220 | CD.ATIME, _doatime, |
| 221 | self) |
| 222 | self.end = (min * 60 + sec) * \ |
| 223 | 75 + frame |
| 224 | func, arg = \ |
| 225 | self.callbacks[CD.PNUM] |
| 226 | if func: |
| 227 | self.parser.setcallback(CD.PNUM, func, arg) |
| 228 | else: |
| 229 | self.parser.removecallback(CD.PNUM) |
| 230 | self.playing = 1 |
| 231 | data = self.player.readda(size) |
| 232 | if data == '': |
| 233 | self.playing = 0 |
| 234 | self.listindex = self.listindex + 1 |
| 235 | continue |
| 236 | try: |
| 237 | self.parser.parseframe(data) |
| 238 | except _Stop: |
| 239 | self.playing = 0 |
| 240 | self.listindex = self.listindex + 1 |
| 241 | finally: |
| 242 | self.playing = 0 |